gdevo182.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* Copyright (C) 1993, 1996 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gdevo182.c,v 1.6 2004/08/10 13:02:36 stefan Exp $*/
  14. /* Okidata Microline 182 printer driver */
  15. /* Contributed by Maarten Koning (smeg@bnr.ca) April 4, 1993 */
  16. /****************************************************************
  17. I use this driver from Unix with the following aliases:
  18. alias psp "gs -q -sDEVICE=oki182 -sOutputFile=\|lpr - <\!*"
  19. alias psphigh "gs -q -sDEVICE=oki182 -r144 -sOutputFile=\|lpr - <\!*"
  20. ps. I have my printer DIP switches set to the following (as viewed
  21. while standing in front of your printer looking down into the
  22. config access hatch located at the top of your printer
  23. in the centre back).
  24. Upper Upper Bottom
  25. Left Right (at right)
  26. x x x
  27. x x x
  28. x x x
  29. x x x
  30. x x x
  31. x x x
  32. x x x
  33. x x x
  34. The upper DIP switches are on a SuperSpeed Serial
  35. card that will do 19200 baud. I have it set at 9600
  36. baud since that seems sufficient to keep the printer
  37. busy.
  38. The important thing is to be in 8-bit mode so that
  39. the graphics data can't match any Okidata commands
  40. (This driver sets the high bit of graphics data to 1).
  41. ****************************************************************/
  42. #include "gdevprn.h"
  43. /*
  44. * Available resolutions are 72x72 or 144x144;
  45. * (144x72) would be possible to do also, but I didn't bother)
  46. */
  47. /* The device descriptor */
  48. private dev_proc_print_page(oki_print_page);
  49. const gx_device_printer far_data gs_oki182_device =
  50. prn_device(prn_std_procs, "oki182",
  51. 80, /* width_10ths, 8.0" */
  52. 110, /* height_10ths, 11" */
  53. 72, /* x_dpi */
  54. 72, /* y_dpi */
  55. 0, 0, 0, 0, /* margins */
  56. 1, oki_print_page);
  57. /* ------ internal routines ------ */
  58. /* out is a pointer to an array of 7 scan lines,
  59. lineSize is the number of bytes between a pixel and
  60. the pixel directly beneath it.
  61. scanBits is the number of bits in each scan line
  62. out is a pointer to an array of column data, which
  63. is how the Okidata wants the graphics image.
  64. each column of graphics data is 7 bits high and
  65. is encoded in a byte - highest pixel in the column
  66. is the lowest bit in the byte. The upper bit of the
  67. byte is set so that the okidata doesn't mistake
  68. graphic image data for graphic commands.
  69. */
  70. private void
  71. oki_transpose(byte *in, byte *out, int scanBits, register int lineSize)
  72. {
  73. register bitMask = 0x80;
  74. register byte *inPtr;
  75. register byte outByte;
  76. while (scanBits-- > 0) {
  77. inPtr = in;
  78. if (*inPtr & bitMask)
  79. outByte = 0x81;
  80. else
  81. outByte = 0x80;
  82. if (*(inPtr += lineSize) & bitMask)
  83. outByte += 0x02;
  84. if (*(inPtr += lineSize) & bitMask)
  85. outByte += 0x04;
  86. if (*(inPtr += lineSize) & bitMask)
  87. outByte += 0x08;
  88. if (*(inPtr += lineSize) & bitMask)
  89. outByte += 0x10;
  90. if (*(inPtr += lineSize) & bitMask)
  91. outByte += 0x20;
  92. if (*(inPtr += lineSize) & bitMask)
  93. outByte += 0x40;
  94. *out++ = outByte;
  95. if ((bitMask >>= 1) == 0) {
  96. bitMask = 0x80;
  97. in ++;
  98. }
  99. }
  100. }
  101. /* This routine tries to compress a sequence of okidata
  102. graphic bytes by trimming off leading and trailing
  103. zeros. Trailing zeros can be thrown away and leading
  104. zeros can be replaced with a much smaller number of spaces.
  105. 'in' is a pointer to the graphic bytes to be compressed.
  106. origWidth is the number of bytes pointed to by 'in'.
  107. highRes is non-zero when 144x144 mode is being used.
  108. numSpaces is set to the number of spaces that should
  109. be printed before the compressed image. newWidth is
  110. the new number of bytes that the return value of this
  111. function points to.
  112. xxx - A future enhancement would be to replace long sequences
  113. of embedded zeros with exit.graphics-<n> spaces-enter.graphics
  114. */
  115. private byte *
  116. oki_compress(byte *in, int origWidth, int highRes,
  117. int *numSpaces, int *newWidth)
  118. {
  119. int spaces = 0;
  120. int columns_per_space = 6;
  121. byte *in_end = in + origWidth;
  122. /* remove trailing zeros (which are realy 0x80's) */
  123. while (in_end > in && in_end[-1] == 0x80)
  124. in_end --;
  125. if (highRes)
  126. columns_per_space = 12;
  127. /* remove leading zeros that can be replaced by spaces */
  128. while(in < in_end && in[0] == 0x80 && memcmp((char *)in,
  129. (char *)in + 1, columns_per_space - 1) == 0) {
  130. spaces++;
  131. in += columns_per_space;
  132. }
  133. *numSpaces = spaces;
  134. /* just in case we compressed this line out of existance */
  135. if (in_end > in)
  136. *newWidth = in_end - in;
  137. else
  138. *newWidth = 0;
  139. return(in);
  140. }
  141. /* Send the page to the printer. */
  142. private int
  143. oki_print_page(gx_device_printer *pdev, FILE *prn_stream)
  144. {
  145. int highRes = pdev->y_pixels_per_inch > 100;
  146. int bits_per_column = 7;
  147. int i, spaces, width;
  148. int lcnt;
  149. int line_size = gdev_prn_raster((gx_device_printer *)pdev);
  150. byte *in = (byte *)gs_malloc(pdev->memory, 16, line_size, "oki_print_page(in)");
  151. byte *out1 = (byte *)gs_malloc(pdev->memory, 8, line_size, "oki_print_page(out1)");
  152. byte *out2 = (byte *)gs_malloc(pdev->memory, 8, line_size, "oki_print_page(out2)");
  153. byte *out3;
  154. int lnum = 0;
  155. int skip = 0;
  156. int code = 0;
  157. if ( in == 0 || out1 == 0 || out2 == 0)
  158. { code = gs_error_VMerror;
  159. gs_note_error(code);
  160. goto bail;
  161. }
  162. /* Initialize the printer. */
  163. /* CAN; 72x72; left margin = 001; disable skip over perforation */
  164. fwrite("\030\034\033%C001\033%S0", 1, 12, prn_stream);
  165. if (highRes) {
  166. fwrite("\033R", 1, 2, prn_stream);
  167. bits_per_column = 14;
  168. }
  169. /* Transfer pixels to printer */
  170. while ( lnum < pdev->height ) {
  171. /* Copy 1 scan line and test for all zero. */
  172. code = gdev_prn_copy_scan_lines(pdev, lnum, in, line_size);
  173. if ( code < 0 )
  174. goto xit;
  175. /* if line is all zero, skip */
  176. if ( in[0] == 0 && !memcmp((char *)in, (char *)in + 1,
  177. line_size - 1)) {
  178. lnum++;
  179. if (highRes)
  180. skip++;
  181. else
  182. skip += 2;
  183. continue;
  184. }
  185. /* use fine line feed to get to the appropriate position. */
  186. while ( skip > 127 ) {
  187. fputs("\033%5\177", prn_stream);
  188. skip -= 127;
  189. }
  190. if ( skip )
  191. fprintf(prn_stream, "\033%%5%c",
  192. (char) (skip & 0xff));
  193. skip = 0;
  194. /* get the rest of the scan lines */
  195. code = gdev_prn_copy_scan_lines(pdev, lnum + 1,
  196. in + line_size, (bits_per_column - 1) * line_size);
  197. if ( code < 0 )
  198. goto xit;
  199. lcnt = code + 1; /* since we already grabbed one line */
  200. if ( lcnt < bits_per_column )
  201. memset(in + lcnt * line_size, 0,
  202. (bits_per_column - lcnt) * line_size);
  203. if (highRes) {
  204. oki_transpose(in, out1, pdev->width, 2 * line_size);
  205. oki_transpose(in + line_size, out2,
  206. pdev->width, 2 * line_size);
  207. } else
  208. oki_transpose(in, out1, pdev->width, line_size);
  209. out3 = oki_compress(out1, pdev->width, highRes,
  210. &spaces, &width);
  211. for (i=0; i < spaces; i++)
  212. putc(' ', prn_stream);
  213. fwrite("\003", 1, 1, prn_stream);
  214. fwrite(out3, 1, width, prn_stream);
  215. if (highRes) {
  216. /* exit graphics; carriage return; 1 bit line feed */
  217. fprintf(prn_stream, "\003\002\015\033%%5%c", (char) 1);
  218. out3 = oki_compress(out2, pdev->width, highRes,
  219. &spaces, &width);
  220. for (i=0; i < spaces; i++)
  221. putc(' ', prn_stream);
  222. fwrite("\003", 1, 1, prn_stream);
  223. fwrite(out3, 1, width, prn_stream);
  224. fprintf(prn_stream, "\003\002\015\033%%5%c", (char) 13);
  225. } else
  226. fwrite("\003\016\003\002", 1, 4, prn_stream);
  227. lnum += bits_per_column;
  228. }
  229. /* Eject the page */
  230. xit:
  231. fputc(014, prn_stream); /* form feed */
  232. fflush(prn_stream);
  233. bail:
  234. if ( out1 != 0 )
  235. gs_free(pdev->memory, (char *)out1, 8, line_size, "oki_print_page(out1)");
  236. if ( out2 != 0 )
  237. gs_free(pdev->memory, (char *)out2, 8, line_size, "oki_print_page(out2)");
  238. if ( in != 0 )
  239. gs_free(pdev->memory, (char *)in, 16, line_size, "oki_print_page(in)");
  240. return code;
  241. }