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