comparison mupdf-source/source/fitz/output-pclm.c @ 2:b50eed0cc0ef upstream

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