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