output-pwg.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. // Copyright (C) 2004-2021 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include <assert.h>
  24. #include <string.h>
  25. typedef struct {
  26. fz_band_writer super;
  27. fz_pwg_options pwg;
  28. } pwg_band_writer;
  29. void
  30. fz_write_pwg_file_header(fz_context *ctx, fz_output *out)
  31. {
  32. static const unsigned char pwgsig[4] = { 'R', 'a', 'S', '2' };
  33. /* Sync word */
  34. fz_write_data(ctx, out, pwgsig, 4);
  35. }
  36. static void
  37. pwg_page_header(fz_context *ctx, fz_output *out, const fz_pwg_options *pwg,
  38. int xres, int yres, int w, int h, int bpp)
  39. {
  40. static const char zero[64] = { 0 };
  41. int i;
  42. /* Page Header: */
  43. fz_write_data(ctx, out, pwg ? pwg->media_class : zero, 64);
  44. fz_write_data(ctx, out, pwg ? pwg->media_color : zero, 64);
  45. fz_write_data(ctx, out, pwg ? pwg->media_type : zero, 64);
  46. fz_write_data(ctx, out, pwg ? pwg->output_type : zero, 64);
  47. fz_write_int32_be(ctx, out, pwg ? pwg->advance_distance : 0);
  48. fz_write_int32_be(ctx, out, pwg ? pwg->advance_media : 0);
  49. fz_write_int32_be(ctx, out, pwg ? pwg->collate : 0);
  50. fz_write_int32_be(ctx, out, pwg ? pwg->cut_media : 0);
  51. fz_write_int32_be(ctx, out, pwg ? pwg->duplex : 0);
  52. fz_write_int32_be(ctx, out, xres);
  53. fz_write_int32_be(ctx, out, yres);
  54. /* CUPS format says that 284->300 are supposed to be the bbox of the
  55. * page in points. PWG says 'Reserved'. */
  56. for (i=284; i < 300; i += 4)
  57. fz_write_data(ctx, out, zero, 4);
  58. fz_write_int32_be(ctx, out, pwg ? pwg->insert_sheet : 0);
  59. fz_write_int32_be(ctx, out, pwg ? pwg->jog : 0);
  60. fz_write_int32_be(ctx, out, pwg ? pwg->leading_edge : 0);
  61. /* CUPS format says that 312->320 are supposed to be the margins of
  62. * the lower left hand edge of page in points. PWG says 'Reserved'. */
  63. for (i=312; i < 320; i += 4)
  64. fz_write_data(ctx, out, zero, 4);
  65. fz_write_int32_be(ctx, out, pwg ? pwg->manual_feed : 0);
  66. fz_write_int32_be(ctx, out, pwg ? pwg->media_position : 0);
  67. fz_write_int32_be(ctx, out, pwg ? pwg->media_weight : 0);
  68. fz_write_int32_be(ctx, out, pwg ? pwg->mirror_print : 0);
  69. fz_write_int32_be(ctx, out, pwg ? pwg->negative_print : 0);
  70. fz_write_int32_be(ctx, out, pwg ? pwg->num_copies : 0);
  71. fz_write_int32_be(ctx, out, pwg ? pwg->orientation : 0);
  72. fz_write_int32_be(ctx, out, pwg ? pwg->output_face_up : 0);
  73. fz_write_int32_be(ctx, out, w * 72/ xres); /* Page size in points */
  74. fz_write_int32_be(ctx, out, h * 72/ yres);
  75. fz_write_int32_be(ctx, out, pwg ? pwg->separations : 0);
  76. fz_write_int32_be(ctx, out, pwg ? pwg->tray_switch : 0);
  77. fz_write_int32_be(ctx, out, pwg ? pwg->tumble : 0);
  78. fz_write_int32_be(ctx, out, w); /* Page image in pixels */
  79. fz_write_int32_be(ctx, out, h);
  80. fz_write_int32_be(ctx, out, pwg ? pwg->media_type_num : 0);
  81. fz_write_int32_be(ctx, out, bpp < 8 ? 1 : 8); /* Bits per color */
  82. fz_write_int32_be(ctx, out, bpp); /* Bits per pixel */
  83. fz_write_int32_be(ctx, out, (w * bpp + 7)/8); /* Bytes per line */
  84. fz_write_int32_be(ctx, out, 0); /* Chunky pixels */
  85. switch (bpp)
  86. {
  87. case 1: fz_write_int32_be(ctx, out, 3); /* Black */ break;
  88. case 8: fz_write_int32_be(ctx, out, 18); /* Sgray */ break;
  89. case 24: fz_write_int32_be(ctx, out, 19); /* Srgb */ break;
  90. case 32: fz_write_int32_be(ctx, out, 6); /* Cmyk */ break;
  91. default: fz_throw(ctx, FZ_ERROR_ARGUMENT, "pixmap bpp must be 1, 8, 24 or 32 to write as pwg");
  92. }
  93. fz_write_int32_be(ctx, out, pwg ? pwg->compression : 0);
  94. fz_write_int32_be(ctx, out, pwg ? pwg->row_count : 0);
  95. fz_write_int32_be(ctx, out, pwg ? pwg->row_feed : 0);
  96. fz_write_int32_be(ctx, out, pwg ? pwg->row_step : 0);
  97. fz_write_int32_be(ctx, out, bpp <= 8 ? 1 : (bpp>>8)); /* Num Colors */
  98. for (i=424; i < 452; i += 4)
  99. fz_write_data(ctx, out, zero, 4);
  100. fz_write_int32_be(ctx, out, 1); /* TotalPageCount */
  101. fz_write_int32_be(ctx, out, 1); /* CrossFeedTransform */
  102. fz_write_int32_be(ctx, out, 1); /* FeedTransform */
  103. fz_write_int32_be(ctx, out, 0); /* ImageBoxLeft */
  104. fz_write_int32_be(ctx, out, 0); /* ImageBoxTop */
  105. fz_write_int32_be(ctx, out, w); /* ImageBoxRight */
  106. fz_write_int32_be(ctx, out, h); /* ImageBoxBottom */
  107. for (i=480; i < 1668; i += 4)
  108. fz_write_data(ctx, out, zero, 4);
  109. fz_write_data(ctx, out, pwg ? pwg->rendering_intent : zero, 64);
  110. fz_write_data(ctx, out, pwg ? pwg->page_size_name : zero, 64);
  111. }
  112. /*
  113. Output a page to a pwg stream to follow a header, or other pages.
  114. */
  115. void
  116. fz_write_pixmap_as_pwg_page(fz_context *ctx, fz_output *out, const fz_pixmap *pixmap, const fz_pwg_options *pwg)
  117. {
  118. fz_band_writer *writer = fz_new_pwg_band_writer(ctx, out, pwg);
  119. fz_try(ctx)
  120. {
  121. fz_write_header(ctx, writer, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, pixmap->xres, pixmap->yres, 0, pixmap->colorspace, pixmap->seps);
  122. fz_write_band(ctx, writer, pixmap->stride, pixmap->h, pixmap->samples);
  123. fz_close_band_writer(ctx, writer);
  124. }
  125. fz_always(ctx)
  126. fz_drop_band_writer(ctx, writer);
  127. fz_catch(ctx)
  128. fz_rethrow(ctx);
  129. }
  130. /*
  131. Output a bitmap page to a pwg stream to follow a header, or other pages.
  132. */
  133. void
  134. fz_write_bitmap_as_pwg_page(fz_context *ctx, fz_output *out, const fz_bitmap *bitmap, const fz_pwg_options *pwg)
  135. {
  136. fz_band_writer *writer = fz_new_mono_pwg_band_writer(ctx, out, pwg);
  137. fz_try(ctx)
  138. {
  139. fz_write_header(ctx, writer, bitmap->w, bitmap->h, bitmap->n, 0, bitmap->xres, bitmap->yres, 0, NULL, NULL);
  140. fz_write_band(ctx, writer, bitmap->stride, bitmap->h, bitmap->samples);
  141. fz_close_band_writer(ctx, writer);
  142. }
  143. fz_always(ctx)
  144. fz_drop_band_writer(ctx, writer);
  145. fz_catch(ctx)
  146. fz_rethrow(ctx);
  147. }
  148. void
  149. fz_write_pixmap_as_pwg(fz_context *ctx, fz_output *out, const fz_pixmap *pixmap, const fz_pwg_options *pwg)
  150. {
  151. fz_write_pwg_file_header(ctx, out);
  152. fz_write_pixmap_as_pwg_page(ctx, out, pixmap, pwg);
  153. }
  154. void
  155. fz_write_bitmap_as_pwg(fz_context *ctx, fz_output *out, const fz_bitmap *bitmap, const fz_pwg_options *pwg)
  156. {
  157. fz_write_pwg_file_header(ctx, out);
  158. fz_write_bitmap_as_pwg_page(ctx, out, bitmap, pwg);
  159. }
  160. /*
  161. Save a pixmap as a pwg
  162. filename: The filename to save as (including extension).
  163. append: If non-zero, then append a new page to existing file.
  164. pwg: NULL, or a pointer to an options structure (initialised to zero
  165. before being filled in, for future expansion).
  166. */
  167. void
  168. fz_save_pixmap_as_pwg(fz_context *ctx, fz_pixmap *pixmap, char *filename, int append, const fz_pwg_options *pwg)
  169. {
  170. fz_output *out = fz_new_output_with_path(ctx, filename, append);
  171. fz_try(ctx)
  172. {
  173. if (!append)
  174. fz_write_pwg_file_header(ctx, out);
  175. fz_write_pixmap_as_pwg_page(ctx, out, pixmap, pwg);
  176. fz_close_output(ctx, out);
  177. }
  178. fz_always(ctx)
  179. fz_drop_output(ctx, out);
  180. fz_catch(ctx)
  181. fz_rethrow(ctx);
  182. }
  183. /*
  184. Save a bitmap as a pwg
  185. filename: The filename to save as (including extension).
  186. append: If non-zero, then append a new page to existing file.
  187. pwg: NULL, or a pointer to an options structure (initialised to zero
  188. before being filled in, for future expansion).
  189. */
  190. void
  191. fz_save_bitmap_as_pwg(fz_context *ctx, fz_bitmap *bitmap, char *filename, int append, const fz_pwg_options *pwg)
  192. {
  193. fz_output *out = fz_new_output_with_path(ctx, filename, append);
  194. fz_try(ctx)
  195. {
  196. if (!append)
  197. fz_write_pwg_file_header(ctx, out);
  198. fz_write_bitmap_as_pwg_page(ctx, out, bitmap, pwg);
  199. fz_close_output(ctx, out);
  200. }
  201. fz_always(ctx)
  202. fz_drop_output(ctx, out);
  203. fz_catch(ctx)
  204. fz_rethrow(ctx);
  205. }
  206. static void
  207. pwg_write_mono_header(fz_context *ctx, fz_band_writer *writer_, fz_colorspace *cs)
  208. {
  209. pwg_band_writer *writer = (pwg_band_writer *)writer_;
  210. pwg_page_header(ctx, writer->super.out, &writer->pwg,
  211. writer->super.xres, writer->super.yres, writer->super.w, writer->super.h, 1);
  212. }
  213. static void
  214. pwg_write_mono_band(fz_context *ctx, fz_band_writer *writer_, int stride, int band_start, int band_height, const unsigned char *samples)
  215. {
  216. pwg_band_writer *writer = (pwg_band_writer *)writer_;
  217. fz_output *out = writer->super.out;
  218. int w = writer->super.w;
  219. int h = writer->super.h;
  220. const unsigned char *sp;
  221. int y, x;
  222. int byte_width;
  223. /* Now output the actual bitmap, using a packbits like compression */
  224. sp = samples;
  225. byte_width = (w+7)/8;
  226. y = 0;
  227. while (y < band_height)
  228. {
  229. int yrep;
  230. assert(sp == samples + y * stride);
  231. /* Count the number of times this line is repeated */
  232. for (yrep = 1; yrep < 256 && y+yrep < h; yrep++)
  233. {
  234. if (memcmp(sp, sp + yrep * stride, byte_width) != 0)
  235. break;
  236. }
  237. fz_write_byte(ctx, out, yrep-1);
  238. /* Encode the line */
  239. x = 0;
  240. while (x < byte_width)
  241. {
  242. int d;
  243. assert(sp == samples + y * stride + x);
  244. /* How far do we have to look to find a repeated value? */
  245. for (d = 1; d < 128 && x+d < byte_width; d++)
  246. {
  247. if (sp[d-1] == sp[d])
  248. break;
  249. }
  250. if (d == 1)
  251. {
  252. int xrep;
  253. /* We immediately have a repeat (or we've hit
  254. * the end of the line). Count the number of
  255. * times this value is repeated. */
  256. for (xrep = 1; xrep < 128 && x+xrep < byte_width; xrep++)
  257. {
  258. if (sp[0] != sp[xrep])
  259. break;
  260. }
  261. fz_write_byte(ctx, out, xrep-1);
  262. fz_write_data(ctx, out, sp, 1);
  263. sp += xrep;
  264. x += xrep;
  265. }
  266. else
  267. {
  268. fz_write_byte(ctx, out, 257-d);
  269. fz_write_data(ctx, out, sp, d);
  270. sp += d;
  271. x += d;
  272. }
  273. }
  274. /* Move to the next line */
  275. sp += stride*yrep - byte_width;
  276. y += yrep;
  277. }
  278. }
  279. /*
  280. Generate a new band writer for
  281. PWG format images.
  282. */
  283. fz_band_writer *fz_new_mono_pwg_band_writer(fz_context *ctx, fz_output *out, const fz_pwg_options *pwg)
  284. {
  285. pwg_band_writer *writer = fz_new_band_writer(ctx, pwg_band_writer, out);
  286. writer->super.header = pwg_write_mono_header;
  287. writer->super.band = pwg_write_mono_band;
  288. if (pwg)
  289. writer->pwg = *pwg;
  290. else
  291. memset(&writer->pwg, 0, sizeof(writer->pwg));
  292. return &writer->super;
  293. }
  294. static void
  295. pwg_write_header(fz_context *ctx, fz_band_writer *writer_, fz_colorspace *cs)
  296. {
  297. pwg_band_writer *writer = (pwg_band_writer *)writer_;
  298. int n = writer->super.n;
  299. if (writer->super.s != 0)
  300. fz_throw(ctx, FZ_ERROR_ARGUMENT, "PWG band writer cannot cope with spot colors");
  301. if (writer->super.alpha != 0)
  302. fz_throw(ctx, FZ_ERROR_ARGUMENT, "PWG band writer cannot cope with alpha");
  303. if (n != 1 && n != 3 && n != 4)
  304. fz_throw(ctx, FZ_ERROR_ARGUMENT, "pixmap must be grayscale, rgb or cmyk to write as pwg");
  305. pwg_page_header(ctx, writer->super.out, &writer->pwg,
  306. writer->super.xres, writer->super.yres, writer->super.w, writer->super.h, n*8);
  307. }
  308. static void
  309. pwg_write_band(fz_context *ctx, fz_band_writer *writer_, int stride, int band_start, int band_height, const unsigned char *samples)
  310. {
  311. pwg_band_writer *writer = (pwg_band_writer *)writer_;
  312. fz_output *out = writer->super.out;
  313. int w = writer->super.w;
  314. int h = writer->super.h;
  315. const unsigned char *sp = samples;
  316. int n = writer->super.n;
  317. int ss = w * n;
  318. int y, x;
  319. /* Now output the actual bitmap, using a packbits like compression */
  320. y = 0;
  321. while (y < h)
  322. {
  323. int yrep;
  324. assert(sp == samples + y * stride);
  325. /* Count the number of times this line is repeated */
  326. for (yrep = 1; yrep < 256 && y+yrep < h; yrep++)
  327. {
  328. if (memcmp(sp, sp + yrep * stride, ss) != 0)
  329. break;
  330. }
  331. fz_write_byte(ctx, out, yrep-1);
  332. /* Encode the line */
  333. x = 0;
  334. while (x < w)
  335. {
  336. int d;
  337. assert(sp == samples + y * stride + x * n);
  338. /* How far do we have to look to find a repeated value? */
  339. for (d = 1; d < 128 && x+d < w; d++)
  340. {
  341. if (memcmp(sp + (d-1)*n, sp + d*n, n) == 0)
  342. break;
  343. }
  344. if (d == 1)
  345. {
  346. int xrep;
  347. /* We immediately have a repeat (or we've hit
  348. * the end of the line). Count the number of
  349. * times this value is repeated. */
  350. for (xrep = 1; xrep < 128 && x+xrep < w; xrep++)
  351. {
  352. if (memcmp(sp, sp + xrep*n, n) != 0)
  353. break;
  354. }
  355. fz_write_byte(ctx, out, xrep-1);
  356. fz_write_data(ctx, out, sp, n);
  357. sp += n*xrep;
  358. x += xrep;
  359. }
  360. else
  361. {
  362. fz_write_byte(ctx, out, 257-d);
  363. x += d;
  364. while (d > 0)
  365. {
  366. fz_write_data(ctx, out, sp, n);
  367. sp += n;
  368. d--;
  369. }
  370. }
  371. }
  372. /* Move to the next line */
  373. sp += stride*(yrep-1);
  374. y += yrep;
  375. }
  376. }
  377. /*
  378. Generate a new band writer for
  379. contone PWG format images.
  380. */
  381. fz_band_writer *fz_new_pwg_band_writer(fz_context *ctx, fz_output *out, const fz_pwg_options *pwg)
  382. {
  383. pwg_band_writer *writer = fz_new_band_writer(ctx, pwg_band_writer, out);
  384. writer->super.header = pwg_write_header;
  385. writer->super.band = pwg_write_band;
  386. if (pwg)
  387. writer->pwg = *pwg;
  388. else
  389. memset(&writer->pwg, 0, sizeof(writer->pwg));
  390. return &writer->super;
  391. }
  392. /* High-level document writer interface */
  393. const char *fz_pwg_write_options_usage =
  394. "PWG output options:\n"
  395. "\tmedia_class=<string>: set the media_class field\n"
  396. "\tmedia_color=<string>: set the media_color field\n"
  397. "\tmedia_type=<string>: set the media_type field\n"
  398. "\toutput_type=<string>: set the output_type field\n"
  399. "\trendering_intent=<string>: set the rendering_intent field\n"
  400. "\tpage_size_name=<string>: set the page_size_name field\n"
  401. "\tadvance_distance=<int>: set the advance_distance field\n"
  402. "\tadvance_media=<int>: set the advance_media field\n"
  403. "\tcollate=<int>: set the collate field\n"
  404. "\tcut_media=<int>: set the cut_media field\n"
  405. "\tduplex=<int>: set the duplex field\n"
  406. "\tinsert_sheet=<int>: set the insert_sheet field\n"
  407. "\tjog=<int>: set the jog field\n"
  408. "\tleading_edge=<int>: set the leading_edge field\n"
  409. "\tmanual_feed=<int>: set the manual_feed field\n"
  410. "\tmedia_position=<int>: set the media_position field\n"
  411. "\tmedia_weight=<int>: set the media_weight field\n"
  412. "\tmirror_print=<int>: set the mirror_print field\n"
  413. "\tnegative_print=<int>: set the negative_print field\n"
  414. "\tnum_copies=<int>: set the num_copies field\n"
  415. "\torientation=<int>: set the orientation field\n"
  416. "\toutput_face_up=<int>: set the output_face_up field\n"
  417. "\tpage_size_x=<int>: set the page_size_x field\n"
  418. "\tpage_size_y=<int>: set the page_size_y field\n"
  419. "\tseparations=<int>: set the separations field\n"
  420. "\ttray_switch=<int>: set the tray_switch field\n"
  421. "\ttumble=<int>: set the tumble field\n"
  422. "\tmedia_type_num=<int>: set the media_type_num field\n"
  423. "\tcompression=<int>: set the compression field\n"
  424. "\trow_count=<int>: set the row_count field\n"
  425. "\trow_feed=<int>: set the row_feed field\n"
  426. "\trow_step=<int>: set the row_step field\n"
  427. "\n";
  428. static void
  429. warn_if_long(fz_context *ctx, const char *str, size_t ret)
  430. {
  431. if (ret > 0)
  432. fz_warn(ctx, "Option %s is too long, truncated.", str);
  433. }
  434. fz_pwg_options *
  435. fz_parse_pwg_options(fz_context *ctx, fz_pwg_options *opts, const char *args)
  436. {
  437. const char *val;
  438. memset(opts, 0, sizeof *opts);
  439. if (fz_has_option(ctx, args, "media_class", &val))
  440. warn_if_long(ctx, "media_class", fz_copy_option(ctx, val, opts->media_class, 64));
  441. if (fz_has_option(ctx, args, "media_color", &val))
  442. warn_if_long(ctx, "media_color", fz_copy_option(ctx, val, opts->media_color, 64));
  443. if (fz_has_option(ctx, args, "media_type", &val))
  444. warn_if_long(ctx, "media_type", fz_copy_option(ctx, val, opts->media_type, 64));
  445. if (fz_has_option(ctx, args, "output_type", &val))
  446. warn_if_long(ctx, "output_type", fz_copy_option(ctx, val, opts->output_type, 64));
  447. if (fz_has_option(ctx, args, "rendering_intent", &val))
  448. warn_if_long(ctx, "rendering_intent", fz_copy_option(ctx, val, opts->rendering_intent, 64));
  449. if (fz_has_option(ctx, args, "page_size_name", &val))
  450. warn_if_long(ctx, "page_size_name", fz_copy_option(ctx, val, opts->page_size_name, 64));
  451. if (fz_has_option(ctx, args, "advance_distance", &val))
  452. opts->advance_distance = fz_atoi(val);
  453. if (fz_has_option(ctx, args, "advance_media", &val))
  454. opts->advance_media = fz_atoi(val);
  455. if (fz_has_option(ctx, args, "collate", &val))
  456. opts->collate = fz_atoi(val);
  457. if (fz_has_option(ctx, args, "cut_media", &val))
  458. opts->cut_media = fz_atoi(val);
  459. if (fz_has_option(ctx, args, "duplex", &val))
  460. opts->duplex = fz_atoi(val);
  461. if (fz_has_option(ctx, args, "insert_sheet", &val))
  462. opts->insert_sheet = fz_atoi(val);
  463. if (fz_has_option(ctx, args, "jog", &val))
  464. opts->jog = fz_atoi(val);
  465. if (fz_has_option(ctx, args, "leading_edge", &val))
  466. opts->leading_edge = fz_atoi(val);
  467. if (fz_has_option(ctx, args, "manual_feed", &val))
  468. opts->manual_feed = fz_atoi(val);
  469. if (fz_has_option(ctx, args, "media_position", &val))
  470. opts->media_position = fz_atoi(val);
  471. if (fz_has_option(ctx, args, "media_weight", &val))
  472. opts->media_weight = fz_atoi(val);
  473. if (fz_has_option(ctx, args, "mirror_print", &val))
  474. opts->mirror_print = fz_atoi(val);
  475. if (fz_has_option(ctx, args, "negative_print", &val))
  476. opts->negative_print = fz_atoi(val);
  477. if (fz_has_option(ctx, args, "num_copies", &val))
  478. opts->num_copies = fz_atoi(val);
  479. if (fz_has_option(ctx, args, "orientation", &val))
  480. opts->orientation = fz_atoi(val);
  481. if (fz_has_option(ctx, args, "output_face_up", &val))
  482. opts->output_face_up = fz_atoi(val);
  483. if (fz_has_option(ctx, args, "page_size_x", &val))
  484. opts->PageSize[0] = fz_atoi(val);
  485. if (fz_has_option(ctx, args, "page_size_y", &val))
  486. opts->PageSize[1] = fz_atoi(val);
  487. if (fz_has_option(ctx, args, "separations", &val))
  488. opts->separations = fz_atoi(val);
  489. if (fz_has_option(ctx, args, "tray_switch", &val))
  490. opts->tray_switch = fz_atoi(val);
  491. if (fz_has_option(ctx, args, "tumble", &val))
  492. opts->tumble = fz_atoi(val);
  493. if (fz_has_option(ctx, args, "media_type_num", &val))
  494. opts->media_type_num = fz_atoi(val);
  495. if (fz_has_option(ctx, args, "compression", &val))
  496. opts->compression = fz_atoi(val);
  497. if (fz_has_option(ctx, args, "row_count", &val))
  498. opts->row_count = fz_atoi(val);
  499. if (fz_has_option(ctx, args, "row_feed", &val))
  500. opts->row_feed = fz_atoi(val);
  501. if (fz_has_option(ctx, args, "row_step", &val))
  502. opts->row_step = fz_atoi(val);
  503. return opts;
  504. }
  505. typedef struct
  506. {
  507. fz_document_writer super;
  508. fz_draw_options draw;
  509. fz_pwg_options pwg;
  510. int mono;
  511. fz_pixmap *pixmap;
  512. fz_output *out;
  513. } fz_pwg_writer;
  514. static fz_device *
  515. pwg_begin_page(fz_context *ctx, fz_document_writer *wri_, fz_rect mediabox)
  516. {
  517. fz_pwg_writer *wri = (fz_pwg_writer*)wri_;
  518. return fz_new_draw_device_with_options(ctx, &wri->draw, mediabox, &wri->pixmap);
  519. }
  520. static void
  521. pwg_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev)
  522. {
  523. fz_pwg_writer *wri = (fz_pwg_writer*)wri_;
  524. fz_bitmap *bitmap = NULL;
  525. fz_var(bitmap);
  526. fz_try(ctx)
  527. {
  528. fz_close_device(ctx, dev);
  529. if (wri->mono)
  530. {
  531. bitmap = fz_new_bitmap_from_pixmap(ctx, wri->pixmap, NULL);
  532. fz_write_bitmap_as_pwg_page(ctx, wri->out, bitmap, &wri->pwg);
  533. }
  534. else
  535. {
  536. fz_write_pixmap_as_pwg_page(ctx, wri->out, wri->pixmap, &wri->pwg);
  537. }
  538. }
  539. fz_always(ctx)
  540. {
  541. fz_drop_device(ctx, dev);
  542. fz_drop_bitmap(ctx, bitmap);
  543. fz_drop_pixmap(ctx, wri->pixmap);
  544. wri->pixmap = NULL;
  545. }
  546. fz_catch(ctx)
  547. {
  548. fz_rethrow(ctx);
  549. }
  550. }
  551. static void
  552. pwg_close_writer(fz_context *ctx, fz_document_writer *wri_)
  553. {
  554. fz_pwg_writer *wri = (fz_pwg_writer*)wri_;
  555. fz_close_output(ctx, wri->out);
  556. }
  557. static void
  558. pwg_drop_writer(fz_context *ctx, fz_document_writer *wri_)
  559. {
  560. fz_pwg_writer *wri = (fz_pwg_writer*)wri_;
  561. fz_drop_pixmap(ctx, wri->pixmap);
  562. fz_drop_output(ctx, wri->out);
  563. }
  564. fz_document_writer *
  565. fz_new_pwg_writer_with_output(fz_context *ctx, fz_output *out, const char *options)
  566. {
  567. fz_pwg_writer *wri = NULL;
  568. const char *val;
  569. fz_var(wri);
  570. fz_try(ctx)
  571. {
  572. wri = fz_new_derived_document_writer(ctx, fz_pwg_writer, pwg_begin_page, pwg_end_page, pwg_close_writer, pwg_drop_writer);
  573. fz_parse_draw_options(ctx, &wri->draw, options);
  574. fz_parse_pwg_options(ctx, &wri->pwg, options);
  575. if (fz_has_option(ctx, options, "colorspace", &val))
  576. if (fz_option_eq(val, "mono"))
  577. wri->mono = 1;
  578. wri->out = out;
  579. fz_write_pwg_file_header(ctx, wri->out);
  580. }
  581. fz_catch(ctx)
  582. {
  583. fz_drop_output(ctx, out);
  584. fz_free(ctx, wri);
  585. fz_rethrow(ctx);
  586. }
  587. return (fz_document_writer*)wri;
  588. }
  589. fz_document_writer *
  590. fz_new_pwg_writer(fz_context *ctx, const char *path, const char *options)
  591. {
  592. fz_output *out = fz_new_output_with_path(ctx, path ? path : "out.pwg", 0);
  593. return fz_new_pwg_writer_with_output(ctx, out, options);
  594. }