Coverage Report

Created: 2026-03-31 07:17

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