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