1 #include "mupdf/fitz.h"
2
3 #include <string.h>
4 #include <limits.h>
5
6 const char *fz_pclm_write_options_usage =
7 "PCLm output options:\n"
8 "\tcompression=none: No compression (default)\n"
9 "\tcompression=flate: Flate compression\n"
10 "\tstrip-height=N: Strip height (default 16)\n"
11 "\n";
12
13 fz_pclm_options *
fz_parse_pclm_options(fz_context * ctx,fz_pclm_options * opts,const char * args)14 fz_parse_pclm_options(fz_context *ctx, fz_pclm_options *opts, const char *args)
15 {
16 const char *val;
17
18 memset(opts, 0, sizeof *opts);
19
20 if (fz_has_option(ctx, args, "compression", &val))
21 {
22 if (fz_option_eq(val, "none"))
23 opts->compress = 0;
24 else if (fz_option_eq(val, "flate"))
25 opts->compress = 1;
26 else
27 fz_throw(ctx, FZ_ERROR_GENERIC, "Unsupported PCLm compression %s (none, or flate only)", val);
28 }
29 if (fz_has_option(ctx, args, "strip-height", &val))
30 {
31 int i = fz_atoi(val);
32 if (i <= 0)
33 fz_throw(ctx, FZ_ERROR_GENERIC, "Unsupported PCLm strip height %d (suggest 16)", i);
34 opts->strip_height = i;
35 }
36
37 return opts;
38 }
39
40 void
fz_write_pixmap_as_pclm(fz_context * ctx,fz_output * out,const fz_pixmap * pixmap,const fz_pclm_options * pclm)41 fz_write_pixmap_as_pclm(fz_context *ctx, fz_output *out, const fz_pixmap *pixmap, const fz_pclm_options *pclm)
42 {
43 fz_band_writer *writer;
44
45 if (!pixmap || !out)
46 return;
47
48 writer = fz_new_pclm_band_writer(ctx, out, pclm);
49 fz_try(ctx)
50 {
51 fz_write_header(ctx, writer, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, pixmap->xres, pixmap->yres, 0, pixmap->colorspace, pixmap->seps);
52 fz_write_band(ctx, writer, pixmap->stride, pixmap->h, pixmap->samples);
53 }
54 fz_always(ctx)
55 fz_drop_band_writer(ctx, writer);
56 fz_catch(ctx)
57 fz_rethrow(ctx);
58 }
59
60 typedef struct pclm_band_writer_s
61 {
62 fz_band_writer super;
63 fz_pclm_options options;
64
65 int obj_num;
66 int xref_max;
67 int64_t *xref;
68 int pages;
69 int page_max;
70 int *page_obj;
71 unsigned char *stripbuf;
72 unsigned char *compbuf;
73 size_t complen;
74 } pclm_band_writer;
75
76 static int
new_obj(fz_context * ctx,pclm_band_writer * writer)77 new_obj(fz_context *ctx, pclm_band_writer *writer)
78 {
79 int64_t pos = fz_tell_output(ctx, writer->super.out);
80
81 if (writer->obj_num >= writer->xref_max)
82 {
83 int new_max = writer->xref_max * 2;
84 if (new_max < writer->obj_num + 8)
85 new_max = writer->obj_num + 8;
86 writer->xref = fz_realloc_array(ctx, writer->xref, new_max, int64_t);
87 writer->xref_max = new_max;
88 }
89
90 writer->xref[writer->obj_num] = pos;
91
92 return writer->obj_num++;
93 }
94
95 static void
pclm_write_header(fz_context * ctx,fz_band_writer * writer_,fz_colorspace * cs)96 pclm_write_header(fz_context *ctx, fz_band_writer *writer_, fz_colorspace *cs)
97 {
98 pclm_band_writer *writer = (pclm_band_writer *)writer_;
99 fz_output *out = writer->super.out;
100 int w = writer->super.w;
101 int h = writer->super.h;
102 int n = writer->super.n;
103 int s = writer->super.s;
104 int a = writer->super.alpha;
105 int xres = writer->super.xres;
106 int yres = writer->super.yres;
107 int sh = writer->options.strip_height;
108 int strips = (h + sh-1)/sh;
109 int i;
110 size_t len;
111 unsigned char *data;
112 fz_buffer *buf = NULL;
113
114 if (a != 0)
115 fz_throw(ctx, FZ_ERROR_GENERIC, "PCLm cannot write alpha channel");
116 if (s != 0)
117 fz_throw(ctx, FZ_ERROR_GENERIC, "PCLm cannot write spot colors");
118 if (n != 3 && n != 1)
119 fz_throw(ctx, FZ_ERROR_GENERIC, "PCLm expected to be Grayscale or RGB");
120
121 fz_free(ctx, writer->stripbuf);
122 writer->stripbuf = NULL;
123 fz_free(ctx, writer->compbuf);
124 writer->compbuf = NULL;
125 writer->stripbuf = Memento_label(fz_malloc(ctx, (size_t)w * sh * n), "pclm_stripbuf");
126 writer->complen = fz_deflate_bound(ctx, (size_t)w * sh * n);
127 writer->compbuf = Memento_label(fz_malloc(ctx, writer->complen), "pclm_compbuf");
128
129 /* Send the file header on the first page */
130 if (writer->pages == 0)
131 fz_write_string(ctx, out, "%PDF-1.4\n%PCLm-1.0\n");
132
133 if (writer->page_max <= writer->pages)
134 {
135 int new_max = writer->page_max * 2;
136 if (new_max == 0)
137 new_max = writer->pages + 8;
138 writer->page_obj = fz_realloc_array(ctx, writer->page_obj, new_max, int);
139 writer->page_max = new_max;
140 }
141 writer->page_obj[writer->pages] = writer->obj_num;
142 writer->pages++;
143
144 /* Send the Page Object */
145 fz_write_printf(ctx, out, "%d 0 obj\n<<\n/Type /Page\n/Parent 2 0 R\n/Resources <<\n/XObject <<\n", new_obj(ctx, writer));
146 for (i = 0; i < strips; i++)
147 fz_write_printf(ctx, out, "/Image%d %d 0 R\n", i, writer->obj_num + 1 + i);
148 fz_write_printf(ctx, out, ">>\n>>\n/MediaBox[ 0 0 %g %g ]\n/Contents [ %d 0 R ]\n>>\nendobj\n",
149 w * 72.0f / xres, h * 72.0f / yres, writer->obj_num);
150
151 /* And the Page contents */
152 /* We need the length to this, so write to a buffer first */
153 fz_var(buf);
154 fz_try(ctx)
155 {
156 buf = fz_new_buffer(ctx, 0);
157 fz_append_printf(ctx, buf, "%g 0 0 %g 0 0 cm\n", 72.0f/xres, 72.0f/yres);
158 for (i = 0; i < strips; i++)
159 {
160 int at = h - (i+1)*sh;
161 int this_sh = sh;
162 if (at < 0)
163 {
164 this_sh += at;
165 at = 0;
166 }
167 fz_append_printf(ctx, buf, "/P <</MCID 0>> BDC q\n%d 0 0 %d 0 %d cm\n/Image%d Do Q\n",
168 w, this_sh, at, i);
169 }
170 len = fz_buffer_storage(ctx, buf, &data);
171 fz_write_printf(ctx, out, "%d 0 obj\n<<\n/Length %zd\n>>\nstream\n", new_obj(ctx, writer), len);
172 fz_write_data(ctx, out, data, len);
173 fz_drop_buffer(ctx, buf);
174 buf = NULL;
175 fz_write_string(ctx, out, "\nendstream\nendobj\n");
176 }
177 fz_catch(ctx)
178 {
179 fz_drop_buffer(ctx, buf);
180 fz_rethrow(ctx);
181 }
182 }
183
184 static void
flush_strip(fz_context * ctx,pclm_band_writer * writer,int fill)185 flush_strip(fz_context *ctx, pclm_band_writer *writer, int fill)
186 {
187 unsigned char *data = writer->stripbuf;
188 fz_output *out = writer->super.out;
189 int w = writer->super.w;
190 int n = writer->super.n;
191 size_t len = (size_t)w*n*fill;
192
193 /* Buffer is full, compress it and write it. */
194 if (writer->options.compress)
195 {
196 size_t destLen = writer->complen;
197 fz_deflate(ctx, writer->compbuf, &destLen, data, len, FZ_DEFLATE_DEFAULT);
198 len = destLen;
199 data = writer->compbuf;
200 }
201 fz_write_printf(ctx, out, "%d 0 obj\n<<\n/Width %d\n/ColorSpace /Device%s\n/Height %d\n%s/Subtype /Image\n",
202 new_obj(ctx, writer), w, n == 1 ? "Gray" : "RGB", fill, writer->options.compress ? "/Filter /FlateDecode\n" : "");
203 fz_write_printf(ctx, out, "/Length %zd\n/Type /XObject\n/BitsPerComponent 8\n>>\nstream\n", len);
204 fz_write_data(ctx, out, data, len);
205 fz_write_string(ctx, out, "\nendstream\nendobj\n");
206 }
207
208 static void
pclm_write_band(fz_context * ctx,fz_band_writer * writer_,int stride,int band_start,int band_height,const unsigned char * sp)209 pclm_write_band(fz_context *ctx, fz_band_writer *writer_, int stride, int band_start, int band_height, const unsigned char *sp)
210 {
211 pclm_band_writer *writer = (pclm_band_writer *)writer_;
212 fz_output *out = writer->super.out;
213 int w = writer->super.w;
214 int h = writer->super.h;
215 int n = writer->super.n;
216 int sh = writer->options.strip_height;
217 int line;
218
219 if (!out)
220 return;
221
222 for (line = 0; line < band_height; line++)
223 {
224 int dstline = (band_start+line) % sh;
225 memcpy(writer->stripbuf + (size_t)w*n*dstline,
226 sp + (size_t)line * w * n,
227 (size_t)w * n);
228 if (dstline+1 == sh)
229 flush_strip(ctx, writer, dstline+1);
230 }
231
232 if (band_start + band_height == h && h % sh != 0)
233 flush_strip(ctx, writer, h % sh);
234 }
235
236 static void
pclm_write_trailer(fz_context * ctx,fz_band_writer * writer_)237 pclm_write_trailer(fz_context *ctx, fz_band_writer *writer_)
238 {
239 }
240
241 static void
pclm_drop_band_writer(fz_context * ctx,fz_band_writer * writer_)242 pclm_drop_band_writer(fz_context *ctx, fz_band_writer *writer_)
243 {
244 pclm_band_writer *writer = (pclm_band_writer *)writer_;
245 fz_output *out = writer->super.out;
246 int i;
247
248 /* We actually do the trailer writing in the drop */
249 if (writer->xref_max > 2)
250 {
251 int64_t t_pos;
252
253 /* Catalog */
254 writer->xref[1] = fz_tell_output(ctx, out);
255 fz_write_printf(ctx, out, "1 0 obj\n<<\n/Type /Catalog\n/Pages 2 0 R\n>>\nendobj\n");
256
257 /* Page table */
258 writer->xref[2] = fz_tell_output(ctx, out);
259 fz_write_printf(ctx, out, "2 0 obj\n<<\n/Count %d\n/Kids [ ", writer->pages);
260
261 for (i = 0; i < writer->pages; i++)
262 fz_write_printf(ctx, out, "%d 0 R ", writer->page_obj[i]);
263 fz_write_string(ctx, out, "]\n/Type /Pages\n>>\nendobj\n");
264
265 /* Xref */
266 t_pos = fz_tell_output(ctx, out);
267 fz_write_printf(ctx, out, "xref\n0 %d\n0000000000 65535 f \n", writer->obj_num);
268 for (i = 1; i < writer->obj_num; i++)
269 fz_write_printf(ctx, out, "%010zd 00000 n \n", writer->xref[i]);
270 fz_write_printf(ctx, out, "trailer\n<<\n/Size %d\n/Root 1 0 R\n>>\nstartxref\n%ld\n%%%%EOF\n", writer->obj_num, t_pos);
271 }
272
273 fz_free(ctx, writer->stripbuf);
274 fz_free(ctx, writer->compbuf);
275 fz_free(ctx, writer->page_obj);
276 fz_free(ctx, writer->xref);
277 }
278
fz_new_pclm_band_writer(fz_context * ctx,fz_output * out,const fz_pclm_options * options)279 fz_band_writer *fz_new_pclm_band_writer(fz_context *ctx, fz_output *out, const fz_pclm_options *options)
280 {
281 pclm_band_writer *writer = fz_new_band_writer(ctx, pclm_band_writer, out);
282
283 writer->super.header = pclm_write_header;
284 writer->super.band = pclm_write_band;
285 writer->super.trailer = pclm_write_trailer;
286 writer->super.drop = pclm_drop_band_writer;
287
288 if (options)
289 writer->options = *options;
290 else
291 memset(&writer->options, 0, sizeof(writer->options));
292
293 if (writer->options.strip_height == 0)
294 writer->options.strip_height = 16;
295 writer->obj_num = 3; /* 1 reserved for catalog, 2 for pages tree. */
296
297 return &writer->super;
298 }
299
300 void
fz_save_pixmap_as_pclm(fz_context * ctx,fz_pixmap * pixmap,char * filename,int append,const fz_pclm_options * pclm)301 fz_save_pixmap_as_pclm(fz_context *ctx, fz_pixmap *pixmap, char *filename, int append, const fz_pclm_options *pclm)
302 {
303 fz_output *out = fz_new_output_with_path(ctx, filename, append);
304 fz_try(ctx)
305 {
306 fz_write_pixmap_as_pclm(ctx, out, pixmap, pclm);
307 fz_close_output(ctx, out);
308 }
309 fz_always(ctx)
310 fz_drop_output(ctx, out);
311 fz_catch(ctx)
312 fz_rethrow(ctx);
313 }
314
315 /* High-level document writer interface */
316
317 typedef struct
318 {
319 fz_document_writer super;
320 fz_draw_options draw;
321 fz_pclm_options pclm;
322 fz_pixmap *pixmap;
323 fz_band_writer *bander;
324 fz_output *out;
325 int pagenum;
326 } fz_pclm_writer;
327
328 static fz_device *
pclm_begin_page(fz_context * ctx,fz_document_writer * wri_,fz_rect mediabox)329 pclm_begin_page(fz_context *ctx, fz_document_writer *wri_, fz_rect mediabox)
330 {
331 fz_pclm_writer *wri = (fz_pclm_writer*)wri_;
332 return fz_new_draw_device_with_options(ctx, &wri->draw, mediabox, &wri->pixmap);
333 }
334
335 static void
pclm_end_page(fz_context * ctx,fz_document_writer * wri_,fz_device * dev)336 pclm_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev)
337 {
338 fz_pclm_writer *wri = (fz_pclm_writer*)wri_;
339 fz_pixmap *pix = wri->pixmap;
340
341 fz_try(ctx)
342 {
343 fz_close_device(ctx, dev);
344 fz_write_header(ctx, wri->bander, pix->w, pix->h, pix->n, pix->alpha, pix->xres, pix->yres, wri->pagenum++, pix->colorspace, pix->seps);
345 fz_write_band(ctx, wri->bander, pix->stride, pix->h, pix->samples);
346 }
347 fz_always(ctx)
348 {
349 fz_drop_device(ctx, dev);
350 fz_drop_pixmap(ctx, pix);
351 wri->pixmap = NULL;
352 }
353 fz_catch(ctx)
354 fz_rethrow(ctx);
355 }
356
357 static void
pclm_close_writer(fz_context * ctx,fz_document_writer * wri_)358 pclm_close_writer(fz_context *ctx, fz_document_writer *wri_)
359 {
360 fz_pclm_writer *wri = (fz_pclm_writer*)wri_;
361
362 fz_drop_band_writer(ctx, wri->bander);
363 wri->bander = NULL;
364
365 fz_close_output(ctx, wri->out);
366 }
367
368 static void
pclm_drop_writer(fz_context * ctx,fz_document_writer * wri_)369 pclm_drop_writer(fz_context *ctx, fz_document_writer *wri_)
370 {
371 fz_pclm_writer *wri = (fz_pclm_writer*)wri_;
372
373 fz_drop_pixmap(ctx, wri->pixmap);
374 fz_drop_output(ctx, wri->out);
375 fz_drop_band_writer(ctx, wri->bander);
376 }
377
378 fz_document_writer *
fz_new_pclm_writer_with_output(fz_context * ctx,fz_output * out,const char * options)379 fz_new_pclm_writer_with_output(fz_context *ctx, fz_output *out, const char *options)
380 {
381 fz_pclm_writer *wri = fz_new_derived_document_writer(ctx, fz_pclm_writer, pclm_begin_page, pclm_end_page, pclm_close_writer, pclm_drop_writer);
382
383 fz_try(ctx)
384 {
385 fz_parse_draw_options(ctx, &wri->draw, options);
386 fz_parse_pclm_options(ctx, &wri->pclm, options);
387 wri->out = out;
388 wri->bander = fz_new_pclm_band_writer(ctx, wri->out, &wri->pclm);
389 }
390 fz_catch(ctx)
391 {
392 fz_free(ctx, wri);
393 fz_rethrow(ctx);
394 }
395
396 return (fz_document_writer*)wri;
397 }
398
399 fz_document_writer *
fz_new_pclm_writer(fz_context * ctx,const char * path,const char * options)400 fz_new_pclm_writer(fz_context *ctx, const char *path, const char *options)
401 {
402 fz_output *out = fz_new_output_with_path(ctx, path ? path : "out.pclm", 0);
403 fz_document_writer *wri = NULL;
404 fz_try(ctx)
405 wri = fz_new_pclm_writer_with_output(ctx, out, options);
406 fz_catch(ctx)
407 {
408 fz_drop_output(ctx, out);
409 fz_rethrow(ctx);
410 }
411 return wri;
412 }
413