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