gdevpsdu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /* Copyright (C) 1997, 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: gdevpsdu.c,v 1.23 2005/03/14 18:08:36 dan Exp $ */
  14. /* Common utilities for PostScript and PDF writers */
  15. #include "stdio_.h" /* for FILE for jpeglib.h */
  16. #include "jpeglib_.h" /* for sdct.h */
  17. #include "memory_.h"
  18. #include "gx.h"
  19. #include "gserrors.h"
  20. #include "gdevpsdf.h"
  21. #include "strimpl.h"
  22. #include "sa85x.h"
  23. #include "scfx.h"
  24. #include "sdct.h"
  25. #include "sjpeg.h"
  26. #include "spprint.h"
  27. #include "sstring.h"
  28. #include "gsovrc.h"
  29. /* Structure descriptors */
  30. public_st_device_psdf();
  31. public_st_psdf_binary_writer();
  32. /* Standard color command names. */
  33. const psdf_set_color_commands_t psdf_set_fill_color_commands = {
  34. "g", "rg", "k", "cs", "sc", "scn"
  35. };
  36. const psdf_set_color_commands_t psdf_set_stroke_color_commands = {
  37. "G", "RG", "K", "CS", "SC", "SCN"
  38. };
  39. /* Define parameter-setting procedures. */
  40. extern stream_state_proc_put_params(s_DCTE_put_params, stream_DCT_state);
  41. /* ---------------- Vector implementation procedures ---------------- */
  42. int
  43. psdf_setlinewidth(gx_device_vector * vdev, floatp width)
  44. {
  45. pprintg1(gdev_vector_stream(vdev), "%g w\n", width);
  46. return 0;
  47. }
  48. int
  49. psdf_setlinecap(gx_device_vector * vdev, gs_line_cap cap)
  50. {
  51. pprintd1(gdev_vector_stream(vdev), "%d J\n", cap);
  52. return 0;
  53. }
  54. int
  55. psdf_setlinejoin(gx_device_vector * vdev, gs_line_join join)
  56. {
  57. pprintd1(gdev_vector_stream(vdev), "%d j\n", join);
  58. return 0;
  59. }
  60. int
  61. psdf_setmiterlimit(gx_device_vector * vdev, floatp limit)
  62. {
  63. pprintg1(gdev_vector_stream(vdev), "%g M\n", limit);
  64. return 0;
  65. }
  66. int
  67. psdf_setdash(gx_device_vector * vdev, const float *pattern, uint count,
  68. floatp offset)
  69. {
  70. stream *s = gdev_vector_stream(vdev);
  71. int i;
  72. stream_puts(s, "[ ");
  73. for (i = 0; i < count; ++i)
  74. pprintg1(s, "%g ", pattern[i]);
  75. pprintg1(s, "] %g d\n", offset);
  76. return 0;
  77. }
  78. int
  79. psdf_setflat(gx_device_vector * vdev, floatp flatness)
  80. {
  81. pprintg1(gdev_vector_stream(vdev), "%g i\n", flatness);
  82. return 0;
  83. }
  84. int
  85. psdf_setlogop(gx_device_vector * vdev, gs_logical_operation_t lop,
  86. gs_logical_operation_t diff)
  87. {
  88. /****** SHOULD AT LEAST DETECT SET-0 & SET-1 ******/
  89. return 0;
  90. }
  91. int
  92. psdf_dorect(gx_device_vector * vdev, fixed x0, fixed y0, fixed x1, fixed y1,
  93. gx_path_type_t type)
  94. {
  95. int code = (*vdev_proc(vdev, beginpath)) (vdev, type);
  96. if (code < 0)
  97. return code;
  98. pprintg4(gdev_vector_stream(vdev), "%g %g %g %g re\n",
  99. fixed2float(x0), fixed2float(y0),
  100. fixed2float(x1 - x0), fixed2float(y1 - y0));
  101. return (*vdev_proc(vdev, endpath)) (vdev, type);
  102. }
  103. int
  104. psdf_beginpath(gx_device_vector * vdev, gx_path_type_t type)
  105. {
  106. return 0;
  107. }
  108. int
  109. psdf_moveto(gx_device_vector * vdev, floatp x0, floatp y0, floatp x, floatp y,
  110. gx_path_type_t type)
  111. {
  112. pprintg2(gdev_vector_stream(vdev), "%g %g m\n", x, y);
  113. return 0;
  114. }
  115. int
  116. psdf_lineto(gx_device_vector * vdev, floatp x0, floatp y0, floatp x, floatp y,
  117. gx_path_type_t type)
  118. {
  119. pprintg2(gdev_vector_stream(vdev), "%g %g l\n", x, y);
  120. return 0;
  121. }
  122. int
  123. psdf_curveto(gx_device_vector * vdev, floatp x0, floatp y0,
  124. floatp x1, floatp y1, floatp x2, floatp y2, floatp x3, floatp y3,
  125. gx_path_type_t type)
  126. {
  127. if (x1 == x0 && y1 == y0 && x2 == x3 && y2 == y3)
  128. pprintg2(gdev_vector_stream(vdev), "%g %g l\n", x3, y3);
  129. else if (x1 == x0 && y1 == y0)
  130. pprintg4(gdev_vector_stream(vdev), "%g %g %g %g v\n",
  131. x2, y2, x3, y3);
  132. else if (x3 == x2 && y3 == y2)
  133. pprintg4(gdev_vector_stream(vdev), "%g %g %g %g y\n",
  134. x1, y1, x2, y2);
  135. else
  136. pprintg6(gdev_vector_stream(vdev), "%g %g %g %g %g %g c\n",
  137. x1, y1, x2, y2, x3, y3);
  138. return 0;
  139. }
  140. int
  141. psdf_closepath(gx_device_vector * vdev, floatp x0, floatp y0,
  142. floatp x_start, floatp y_start, gx_path_type_t type)
  143. {
  144. stream_puts(gdev_vector_stream(vdev), "h\n");
  145. return 0;
  146. }
  147. /* endpath is deliberately omitted. */
  148. /* ---------------- Utilities ---------------- */
  149. gx_color_index
  150. psdf_adjust_color_index(gx_device_vector *vdev, gx_color_index color)
  151. {
  152. /*
  153. * Since gx_no_color_index is all 1's, we can't represent
  154. * a CMYK color consisting of full ink in all 4 components.
  155. * However, this color must be available for registration marks.
  156. * gxcmap.c fudges this by changing the K component to 254;
  157. * undo this fudge here.
  158. */
  159. return (color == (gx_no_color_index ^ 1) ? gx_no_color_index : color);
  160. }
  161. /* Round a double value to a specified precision. */
  162. double
  163. psdf_round(double v, int precision, int radix)
  164. {
  165. double mul = 1;
  166. double w = v;
  167. if (w <= 0)
  168. return w;
  169. while (w < precision) {
  170. w *= radix;
  171. mul *= radix;
  172. }
  173. return (int)(w + 0.5) / mul;
  174. }
  175. /*
  176. * Since we only have 8 bits of color to start with, round the
  177. * values to 3 digits for more compact output.
  178. */
  179. private inline double
  180. round_byte_color(gx_color_index cv)
  181. {
  182. return (int)((uint)cv * (1000.0 / 255.0) + 0.5) / 1000.0;
  183. }
  184. int
  185. psdf_set_color(gx_device_vector * vdev, const gx_drawing_color * pdc,
  186. const psdf_set_color_commands_t *ppscc)
  187. {
  188. const char *setcolor;
  189. if (!gx_dc_is_pure(pdc))
  190. return_error(gs_error_rangecheck);
  191. {
  192. stream *s = gdev_vector_stream(vdev);
  193. gx_color_index color =
  194. psdf_adjust_color_index(vdev, gx_dc_pure_color(pdc));
  195. /*
  196. * Normally we would precompute all of v0 .. v3, but gcc 2.7.2.3
  197. * generates incorrect code for Intel CPUs if we do this. The code
  198. * below is longer, but does less computation in some cases.
  199. */
  200. double v3 = round_byte_color(color & 0xff);
  201. switch (vdev->color_info.num_components) {
  202. case 4:
  203. /* if (v0 == 0 && v1 == 0 && v2 == 0 && ...) */
  204. if ((color & 0xffffff00) == 0 && ppscc->setgray != 0) {
  205. v3 = 1.0 - v3;
  206. goto g;
  207. }
  208. pprintg4(s, "%g %g %g %g", round_byte_color(color >> 24),
  209. round_byte_color((color >> 16) & 0xff),
  210. round_byte_color((color >> 8) & 0xff), v3);
  211. setcolor = ppscc->setcmykcolor;
  212. break;
  213. case 3:
  214. /* if (v1 == v2 && v2 == v3 && ...) */
  215. if (!((color ^ (color >> 8)) & 0xffff) && ppscc->setgray != 0)
  216. goto g;
  217. pprintg3(s, "%g %g %g", round_byte_color((color >> 16) & 0xff),
  218. round_byte_color((color >> 8) & 0xff), v3);
  219. setcolor = ppscc->setrgbcolor;
  220. break;
  221. case 1:
  222. g:
  223. pprintg1(s, "%g", v3);
  224. setcolor = ppscc->setgray;
  225. break;
  226. default: /* can't happen */
  227. return_error(gs_error_rangecheck);
  228. }
  229. if (setcolor)
  230. pprints1(s, " %s\n", setcolor);
  231. }
  232. return 0;
  233. }
  234. /* ---------------- Binary data writing ---------------- */
  235. /* Begin writing binary data. */
  236. int
  237. psdf_begin_binary(gx_device_psdf * pdev, psdf_binary_writer * pbw)
  238. {
  239. gs_memory_t *mem = pbw->memory = pdev->v_memory;
  240. pbw->target = pdev->strm;
  241. pbw->dev = pdev;
  242. pbw->strm = 0; /* for GC in case of failure */
  243. /* If not binary, set up the encoding stream. */
  244. if (!pdev->binary_ok) {
  245. #define BUF_SIZE 100 /* arbitrary */
  246. byte *buf = gs_alloc_bytes(mem, BUF_SIZE, "psdf_begin_binary(buf)");
  247. stream_A85E_state *ss = (stream_A85E_state *)
  248. s_alloc_state(mem, s_A85E_template.stype,
  249. "psdf_begin_binary(stream_state)");
  250. stream *s = s_alloc(mem, "psdf_begin_binary(stream)");
  251. if (buf == 0 || ss == 0 || s == 0) {
  252. gs_free_object(mem, s, "psdf_begin_binary(stream)");
  253. gs_free_object(mem, ss, "psdf_begin_binary(stream_state)");
  254. gs_free_object(mem, buf, "psdf_begin_binary(buf)");
  255. return_error(gs_error_VMerror);
  256. }
  257. ss->template = &s_A85E_template;
  258. s_init_filter(s, (stream_state *)ss, buf, BUF_SIZE, pdev->strm);
  259. #undef BUF_SIZE
  260. pbw->strm = s;
  261. } else {
  262. pbw->strm = pdev->strm;
  263. }
  264. return 0;
  265. }
  266. /* Add an encoding filter. The client must have allocated the stream state, */
  267. /* if any, using pdev->v_memory. */
  268. int
  269. psdf_encode_binary(psdf_binary_writer * pbw, const stream_template * template,
  270. stream_state * ss)
  271. {
  272. return (s_add_filter(&pbw->strm, template, ss, pbw->memory) == 0 ?
  273. gs_note_error(gs_error_VMerror) : 0);
  274. }
  275. /*
  276. * Acquire parameters, and optionally set up the filter for, a DCTEncode
  277. * filter. This is a separate procedure so it can be used to validate
  278. * filter parameters when they are set, rather than waiting until they are
  279. * used. pbw = NULL means just set up the stream state.
  280. */
  281. int
  282. psdf_DCT_filter(gs_param_list *plist /* may be NULL */,
  283. stream_state /*stream_DCTE_state*/ *st,
  284. int Columns, int Rows, int Colors,
  285. psdf_binary_writer *pbw /* may be NULL */)
  286. {
  287. stream_DCT_state *const ss = (stream_DCT_state *) st;
  288. gs_memory_t *mem = st->memory;
  289. jpeg_compress_data *jcdp;
  290. gs_c_param_list rcc_list;
  291. int code;
  292. /*
  293. * "Wrap" the actual Dict or ACSDict parameter list in one that
  294. * sets Rows, Columns, and Colors.
  295. */
  296. gs_c_param_list_write(&rcc_list, mem);
  297. if ((code = param_write_int((gs_param_list *)&rcc_list, "Rows",
  298. &Rows)) < 0 ||
  299. (code = param_write_int((gs_param_list *)&rcc_list, "Columns",
  300. &Columns)) < 0 ||
  301. (code = param_write_int((gs_param_list *)&rcc_list, "Colors",
  302. &Colors)) < 0
  303. ) {
  304. goto rcc_fail;
  305. }
  306. gs_c_param_list_read(&rcc_list);
  307. if (plist)
  308. gs_c_param_list_set_target(&rcc_list, plist);
  309. /* Allocate space for IJG parameters. */
  310. jcdp = gs_alloc_struct_immovable(mem, jpeg_compress_data,
  311. &st_jpeg_compress_data, "zDCTE");
  312. if (jcdp == 0)
  313. return_error(gs_error_VMerror);
  314. ss->data.compress = jcdp;
  315. jcdp->memory = ss->jpeg_memory = mem; /* set now for allocation */
  316. if ((code = gs_jpeg_create_compress(ss)) < 0)
  317. goto dcte_fail; /* correct to do jpeg_destroy here */
  318. /* Read parameters from dictionary */
  319. s_DCTE_put_params((gs_param_list *)&rcc_list, ss); /* ignore errors */
  320. /* Create the filter. */
  321. jcdp->template = s_DCTE_template;
  322. /* Make sure we get at least a full scan line of input. */
  323. ss->scan_line_size = jcdp->cinfo.input_components *
  324. jcdp->cinfo.image_width;
  325. jcdp->template.min_in_size =
  326. max(s_DCTE_template.min_in_size, ss->scan_line_size);
  327. /* Make sure we can write the user markers in a single go. */
  328. jcdp->template.min_out_size =
  329. max(s_DCTE_template.min_out_size, ss->Markers.size);
  330. if (pbw)
  331. code = psdf_encode_binary(pbw, &jcdp->template, st);
  332. if (code >= 0) {
  333. gs_c_param_list_release(&rcc_list);
  334. return 0;
  335. }
  336. dcte_fail:
  337. gs_jpeg_destroy(ss);
  338. gs_free_object(mem, jcdp, "setup_image_compression");
  339. rcc_fail:
  340. gs_c_param_list_release(&rcc_list);
  341. return code;
  342. }
  343. /* Add a 2-D CCITTFax encoding filter. */
  344. /* Set EndOfBlock iff the stream is not ASCII85 encoded. */
  345. int
  346. psdf_CFE_binary(psdf_binary_writer * pbw, int w, int h, bool invert)
  347. {
  348. gs_memory_t *mem = pbw->memory;
  349. const stream_template *template = &s_CFE_template;
  350. stream_CFE_state *st =
  351. gs_alloc_struct(mem, stream_CFE_state, template->stype,
  352. "psdf_CFE_binary");
  353. int code;
  354. if (st == 0)
  355. return_error(gs_error_VMerror);
  356. (*template->set_defaults) ((stream_state *) st);
  357. st->K = -1;
  358. st->Columns = w;
  359. st->Rows = 0;
  360. st->BlackIs1 = !invert;
  361. st->EndOfBlock = pbw->strm->state->template != &s_A85E_template;
  362. code = psdf_encode_binary(pbw, template, (stream_state *) st);
  363. if (code < 0)
  364. gs_free_object(mem, st, "psdf_CFE_binary");
  365. return code;
  366. }
  367. /* Finish writing binary data. */
  368. int
  369. psdf_end_binary(psdf_binary_writer * pbw)
  370. {
  371. int status = s_close_filters(&pbw->strm, pbw->target);
  372. return (status >= 0 ? 0 : gs_note_error(gs_error_ioerror));
  373. }
  374. /* ---------------- Overprint, Get Bits ---------------- */
  375. /*
  376. * High level devices cannot perform get_bits or get_bits_rectangle
  377. * operations, for obvious reasons.
  378. */
  379. int
  380. psdf_get_bits(gx_device * dev, int y, byte * data, byte ** actual_data)
  381. {
  382. return_error(gs_error_unregistered);
  383. }
  384. int
  385. psdf_get_bits_rectangle(
  386. gx_device * dev,
  387. const gs_int_rect * prect,
  388. gs_get_bits_params_t * params,
  389. gs_int_rect ** unread )
  390. {
  391. return_error(gs_error_unregistered);
  392. }
  393. /*
  394. * Create compositor procedure for PostScript/PDF writer. Since these
  395. * devices directly support overprint (and have access to the imager
  396. * state), no compositor is required for overprint support. Hence, this
  397. * routine just recognizes and discards invocations of the overprint
  398. * compositor.
  399. */
  400. int
  401. psdf_create_compositor(
  402. gx_device * dev,
  403. gx_device ** pcdev,
  404. const gs_composite_t * pct,
  405. gs_imager_state * pis,
  406. gs_memory_t * mem )
  407. {
  408. if (gs_is_overprint_compositor(pct)) {
  409. *pcdev = dev;
  410. return 0;
  411. } else
  412. return gx_default_create_compositor(dev, pcdev, pct, pis, mem);
  413. }