comparison mupdf-source/source/fitz/output-pwg.c @ 2:b50eed0cc0ef upstream

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