Coverage Report

Created: 2026-02-14 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mupdf/source/fitz/output-jpeg.c
Line
Count
Source
1
// Copyright (C) 2004-2026 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 <jpeglib.h>
26
27
#ifdef SHARE_JPEG
28
29
#define JZ_CTX_FROM_CINFO(c) (fz_context *)((c)->client_data)
30
31
static void fz_jpg_mem_init(j_common_ptr cinfo, fz_context *ctx)
32
{
33
  cinfo->client_data = ctx;
34
}
35
36
#define fz_jpg_mem_term(cinfo)
37
38
#else /* SHARE_JPEG */
39
40
typedef void * backing_store_ptr;
41
42
#include "jmemcust.h"
43
44
0
#define JZ_CTX_FROM_CINFO(c) (fz_context *)(GET_CUST_MEM_DATA(c)->priv)
45
46
static void *
47
fz_jpg_mem_alloc(j_common_ptr cinfo, size_t size)
48
0
{
49
0
  fz_context *ctx = JZ_CTX_FROM_CINFO(cinfo);
50
0
  return fz_malloc_no_throw(ctx, size);
51
0
}
52
53
static void
54
fz_jpg_mem_free(j_common_ptr cinfo, void *object, size_t size)
55
0
{
56
0
  fz_context *ctx = JZ_CTX_FROM_CINFO(cinfo);
57
0
  fz_free(ctx, object);
58
0
}
59
60
static void
61
fz_jpg_mem_init(j_common_ptr cinfo, fz_context *ctx)
62
0
{
63
0
  jpeg_cust_mem_data *custmptr;
64
0
  custmptr = fz_malloc_struct(ctx, jpeg_cust_mem_data);
65
0
  if (!jpeg_cust_mem_init(custmptr, (void *) ctx, NULL, NULL, NULL,
66
0
        fz_jpg_mem_alloc, fz_jpg_mem_free,
67
0
        fz_jpg_mem_alloc, fz_jpg_mem_free, NULL))
68
0
  {
69
0
    fz_free(ctx, custmptr);
70
0
    fz_throw(ctx, FZ_ERROR_LIBRARY, "cannot initialize custom JPEG memory handler");
71
0
  }
72
0
  cinfo->client_data = custmptr;
73
0
}
74
75
static void
76
fz_jpg_mem_term(j_common_ptr cinfo)
77
0
{
78
0
  if (cinfo->client_data)
79
0
  {
80
0
    fz_context *ctx = JZ_CTX_FROM_CINFO(cinfo);
81
0
    fz_free(ctx, cinfo->client_data);
82
0
    cinfo->client_data = NULL;
83
0
  }
84
0
}
85
86
#endif
87
88
0
#define OUTPUT_BUF_SIZE (16<<10)
89
90
typedef struct {
91
  struct jpeg_destination_mgr pub;
92
  fz_output *out;
93
  JOCTET buffer[OUTPUT_BUF_SIZE];
94
} my_destination_mgr;
95
96
typedef my_destination_mgr * my_dest_ptr;
97
98
static void error_exit(j_common_ptr cinfo)
99
0
{
100
0
  char msg[JMSG_LENGTH_MAX];
101
0
  fz_context *ctx = JZ_CTX_FROM_CINFO(cinfo);
102
0
  cinfo->err->format_message(cinfo, msg);
103
0
  fz_throw(ctx, FZ_ERROR_LIBRARY, "jpeg error: %s", msg);
104
0
}
105
106
static void init_destination(j_compress_ptr cinfo)
107
0
{
108
0
  my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
109
0
  dest->pub.next_output_byte = dest->buffer;
110
0
  dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
111
0
}
112
113
static boolean empty_output_buffer(j_compress_ptr cinfo)
114
0
{
115
0
  fz_context *ctx = JZ_CTX_FROM_CINFO(cinfo);
116
0
  my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
117
0
  fz_output *out = dest->out;
118
119
0
  fz_write_data(ctx, out, dest->buffer, OUTPUT_BUF_SIZE);
120
121
0
  dest->pub.next_output_byte = dest->buffer;
122
0
  dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
123
124
0
  return TRUE;
125
0
}
126
127
static void term_destination(j_compress_ptr cinfo)
128
0
{
129
0
  fz_context *ctx = JZ_CTX_FROM_CINFO(cinfo);
130
0
  my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
131
0
  fz_output *out = dest->out;
132
0
  size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
133
134
0
  fz_write_data(ctx, out, dest->buffer, datacount);
135
0
}
136
137
void
138
fz_write_pixmap_as_jpeg(fz_context *ctx, fz_output *out, fz_pixmap *pix, int quality, int invert_cmyk)
139
0
{
140
0
  struct jpeg_compress_struct cinfo;
141
0
  struct jpeg_error_mgr err;
142
0
  my_destination_mgr dest;
143
0
  JSAMPROW row_pointer[1];
144
0
  fz_colorspace *cs = pix->colorspace;
145
0
  int n = pix->n;
146
0
  int alpha = pix->alpha;
147
148
0
  if (pix->s > 0)
149
0
    fz_throw(ctx, FZ_ERROR_ARGUMENT, "pixmap may not have separations to save as JPEG");
150
0
  if (cs && !fz_colorspace_is_gray(ctx, cs) && !fz_colorspace_is_rgb(ctx, cs) && !fz_colorspace_is_cmyk(ctx, cs))
151
0
    fz_throw(ctx, FZ_ERROR_ARGUMENT, "pixmap must be Grayscale, RGB, or CMYK to save as JPEG");
152
153
  /* Treat alpha only as greyscale */
154
0
  if (n == 1 && alpha)
155
0
    alpha = 0;
156
0
  n -= alpha;
157
158
0
  if (alpha > 0)
159
0
    fz_throw(ctx, FZ_ERROR_ARGUMENT, "pixmap may not have alpha to save as JPEG");
160
161
0
  cinfo.mem = NULL;
162
0
  cinfo.global_state = 0;
163
0
  cinfo.err = jpeg_std_error(&err);
164
0
  err.error_exit = error_exit;
165
166
0
  cinfo.client_data = NULL;
167
0
  fz_jpg_mem_init((j_common_ptr)&cinfo, ctx);
168
169
0
  fz_try(ctx)
170
0
  {
171
0
    jpeg_create_compress(&cinfo);
172
173
0
    cinfo.dest = (void*) &dest;
174
0
    dest.pub.init_destination = init_destination;
175
0
    dest.pub.empty_output_buffer = empty_output_buffer;
176
0
    dest.pub.term_destination = term_destination;
177
0
    dest.out = out;
178
179
0
    cinfo.image_width = pix->w;
180
0
    cinfo.image_height = pix->h;
181
0
    cinfo.input_components = n;
182
0
    switch (n) {
183
0
    case 1:
184
0
      cinfo.in_color_space = JCS_GRAYSCALE;
185
0
      break;
186
0
    case 3:
187
0
      cinfo.in_color_space = JCS_RGB;
188
0
      break;
189
0
    case 4:
190
0
      cinfo.in_color_space = JCS_CMYK;
191
0
      break;
192
0
    }
193
194
0
    jpeg_set_defaults(&cinfo);
195
0
    jpeg_set_quality(&cinfo, quality, FALSE);
196
197
    /* Write image resolution */
198
0
    cinfo.density_unit = 1; /* dots/inch */
199
0
    cinfo.X_density = pix->xres;
200
0
    cinfo.Y_density = pix->yres;
201
202
    /* Disable chroma subsampling */
203
0
    cinfo.comp_info[0].h_samp_factor = 1;
204
0
    cinfo.comp_info[0].v_samp_factor = 1;
205
206
    /* Progressive JPEGs are smaller */
207
0
    jpeg_simple_progression(&cinfo);
208
209
0
    jpeg_start_compress(&cinfo, TRUE);
210
211
0
    if (fz_colorspace_is_cmyk(ctx, pix->colorspace) && invert_cmyk)
212
0
      fz_invert_pixmap_raw(ctx, pix);
213
214
0
    while (cinfo.next_scanline < cinfo.image_height) {
215
0
      row_pointer[0] = &pix->samples[cinfo.next_scanline * pix->stride];
216
0
      (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
217
0
    }
218
219
0
    if (fz_colorspace_is_cmyk(ctx, pix->colorspace) && invert_cmyk)
220
0
      fz_invert_pixmap_raw(ctx, pix);
221
222
0
    jpeg_finish_compress(&cinfo);
223
0
  }
224
0
  fz_always(ctx)
225
0
  {
226
0
    jpeg_destroy_compress(&cinfo);
227
0
    fz_jpg_mem_term((j_common_ptr)&cinfo);
228
0
  }
229
0
  fz_catch(ctx)
230
0
  {
231
0
    fz_rethrow(ctx);
232
0
  }
233
0
}
234
235
void
236
fz_save_pixmap_as_jpeg(fz_context *ctx, fz_pixmap *pixmap, const char *filename, int quality)
237
0
{
238
0
  fz_output *out = fz_new_output_with_path(ctx, filename, 0);
239
0
  fz_try(ctx)
240
0
  {
241
0
    fz_write_pixmap_as_jpeg(ctx, out, pixmap, quality, 1);
242
0
    fz_close_output(ctx, out);
243
0
  }
244
0
  fz_always(ctx)
245
0
  {
246
0
    fz_drop_output(ctx, out);
247
0
  }
248
0
  fz_catch(ctx)
249
0
  {
250
0
    fz_rethrow(ctx);
251
0
  }
252
0
}
253
254
static fz_buffer *
255
jpeg_from_pixmap(fz_context *ctx, fz_pixmap *pix, fz_color_params color_params, int quality, int invert_cmyk, int drop)
256
0
{
257
0
  fz_buffer *buf = NULL;
258
0
  fz_output *out = NULL;
259
0
  fz_pixmap *pix2 = NULL;
260
261
0
  fz_var(buf);
262
0
  fz_var(out);
263
0
  fz_var(pix2);
264
265
0
  if (pix->w == 0 || pix->h == 0)
266
0
  {
267
0
    if (drop)
268
0
      fz_drop_pixmap(ctx, pix);
269
0
    return NULL;
270
0
  }
271
272
0
  fz_try(ctx)
273
0
  {
274
0
    if (pix->colorspace && pix->colorspace != fz_device_gray(ctx) && pix->colorspace != fz_device_rgb(ctx) && pix->colorspace != fz_device_cmyk(ctx))
275
0
    {
276
0
      pix2 = fz_convert_pixmap(ctx, pix, fz_device_rgb(ctx), NULL, NULL, color_params, 1);
277
0
      if (drop)
278
0
      {
279
0
        fz_drop_pixmap(ctx, pix);
280
0
        pix = pix2;
281
0
        pix2 = NULL;
282
0
      }
283
0
      else
284
0
      {
285
0
        pix = pix2;
286
0
      }
287
0
    }
288
0
    buf = fz_new_buffer(ctx, 1024);
289
0
    out = fz_new_output_with_buffer(ctx, buf);
290
0
    fz_write_pixmap_as_jpeg(ctx, out, pix, quality, invert_cmyk);
291
0
    fz_close_output(ctx, out);
292
0
  }
293
0
  fz_always(ctx)
294
0
  {
295
0
    if (drop)
296
0
      fz_drop_pixmap(ctx, pix);
297
0
    fz_drop_pixmap(ctx, pix2);
298
0
    fz_drop_output(ctx, out);
299
0
  }
300
0
  fz_catch(ctx)
301
0
  {
302
0
    fz_drop_buffer(ctx, buf);
303
0
    fz_rethrow(ctx);
304
0
  }
305
0
  return buf;
306
0
}
307
308
fz_buffer *
309
fz_new_buffer_from_image_as_jpeg(fz_context *ctx, fz_image *image, fz_color_params color_params, int quality, int invert_cmyk)
310
0
{
311
0
  fz_pixmap *pix = fz_get_pixmap_from_image(ctx, image, NULL, NULL, NULL, NULL);
312
0
  return jpeg_from_pixmap(ctx, pix, color_params, quality, invert_cmyk, 1);
313
0
}
314
315
fz_buffer *
316
fz_new_buffer_from_pixmap_as_jpeg(fz_context *ctx, fz_pixmap *pix, fz_color_params color_params, int quality, int invert_cmyk)
317
0
{
318
0
  return jpeg_from_pixmap(ctx, pix, color_params, quality, invert_cmyk, 0);
319
0
}