comparison mupdf-source/source/fitz/output-png.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 "z-imp.h"
26
27 #include <string.h>
28
29 static inline void big32(unsigned char *buf, unsigned int v)
30 {
31 buf[0] = (v >> 24) & 0xff;
32 buf[1] = (v >> 16) & 0xff;
33 buf[2] = (v >> 8) & 0xff;
34 buf[3] = (v) & 0xff;
35 }
36
37 static void putchunk(fz_context *ctx, fz_output *out, char *tag, unsigned char *data, size_t size)
38 {
39 unsigned int sum;
40
41 if ((uint32_t)size != size)
42 fz_throw(ctx, FZ_ERROR_LIMIT, "PNG chunk too large");
43
44 fz_write_int32_be(ctx, out, (int)size);
45 fz_write_data(ctx, out, tag, 4);
46 fz_write_data(ctx, out, data, size);
47 sum = crc32(0, NULL, 0);
48 sum = crc32(sum, (unsigned char*)tag, 4);
49 sum = crc32(sum, data, (unsigned int)size);
50 fz_write_int32_be(ctx, out, sum);
51 }
52
53 void
54 fz_save_pixmap_as_png(fz_context *ctx, fz_pixmap *pixmap, const char *filename)
55 {
56 fz_output *out = fz_new_output_with_path(ctx, filename, 0);
57 fz_band_writer *writer = NULL;
58
59 fz_var(writer);
60
61 fz_try(ctx)
62 {
63 writer = fz_new_png_band_writer(ctx, out);
64 fz_write_header(ctx, writer, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, pixmap->xres, pixmap->yres, 0, pixmap->colorspace, pixmap->seps);
65 fz_write_band(ctx, writer, pixmap->stride, pixmap->h, pixmap->samples);
66 fz_close_band_writer(ctx, writer);
67 fz_close_output(ctx, out);
68 }
69 fz_always(ctx)
70 {
71 fz_drop_band_writer(ctx, writer);
72 fz_drop_output(ctx, out);
73 }
74 fz_catch(ctx)
75 {
76 fz_rethrow(ctx);
77 }
78 }
79
80 void
81 fz_write_pixmap_as_png(fz_context *ctx, fz_output *out, const fz_pixmap *pixmap)
82 {
83 fz_band_writer *writer;
84
85 if (!out)
86 return;
87
88 writer = fz_new_png_band_writer(ctx, out);
89
90 fz_try(ctx)
91 {
92 fz_write_header(ctx, writer, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, pixmap->xres, pixmap->yres, 0, pixmap->colorspace, pixmap->seps);
93 fz_write_band(ctx, writer, pixmap->stride, pixmap->h, pixmap->samples);
94 fz_close_band_writer(ctx, writer);
95 }
96 fz_always(ctx)
97 {
98 fz_drop_band_writer(ctx, writer);
99 }
100 fz_catch(ctx)
101 {
102 fz_rethrow(ctx);
103 }
104 }
105
106 typedef struct png_band_writer_s
107 {
108 fz_band_writer super;
109 unsigned char *udata;
110 unsigned char *cdata;
111 size_t usize, csize;
112 z_stream stream;
113 int stream_started;
114 int stream_ended;
115 } png_band_writer;
116
117 static void
118 png_write_icc(fz_context *ctx, png_band_writer *writer, fz_colorspace *cs)
119 {
120 #if FZ_ENABLE_ICC
121 if (cs && !(cs->flags & FZ_COLORSPACE_IS_DEVICE) && (cs->flags & FZ_COLORSPACE_IS_ICC) && cs->u.icc.buffer)
122 {
123 fz_output *out = writer->super.out;
124 size_t size, csize;
125 fz_buffer *buffer = cs->u.icc.buffer;
126 unsigned char *pos, *cdata, *chunk = NULL;
127 const char *name;
128
129 /* Deflate the profile */
130 cdata = fz_new_deflated_data_from_buffer(ctx, &csize, buffer, FZ_DEFLATE_DEFAULT);
131
132 if (!cdata)
133 return;
134
135 name = cs->name;
136 size = csize + strlen(name) + 2;
137
138 fz_try(ctx)
139 {
140 chunk = fz_calloc(ctx, size, 1);
141 pos = chunk;
142 memcpy(chunk, name, strlen(name));
143 pos += strlen(name) + 2;
144 memcpy(pos, cdata, csize);
145 putchunk(ctx, out, "iCCP", chunk, size);
146 }
147 fz_always(ctx)
148 {
149 fz_free(ctx, cdata);
150 fz_free(ctx, chunk);
151 }
152 fz_catch(ctx)
153 {
154 fz_rethrow(ctx);
155 }
156 }
157 #endif
158 }
159
160 static void
161 png_write_header(fz_context *ctx, fz_band_writer *writer_, fz_colorspace *cs)
162 {
163 png_band_writer *writer = (png_band_writer *)(void *)writer_;
164 fz_output *out = writer->super.out;
165 int w = writer->super.w;
166 int h = writer->super.h;
167 int n = writer->super.n;
168 int alpha = writer->super.alpha;
169 static const unsigned char pngsig[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
170 unsigned char head[13];
171 int color;
172
173 if (writer->super.s != 0)
174 fz_throw(ctx, FZ_ERROR_ARGUMENT, "PNGs cannot contain spot colors");
175 if (fz_colorspace_type(ctx, cs) == FZ_COLORSPACE_BGR)
176 fz_throw(ctx, FZ_ERROR_ARGUMENT, "pixmap can not be bgr");
177 if (cs && !fz_colorspace_is_gray(ctx, cs) && !fz_colorspace_is_rgb(ctx, cs))
178 fz_throw(ctx, FZ_ERROR_ARGUMENT, "pixmap must be grayscale or rgb to write as png");
179
180 /* Treat alpha only as greyscale */
181 if (n == 1 && alpha)
182 alpha = 0;
183 n -= alpha;
184
185 switch (n)
186 {
187 case 1: color = (alpha ? 4 : 0); break; /* 0 = Greyscale, 4 = Greyscale + Alpha */
188 case 3: color = (alpha ? 6 : 2); break; /* 2 = RGB, 6 = RGBA */
189 default:
190 fz_throw(ctx, FZ_ERROR_ARGUMENT, "pixmap must be grayscale or rgb to write as png");
191 }
192
193 big32(head+0, w);
194 big32(head+4, h);
195 head[8] = 8; /* depth */
196 head[9] = color;
197 head[10] = 0; /* compression */
198 head[11] = 0; /* filter */
199 head[12] = 0; /* interlace */
200
201 fz_write_data(ctx, out, pngsig, 8);
202 putchunk(ctx, out, "IHDR", head, 13);
203
204 big32(head+0, writer->super.xres * 100/2.54f + 0.5f);
205 big32(head+4, writer->super.yres * 100/2.54f + 0.5f);
206 head[8] = 1; /* metre */
207 putchunk(ctx, out, "pHYs", head, 9);
208
209 png_write_icc(ctx, writer, cs);
210 }
211
212 static void
213 png_write_band(fz_context *ctx, fz_band_writer *writer_, int stride, int band_start, int band_height, const unsigned char *sp)
214 {
215 png_band_writer *writer = (png_band_writer *)(void *)writer_;
216 fz_output *out = writer->super.out;
217 unsigned char *dp;
218 int y, x, k, err, finalband;
219 int w, h, n;
220 size_t remain;
221
222 if (!out)
223 return;
224
225 w = writer->super.w;
226 h = writer->super.h;
227 n = writer->super.n;
228
229 finalband = (band_start+band_height >= h);
230 if (finalband)
231 band_height = h - band_start;
232
233 if (writer->udata == NULL)
234 {
235 size_t usize = w;
236
237 if (usize > SIZE_MAX / n - 1)
238 fz_throw(ctx, FZ_ERROR_LIMIT, "png data too large.");
239 usize = usize * n + 1;
240 if (usize > SIZE_MAX / band_height)
241 fz_throw(ctx, FZ_ERROR_LIMIT, "png data too large.");
242 usize *= band_height;
243 writer->stream.opaque = ctx;
244 writer->stream.zalloc = fz_zlib_alloc;
245 writer->stream.zfree = fz_zlib_free;
246 writer->stream_started = 1;
247 err = deflateInit(&writer->stream, Z_DEFAULT_COMPRESSION);
248 if (err != Z_OK)
249 fz_throw(ctx, FZ_ERROR_LIBRARY, "compression error %d", err);
250 writer->usize = usize;
251 /* Now figure out how large a buffer we need to compress into.
252 * deflateBound always expands a bit, and it's limited by being
253 * a uLong rather than a size_t. */
254 writer->csize = writer->usize >= UINT32_MAX ? UINT32_MAX : deflateBound(&writer->stream, (uLong)writer->usize);
255 if (writer->csize < writer->usize || writer->csize > UINT32_MAX) /* Check for overflow */
256 writer->csize = UINT32_MAX;
257 writer->udata = Memento_label(fz_malloc(ctx, writer->usize), "png_write_udata");
258 writer->cdata = Memento_label(fz_malloc(ctx, writer->csize), "png_write_cdata");
259 }
260
261 dp = writer->udata;
262 stride -= w*n;
263 if (writer->super.alpha)
264 {
265 /* Unpremultiply data */
266 for (y = 0; y < band_height; y++)
267 {
268 *dp++ = 0; /* none prediction filter */
269 for (x = 0; x < w; x++)
270 {
271 int a = sp[n-1];
272 int inva = a ? 256*255/a : 0;
273 for (k = 0; k < n-1; k++)
274 dp[k] = (sp[k] * inva + 128)>>8;
275 dp[k] = a;
276 sp += n;
277 dp += n;
278 }
279 sp += stride;
280 }
281 }
282 else
283 {
284 for (y = 0; y < band_height; y++)
285 {
286 *dp++ = 0; /* none prediction filter */
287 for (x = 0; x < w; x++)
288 {
289 for (k = 0; k < n; k++)
290 dp[k] = sp[k];
291 sp += n;
292 dp += n;
293 }
294 sp += stride;
295 }
296 }
297
298 remain = dp - writer->udata;
299 dp = writer->udata;
300
301 do
302 {
303 size_t eaten;
304
305 writer->stream.next_in = dp;
306 writer->stream.avail_in = (uInt)(remain <= UINT32_MAX ? remain : UINT32_MAX);
307 writer->stream.next_out = writer->cdata;
308 writer->stream.avail_out = writer->csize <= UINT32_MAX ? (uInt)writer->csize : UINT32_MAX;
309
310 err = deflate(&writer->stream, (finalband && remain == writer->stream.avail_in) ? Z_FINISH : Z_NO_FLUSH);
311 if (err != Z_OK && err != Z_STREAM_END)
312 fz_throw(ctx, FZ_ERROR_LIBRARY, "compression error %d", err);
313
314 /* We are guaranteed that writer->stream.next_in will have been updated for the
315 * data that has been eaten. */
316 eaten = (writer->stream.next_in - dp);
317 remain -= eaten;
318 dp += eaten;
319
320 /* We are guaranteed that writer->stream.next_out will have been updated for the
321 * data that has been written. */
322 if (writer->stream.next_out != writer->cdata)
323 putchunk(ctx, out, "IDAT", writer->cdata, writer->stream.next_out - writer->cdata);
324
325 /* Zlib only guarantees to have finished when we have no more data to feed in, and
326 * the last call to deflate did not return with avail_out == 0. (i.e. no more is
327 * buffered internally.) */
328 }
329 while (remain != 0 || writer->stream.avail_out == 0);
330 }
331
332 static void
333 png_write_trailer(fz_context *ctx, fz_band_writer *writer_)
334 {
335 png_band_writer *writer = (png_band_writer *)(void *)writer_;
336 fz_output *out = writer->super.out;
337 unsigned char block[1];
338 int err;
339
340 writer->stream_ended = 1;
341 err = deflateEnd(&writer->stream);
342 if (err != Z_OK)
343 fz_throw(ctx, FZ_ERROR_LIBRARY, "compression error %d", err);
344
345 putchunk(ctx, out, "IEND", block, 0);
346 }
347
348 static void
349 png_drop_band_writer(fz_context *ctx, fz_band_writer *writer_)
350 {
351 png_band_writer *writer = (png_band_writer *)(void *)writer_;
352
353 if (writer->stream_started && !writer->stream_ended)
354 {
355 int err = deflateEnd(&writer->stream);
356 if (err != Z_OK)
357 fz_warn(ctx, "ignoring compression error %d", err);
358 }
359
360 fz_free(ctx, writer->cdata);
361 fz_free(ctx, writer->udata);
362 }
363
364 fz_band_writer *fz_new_png_band_writer(fz_context *ctx, fz_output *out)
365 {
366 png_band_writer *writer = fz_new_band_writer(ctx, png_band_writer, out);
367
368 writer->super.header = png_write_header;
369 writer->super.band = png_write_band;
370 writer->super.trailer = png_write_trailer;
371 writer->super.drop = png_drop_band_writer;
372
373 return &writer->super;
374 }
375
376 /* We use an auxiliary function to do pixmap_as_png, as it can enable us to
377 * drop pix early in the case where we have to convert, potentially saving
378 * us having to have 2 copies of the pixmap and a buffer open at once. */
379 static fz_buffer *
380 png_from_pixmap(fz_context *ctx, fz_pixmap *pix, fz_color_params color_params, int drop)
381 {
382 fz_buffer *buf = NULL;
383 fz_output *out = NULL;
384 fz_pixmap *pix2 = NULL;
385
386 fz_var(buf);
387 fz_var(out);
388 fz_var(pix2);
389
390 if (pix->w == 0 || pix->h == 0)
391 {
392 if (drop)
393 fz_drop_pixmap(ctx, pix);
394 return NULL;
395 }
396
397 fz_try(ctx)
398 {
399 if (pix->colorspace && pix->colorspace != fz_device_gray(ctx) && pix->colorspace != fz_device_rgb(ctx))
400 {
401 pix2 = fz_convert_pixmap(ctx, pix, fz_device_rgb(ctx), NULL, NULL, color_params, 1);
402 if (drop)
403 fz_drop_pixmap(ctx, pix);
404 pix = pix2;
405 }
406 buf = fz_new_buffer(ctx, 1024);
407 out = fz_new_output_with_buffer(ctx, buf);
408 fz_write_pixmap_as_png(ctx, out, pix);
409 fz_close_output(ctx, out);
410 }
411 fz_always(ctx)
412 {
413 fz_drop_pixmap(ctx, drop ? pix : pix2);
414 fz_drop_output(ctx, out);
415 }
416 fz_catch(ctx)
417 {
418 fz_drop_buffer(ctx, buf);
419 fz_rethrow(ctx);
420 }
421 return buf;
422 }
423
424 fz_buffer *
425 fz_new_buffer_from_image_as_png(fz_context *ctx, fz_image *image, fz_color_params color_params)
426 {
427 fz_pixmap *pix = fz_get_pixmap_from_image(ctx, image, NULL, NULL, NULL, NULL);
428 return png_from_pixmap(ctx, pix, color_params, 1);
429 }
430
431 fz_buffer *
432 fz_new_buffer_from_pixmap_as_png(fz_context *ctx, fz_pixmap *pix, fz_color_params color_params)
433 {
434 return png_from_pixmap(ctx, pix, color_params, 0);
435 }