gdevpsim.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /* Copyright (C) 1994, 2000 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: gdevpsim.c,v 1.14 2004/10/07 05:18:34 ray Exp $ */
  14. /* PostScript image output device */
  15. #include "gdevprn.h"
  16. #include "gdevpsu.h"
  17. #include "stream.h"
  18. #include "strimpl.h"
  19. #include "sa85x.h"
  20. #include "srlx.h"
  21. /*
  22. * There are two drivers in this file, both of which produce PostScript
  23. * output consisting of a single bitmap per page. The psmono/psgray
  24. * driver produces monochrome Level 1 images using home-grown run length
  25. * compression; the psrgb driver produces planar RGB Level 2 images
  26. * using the RunLengthEncode filter.
  27. */
  28. /* ---------------- Shared code ---------------- */
  29. /* Define the device parameters. */
  30. #ifndef X_DPI
  31. # define X_DPI 300
  32. #endif
  33. #ifndef Y_DPI
  34. # define Y_DPI 300
  35. #endif
  36. /* Write the file (if necessary) and page headers. */
  37. private void
  38. ps_image_write_headers(FILE *f, gx_device_printer *pdev,
  39. const char *const setup[],
  40. gx_device_pswrite_common_t *pdpc)
  41. {
  42. if (gdev_prn_file_is_new(pdev)) {
  43. gs_rect bbox;
  44. bbox.p.x = 0;
  45. bbox.p.y = 0;
  46. bbox.q.x = pdev->width / pdev->HWResolution[0] * 72.0;
  47. bbox.q.y = pdev->height / pdev->HWResolution[1] * 72.0;
  48. psw_begin_file_header(f, (gx_device *)pdev, &bbox, pdpc, false);
  49. psw_print_lines(f, setup);
  50. psw_end_file_header(f);
  51. }
  52. {
  53. byte buf[100]; /* arbitrary */
  54. stream s;
  55. s_init(&s, pdev->memory);
  56. swrite_file(&s, f, buf, sizeof(buf));
  57. psw_write_page_header(&s, (gx_device *)pdev, pdpc, true, pdev->PageCount + 1, 10);
  58. sflush(&s);
  59. }
  60. }
  61. /* ---------------- Level 1 monochrome driver ---------------- */
  62. /*
  63. * This driver produces a bitmap in the form of a PostScript file that can
  64. * be fed to any PostScript printer. It uses a run-length compression
  65. * method that executes quickly (unlike some produced by PostScript
  66. * drivers!).
  67. *
  68. * There are two devices here, one for 1-bit black-and-white and one
  69. * for 8-bit gray. In fact, the same code could also handle 2- and
  70. * 4-bit gray output.
  71. */
  72. /* The device descriptor */
  73. private dev_proc_print_page(psmono_print_page);
  74. private dev_proc_close_device(psmono_close);
  75. const gx_device_printer gs_psmono_device =
  76. prn_device(prn_std_procs, "psmono",
  77. DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  78. X_DPI, Y_DPI,
  79. 0, 0, 0, 0, /* margins */
  80. 1, psmono_print_page);
  81. private const gx_device_procs psgray_procs =
  82. prn_color_procs(gdev_prn_open, gdev_prn_output_page, psmono_close,
  83. gx_default_gray_map_rgb_color, gx_default_gray_map_color_rgb);
  84. const gx_device_printer gs_psgray_device = {
  85. prn_device_body(gx_device_printer, psgray_procs, "psgray",
  86. DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  87. X_DPI, Y_DPI,
  88. 0, 0, 0, 0, /* margins */
  89. 1, 8, 255, 0, 256, 1, psmono_print_page)
  90. };
  91. private const char *const psmono_setup[] = {
  92. /* Initialize the strings for filling runs. */
  93. "/.ImageFills [ 0 1 255 {",
  94. " 256 string dup 0 1 7 { 3 index put dup } for { 8 16 32 64 128 } {",
  95. " 2 copy 0 exch getinterval putinterval dup",
  96. " } forall pop exch pop",
  97. "} bind for ] def",
  98. /* Initialize the procedure table for input dispatching. */
  99. "/.ImageProcs [",
  100. /* Stack: <buffer> <file> <xdigits> <previous> <byte> */
  101. " 32 { { pop .ImageItem } } repeat",
  102. " 16 { {", /* 0x20-0x2f: (N-0x20) data bytes follow */
  103. " 32 sub 3 -1 roll add 3 index exch 0 exch getinterval 2 index exch",
  104. " readhexstring pop exch pop 0 exch dup",
  105. " } bind } repeat",
  106. " 16 { {", /* 0x30-0x3f: prefix hex digit (N-0x30) to next count */
  107. " 48 sub 3 -1 roll add 4 bitshift exch .ImageItem",
  108. " } bind } repeat",
  109. " 32 { {", /* 0x40-0x5f: repeat last data byte (N-0x40) times */
  110. " 64 sub 3 -1 roll add .ImageFills 2 index dup length 1 sub get get",
  111. " exch 0 exch getinterval 0 3 1 roll",
  112. " } bind } repeat",
  113. " 160 { { pop .ImageItem } } repeat",
  114. "] readonly def",
  115. /* Read one item from a compressed image. */
  116. /* Stack contents: <buffer> <file> <xdigits> <previous> */
  117. "/.ImageItem {",
  118. " 2 index read pop dup .ImageProcs exch get exec",
  119. "} bind def",
  120. /* Read and print an entire compressed image. */
  121. "/.ImageRead {" /* <width> <height> <bpc> .ImageRead - */
  122. " gsave [",
  123. /* Stack: width height bpc -mark- */
  124. " 1 0 0 -1 0 7 index",
  125. /* Stack: width height bpc -mark- 1 0 0 -1 0 height */
  126. " ] { .ImageItem }",
  127. /* Stack: width height bpc <matrix> <proc> */
  128. " 4 index 3 index mul 7 add 8 idiv string currentfile 0 ()",
  129. /* Stack: width height bpc <matrix> <proc> <buffer> <file> 0 () */
  130. " 9 4 roll",
  131. /* Stack: <buffer> <file> 0 () width height bpc <matrix> <proc> */
  132. " image pop pop pop pop grestore",
  133. "} def",
  134. 0
  135. };
  136. static const gx_device_pswrite_common_t psmono_values =
  137. PSWRITE_COMMON_VALUES(1, 0 /*false*/, 1);
  138. #define data_run_code 0x20
  139. #define xdigit_code 0x30
  140. #define max_data_per_line 35
  141. #define repeat_run_code 0x40
  142. #define max_repeat_run_code 31
  143. #define max_repeat_run 255
  144. /* Send the page to the printer. */
  145. private void write_data_run(const byte *, int, FILE *, byte);
  146. private int
  147. psmono_print_page(gx_device_printer * pdev, FILE * prn_stream)
  148. {
  149. int line_size = gdev_mem_bytes_per_scan_line((gx_device *) pdev);
  150. int lnum;
  151. byte *line = gs_alloc_bytes(pdev->memory, line_size, "psmono_print_page");
  152. byte invert = (pdev->color_info.depth == 1 ? 0xff : 0);
  153. gx_device_pswrite_common_t pswrite_common;
  154. if (line == 0)
  155. return_error(gs_error_VMerror);
  156. pswrite_common = psmono_values;
  157. /* If this is the first page of the file, */
  158. /* write the setup code. */
  159. ps_image_write_headers(prn_stream, pdev, psmono_setup, &pswrite_common);
  160. /* Write the .ImageRead command. */
  161. fprintf(prn_stream,
  162. "%d %d %d .ImageRead\n",
  163. pdev->width, pdev->height, pdev->color_info.depth);
  164. /* Compress each scan line in turn. */
  165. for (lnum = 0; lnum < pdev->height; lnum++) {
  166. const byte *p;
  167. int left = line_size;
  168. byte *data;
  169. gdev_prn_get_bits(pdev, lnum, line, &data);
  170. p = data;
  171. /* Loop invariant: p + left = data + line_size. */
  172. #define min_repeat_run 10
  173. while (left >= min_repeat_run) { /* Detect a maximal run of non-repeated data. */
  174. const byte *p1 = p;
  175. int left1 = left;
  176. byte b;
  177. int count, count_left;
  178. while (left1 >= min_repeat_run &&
  179. ((b = *p1) != p1[1] ||
  180. b != p1[2] || b != p1[3] || b != p1[4] ||
  181. b != p1[5] || b != p1[6] || b != p1[7] ||
  182. b != p1[8] || b != p1[9])
  183. )
  184. ++p1, --left1;
  185. if (left1 < min_repeat_run)
  186. break; /* no repeated data left */
  187. write_data_run(p, (int)(p1 - p + 1), prn_stream,
  188. invert);
  189. /* Detect a maximal run of repeated data. */
  190. p = ++p1 + (min_repeat_run - 1);
  191. left = --left1 - (min_repeat_run - 1);
  192. while (left > 0 && *p == b)
  193. ++p, --left;
  194. for (count = p - p1; count > 0;
  195. count -= count_left
  196. ) {
  197. count_left = min(count, max_repeat_run);
  198. if (count_left > max_repeat_run_code)
  199. fputc(xdigit_code + (count_left >> 4),
  200. prn_stream),
  201. fputc(repeat_run_code + (count_left & 0xf),
  202. prn_stream);
  203. else
  204. putc(repeat_run_code + count_left,
  205. prn_stream);
  206. }
  207. if (ferror(prn_stream))
  208. return_error(gs_error_ioerror);
  209. }
  210. /* Write the remaining data, if any. */
  211. write_data_run(p, left, prn_stream, invert);
  212. }
  213. /* Clean up and return. */
  214. fputs("\n", prn_stream);
  215. psw_write_page_trailer(prn_stream, 1, true);
  216. gs_free_object(pdev->memory, line, "psmono_print_page");
  217. if (ferror(prn_stream))
  218. return_error(gs_error_ioerror);
  219. return 0;
  220. }
  221. /* Close the file. */
  222. private int
  223. psmono_close(gx_device *dev)
  224. {
  225. int code = psw_end_file(((gx_device_printer *)dev)->file, dev,
  226. &psmono_values, NULL, dev->PageCount);
  227. if (code < 0)
  228. return code;
  229. return gdev_prn_close(dev);
  230. }
  231. /* Write a run of data on the file. */
  232. private void
  233. write_data_run(const byte * data, int count, FILE * f, byte invert)
  234. {
  235. const byte *p = data;
  236. const char *const hex_digits = "0123456789abcdef";
  237. int left = count;
  238. char line[sizeof(count) * 2 + max_data_per_line * 2 + 3];
  239. char *q = line;
  240. /* Write the count. */
  241. if (!count)
  242. return;
  243. {
  244. int shift = sizeof(count) * 8;
  245. while ((shift -= 4) > 0 && (count >> shift) == 0);
  246. for (; shift > 0; shift -= 4)
  247. *q++ = xdigit_code + ((count >> shift) & 0xf);
  248. *q++ = data_run_code + (count & 0xf);
  249. }
  250. /* Write the data. */
  251. while (left > 0) {
  252. register int wcount = min(left, max_data_per_line);
  253. left -= wcount;
  254. for (; wcount > 0; ++p, --wcount) {
  255. byte b = *p ^ invert;
  256. *q++ = hex_digits[b >> 4];
  257. *q++ = hex_digits[b & 0xf];
  258. }
  259. *q++ = '\n';
  260. fwrite(line, 1, q - line, f);
  261. q = line;
  262. }
  263. }
  264. /* ---------------- Level 2 RGB driver ---------------- */
  265. /*
  266. * This driver produces plane-separated, run-length-encoded, 24-bit RGB
  267. * images suitable for a PostScript Level 2 printer. LZW compression would
  268. * be better, but Unisys' claim to own the compression algorithm and their
  269. * demand for licensing and payment even for freely distributed software
  270. * rule this out.
  271. */
  272. /* The device descriptor */
  273. private dev_proc_print_page(psrgb_print_page);
  274. private dev_proc_close_device(psrgb_close);
  275. private const gx_device_procs psrgb_procs =
  276. prn_color_procs(gdev_prn_open, gdev_prn_output_page, psrgb_close,
  277. gx_default_rgb_map_rgb_color, gx_default_rgb_map_color_rgb);
  278. const gx_device_printer gs_psrgb_device = {
  279. prn_device_body(gx_device_printer, psrgb_procs, "psrgb",
  280. DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  281. X_DPI, Y_DPI,
  282. 0, 0, 0, 0, /* margins */
  283. 3, 24, 255, 255, 256, 256, psrgb_print_page)
  284. };
  285. private const char *const psrgb_setup[] = {
  286. "/rgbimage {", /* <width> <height> rgbimage - */
  287. " gsave 2 copy scale /h exch def /w exch def",
  288. " /s1 w string def /s2 w string def /s3 w string def",
  289. " /f currentfile /ASCII85Decode filter /RunLengthDecode filter def",
  290. " w h 8 [w 0 0 h neg 0 h]",
  291. " {f s1 readstring pop} {f s2 readstring pop} {f s3 readstring pop}",
  292. " true 3 colorimage grestore",
  293. "} bind def",
  294. 0
  295. };
  296. static const gx_device_pswrite_common_t psrgb_values =
  297. PSWRITE_COMMON_VALUES(2, 0 /*false*/, 1);
  298. /* Send the page to the printer. */
  299. private int
  300. psrgb_print_page(gx_device_printer * pdev, FILE * prn_stream)
  301. {
  302. gs_memory_t *mem = pdev->memory;
  303. int width = pdev->width;
  304. byte *lbuf = gs_alloc_bytes(mem, width * 3,
  305. "psrgb_print_page(lbuf)");
  306. int lnum;
  307. stream fs, a85s, rls;
  308. stream_A85E_state a85state;
  309. stream_RLE_state rlstate;
  310. byte fsbuf[200]; /* arbitrary, must be >2 */
  311. byte a85sbuf[100]; /* arbitrary, must be >=6 */
  312. byte rlsbuf[200]; /* arbitrary, must be >128 */
  313. gx_device_pswrite_common_t pswrite_common;
  314. pswrite_common = psrgb_values;
  315. if (lbuf == 0)
  316. return_error(gs_error_VMerror);
  317. ps_image_write_headers(prn_stream, pdev, psrgb_setup, &pswrite_common);
  318. fprintf(prn_stream, "%d %d rgbimage\n", width, pdev->height);
  319. s_init(&fs, mem);
  320. swrite_file(&fs, prn_stream, fsbuf, sizeof(fsbuf));
  321. fs.memory = 0;
  322. if (s_A85E_template.set_defaults)
  323. (*s_A85E_template.set_defaults) ((stream_state *) & a85state);
  324. s_init(&a85s, mem);
  325. s_std_init(&a85s, a85sbuf, sizeof(a85sbuf), &s_filter_write_procs,
  326. s_mode_write);
  327. a85s.memory = 0;
  328. a85state.memory = 0;
  329. a85state.template = &s_A85E_template;
  330. (*s_A85E_template.init) ((stream_state *) & a85state);
  331. a85s.state = (stream_state *) & a85state;
  332. a85s.procs.process = s_A85E_template.process;
  333. a85s.strm = &fs;
  334. (*s_RLE_template.set_defaults) ((stream_state *) & rlstate);
  335. s_init(&rls, mem);
  336. s_std_init(&rls, rlsbuf, sizeof(rlsbuf), &s_filter_write_procs,
  337. s_mode_write);
  338. rls.memory = 0;
  339. rlstate.memory = 0;
  340. rlstate.template = &s_RLE_template;
  341. (*s_RLE_template.init) ((stream_state *) & rlstate);
  342. rls.state = (stream_state *) & rlstate;
  343. rls.procs.process = s_RLE_template.process;
  344. rls.strm = &a85s;
  345. for (lnum = 0; lnum < pdev->height; ++lnum) {
  346. byte *data;
  347. int i, c;
  348. gdev_prn_get_bits(pdev, lnum, lbuf, &data);
  349. for (c = 0; c < 3; ++c) {
  350. const byte *p;
  351. for (i = 0, p = data + c; i < width; ++i, p += 3)
  352. sputc(&rls, *p);
  353. if (rls.end_status == ERRC)
  354. return_error(gs_error_ioerror);
  355. }
  356. }
  357. sclose(&rls);
  358. sclose(&a85s);
  359. sflush(&fs);
  360. fputs("\n", prn_stream);
  361. psw_write_page_trailer(prn_stream, 1, true);
  362. gs_free_object(mem, lbuf, "psrgb_print_page(lbuf)");
  363. if (ferror(prn_stream))
  364. return_error(gs_error_ioerror);
  365. return 0;
  366. }
  367. /* Close the file. */
  368. private int
  369. psrgb_close(gx_device *dev)
  370. {
  371. int code = psw_end_file(((gx_device_printer *)dev)->file, dev,
  372. &psrgb_values, NULL, dev->PageCount);
  373. if (code < 0)
  374. return code;
  375. return gdev_prn_close(dev);
  376. }