gdevo182.c 8.2 KB

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