/src/mupdf/source/fitz/output-pwg.c
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
36 | 0 | static const unsigned char pwgsig[4] = { 'R', 'a', 'S', '2' }; |
37 | | |
38 | | /* Sync word */ |
39 | 0 | fz_write_data(ctx, out, pwgsig, 4); |
40 | 0 | } |
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 | 0 | { |
46 | 0 | static const char zero[64] = { 0 }; |
47 | 0 | int i; |
48 | | |
49 | | /* Page Header: */ |
50 | 0 | fz_write_data(ctx, out, pwg ? pwg->media_class : zero, 64); |
51 | 0 | fz_write_data(ctx, out, pwg ? pwg->media_color : zero, 64); |
52 | 0 | fz_write_data(ctx, out, pwg ? pwg->media_type : zero, 64); |
53 | 0 | fz_write_data(ctx, out, pwg ? pwg->output_type : zero, 64); |
54 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->advance_distance : 0); |
55 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->advance_media : 0); |
56 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->collate : 0); |
57 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->cut_media : 0); |
58 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->duplex : 0); |
59 | 0 | fz_write_int32_be(ctx, out, xres); |
60 | 0 | 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 | 0 | for (i=284; i < 300; i += 4) |
64 | 0 | fz_write_data(ctx, out, zero, 4); |
65 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->insert_sheet : 0); |
66 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->jog : 0); |
67 | 0 | 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 | 0 | for (i=312; i < 320; i += 4) |
71 | 0 | fz_write_data(ctx, out, zero, 4); |
72 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->manual_feed : 0); |
73 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->media_position : 0); |
74 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->media_weight : 0); |
75 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->mirror_print : 0); |
76 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->negative_print : 0); |
77 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->num_copies : 0); |
78 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->orientation : 0); |
79 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->output_face_up : 0); |
80 | 0 | fz_write_int32_be(ctx, out, w * 72/ xres); /* Page size in points */ |
81 | 0 | fz_write_int32_be(ctx, out, h * 72/ yres); |
82 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->separations : 0); |
83 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->tray_switch : 0); |
84 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->tumble : 0); |
85 | 0 | fz_write_int32_be(ctx, out, w); /* Page image in pixels */ |
86 | 0 | fz_write_int32_be(ctx, out, h); |
87 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->media_type_num : 0); |
88 | 0 | fz_write_int32_be(ctx, out, bpp < 8 ? 1 : 8); /* Bits per color */ |
89 | 0 | fz_write_int32_be(ctx, out, bpp); /* Bits per pixel */ |
90 | 0 | fz_write_int32_be(ctx, out, (w * bpp + 7)/8); /* Bytes per line */ |
91 | 0 | fz_write_int32_be(ctx, out, 0); /* Chunky pixels */ |
92 | 0 | switch (bpp) |
93 | 0 | { |
94 | 0 | case 1: fz_write_int32_be(ctx, out, 3); /* Black */ break; |
95 | 0 | case 8: fz_write_int32_be(ctx, out, 18); /* Sgray */ break; |
96 | 0 | case 24: fz_write_int32_be(ctx, out, 19); /* Srgb */ break; |
97 | 0 | case 32: fz_write_int32_be(ctx, out, 6); /* Cmyk */ break; |
98 | 0 | default: fz_throw(ctx, FZ_ERROR_GENERIC, "pixmap bpp must be 1, 8, 24 or 32 to write as pwg"); |
99 | 0 | } |
100 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->compression : 0); |
101 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->row_count : 0); |
102 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->row_feed : 0); |
103 | 0 | fz_write_int32_be(ctx, out, pwg ? pwg->row_step : 0); |
104 | 0 | fz_write_int32_be(ctx, out, bpp <= 8 ? 1 : (bpp>>8)); /* Num Colors */ |
105 | 0 | for (i=424; i < 452; i += 4) |
106 | 0 | fz_write_data(ctx, out, zero, 4); |
107 | 0 | fz_write_int32_be(ctx, out, 1); /* TotalPageCount */ |
108 | 0 | fz_write_int32_be(ctx, out, 1); /* CrossFeedTransform */ |
109 | 0 | fz_write_int32_be(ctx, out, 1); /* FeedTransform */ |
110 | 0 | fz_write_int32_be(ctx, out, 0); /* ImageBoxLeft */ |
111 | 0 | fz_write_int32_be(ctx, out, 0); /* ImageBoxTop */ |
112 | 0 | fz_write_int32_be(ctx, out, w); /* ImageBoxRight */ |
113 | 0 | fz_write_int32_be(ctx, out, h); /* ImageBoxBottom */ |
114 | 0 | for (i=480; i < 1668; i += 4) |
115 | 0 | fz_write_data(ctx, out, zero, 4); |
116 | 0 | fz_write_data(ctx, out, pwg ? pwg->rendering_intent : zero, 64); |
117 | 0 | fz_write_data(ctx, out, pwg ? pwg->page_size_name : zero, 64); |
118 | 0 | } |
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 | 0 | { |
126 | 0 | fz_band_writer *writer = fz_new_pwg_band_writer(ctx, out, pwg); |
127 | |
|
128 | 0 | fz_try(ctx) |
129 | 0 | { |
130 | 0 | fz_write_header(ctx, writer, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, pixmap->xres, pixmap->yres, 0, pixmap->colorspace, pixmap->seps); |
131 | 0 | fz_write_band(ctx, writer, pixmap->stride, pixmap->h, pixmap->samples); |
132 | 0 | fz_close_band_writer(ctx, writer); |
133 | 0 | } |
134 | 0 | fz_always(ctx) |
135 | 0 | fz_drop_band_writer(ctx, writer); |
136 | 0 | fz_catch(ctx) |
137 | 0 | fz_rethrow(ctx); |
138 | 0 | } |
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 | 0 | { |
146 | 0 | fz_band_writer *writer = fz_new_mono_pwg_band_writer(ctx, out, pwg); |
147 | |
|
148 | 0 | fz_try(ctx) |
149 | 0 | { |
150 | 0 | fz_write_header(ctx, writer, bitmap->w, bitmap->h, bitmap->n, 0, bitmap->xres, bitmap->yres, 0, NULL, NULL); |
151 | 0 | fz_write_band(ctx, writer, bitmap->stride, bitmap->h, bitmap->samples); |
152 | 0 | fz_close_band_writer(ctx, writer); |
153 | 0 | } |
154 | 0 | fz_always(ctx) |
155 | 0 | fz_drop_band_writer(ctx, writer); |
156 | 0 | fz_catch(ctx) |
157 | 0 | fz_rethrow(ctx); |
158 | 0 | } |
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 | 0 | { |
163 | 0 | fz_write_pwg_file_header(ctx, out); |
164 | 0 | fz_write_pixmap_as_pwg_page(ctx, out, pixmap, pwg); |
165 | 0 | } |
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 | 0 | { |
170 | 0 | fz_write_pwg_file_header(ctx, out); |
171 | 0 | fz_write_bitmap_as_pwg_page(ctx, out, bitmap, pwg); |
172 | 0 | } |
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 | 0 | { |
187 | 0 | fz_output *out = fz_new_output_with_path(ctx, filename, append); |
188 | 0 | fz_try(ctx) |
189 | 0 | { |
190 | 0 | if (!append) |
191 | 0 | fz_write_pwg_file_header(ctx, out); |
192 | 0 | fz_write_pixmap_as_pwg_page(ctx, out, pixmap, pwg); |
193 | 0 | fz_close_output(ctx, out); |
194 | 0 | } |
195 | 0 | fz_always(ctx) |
196 | 0 | fz_drop_output(ctx, out); |
197 | 0 | fz_catch(ctx) |
198 | 0 | fz_rethrow(ctx); |
199 | 0 | } |
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 | 0 | { |
214 | 0 | fz_output *out = fz_new_output_with_path(ctx, filename, append); |
215 | 0 | fz_try(ctx) |
216 | 0 | { |
217 | 0 | if (!append) |
218 | 0 | fz_write_pwg_file_header(ctx, out); |
219 | 0 | fz_write_bitmap_as_pwg_page(ctx, out, bitmap, pwg); |
220 | 0 | fz_close_output(ctx, out); |
221 | 0 | } |
222 | 0 | fz_always(ctx) |
223 | 0 | fz_drop_output(ctx, out); |
224 | 0 | fz_catch(ctx) |
225 | 0 | fz_rethrow(ctx); |
226 | 0 | } |
227 | | |
228 | | static void |
229 | | pwg_write_mono_header(fz_context *ctx, fz_band_writer *writer_, fz_colorspace *cs) |
230 | 0 | { |
231 | 0 | pwg_band_writer *writer = (pwg_band_writer *)writer_; |
232 | |
|
233 | 0 | pwg_page_header(ctx, writer->super.out, &writer->pwg, |
234 | 0 | writer->super.xres, writer->super.yres, writer->super.w, writer->super.h, 1); |
235 | 0 | } |
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 | 0 | { |
240 | 0 | pwg_band_writer *writer = (pwg_band_writer *)writer_; |
241 | 0 | fz_output *out = writer->super.out; |
242 | 0 | int w = writer->super.w; |
243 | 0 | int h = writer->super.h; |
244 | 0 | const unsigned char *sp; |
245 | 0 | int y, x; |
246 | 0 | int byte_width; |
247 | | |
248 | | /* Now output the actual bitmap, using a packbits like compression */ |
249 | 0 | sp = samples; |
250 | 0 | byte_width = (w+7)/8; |
251 | 0 | y = 0; |
252 | 0 | while (y < band_height) |
253 | 0 | { |
254 | 0 | int yrep; |
255 | |
|
256 | 0 | assert(sp == samples + y * stride); |
257 | | |
258 | | /* Count the number of times this line is repeated */ |
259 | 0 | for (yrep = 1; yrep < 256 && y+yrep < h; yrep++) |
260 | 0 | { |
261 | 0 | if (memcmp(sp, sp + yrep * stride, byte_width) != 0) |
262 | 0 | break; |
263 | 0 | } |
264 | 0 | fz_write_byte(ctx, out, yrep-1); |
265 | | |
266 | | /* Encode the line */ |
267 | 0 | x = 0; |
268 | 0 | while (x < byte_width) |
269 | 0 | { |
270 | 0 | int d; |
271 | |
|
272 | 0 | assert(sp == samples + y * stride + x); |
273 | | |
274 | | /* How far do we have to look to find a repeated value? */ |
275 | 0 | for (d = 1; d < 128 && x+d < byte_width; d++) |
276 | 0 | { |
277 | 0 | if (sp[d-1] == sp[d]) |
278 | 0 | break; |
279 | 0 | } |
280 | 0 | if (d == 1) |
281 | 0 | { |
282 | 0 | 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 | 0 | for (xrep = 1; xrep < 128 && x+xrep < byte_width; xrep++) |
288 | 0 | { |
289 | 0 | if (sp[0] != sp[xrep]) |
290 | 0 | break; |
291 | 0 | } |
292 | 0 | fz_write_byte(ctx, out, xrep-1); |
293 | 0 | fz_write_data(ctx, out, sp, 1); |
294 | 0 | sp += xrep; |
295 | 0 | x += xrep; |
296 | 0 | } |
297 | 0 | else |
298 | 0 | { |
299 | 0 | fz_write_byte(ctx, out, 257-d); |
300 | 0 | fz_write_data(ctx, out, sp, d); |
301 | 0 | sp += d; |
302 | 0 | x += d; |
303 | 0 | } |
304 | 0 | } |
305 | | |
306 | | /* Move to the next line */ |
307 | 0 | sp += stride*yrep - byte_width; |
308 | 0 | y += yrep; |
309 | 0 | } |
310 | 0 | } |
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 | 0 | { |
318 | 0 | pwg_band_writer *writer = fz_new_band_writer(ctx, pwg_band_writer, out); |
319 | |
|
320 | 0 | writer->super.header = pwg_write_mono_header; |
321 | 0 | writer->super.band = pwg_write_mono_band; |
322 | 0 | if (pwg) |
323 | 0 | writer->pwg = *pwg; |
324 | 0 | else |
325 | 0 | memset(&writer->pwg, 0, sizeof(writer->pwg)); |
326 | |
|
327 | 0 | return &writer->super; |
328 | 0 | } |
329 | | |
330 | | static void |
331 | | pwg_write_header(fz_context *ctx, fz_band_writer *writer_, fz_colorspace *cs) |
332 | 0 | { |
333 | 0 | pwg_band_writer *writer = (pwg_band_writer *)writer_; |
334 | 0 | int n = writer->super.n; |
335 | |
|
336 | 0 | if (writer->super.s != 0) |
337 | 0 | fz_throw(ctx, FZ_ERROR_GENERIC, "PWG band writer cannot cope with spot colors"); |
338 | 0 | if (writer->super.alpha != 0) |
339 | 0 | fz_throw(ctx, FZ_ERROR_GENERIC, "PWG band writer cannot cope with alpha"); |
340 | 0 | if (n != 1 && n != 3 && n != 4) |
341 | 0 | fz_throw(ctx, FZ_ERROR_GENERIC, "pixmap must be grayscale, rgb or cmyk to write as pwg"); |
342 | | |
343 | 0 | pwg_page_header(ctx, writer->super.out, &writer->pwg, |
344 | 0 | writer->super.xres, writer->super.yres, writer->super.w, writer->super.h, n*8); |
345 | 0 | } |
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 | 0 | { |
350 | 0 | pwg_band_writer *writer = (pwg_band_writer *)writer_; |
351 | 0 | fz_output *out = writer->super.out; |
352 | 0 | int w = writer->super.w; |
353 | 0 | int h = writer->super.h; |
354 | 0 | const unsigned char *sp = samples; |
355 | 0 | int n = writer->super.n; |
356 | 0 | int ss = w * n; |
357 | 0 | int y, x; |
358 | | |
359 | | /* Now output the actual bitmap, using a packbits like compression */ |
360 | 0 | y = 0; |
361 | 0 | while (y < h) |
362 | 0 | { |
363 | 0 | int yrep; |
364 | |
|
365 | 0 | assert(sp == samples + y * stride); |
366 | | |
367 | | /* Count the number of times this line is repeated */ |
368 | 0 | for (yrep = 1; yrep < 256 && y+yrep < h; yrep++) |
369 | 0 | { |
370 | 0 | if (memcmp(sp, sp + yrep * stride, ss) != 0) |
371 | 0 | break; |
372 | 0 | } |
373 | 0 | fz_write_byte(ctx, out, yrep-1); |
374 | | |
375 | | /* Encode the line */ |
376 | 0 | x = 0; |
377 | 0 | while (x < w) |
378 | 0 | { |
379 | 0 | int d; |
380 | |
|
381 | 0 | assert(sp == samples + y * stride + x * n); |
382 | | |
383 | | /* How far do we have to look to find a repeated value? */ |
384 | 0 | for (d = 1; d < 128 && x+d < w; d++) |
385 | 0 | { |
386 | 0 | if (memcmp(sp + (d-1)*n, sp + d*n, n) == 0) |
387 | 0 | break; |
388 | 0 | } |
389 | 0 | if (d == 1) |
390 | 0 | { |
391 | 0 | 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 | 0 | for (xrep = 1; xrep < 128 && x+xrep < w; xrep++) |
397 | 0 | { |
398 | 0 | if (memcmp(sp, sp + xrep*n, n) != 0) |
399 | 0 | break; |
400 | 0 | } |
401 | 0 | fz_write_byte(ctx, out, xrep-1); |
402 | 0 | fz_write_data(ctx, out, sp, n); |
403 | 0 | sp += n*xrep; |
404 | 0 | x += xrep; |
405 | 0 | } |
406 | 0 | else |
407 | 0 | { |
408 | 0 | fz_write_byte(ctx, out, 257-d); |
409 | 0 | x += d; |
410 | 0 | while (d > 0) |
411 | 0 | { |
412 | 0 | fz_write_data(ctx, out, sp, n); |
413 | 0 | sp += n; |
414 | 0 | d--; |
415 | 0 | } |
416 | 0 | } |
417 | 0 | } |
418 | | |
419 | | /* Move to the next line */ |
420 | 0 | sp += stride*(yrep-1); |
421 | 0 | y += yrep; |
422 | 0 | } |
423 | 0 | } |
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 | 0 | { |
431 | 0 | pwg_band_writer *writer = fz_new_band_writer(ctx, pwg_band_writer, out); |
432 | |
|
433 | 0 | writer->super.header = pwg_write_header; |
434 | 0 | writer->super.band = pwg_write_band; |
435 | 0 | if (pwg) |
436 | 0 | writer->pwg = *pwg; |
437 | 0 | else |
438 | 0 | memset(&writer->pwg, 0, sizeof(writer->pwg)); |
439 | |
|
440 | 0 | return &writer->super; |
441 | 0 | } |
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 | 0 | { |
484 | 0 | if (ret > 0) |
485 | 0 | fz_warn(ctx, "Option %s is too long, truncated.", str); |
486 | 0 | } |
487 | | |
488 | | fz_pwg_options * |
489 | | fz_parse_pwg_options(fz_context *ctx, fz_pwg_options *opts, const char *args) |
490 | 0 | { |
491 | 0 | const char *val; |
492 | |
|
493 | 0 | memset(opts, 0, sizeof *opts); |
494 | |
|
495 | 0 | if (fz_has_option(ctx, args, "media_class", &val)) |
496 | 0 | warn_if_long(ctx, "media_class", fz_copy_option(ctx, val, opts->media_class, 64)); |
497 | 0 | if (fz_has_option(ctx, args, "media_color", &val)) |
498 | 0 | warn_if_long(ctx, "media_color", fz_copy_option(ctx, val, opts->media_color, 64)); |
499 | 0 | if (fz_has_option(ctx, args, "media_type", &val)) |
500 | 0 | warn_if_long(ctx, "media_type", fz_copy_option(ctx, val, opts->media_type, 64)); |
501 | 0 | if (fz_has_option(ctx, args, "output_type", &val)) |
502 | 0 | warn_if_long(ctx, "output_type", fz_copy_option(ctx, val, opts->output_type, 64)); |
503 | 0 | if (fz_has_option(ctx, args, "rendering_intent", &val)) |
504 | 0 | warn_if_long(ctx, "rendering_intent", fz_copy_option(ctx, val, opts->rendering_intent, 64)); |
505 | 0 | if (fz_has_option(ctx, args, "page_size_name", &val)) |
506 | 0 | warn_if_long(ctx, "page_size_name", fz_copy_option(ctx, val, opts->page_size_name, 64)); |
507 | 0 | if (fz_has_option(ctx, args, "advance_distance", &val)) |
508 | 0 | opts->advance_distance = fz_atoi(val); |
509 | 0 | if (fz_has_option(ctx, args, "advance_media", &val)) |
510 | 0 | opts->advance_media = fz_atoi(val); |
511 | 0 | if (fz_has_option(ctx, args, "collate", &val)) |
512 | 0 | opts->collate = fz_atoi(val); |
513 | 0 | if (fz_has_option(ctx, args, "cut_media", &val)) |
514 | 0 | opts->cut_media = fz_atoi(val); |
515 | 0 | if (fz_has_option(ctx, args, "duplex", &val)) |
516 | 0 | opts->duplex = fz_atoi(val); |
517 | 0 | if (fz_has_option(ctx, args, "insert_sheet", &val)) |
518 | 0 | opts->insert_sheet = fz_atoi(val); |
519 | 0 | if (fz_has_option(ctx, args, "jog", &val)) |
520 | 0 | opts->jog = fz_atoi(val); |
521 | 0 | if (fz_has_option(ctx, args, "leading_edge", &val)) |
522 | 0 | opts->leading_edge = fz_atoi(val); |
523 | 0 | if (fz_has_option(ctx, args, "manual_feed", &val)) |
524 | 0 | opts->manual_feed = fz_atoi(val); |
525 | 0 | if (fz_has_option(ctx, args, "media_position", &val)) |
526 | 0 | opts->media_position = fz_atoi(val); |
527 | 0 | if (fz_has_option(ctx, args, "media_weight", &val)) |
528 | 0 | opts->media_weight = fz_atoi(val); |
529 | 0 | if (fz_has_option(ctx, args, "mirror_print", &val)) |
530 | 0 | opts->mirror_print = fz_atoi(val); |
531 | 0 | if (fz_has_option(ctx, args, "negative_print", &val)) |
532 | 0 | opts->negative_print = fz_atoi(val); |
533 | 0 | if (fz_has_option(ctx, args, "num_copies", &val)) |
534 | 0 | opts->num_copies = fz_atoi(val); |
535 | 0 | if (fz_has_option(ctx, args, "orientation", &val)) |
536 | 0 | opts->orientation = fz_atoi(val); |
537 | 0 | if (fz_has_option(ctx, args, "output_face_up", &val)) |
538 | 0 | opts->output_face_up = fz_atoi(val); |
539 | 0 | if (fz_has_option(ctx, args, "page_size_x", &val)) |
540 | 0 | opts->PageSize[0] = fz_atoi(val); |
541 | 0 | if (fz_has_option(ctx, args, "page_size_y", &val)) |
542 | 0 | opts->PageSize[1] = fz_atoi(val); |
543 | 0 | if (fz_has_option(ctx, args, "separations", &val)) |
544 | 0 | opts->separations = fz_atoi(val); |
545 | 0 | if (fz_has_option(ctx, args, "tray_switch", &val)) |
546 | 0 | opts->tray_switch = fz_atoi(val); |
547 | 0 | if (fz_has_option(ctx, args, "tumble", &val)) |
548 | 0 | opts->tumble = fz_atoi(val); |
549 | 0 | if (fz_has_option(ctx, args, "media_type_num", &val)) |
550 | 0 | opts->media_type_num = fz_atoi(val); |
551 | 0 | if (fz_has_option(ctx, args, "compression", &val)) |
552 | 0 | opts->compression = fz_atoi(val); |
553 | 0 | if (fz_has_option(ctx, args, "row_count", &val)) |
554 | 0 | opts->row_count = fz_atoi(val); |
555 | 0 | if (fz_has_option(ctx, args, "row_feed", &val)) |
556 | 0 | opts->row_feed = fz_atoi(val); |
557 | 0 | if (fz_has_option(ctx, args, "row_step", &val)) |
558 | 0 | opts->row_step = fz_atoi(val); |
559 | |
|
560 | 0 | return opts; |
561 | 0 | } |
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 | 0 | { |
576 | 0 | fz_pwg_writer *wri = (fz_pwg_writer*)wri_; |
577 | 0 | return fz_new_draw_device_with_options(ctx, &wri->draw, mediabox, &wri->pixmap); |
578 | 0 | } |
579 | | |
580 | | static void |
581 | | pwg_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev) |
582 | 0 | { |
583 | 0 | fz_pwg_writer *wri = (fz_pwg_writer*)wri_; |
584 | 0 | fz_bitmap *bitmap = NULL; |
585 | |
|
586 | 0 | fz_var(bitmap); |
587 | |
|
588 | 0 | fz_try(ctx) |
589 | 0 | { |
590 | 0 | fz_close_device(ctx, dev); |
591 | 0 | if (wri->mono) |
592 | 0 | { |
593 | 0 | bitmap = fz_new_bitmap_from_pixmap(ctx, wri->pixmap, NULL); |
594 | 0 | fz_write_bitmap_as_pwg_page(ctx, wri->out, bitmap, &wri->pwg); |
595 | 0 | } |
596 | 0 | else |
597 | 0 | { |
598 | 0 | fz_write_pixmap_as_pwg_page(ctx, wri->out, wri->pixmap, &wri->pwg); |
599 | 0 | } |
600 | 0 | } |
601 | 0 | fz_always(ctx) |
602 | 0 | { |
603 | 0 | fz_drop_device(ctx, dev); |
604 | 0 | fz_drop_bitmap(ctx, bitmap); |
605 | 0 | fz_drop_pixmap(ctx, wri->pixmap); |
606 | 0 | wri->pixmap = NULL; |
607 | 0 | } |
608 | 0 | fz_catch(ctx) |
609 | 0 | { |
610 | 0 | fz_rethrow(ctx); |
611 | 0 | } |
612 | 0 | } |
613 | | |
614 | | static void |
615 | | pwg_close_writer(fz_context *ctx, fz_document_writer *wri_) |
616 | 0 | { |
617 | 0 | fz_pwg_writer *wri = (fz_pwg_writer*)wri_; |
618 | 0 | fz_close_output(ctx, wri->out); |
619 | 0 | } |
620 | | |
621 | | static void |
622 | | pwg_drop_writer(fz_context *ctx, fz_document_writer *wri_) |
623 | 0 | { |
624 | 0 | fz_pwg_writer *wri = (fz_pwg_writer*)wri_; |
625 | 0 | fz_drop_pixmap(ctx, wri->pixmap); |
626 | 0 | fz_drop_output(ctx, wri->out); |
627 | 0 | } |
628 | | |
629 | | fz_document_writer * |
630 | | fz_new_pwg_writer_with_output(fz_context *ctx, fz_output *out, const char *options) |
631 | 0 | { |
632 | 0 | fz_pwg_writer *wri = NULL; |
633 | 0 | const char *val; |
634 | |
|
635 | 0 | fz_var(wri); |
636 | |
|
637 | 0 | fz_try(ctx) |
638 | 0 | { |
639 | 0 | wri = fz_new_derived_document_writer(ctx, fz_pwg_writer, pwg_begin_page, pwg_end_page, pwg_close_writer, pwg_drop_writer); |
640 | 0 | fz_parse_draw_options(ctx, &wri->draw, options); |
641 | 0 | fz_parse_pwg_options(ctx, &wri->pwg, options); |
642 | 0 | if (fz_has_option(ctx, options, "colorspace", &val)) |
643 | 0 | if (fz_option_eq(val, "mono")) |
644 | 0 | wri->mono = 1; |
645 | 0 | wri->out = out; |
646 | 0 | fz_write_pwg_file_header(ctx, wri->out); |
647 | 0 | } |
648 | 0 | fz_catch(ctx) |
649 | 0 | { |
650 | 0 | fz_drop_output(ctx, out); |
651 | 0 | fz_free(ctx, wri); |
652 | 0 | fz_rethrow(ctx); |
653 | 0 | } |
654 | | |
655 | 0 | return (fz_document_writer*)wri; |
656 | 0 | } |
657 | | |
658 | | fz_document_writer * |
659 | | fz_new_pwg_writer(fz_context *ctx, const char *path, const char *options) |
660 | 0 | { |
661 | 0 | fz_output *out = fz_new_output_with_path(ctx, path ? path : "out.pwg", 0); |
662 | 0 | return fz_new_pwg_writer_with_output(ctx, out, options); |
663 | 0 | } |