1 #include "mupdf/fitz.h"
4 fz_output_pwg_file_header(fz_output *out)
6 static const unsigned char pwgsig[4] = { 'R', 'a', 'S', '2' };
9 fz_write(out, pwgsig, 4);
13 output_header(fz_output *out, const fz_pwg_options *pwg, int xres, int yres, int w, int h, int bpp)
15 static const char zero[64] = { 0 };
19 fz_write(out, pwg ? pwg->media_class : zero, 64);
20 fz_write(out, pwg ? pwg->media_color : zero, 64);
21 fz_write(out, pwg ? pwg->media_type : zero, 64);
22 fz_write(out, pwg ? pwg->output_type : zero, 64);
23 fz_write_int32be(out, pwg ? pwg->advance_distance : 0);
24 fz_write_int32be(out, pwg ? pwg->advance_media : 0);
25 fz_write_int32be(out, pwg ? pwg->collate : 0);
26 fz_write_int32be(out, pwg ? pwg->cut_media : 0);
27 fz_write_int32be(out, pwg ? pwg->duplex : 0);
28 fz_write_int32be(out, xres);
29 fz_write_int32be(out, yres);
30 /* CUPS format says that 284->300 are supposed to be the bbox of the
31 * page in points. PWG says 'Reserved'. */
32 for (i=284; i < 300; i += 4)
33 fz_write(out, zero, 4);
34 fz_write_int32be(out, pwg ? pwg->insert_sheet : 0);
35 fz_write_int32be(out, pwg ? pwg->jog : 0);
36 fz_write_int32be(out, pwg ? pwg->leading_edge : 0);
37 /* CUPS format says that 312->320 are supposed to be the margins of
38 * the lower left hand edge of page in points. PWG says 'Reserved'. */
39 for (i=312; i < 320; i += 4)
40 fz_write(out, zero, 4);
41 fz_write_int32be(out, pwg ? pwg->manual_feed : 0);
42 fz_write_int32be(out, pwg ? pwg->media_position : 0);
43 fz_write_int32be(out, pwg ? pwg->media_weight : 0);
44 fz_write_int32be(out, pwg ? pwg->mirror_print : 0);
45 fz_write_int32be(out, pwg ? pwg->negative_print : 0);
46 fz_write_int32be(out, pwg ? pwg->num_copies : 0);
47 fz_write_int32be(out, pwg ? pwg->orientation : 0);
48 fz_write_int32be(out, pwg ? pwg->output_face_up : 0);
49 fz_write_int32be(out, w * 72/ xres); /* Page size in points */
50 fz_write_int32be(out, h * 72/ yres);
51 fz_write_int32be(out, pwg ? pwg->separations : 0);
52 fz_write_int32be(out, pwg ? pwg->tray_switch : 0);
53 fz_write_int32be(out, pwg ? pwg->tumble : 0);
54 fz_write_int32be(out, w); /* Page image in pixels */
55 fz_write_int32be(out, h);
56 fz_write_int32be(out, pwg ? pwg->media_type_num : 0);
57 fz_write_int32be(out, bpp < 8 ? 1 : 8); /* Bits per color */
58 fz_write_int32be(out, bpp); /* Bits per pixel */
59 fz_write_int32be(out, (w * bpp + 7)/8); /* Bytes per line */
60 fz_write_int32be(out, 0); /* Chunky pixels */
63 case 1: fz_write_int32be(out, 3); /* Black */ break;
64 case 8: fz_write_int32be(out, 18); /* Sgray */ break;
65 case 24: fz_write_int32be(out, 19); /* Srgb */ break;
66 case 32: fz_write_int32be(out, 6); /* Cmyk */ break;
67 default: fz_throw(out->ctx, FZ_ERROR_GENERIC, "pixmap bpp must be 1, 8, 24 or 32 to write as pwg");
69 fz_write_int32be(out, pwg ? pwg->compression : 0);
70 fz_write_int32be(out, pwg ? pwg->row_count : 0);
71 fz_write_int32be(out, pwg ? pwg->row_feed : 0);
72 fz_write_int32be(out, pwg ? pwg->row_step : 0);
73 fz_write_int32be(out, bpp <= 8 ? 1 : 3); /* Num Colors */
74 for (i=424; i < 452; i += 4)
75 fz_write(out, zero, 4);
76 fz_write_int32be(out, 1); /* TotalPageCount */
77 fz_write_int32be(out, 1); /* CrossFeedTransform */
78 fz_write_int32be(out, 1); /* FeedTransform */
79 fz_write_int32be(out, 0); /* ImageBoxLeft */
80 fz_write_int32be(out, 0); /* ImageBoxTop */
81 fz_write_int32be(out, w); /* ImageBoxRight */
82 fz_write_int32be(out, h); /* ImageBoxBottom */
83 for (i=480; i < 1668; i += 4)
84 fz_write(out, zero, 4);
85 fz_write(out, pwg ? pwg->rendering_intent : zero, 64);
86 fz_write(out, pwg ? pwg->page_size_name : zero, 64);
90 fz_output_pwg_page(fz_output *out, const fz_pixmap *pixmap, const fz_pwg_options *pwg)
101 if (pixmap->n != 1 && pixmap->n != 2 && pixmap->n != 4 && pixmap->n != 5)
102 fz_throw(ctx, FZ_ERROR_GENERIC, "pixmap must be grayscale, rgb or cmyk to write as pwg");
109 output_header(out, pwg, pixmap->xres, pixmap->yres, pixmap->w, pixmap->h, dn*8);
111 /* Now output the actual bitmap, using a packbits like compression */
112 sp = pixmap->samples;
115 while (y < pixmap->h)
119 assert(sp == pixmap->samples + y * ss);
121 /* Count the number of times this line is repeated */
122 for (yrep = 1; yrep < 256 && y+yrep < pixmap->h; yrep++)
124 if (memcmp(sp, sp + yrep * ss, ss) != 0)
127 fz_write_byte(out, yrep-1);
129 /* Encode the line */
131 while (x < pixmap->w)
135 assert(sp == pixmap->samples + y * ss + x * sn);
137 /* How far do we have to look to find a repeated value? */
138 for (d = 1; d < 128 && x+d < pixmap->w; d++)
140 if (memcmp(sp + (d-1)*sn, sp + d*sn, sn) == 0)
147 /* We immediately have a repeat (or we've hit
148 * the end of the line). Count the number of
149 * times this value is repeated. */
150 for (xrep = 1; xrep < 128 && x+xrep < pixmap->w; xrep++)
152 if (memcmp(sp, sp + xrep*sn, sn) != 0)
155 fz_write_byte(out, xrep-1);
156 fz_write(out, sp, dn);
162 fz_write_byte(out, 257-d);
166 fz_write(out, sp, dn);
173 /* Move to the next line */
180 fz_output_pwg_bitmap_page(fz_output *out, const fz_bitmap *bitmap, const fz_pwg_options *pwg)
189 output_header(out, pwg, bitmap->xres, bitmap->yres, bitmap->w, bitmap->h, 1);
191 /* Now output the actual bitmap, using a packbits like compression */
192 sp = bitmap->samples;
194 byte_width = (bitmap->w+7)/8;
196 while (y < bitmap->h)
200 assert(sp == bitmap->samples + y * ss);
202 /* Count the number of times this line is repeated */
203 for (yrep = 1; yrep < 256 && y+yrep < bitmap->h; yrep++)
205 if (memcmp(sp, sp + yrep * ss, byte_width) != 0)
208 fz_write_byte(out, yrep-1);
210 /* Encode the line */
212 while (x < byte_width)
216 assert(sp == bitmap->samples + y * ss + x);
218 /* How far do we have to look to find a repeated value? */
219 for (d = 1; d < 128 && x+d < byte_width; d++)
221 if (sp[d-1] == sp[d])
228 /* We immediately have a repeat (or we've hit
229 * the end of the line). Count the number of
230 * times this value is repeated. */
231 for (xrep = 1; xrep < 128 && x+xrep < byte_width; xrep++)
233 if (sp[0] != sp[xrep])
236 fz_write_byte(out, xrep-1);
237 fz_write(out, sp, 1);
243 fz_write_byte(out, 257-d);
244 fz_write(out, sp, d);
250 /* Move to the next line */
251 sp += ss*yrep - byte_width;
257 fz_output_pwg(fz_output *out, const fz_pixmap *pixmap, const fz_pwg_options *pwg)
259 fz_output_pwg_file_header(out);
260 fz_output_pwg_page(out, pixmap, pwg);
264 fz_write_pwg(fz_context *ctx, fz_pixmap *pixmap, char *filename, int append, const fz_pwg_options *pwg)
267 fz_output *out = NULL;
269 fp = fopen(filename, append ? "ab" : "wb");
272 fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno));
279 out = fz_new_output_with_file(ctx, fp);
281 fz_output_pwg_file_header(out);
282 fz_output_pwg_page(out, pixmap, pwg);
286 fz_close_output(out);
296 fz_write_pwg_bitmap(fz_context *ctx, fz_bitmap *bitmap, char *filename, int append, const fz_pwg_options *pwg)
299 fz_output *out = NULL;
301 fp = fopen(filename, append ? "ab" : "wb");
304 fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno));
311 out = fz_new_output_with_file(ctx, fp);
313 fz_output_pwg_file_header(out);
314 fz_output_pwg_bitmap_page(out, bitmap, pwg);
318 fz_close_output(out);