gdevlj56.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* Copyright (C) 1997, 1999 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: gdevlj56.c,v 1.3 2001/08/01 00:48:23 stefan911 Exp $ */
  16. /* H-P LaserJet 5 & 6 drivers for Ghostscript */
  17. #include "gdevprn.h"
  18. #include "stream.h"
  19. #include "gdevpcl.h"
  20. #include "gdevpxat.h"
  21. #include "gdevpxen.h"
  22. #include "gdevpxop.h"
  23. #include "gdevpxut.h"
  24. /* Define the default resolution. */
  25. #ifndef X_DPI
  26. # define X_DPI 600
  27. #endif
  28. #ifndef Y_DPI
  29. # define Y_DPI 600
  30. #endif
  31. /* Define the number of blank lines that make it worthwhile to */
  32. /* start a new image. */
  33. #define MIN_SKIP_LINES 2
  34. /* We round up the LINE_SIZE to a multiple of a ulong for faster scanning. */
  35. #define W sizeof(word)
  36. private dev_proc_open_device(ljet5_open);
  37. private dev_proc_close_device(ljet5_close);
  38. private dev_proc_print_page(ljet5_print_page);
  39. private const gx_device_procs ljet5_procs =
  40. prn_procs(ljet5_open, gdev_prn_output_page, ljet5_close);
  41. const gx_device_printer gs_lj5mono_device =
  42. prn_device(ljet5_procs, "lj5mono",
  43. DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  44. X_DPI, Y_DPI,
  45. 0, 0, 0, 0,
  46. 1, ljet5_print_page);
  47. private const gx_device_procs lj5gray_procs =
  48. prn_color_procs(ljet5_open, gdev_prn_output_page, ljet5_close,
  49. gx_default_gray_map_rgb_color,
  50. gx_default_gray_map_color_rgb);
  51. const gx_device_printer gs_lj5gray_device = {
  52. prn_device_body(gx_device_printer, lj5gray_procs, "lj5gray",
  53. DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  54. X_DPI, Y_DPI,
  55. 0, 0, 0, 0,
  56. 1, 8, 255, 0, 256, 1, ljet5_print_page)
  57. };
  58. /* Open the printer, writing the stream header. */
  59. private int
  60. ljet5_open(gx_device * pdev)
  61. {
  62. int code = gdev_prn_open(pdev);
  63. if (code < 0)
  64. return code;
  65. code = gdev_prn_open_printer(pdev, true);
  66. if (code < 0)
  67. return code;
  68. {
  69. gx_device_printer *const ppdev = (gx_device_printer *)pdev;
  70. stream fs;
  71. stream *const s = &fs;
  72. byte buf[50]; /* arbitrary */
  73. swrite_file(s, ppdev->file, buf, sizeof(buf));
  74. px_write_file_header(s, pdev);
  75. sflush(s); /* don't close */
  76. }
  77. return 0;
  78. }
  79. /* Close the printer, writing the stream trailer. */
  80. private int
  81. ljet5_close(gx_device * pdev)
  82. {
  83. gx_device_printer *const ppdev = (gx_device_printer *)pdev;
  84. int code = gdev_prn_open_printer(pdev, true);
  85. if (code < 0)
  86. return code;
  87. px_write_file_trailer(ppdev->file);
  88. return gdev_prn_close(pdev);
  89. }
  90. /* Send the page to the printer. For now, just send the whole image. */
  91. private int
  92. ljet5_print_page(gx_device_printer * pdev, FILE * prn_stream)
  93. {
  94. gs_memory_t *mem = pdev->memory;
  95. uint line_size = gdev_mem_bytes_per_scan_line((gx_device *) pdev);
  96. uint line_size_words = (line_size + W - 1) / W;
  97. uint out_size = line_size + (line_size / 127) + 1;
  98. word *line = (word *)gs_alloc_byte_array(mem, line_size_words, W, "ljet5(line)");
  99. byte *out = gs_alloc_bytes(mem, out_size, "ljet5(out)");
  100. int code = 0;
  101. int lnum;
  102. stream fs;
  103. stream *const s = &fs;
  104. byte buf[200]; /* arbitrary */
  105. if (line == 0 || out == 0) {
  106. code = gs_note_error(gs_error_VMerror);
  107. goto done;
  108. }
  109. swrite_file(s, prn_stream, buf, sizeof(buf));
  110. /* Write the page header. */
  111. {
  112. static const byte page_header[] = {
  113. pxtBeginPage,
  114. DUSP(0, 0), DA(pxaPoint),
  115. pxtSetCursor
  116. };
  117. static const byte mono_header[] = {
  118. DUB(eGray), DA(pxaColorSpace),
  119. DUB(e8Bit), DA(pxaPaletteDepth),
  120. pxt_ubyte_array, pxt_ubyte, 2, 0xff, 0x00, DA(pxaPaletteData),
  121. pxtSetColorSpace
  122. };
  123. static const byte gray_header[] = {
  124. DUB(eGray), DA(pxaColorSpace),
  125. pxtSetColorSpace
  126. };
  127. px_write_page_header(s, (gx_device *)pdev);
  128. px_write_select_media(s, (gx_device *)pdev, NULL);
  129. PX_PUT_LIT(s, page_header);
  130. if (pdev->color_info.depth == 1)
  131. PX_PUT_LIT(s, mono_header);
  132. else
  133. PX_PUT_LIT(s, gray_header);
  134. }
  135. /* Write the image header. */
  136. {
  137. static const byte mono_image_header[] = {
  138. DA(pxaDestinationSize),
  139. DUB(eIndexedPixel), DA(pxaColorMapping),
  140. DUB(e1Bit), DA(pxaColorDepth),
  141. pxtBeginImage
  142. };
  143. static const byte gray_image_header[] = {
  144. DA(pxaDestinationSize),
  145. DUB(eDirectPixel), DA(pxaColorMapping),
  146. DUB(e8Bit), DA(pxaColorDepth),
  147. pxtBeginImage
  148. };
  149. px_put_us(s, pdev->width);
  150. px_put_a(s, pxaSourceWidth);
  151. px_put_us(s, pdev->height);
  152. px_put_a(s, pxaSourceHeight);
  153. px_put_usp(s, pdev->width, pdev->height);
  154. if (pdev->color_info.depth == 1)
  155. PX_PUT_LIT(s, mono_image_header);
  156. else
  157. PX_PUT_LIT(s, gray_image_header);
  158. }
  159. /* Write the image data, compressing each line. */
  160. for (lnum = 0; lnum < pdev->height; ++lnum) {
  161. int ncompr;
  162. static const byte line_header[] = {
  163. DA(pxaStartLine),
  164. DUS(1), DA(pxaBlockHeight),
  165. DUB(eRLECompression), DA(pxaCompressMode),
  166. pxtReadImage
  167. };
  168. code = gdev_prn_copy_scan_lines(pdev, lnum, (byte *) line, line_size);
  169. if (code < 0)
  170. goto fin;
  171. px_put_us(s, lnum);
  172. PX_PUT_LIT(s, line_header);
  173. ncompr = gdev_pcl_mode2compress_padded(line, line + line_size_words,
  174. out, true);
  175. px_put_data_length(s, ncompr);
  176. px_put_bytes(s, out, ncompr);
  177. }
  178. /* Finish up. */
  179. fin:
  180. spputc(s, pxtEndImage);
  181. spputc(s, pxtEndPage);
  182. sflush(s);
  183. done:
  184. gs_free_object(mem, out, "ljet5(out)");
  185. gs_free_object(mem, line, "ljet5(line)");
  186. return code;
  187. }