]> git.saurik.com Git - wxWidgets.git/blame - src/png/pngwrite.c
Disable some wxWebView tests that fail on the buildbot but not locally.
[wxWidgets.git] / src / png / pngwrite.c
CommitLineData
0272a10d
VZ
1
2/* pngwrite.c - general routines to write a PNG file
3 *
9c0d9ce3
DS
4 * Last changed in libpng 1.5.6 [November 3, 2011]
5 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
0272a10d
VZ
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
b61cc19c
PC
8 *
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
0272a10d
VZ
12 */
13
b61cc19c 14#include "pngpriv.h"
0272a10d 15
9c0d9ce3
DS
16#ifdef PNG_WRITE_SUPPORTED
17
0272a10d
VZ
18/* Writes all the PNG information. This is the suggested way to use the
19 * library. If you have a new chunk to add, make a function to write it,
20 * and put it in the correct location here. If you want the chunk written
21 * after the image data, put it in png_write_end(). I strongly encourage
22 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
23 * the chunk, as that will keep the code from breaking if you want to just
24 * write a plain PNG file. If you have long comments, I suggest writing
25 * them in png_write_end(), and compressing them.
26 */
27void PNGAPI
28png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
29{
970f6abe 30 png_debug(1, "in png_write_info_before_PLTE");
b61cc19c 31
0272a10d
VZ
32 if (png_ptr == NULL || info_ptr == NULL)
33 return;
9c0d9ce3 34
0272a10d
VZ
35 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
36 {
b61cc19c
PC
37 /* Write PNG signature */
38 png_write_sig(png_ptr);
9c0d9ce3 39
b61cc19c
PC
40#ifdef PNG_MNG_FEATURES_SUPPORTED
41 if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) && \
9c0d9ce3 42 (png_ptr->mng_features_permitted))
0272a10d 43 {
970f6abe 44 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
b61cc19c 45 png_ptr->mng_features_permitted = 0;
0272a10d
VZ
46 }
47#endif
9c0d9ce3 48
b61cc19c 49 /* Write IHDR information. */
0272a10d 50 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
9c0d9ce3
DS
51 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
52 info_ptr->filter_type,
b61cc19c 53#ifdef PNG_WRITE_INTERLACING_SUPPORTED
9c0d9ce3 54 info_ptr->interlace_type);
0272a10d 55#else
9c0d9ce3 56 0);
0272a10d 57#endif
b61cc19c
PC
58 /* The rest of these check to see if the valid field has the appropriate
59 * flag set, and if it does, writes the chunk.
60 */
61#ifdef PNG_WRITE_gAMA_SUPPORTED
0272a10d 62 if (info_ptr->valid & PNG_INFO_gAMA)
9c0d9ce3 63 png_write_gAMA_fixed(png_ptr, info_ptr->gamma);
0272a10d 64#endif
b61cc19c 65#ifdef PNG_WRITE_sRGB_SUPPORTED
0272a10d
VZ
66 if (info_ptr->valid & PNG_INFO_sRGB)
67 png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
68#endif
9c0d9ce3 69
b61cc19c 70#ifdef PNG_WRITE_iCCP_SUPPORTED
0272a10d
VZ
71 if (info_ptr->valid & PNG_INFO_iCCP)
72 png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
9c0d9ce3 73 (png_charp)info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
0272a10d 74#endif
b61cc19c 75#ifdef PNG_WRITE_sBIT_SUPPORTED
0272a10d
VZ
76 if (info_ptr->valid & PNG_INFO_sBIT)
77 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
78#endif
b61cc19c 79#ifdef PNG_WRITE_cHRM_SUPPORTED
0272a10d 80 if (info_ptr->valid & PNG_INFO_cHRM)
0272a10d 81 png_write_cHRM_fixed(png_ptr,
9c0d9ce3
DS
82 info_ptr->x_white, info_ptr->y_white,
83 info_ptr->x_red, info_ptr->y_red,
84 info_ptr->x_green, info_ptr->y_green,
85 info_ptr->x_blue, info_ptr->y_blue);
0272a10d 86#endif
9c0d9ce3 87
b61cc19c 88#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
0272a10d
VZ
89 if (info_ptr->unknown_chunks_num)
90 {
b61cc19c 91 png_unknown_chunk *up;
0272a10d 92
b61cc19c 93 png_debug(5, "writing extra chunks");
0272a10d 94
b61cc19c
PC
95 for (up = info_ptr->unknown_chunks;
96 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
97 up++)
98 {
99 int keep = png_handle_as_unknown(png_ptr, up->name);
9c0d9ce3 100
0272a10d 101 if (keep != PNG_HANDLE_CHUNK_NEVER &&
9c0d9ce3
DS
102 up->location &&
103 !(up->location & PNG_HAVE_PLTE) &&
104 !(up->location & PNG_HAVE_IDAT) &&
105 !(up->location & PNG_AFTER_IDAT) &&
106 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
107 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
0272a10d 108 {
970f6abe
VZ
109 if (up->size == 0)
110 png_warning(png_ptr, "Writing zero-length unknown chunk");
9c0d9ce3 111
0272a10d
VZ
112 png_write_chunk(png_ptr, up->name, up->data, up->size);
113 }
b61cc19c 114 }
0272a10d
VZ
115 }
116#endif
117 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
118 }
119}
120
121void PNGAPI
122png_write_info(png_structp png_ptr, png_infop info_ptr)
123{
124#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
125 int i;
126#endif
127
970f6abe 128 png_debug(1, "in png_write_info");
0272a10d
VZ
129
130 if (png_ptr == NULL || info_ptr == NULL)
131 return;
132
133 png_write_info_before_PLTE(png_ptr, info_ptr);
134
135 if (info_ptr->valid & PNG_INFO_PLTE)
136 png_write_PLTE(png_ptr, info_ptr->palette,
9c0d9ce3
DS
137 (png_uint_32)info_ptr->num_palette);
138
0272a10d
VZ
139 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
140 png_error(png_ptr, "Valid palette required for paletted images");
141
b61cc19c 142#ifdef PNG_WRITE_tRNS_SUPPORTED
0272a10d 143 if (info_ptr->valid & PNG_INFO_tRNS)
b61cc19c
PC
144 {
145#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
146 /* Invert the alpha channel (in tRNS) */
147 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
9c0d9ce3 148 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
0272a10d 149 {
b61cc19c
PC
150 int j;
151 for (j = 0; j<(int)info_ptr->num_trans; j++)
9c0d9ce3
DS
152 info_ptr->trans_alpha[j] =
153 (png_byte)(255 - info_ptr->trans_alpha[j]);
b61cc19c 154 }
0272a10d 155#endif
b61cc19c 156 png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
9c0d9ce3 157 info_ptr->num_trans, info_ptr->color_type);
b61cc19c 158 }
0272a10d 159#endif
b61cc19c 160#ifdef PNG_WRITE_bKGD_SUPPORTED
0272a10d
VZ
161 if (info_ptr->valid & PNG_INFO_bKGD)
162 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
163#endif
9c0d9ce3 164
b61cc19c 165#ifdef PNG_WRITE_hIST_SUPPORTED
0272a10d
VZ
166 if (info_ptr->valid & PNG_INFO_hIST)
167 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
168#endif
9c0d9ce3 169
b61cc19c 170#ifdef PNG_WRITE_oFFs_SUPPORTED
0272a10d
VZ
171 if (info_ptr->valid & PNG_INFO_oFFs)
172 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
9c0d9ce3 173 info_ptr->offset_unit_type);
0272a10d 174#endif
9c0d9ce3 175
b61cc19c 176#ifdef PNG_WRITE_pCAL_SUPPORTED
0272a10d
VZ
177 if (info_ptr->valid & PNG_INFO_pCAL)
178 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
9c0d9ce3
DS
179 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
180 info_ptr->pcal_units, info_ptr->pcal_params);
0272a10d 181#endif
b61cc19c 182
b61cc19c 183#ifdef PNG_WRITE_sCAL_SUPPORTED
9c0d9ce3 184 if (info_ptr->valid & PNG_INFO_sCAL)
0272a10d
VZ
185 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
186 info_ptr->scal_s_width, info_ptr->scal_s_height);
b61cc19c
PC
187#endif /* sCAL */
188
189#ifdef PNG_WRITE_pHYs_SUPPORTED
0272a10d
VZ
190 if (info_ptr->valid & PNG_INFO_pHYs)
191 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
9c0d9ce3 192 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
b61cc19c
PC
193#endif /* pHYs */
194
195#ifdef PNG_WRITE_tIME_SUPPORTED
0272a10d
VZ
196 if (info_ptr->valid & PNG_INFO_tIME)
197 {
198 png_write_tIME(png_ptr, &(info_ptr->mod_time));
199 png_ptr->mode |= PNG_WROTE_tIME;
200 }
b61cc19c
PC
201#endif /* tIME */
202
203#ifdef PNG_WRITE_sPLT_SUPPORTED
0272a10d 204 if (info_ptr->valid & PNG_INFO_sPLT)
9c0d9ce3
DS
205 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
206 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
b61cc19c
PC
207#endif /* sPLT */
208
209#ifdef PNG_WRITE_TEXT_SUPPORTED
0272a10d
VZ
210 /* Check to see if we need to write text chunks */
211 for (i = 0; i < info_ptr->num_text; i++)
212 {
970f6abe 213 png_debug2(2, "Writing header text chunk %d, type %d", i,
9c0d9ce3 214 info_ptr->text[i].compression);
b61cc19c 215 /* An internationalized chunk? */
0272a10d
VZ
216 if (info_ptr->text[i].compression > 0)
217 {
b61cc19c 218#ifdef PNG_WRITE_iTXt_SUPPORTED
9c0d9ce3
DS
219 /* Write international chunk */
220 png_write_iTXt(png_ptr,
221 info_ptr->text[i].compression,
222 info_ptr->text[i].key,
223 info_ptr->text[i].lang,
224 info_ptr->text[i].lang_key,
225 info_ptr->text[i].text);
0272a10d
VZ
226#else
227 png_warning(png_ptr, "Unable to write international text");
228#endif
229 /* Mark this chunk as written */
230 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
231 }
9c0d9ce3 232
0272a10d
VZ
233 /* If we want a compressed text chunk */
234 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
235 {
b61cc19c
PC
236#ifdef PNG_WRITE_zTXt_SUPPORTED
237 /* Write compressed chunk */
0272a10d 238 png_write_zTXt(png_ptr, info_ptr->text[i].key,
9c0d9ce3
DS
239 info_ptr->text[i].text, 0,
240 info_ptr->text[i].compression);
0272a10d
VZ
241#else
242 png_warning(png_ptr, "Unable to write compressed text");
243#endif
244 /* Mark this chunk as written */
245 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
246 }
9c0d9ce3 247
0272a10d
VZ
248 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
249 {
b61cc19c
PC
250#ifdef PNG_WRITE_tEXt_SUPPORTED
251 /* Write uncompressed chunk */
0272a10d 252 png_write_tEXt(png_ptr, info_ptr->text[i].key,
9c0d9ce3
DS
253 info_ptr->text[i].text,
254 0);
b61cc19c
PC
255 /* Mark this chunk as written */
256 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
0272a10d 257#else
b61cc19c 258 /* Can't get here */
0272a10d
VZ
259 png_warning(png_ptr, "Unable to write uncompressed text");
260#endif
0272a10d
VZ
261 }
262 }
b61cc19c
PC
263#endif /* tEXt */
264
265#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
0272a10d
VZ
266 if (info_ptr->unknown_chunks_num)
267 {
b61cc19c 268 png_unknown_chunk *up;
0272a10d 269
b61cc19c 270 png_debug(5, "writing extra chunks");
0272a10d 271
b61cc19c
PC
272 for (up = info_ptr->unknown_chunks;
273 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
274 up++)
275 {
276 int keep = png_handle_as_unknown(png_ptr, up->name);
0272a10d 277 if (keep != PNG_HANDLE_CHUNK_NEVER &&
9c0d9ce3
DS
278 up->location &&
279 (up->location & PNG_HAVE_PLTE) &&
280 !(up->location & PNG_HAVE_IDAT) &&
281 !(up->location & PNG_AFTER_IDAT) &&
282 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
283 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
0272a10d
VZ
284 {
285 png_write_chunk(png_ptr, up->name, up->data, up->size);
286 }
b61cc19c 287 }
0272a10d
VZ
288 }
289#endif
290}
291
292/* Writes the end of the PNG file. If you don't want to write comments or
293 * time information, you can pass NULL for info. If you already wrote these
294 * in png_write_info(), do not write them again here. If you have long
295 * comments, I suggest writing them here, and compressing them.
296 */
297void PNGAPI
298png_write_end(png_structp png_ptr, png_infop info_ptr)
299{
970f6abe 300 png_debug(1, "in png_write_end");
b61cc19c 301
0272a10d
VZ
302 if (png_ptr == NULL)
303 return;
9c0d9ce3 304
0272a10d
VZ
305 if (!(png_ptr->mode & PNG_HAVE_IDAT))
306 png_error(png_ptr, "No IDATs written into file");
307
b61cc19c 308 /* See if user wants us to write information chunks */
0272a10d
VZ
309 if (info_ptr != NULL)
310 {
b61cc19c 311#ifdef PNG_WRITE_TEXT_SUPPORTED
0272a10d
VZ
312 int i; /* local index variable */
313#endif
b61cc19c
PC
314#ifdef PNG_WRITE_tIME_SUPPORTED
315 /* Check to see if user has supplied a time chunk */
0272a10d 316 if ((info_ptr->valid & PNG_INFO_tIME) &&
9c0d9ce3 317 !(png_ptr->mode & PNG_WROTE_tIME))
0272a10d 318 png_write_tIME(png_ptr, &(info_ptr->mod_time));
9c0d9ce3 319
0272a10d 320#endif
b61cc19c
PC
321#ifdef PNG_WRITE_TEXT_SUPPORTED
322 /* Loop through comment chunks */
0272a10d
VZ
323 for (i = 0; i < info_ptr->num_text; i++)
324 {
970f6abe 325 png_debug2(2, "Writing trailer text chunk %d, type %d", i,
0272a10d 326 info_ptr->text[i].compression);
b61cc19c 327 /* An internationalized chunk? */
0272a10d
VZ
328 if (info_ptr->text[i].compression > 0)
329 {
b61cc19c
PC
330#ifdef PNG_WRITE_iTXt_SUPPORTED
331 /* Write international chunk */
332 png_write_iTXt(png_ptr,
9c0d9ce3
DS
333 info_ptr->text[i].compression,
334 info_ptr->text[i].key,
335 info_ptr->text[i].lang,
336 info_ptr->text[i].lang_key,
337 info_ptr->text[i].text);
0272a10d 338#else
b61cc19c 339 png_warning(png_ptr, "Unable to write international text");
0272a10d 340#endif
b61cc19c
PC
341 /* Mark this chunk as written */
342 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
0272a10d 343 }
9c0d9ce3 344
0272a10d
VZ
345 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
346 {
b61cc19c
PC
347#ifdef PNG_WRITE_zTXt_SUPPORTED
348 /* Write compressed chunk */
0272a10d 349 png_write_zTXt(png_ptr, info_ptr->text[i].key,
9c0d9ce3
DS
350 info_ptr->text[i].text, 0,
351 info_ptr->text[i].compression);
0272a10d
VZ
352#else
353 png_warning(png_ptr, "Unable to write compressed text");
354#endif
355 /* Mark this chunk as written */
356 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
357 }
9c0d9ce3 358
0272a10d
VZ
359 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
360 {
b61cc19c
PC
361#ifdef PNG_WRITE_tEXt_SUPPORTED
362 /* Write uncompressed chunk */
0272a10d 363 png_write_tEXt(png_ptr, info_ptr->text[i].key,
9c0d9ce3 364 info_ptr->text[i].text, 0);
0272a10d
VZ
365#else
366 png_warning(png_ptr, "Unable to write uncompressed text");
367#endif
368
369 /* Mark this chunk as written */
370 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
371 }
372 }
373#endif
b61cc19c 374#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
0272a10d
VZ
375 if (info_ptr->unknown_chunks_num)
376 {
b61cc19c 377 png_unknown_chunk *up;
0272a10d 378
b61cc19c 379 png_debug(5, "writing extra chunks");
0272a10d 380
b61cc19c
PC
381 for (up = info_ptr->unknown_chunks;
382 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
383 up++)
384 {
385 int keep = png_handle_as_unknown(png_ptr, up->name);
0272a10d 386 if (keep != PNG_HANDLE_CHUNK_NEVER &&
9c0d9ce3
DS
387 up->location &&
388 (up->location & PNG_AFTER_IDAT) &&
389 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
390 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
0272a10d
VZ
391 {
392 png_write_chunk(png_ptr, up->name, up->data, up->size);
393 }
b61cc19c 394 }
0272a10d
VZ
395 }
396#endif
397 }
398
399 png_ptr->mode |= PNG_AFTER_IDAT;
400
b61cc19c 401 /* Write end of PNG file */
0272a10d 402 png_write_IEND(png_ptr);
970f6abe
VZ
403 /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
404 * and restored again in libpng-1.2.30, may cause some applications that
405 * do not set png_ptr->output_flush_fn to crash. If your application
406 * experiences a problem, please try building libpng with
407 * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
b61cc19c 408 * png-mng-implement at lists.sf.net .
970f6abe 409 */
b61cc19c
PC
410#ifdef PNG_WRITE_FLUSH_SUPPORTED
411# ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
970f6abe 412 png_flush(png_ptr);
b61cc19c 413# endif
970f6abe 414#endif
0272a10d
VZ
415}
416
b61cc19c
PC
417#ifdef PNG_CONVERT_tIME_SUPPORTED
418/* "tm" structure is not supported on WindowsCE */
0272a10d 419void PNGAPI
9c0d9ce3 420png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm FAR * ttime)
0272a10d 421{
970f6abe 422 png_debug(1, "in png_convert_from_struct_tm");
b61cc19c 423
0272a10d
VZ
424 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
425 ptime->month = (png_byte)(ttime->tm_mon + 1);
426 ptime->day = (png_byte)ttime->tm_mday;
427 ptime->hour = (png_byte)ttime->tm_hour;
428 ptime->minute = (png_byte)ttime->tm_min;
429 ptime->second = (png_byte)ttime->tm_sec;
430}
431
432void PNGAPI
433png_convert_from_time_t(png_timep ptime, time_t ttime)
434{
435 struct tm *tbuf;
436
970f6abe 437 png_debug(1, "in png_convert_from_time_t");
b61cc19c 438
0272a10d
VZ
439 tbuf = gmtime(&ttime);
440 png_convert_from_struct_tm(ptime, tbuf);
441}
442#endif
0272a10d
VZ
443
444/* Initialize png_ptr structure, and allocate any memory needed */
9c0d9ce3
DS
445PNG_FUNCTION(png_structp,PNGAPI
446png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
447 png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
0272a10d
VZ
448{
449#ifdef PNG_USER_MEM_SUPPORTED
450 return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
9c0d9ce3 451 warn_fn, NULL, NULL, NULL));
0272a10d
VZ
452}
453
454/* Alternate initialize png_ptr structure, and allocate any memory needed */
9c0d9ce3
DS
455static void png_reset_filter_heuristics(png_structp png_ptr); /* forward decl */
456
457PNG_FUNCTION(png_structp,PNGAPI
458png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
459 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
460 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
0272a10d
VZ
461{
462#endif /* PNG_USER_MEM_SUPPORTED */
b61cc19c 463 volatile int png_cleanup_needed = 0;
970f6abe 464#ifdef PNG_SETJMP_SUPPORTED
b61cc19c 465 volatile
970f6abe 466#endif
b61cc19c 467 png_structp png_ptr;
0272a10d
VZ
468#ifdef PNG_SETJMP_SUPPORTED
469#ifdef USE_FAR_KEYWORD
9c0d9ce3 470 jmp_buf tmp_jmpbuf;
0272a10d
VZ
471#endif
472#endif
b61cc19c 473
970f6abe 474 png_debug(1, "in png_create_write_struct");
b61cc19c 475
0272a10d
VZ
476#ifdef PNG_USER_MEM_SUPPORTED
477 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
9c0d9ce3 478 (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
0272a10d
VZ
479#else
480 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
481#endif /* PNG_USER_MEM_SUPPORTED */
482 if (png_ptr == NULL)
483 return (NULL);
484
b61cc19c 485 /* Added at libpng-1.2.6 */
0272a10d 486#ifdef PNG_SET_USER_LIMITS_SUPPORTED
b61cc19c
PC
487 png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
488 png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
0272a10d
VZ
489#endif
490
491#ifdef PNG_SETJMP_SUPPORTED
b61cc19c
PC
492/* Applications that neglect to set up their own setjmp() and then
493 encounter a png_error() will longjmp here. Since the jmpbuf is
494 then meaningless we abort instead of returning. */
0272a10d 495#ifdef USE_FAR_KEYWORD
9c0d9ce3 496 if (setjmp(tmp_jmpbuf))
0272a10d 497#else
b61cc19c 498 if (setjmp(png_jmpbuf(png_ptr))) /* sets longjmp to match setjmp */
0272a10d 499#endif
0272a10d 500#ifdef USE_FAR_KEYWORD
9c0d9ce3 501 png_memcpy(png_jmpbuf(png_ptr), tmp_jmpbuf, png_sizeof(jmp_buf));
0272a10d 502#endif
b61cc19c 503 PNG_ABORT();
0272a10d
VZ
504#endif
505
506#ifdef PNG_USER_MEM_SUPPORTED
507 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
508#endif /* PNG_USER_MEM_SUPPORTED */
509 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
510
9c0d9ce3
DS
511 if (!png_user_version_check(png_ptr, user_png_ver))
512 png_cleanup_needed = 1;
0272a10d 513
b61cc19c 514 /* Initialize zbuf - compression buffer */
0272a10d 515 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
9c0d9ce3 516
b61cc19c 517 if (!png_cleanup_needed)
0272a10d 518 {
b61cc19c 519 png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,
9c0d9ce3 520 png_ptr->zbuf_size);
b61cc19c
PC
521 if (png_ptr->zbuf == NULL)
522 png_cleanup_needed = 1;
0272a10d 523 }
9c0d9ce3 524
b61cc19c 525 if (png_cleanup_needed)
0272a10d 526 {
b61cc19c
PC
527 /* Clean up PNG structure and deallocate any memory. */
528 png_free(png_ptr, png_ptr->zbuf);
529 png_ptr->zbuf = NULL;
530#ifdef PNG_USER_MEM_SUPPORTED
531 png_destroy_struct_2((png_voidp)png_ptr,
9c0d9ce3 532 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
0272a10d 533#else
b61cc19c 534 png_destroy_struct((png_voidp)png_ptr);
0272a10d 535#endif
b61cc19c
PC
536 return (NULL);
537 }
0272a10d 538
b61cc19c 539 png_set_write_fn(png_ptr, NULL, NULL, NULL);
0272a10d 540
b61cc19c 541#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
9c0d9ce3 542 png_reset_filter_heuristics(png_ptr);
0272a10d 543#endif
b61cc19c
PC
544
545 return (png_ptr);
0272a10d
VZ
546}
547
b61cc19c 548
0272a10d
VZ
549/* Write a few rows of image data. If the image is interlaced,
550 * either you will have to write the 7 sub images, or, if you
551 * have called png_set_interlace_handling(), you will have to
552 * "write" the image seven times.
553 */
554void PNGAPI
555png_write_rows(png_structp png_ptr, png_bytepp row,
9c0d9ce3 556 png_uint_32 num_rows)
0272a10d
VZ
557{
558 png_uint_32 i; /* row counter */
559 png_bytepp rp; /* row pointer */
560
970f6abe 561 png_debug(1, "in png_write_rows");
0272a10d
VZ
562
563 if (png_ptr == NULL)
564 return;
565
b61cc19c 566 /* Loop through the rows */
0272a10d
VZ
567 for (i = 0, rp = row; i < num_rows; i++, rp++)
568 {
569 png_write_row(png_ptr, *rp);
570 }
571}
572
573/* Write the image. You only need to call this function once, even
574 * if you are writing an interlaced image.
575 */
576void PNGAPI
577png_write_image(png_structp png_ptr, png_bytepp image)
578{
579 png_uint_32 i; /* row index */
580 int pass, num_pass; /* pass variables */
581 png_bytepp rp; /* points to current row */
582
583 if (png_ptr == NULL)
584 return;
585
970f6abe 586 png_debug(1, "in png_write_image");
b61cc19c
PC
587
588#ifdef PNG_WRITE_INTERLACING_SUPPORTED
589 /* Initialize interlace handling. If image is not interlaced,
590 * this will set pass to 1
591 */
0272a10d
VZ
592 num_pass = png_set_interlace_handling(png_ptr);
593#else
594 num_pass = 1;
595#endif
b61cc19c 596 /* Loop through passes */
0272a10d
VZ
597 for (pass = 0; pass < num_pass; pass++)
598 {
b61cc19c 599 /* Loop through image */
0272a10d
VZ
600 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
601 {
602 png_write_row(png_ptr, *rp);
603 }
604 }
605}
606
b61cc19c 607/* Called by user to write a row of image data */
0272a10d 608void PNGAPI
9c0d9ce3 609png_write_row(png_structp png_ptr, png_const_bytep row)
0272a10d 610{
9c0d9ce3
DS
611 /* 1.5.6: moved from png_struct to be a local structure: */
612 png_row_info row_info;
613
0272a10d
VZ
614 if (png_ptr == NULL)
615 return;
b61cc19c 616
9c0d9ce3 617 png_debug2(1, "in png_write_row (row %u, pass %d)",
0272a10d
VZ
618 png_ptr->row_number, png_ptr->pass);
619
b61cc19c 620 /* Initialize transformations and other stuff if first time */
0272a10d
VZ
621 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
622 {
b61cc19c
PC
623 /* Make sure we wrote the header info */
624 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
625 png_error(png_ptr,
9c0d9ce3 626 "png_write_info was never called before png_write_row");
0272a10d 627
b61cc19c 628 /* Check for transforms that have been set but were defined out */
0272a10d 629#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
b61cc19c
PC
630 if (png_ptr->transformations & PNG_INVERT_MONO)
631 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
0272a10d 632#endif
9c0d9ce3 633
0272a10d 634#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
b61cc19c
PC
635 if (png_ptr->transformations & PNG_FILLER)
636 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
0272a10d 637#endif
b61cc19c
PC
638#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
639 defined(PNG_READ_PACKSWAP_SUPPORTED)
640 if (png_ptr->transformations & PNG_PACKSWAP)
641 png_warning(png_ptr,
642 "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
0272a10d 643#endif
9c0d9ce3 644
0272a10d 645#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
b61cc19c
PC
646 if (png_ptr->transformations & PNG_PACK)
647 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
0272a10d 648#endif
9c0d9ce3 649
0272a10d 650#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
b61cc19c
PC
651 if (png_ptr->transformations & PNG_SHIFT)
652 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
0272a10d 653#endif
9c0d9ce3 654
0272a10d 655#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
b61cc19c
PC
656 if (png_ptr->transformations & PNG_BGR)
657 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
0272a10d 658#endif
9c0d9ce3 659
0272a10d 660#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
b61cc19c
PC
661 if (png_ptr->transformations & PNG_SWAP_BYTES)
662 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
0272a10d
VZ
663#endif
664
665 png_write_start_row(png_ptr);
666 }
667
b61cc19c
PC
668#ifdef PNG_WRITE_INTERLACING_SUPPORTED
669 /* If interlaced and not interested in row, return */
0272a10d
VZ
670 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
671 {
672 switch (png_ptr->pass)
673 {
674 case 0:
675 if (png_ptr->row_number & 0x07)
676 {
677 png_write_finish_row(png_ptr);
678 return;
679 }
680 break;
9c0d9ce3 681
0272a10d
VZ
682 case 1:
683 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
684 {
685 png_write_finish_row(png_ptr);
686 return;
687 }
688 break;
9c0d9ce3 689
0272a10d
VZ
690 case 2:
691 if ((png_ptr->row_number & 0x07) != 4)
692 {
693 png_write_finish_row(png_ptr);
694 return;
695 }
696 break;
9c0d9ce3 697
0272a10d
VZ
698 case 3:
699 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
700 {
701 png_write_finish_row(png_ptr);
702 return;
703 }
704 break;
9c0d9ce3 705
0272a10d
VZ
706 case 4:
707 if ((png_ptr->row_number & 0x03) != 2)
708 {
709 png_write_finish_row(png_ptr);
710 return;
711 }
712 break;
9c0d9ce3 713
0272a10d
VZ
714 case 5:
715 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
716 {
717 png_write_finish_row(png_ptr);
718 return;
719 }
720 break;
9c0d9ce3 721
0272a10d
VZ
722 case 6:
723 if (!(png_ptr->row_number & 0x01))
724 {
725 png_write_finish_row(png_ptr);
726 return;
727 }
728 break;
9c0d9ce3
DS
729
730 default: /* error: ignore it */
731 break;
0272a10d
VZ
732 }
733 }
734#endif
735
b61cc19c 736 /* Set up row info for transformations */
9c0d9ce3
DS
737 row_info.color_type = png_ptr->color_type;
738 row_info.width = png_ptr->usr_width;
739 row_info.channels = png_ptr->usr_channels;
740 row_info.bit_depth = png_ptr->usr_bit_depth;
741 row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels);
742 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
743
744 png_debug1(3, "row_info->color_type = %d", row_info.color_type);
745 png_debug1(3, "row_info->width = %u", row_info.width);
746 png_debug1(3, "row_info->channels = %d", row_info.channels);
747 png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth);
748 png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth);
749 png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes);
0272a10d
VZ
750
751 /* Copy user's row into buffer, leaving room for filter byte. */
9c0d9ce3 752 png_memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes);
0272a10d 753
b61cc19c
PC
754#ifdef PNG_WRITE_INTERLACING_SUPPORTED
755 /* Handle interlacing */
0272a10d 756 if (png_ptr->interlaced && png_ptr->pass < 6 &&
9c0d9ce3 757 (png_ptr->transformations & PNG_INTERLACE))
0272a10d 758 {
9c0d9ce3 759 png_do_write_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass);
b61cc19c 760 /* This should always get caught above, but still ... */
9c0d9ce3 761 if (!(row_info.width))
0272a10d
VZ
762 {
763 png_write_finish_row(png_ptr);
764 return;
765 }
766 }
767#endif
768
9c0d9ce3 769#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
b61cc19c 770 /* Handle other transformations */
0272a10d 771 if (png_ptr->transformations)
9c0d9ce3
DS
772 png_do_write_transformations(png_ptr, &row_info);
773#endif
774
775 /* At this point the row_info pixel depth must match the 'transformed' depth,
776 * which is also the output depth.
777 */
778 if (row_info.pixel_depth != png_ptr->pixel_depth ||
779 row_info.pixel_depth != png_ptr->transformed_pixel_depth)
780 png_error(png_ptr, "internal write transform logic error");
0272a10d 781
b61cc19c 782#ifdef PNG_MNG_FEATURES_SUPPORTED
0272a10d
VZ
783 /* Write filter_method 64 (intrapixel differencing) only if
784 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
785 * 2. Libpng did not write a PNG signature (this filter_method is only
786 * used in PNG datastreams that are embedded in MNG datastreams) and
787 * 3. The application called png_permit_mng_features with a mask that
788 * included PNG_FLAG_MNG_FILTER_64 and
789 * 4. The filter_method is 64 and
790 * 5. The color_type is RGB or RGBA
791 */
970f6abe 792 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
9c0d9ce3 793 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
0272a10d
VZ
794 {
795 /* Intrapixel differencing */
9c0d9ce3 796 png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1);
0272a10d
VZ
797 }
798#endif
799
800 /* Find a filter if necessary, filter the row and write it out. */
9c0d9ce3 801 png_write_find_filter(png_ptr, &row_info);
0272a10d
VZ
802
803 if (png_ptr->write_row_fn != NULL)
804 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
805}
806
b61cc19c 807#ifdef PNG_WRITE_FLUSH_SUPPORTED
0272a10d
VZ
808/* Set the automatic flush interval or 0 to turn flushing off */
809void PNGAPI
810png_set_flush(png_structp png_ptr, int nrows)
811{
970f6abe 812 png_debug(1, "in png_set_flush");
b61cc19c 813
0272a10d
VZ
814 if (png_ptr == NULL)
815 return;
9c0d9ce3 816
0272a10d
VZ
817 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
818}
819
b61cc19c 820/* Flush the current output buffers now */
0272a10d
VZ
821void PNGAPI
822png_write_flush(png_structp png_ptr)
823{
824 int wrote_IDAT;
825
970f6abe 826 png_debug(1, "in png_write_flush");
b61cc19c 827
0272a10d
VZ
828 if (png_ptr == NULL)
829 return;
9c0d9ce3 830
0272a10d
VZ
831 /* We have already written out all of the data */
832 if (png_ptr->row_number >= png_ptr->num_rows)
b61cc19c 833 return;
0272a10d
VZ
834
835 do
836 {
837 int ret;
838
b61cc19c 839 /* Compress the data */
0272a10d
VZ
840 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
841 wrote_IDAT = 0;
842
b61cc19c 843 /* Check for compression errors */
0272a10d
VZ
844 if (ret != Z_OK)
845 {
846 if (png_ptr->zstream.msg != NULL)
847 png_error(png_ptr, png_ptr->zstream.msg);
9c0d9ce3 848
0272a10d
VZ
849 else
850 png_error(png_ptr, "zlib error");
851 }
852
853 if (!(png_ptr->zstream.avail_out))
854 {
b61cc19c 855 /* Write the IDAT and reset the zlib output buffer */
9c0d9ce3 856 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
0272a10d
VZ
857 wrote_IDAT = 1;
858 }
9c0d9ce3 859 } while (wrote_IDAT == 1);
0272a10d
VZ
860
861 /* If there is any data left to be output, write it into a new IDAT */
862 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
863 {
b61cc19c 864 /* Write the IDAT and reset the zlib output buffer */
0272a10d 865 png_write_IDAT(png_ptr, png_ptr->zbuf,
9c0d9ce3 866 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
0272a10d
VZ
867 }
868 png_ptr->flush_rows = 0;
869 png_flush(png_ptr);
870}
871#endif /* PNG_WRITE_FLUSH_SUPPORTED */
872
b61cc19c 873/* Free all memory used by the write */
0272a10d
VZ
874void PNGAPI
875png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
876{
877 png_structp png_ptr = NULL;
878 png_infop info_ptr = NULL;
879#ifdef PNG_USER_MEM_SUPPORTED
880 png_free_ptr free_fn = NULL;
881 png_voidp mem_ptr = NULL;
882#endif
883
970f6abe 884 png_debug(1, "in png_destroy_write_struct");
b61cc19c 885
0272a10d 886 if (png_ptr_ptr != NULL)
0272a10d 887 png_ptr = *png_ptr_ptr;
0272a10d 888
970f6abe
VZ
889#ifdef PNG_USER_MEM_SUPPORTED
890 if (png_ptr != NULL)
891 {
892 free_fn = png_ptr->free_fn;
893 mem_ptr = png_ptr->mem_ptr;
894 }
895#endif
896
0272a10d
VZ
897 if (info_ptr_ptr != NULL)
898 info_ptr = *info_ptr_ptr;
899
900 if (info_ptr != NULL)
901 {
970f6abe
VZ
902 if (png_ptr != NULL)
903 {
9c0d9ce3 904 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
0272a10d 905
b61cc19c 906#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
9c0d9ce3
DS
907 if (png_ptr->num_chunk_list)
908 {
909 png_free(png_ptr, png_ptr->chunk_list);
910 png_ptr->num_chunk_list = 0;
911 }
0272a10d 912#endif
970f6abe 913 }
0272a10d
VZ
914
915#ifdef PNG_USER_MEM_SUPPORTED
916 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
9c0d9ce3 917 (png_voidp)mem_ptr);
0272a10d
VZ
918#else
919 png_destroy_struct((png_voidp)info_ptr);
920#endif
921 *info_ptr_ptr = NULL;
922 }
923
924 if (png_ptr != NULL)
925 {
926 png_write_destroy(png_ptr);
927#ifdef PNG_USER_MEM_SUPPORTED
928 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
9c0d9ce3 929 (png_voidp)mem_ptr);
0272a10d
VZ
930#else
931 png_destroy_struct((png_voidp)png_ptr);
932#endif
933 *png_ptr_ptr = NULL;
934 }
935}
936
937
938/* Free any memory used in png_ptr struct (old method) */
939void /* PRIVATE */
940png_write_destroy(png_structp png_ptr)
941{
942#ifdef PNG_SETJMP_SUPPORTED
b61cc19c 943 jmp_buf tmp_jmp; /* Save jump buffer */
0272a10d
VZ
944#endif
945 png_error_ptr error_fn;
9c0d9ce3 946#ifdef PNG_WARNINGS_SUPPORTED
0272a10d 947 png_error_ptr warning_fn;
9c0d9ce3 948#endif
0272a10d
VZ
949 png_voidp error_ptr;
950#ifdef PNG_USER_MEM_SUPPORTED
951 png_free_ptr free_fn;
952#endif
953
970f6abe 954 png_debug(1, "in png_write_destroy");
b61cc19c
PC
955
956 /* Free any memory zlib uses */
9c0d9ce3
DS
957 if (png_ptr->zlib_state != PNG_ZLIB_UNINITIALIZED)
958 deflateEnd(&png_ptr->zstream);
0272a10d 959
b61cc19c 960 /* Free our memory. png_free checks NULL for us. */
0272a10d
VZ
961 png_free(png_ptr, png_ptr->zbuf);
962 png_free(png_ptr, png_ptr->row_buf);
b61cc19c 963#ifdef PNG_WRITE_FILTER_SUPPORTED
0272a10d
VZ
964 png_free(png_ptr, png_ptr->prev_row);
965 png_free(png_ptr, png_ptr->sub_row);
966 png_free(png_ptr, png_ptr->up_row);
967 png_free(png_ptr, png_ptr->avg_row);
968 png_free(png_ptr, png_ptr->paeth_row);
970f6abe 969#endif
0272a10d 970
b61cc19c 971#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
9c0d9ce3
DS
972 /* Use this to save a little code space, it doesn't free the filter_costs */
973 png_reset_filter_heuristics(png_ptr);
0272a10d
VZ
974 png_free(png_ptr, png_ptr->filter_costs);
975 png_free(png_ptr, png_ptr->inv_filter_costs);
976#endif
977
978#ifdef PNG_SETJMP_SUPPORTED
b61cc19c 979 /* Reset structure */
9c0d9ce3 980 png_memcpy(tmp_jmp, png_ptr->longjmp_buffer, png_sizeof(jmp_buf));
0272a10d
VZ
981#endif
982
983 error_fn = png_ptr->error_fn;
9c0d9ce3 984#ifdef PNG_WARNINGS_SUPPORTED
0272a10d 985 warning_fn = png_ptr->warning_fn;
9c0d9ce3 986#endif
0272a10d
VZ
987 error_ptr = png_ptr->error_ptr;
988#ifdef PNG_USER_MEM_SUPPORTED
989 free_fn = png_ptr->free_fn;
990#endif
991
970f6abe 992 png_memset(png_ptr, 0, png_sizeof(png_struct));
0272a10d
VZ
993
994 png_ptr->error_fn = error_fn;
9c0d9ce3 995#ifdef PNG_WARNINGS_SUPPORTED
0272a10d 996 png_ptr->warning_fn = warning_fn;
9c0d9ce3 997#endif
0272a10d
VZ
998 png_ptr->error_ptr = error_ptr;
999#ifdef PNG_USER_MEM_SUPPORTED
1000 png_ptr->free_fn = free_fn;
1001#endif
1002
1003#ifdef PNG_SETJMP_SUPPORTED
9c0d9ce3 1004 png_memcpy(png_ptr->longjmp_buffer, tmp_jmp, png_sizeof(jmp_buf));
0272a10d
VZ
1005#endif
1006}
1007
1008/* Allow the application to select one or more row filters to use. */
1009void PNGAPI
1010png_set_filter(png_structp png_ptr, int method, int filters)
1011{
970f6abe 1012 png_debug(1, "in png_set_filter");
b61cc19c 1013
0272a10d
VZ
1014 if (png_ptr == NULL)
1015 return;
9c0d9ce3 1016
b61cc19c 1017#ifdef PNG_MNG_FEATURES_SUPPORTED
970f6abe 1018 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
9c0d9ce3
DS
1019 (method == PNG_INTRAPIXEL_DIFFERENCING))
1020 method = PNG_FILTER_TYPE_BASE;
1021
0272a10d
VZ
1022#endif
1023 if (method == PNG_FILTER_TYPE_BASE)
1024 {
1025 switch (filters & (PNG_ALL_FILTERS | 0x07))
1026 {
b61cc19c 1027#ifdef PNG_WRITE_FILTER_SUPPORTED
0272a10d
VZ
1028 case 5:
1029 case 6:
1030 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
b61cc19c 1031#endif /* PNG_WRITE_FILTER_SUPPORTED */
0272a10d 1032 case PNG_FILTER_VALUE_NONE:
9c0d9ce3
DS
1033 png_ptr->do_filter = PNG_FILTER_NONE; break;
1034
b61cc19c 1035#ifdef PNG_WRITE_FILTER_SUPPORTED
0272a10d 1036 case PNG_FILTER_VALUE_SUB:
9c0d9ce3
DS
1037 png_ptr->do_filter = PNG_FILTER_SUB; break;
1038
0272a10d 1039 case PNG_FILTER_VALUE_UP:
9c0d9ce3
DS
1040 png_ptr->do_filter = PNG_FILTER_UP; break;
1041
0272a10d 1042 case PNG_FILTER_VALUE_AVG:
9c0d9ce3
DS
1043 png_ptr->do_filter = PNG_FILTER_AVG; break;
1044
0272a10d 1045 case PNG_FILTER_VALUE_PAETH:
9c0d9ce3
DS
1046 png_ptr->do_filter = PNG_FILTER_PAETH; break;
1047
1048 default:
1049 png_ptr->do_filter = (png_byte)filters; break;
0272a10d 1050#else
9c0d9ce3
DS
1051 default:
1052 png_warning(png_ptr, "Unknown row filter for method 0");
b61cc19c 1053#endif /* PNG_WRITE_FILTER_SUPPORTED */
0272a10d
VZ
1054 }
1055
1056 /* If we have allocated the row_buf, this means we have already started
1057 * with the image and we should have allocated all of the filter buffers
1058 * that have been selected. If prev_row isn't already allocated, then
1059 * it is too late to start using the filters that need it, since we
1060 * will be missing the data in the previous row. If an application
1061 * wants to start and stop using particular filters during compression,
1062 * it should start out with all of the filters, and then add and
1063 * remove them after the start of compression.
1064 */
1065 if (png_ptr->row_buf != NULL)
1066 {
b61cc19c 1067#ifdef PNG_WRITE_FILTER_SUPPORTED
0272a10d
VZ
1068 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
1069 {
1070 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
9c0d9ce3 1071 (png_ptr->rowbytes + 1));
0272a10d
VZ
1072 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
1073 }
1074
1075 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
1076 {
1077 if (png_ptr->prev_row == NULL)
1078 {
1079 png_warning(png_ptr, "Can't add Up filter after starting");
9c0d9ce3
DS
1080 png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
1081 ~PNG_FILTER_UP);
0272a10d 1082 }
9c0d9ce3 1083
0272a10d
VZ
1084 else
1085 {
1086 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
9c0d9ce3 1087 (png_ptr->rowbytes + 1));
0272a10d
VZ
1088 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
1089 }
1090 }
1091
1092 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
1093 {
1094 if (png_ptr->prev_row == NULL)
1095 {
1096 png_warning(png_ptr, "Can't add Average filter after starting");
9c0d9ce3
DS
1097 png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
1098 ~PNG_FILTER_AVG);
0272a10d 1099 }
9c0d9ce3 1100
0272a10d
VZ
1101 else
1102 {
1103 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
9c0d9ce3 1104 (png_ptr->rowbytes + 1));
0272a10d
VZ
1105 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
1106 }
1107 }
1108
1109 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
1110 png_ptr->paeth_row == NULL)
1111 {
1112 if (png_ptr->prev_row == NULL)
1113 {
1114 png_warning(png_ptr, "Can't add Paeth filter after starting");
1115 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
1116 }
9c0d9ce3 1117
0272a10d
VZ
1118 else
1119 {
1120 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
9c0d9ce3 1121 (png_ptr->rowbytes + 1));
0272a10d
VZ
1122 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
1123 }
1124 }
1125
1126 if (png_ptr->do_filter == PNG_NO_FILTERS)
b61cc19c 1127#endif /* PNG_WRITE_FILTER_SUPPORTED */
0272a10d
VZ
1128 png_ptr->do_filter = PNG_FILTER_NONE;
1129 }
1130 }
1131 else
1132 png_error(png_ptr, "Unknown custom filter method");
1133}
1134
1135/* This allows us to influence the way in which libpng chooses the "best"
1136 * filter for the current scanline. While the "minimum-sum-of-absolute-
1137 * differences metric is relatively fast and effective, there is some
1138 * question as to whether it can be improved upon by trying to keep the
1139 * filtered data going to zlib more consistent, hopefully resulting in
1140 * better compression.
1141 */
b61cc19c 1142#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */
9c0d9ce3
DS
1143/* Convenience reset API. */
1144static void
1145png_reset_filter_heuristics(png_structp png_ptr)
0272a10d 1146{
9c0d9ce3
DS
1147 /* Clear out any old values in the 'weights' - this must be done because if
1148 * the app calls set_filter_heuristics multiple times with different
1149 * 'num_weights' values we would otherwise potentially have wrong sized
1150 * arrays.
1151 */
1152 png_ptr->num_prev_filters = 0;
1153 png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1154 if (png_ptr->prev_filters != NULL)
0272a10d 1155 {
9c0d9ce3
DS
1156 png_bytep old = png_ptr->prev_filters;
1157 png_ptr->prev_filters = NULL;
1158 png_free(png_ptr, old);
0272a10d 1159 }
9c0d9ce3 1160 if (png_ptr->filter_weights != NULL)
0272a10d 1161 {
9c0d9ce3
DS
1162 png_uint_16p old = png_ptr->filter_weights;
1163 png_ptr->filter_weights = NULL;
1164 png_free(png_ptr, old);
0272a10d
VZ
1165 }
1166
9c0d9ce3 1167 if (png_ptr->inv_filter_weights != NULL)
0272a10d 1168 {
9c0d9ce3
DS
1169 png_uint_16p old = png_ptr->inv_filter_weights;
1170 png_ptr->inv_filter_weights = NULL;
1171 png_free(png_ptr, old);
0272a10d
VZ
1172 }
1173
9c0d9ce3
DS
1174 /* Leave the filter_costs - this array is fixed size. */
1175}
1176
1177static int
1178png_init_filter_heuristics(png_structp png_ptr, int heuristic_method,
1179 int num_weights)
1180{
1181 if (png_ptr == NULL)
1182 return 0;
0272a10d 1183
9c0d9ce3
DS
1184 /* Clear out the arrays */
1185 png_reset_filter_heuristics(png_ptr);
1186
1187 /* Check arguments; the 'reset' function makes the correct settings for the
1188 * unweighted case, but we must handle the weight case by initializing the
1189 * arrays for the caller.
1190 */
1191 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
0272a10d 1192 {
9c0d9ce3
DS
1193 int i;
1194
1195 if (num_weights > 0)
0272a10d
VZ
1196 {
1197 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
9c0d9ce3 1198 (png_uint_32)(png_sizeof(png_byte) * num_weights));
0272a10d
VZ
1199
1200 /* To make sure that the weighting starts out fairly */
1201 for (i = 0; i < num_weights; i++)
1202 {
1203 png_ptr->prev_filters[i] = 255;
1204 }
0272a10d 1205
0272a10d 1206 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
9c0d9ce3 1207 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
0272a10d
VZ
1208
1209 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
9c0d9ce3
DS
1210 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
1211
0272a10d
VZ
1212 for (i = 0; i < num_weights; i++)
1213 {
1214 png_ptr->inv_filter_weights[i] =
1215 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1216 }
9c0d9ce3
DS
1217
1218 /* Safe to set this now */
1219 png_ptr->num_prev_filters = (png_byte)num_weights;
1220 }
1221
1222 /* If, in the future, there are other filter methods, this would
1223 * need to be based on png_ptr->filter.
1224 */
1225 if (png_ptr->filter_costs == NULL)
1226 {
1227 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
1228 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
1229
1230 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
1231 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
1232 }
1233
1234 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1235 {
1236 png_ptr->inv_filter_costs[i] =
1237 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
0272a10d
VZ
1238 }
1239
9c0d9ce3
DS
1240 /* All the arrays are inited, safe to set this: */
1241 png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_WEIGHTED;
1242
1243 /* Return the 'ok' code. */
1244 return 1;
1245 }
1246 else if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT ||
1247 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
1248 {
1249 return 1;
1250 }
1251 else
1252 {
1253 png_warning(png_ptr, "Unknown filter heuristic method");
1254 return 0;
1255 }
1256}
1257
1258/* Provide floating and fixed point APIs */
1259#ifdef PNG_FLOATING_POINT_SUPPORTED
1260void PNGAPI
1261png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
1262 int num_weights, png_const_doublep filter_weights,
1263 png_const_doublep filter_costs)
1264{
1265 png_debug(1, "in png_set_filter_heuristics");
1266
1267 /* The internal API allocates all the arrays and ensures that the elements of
1268 * those arrays are set to the default value.
1269 */
1270 if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
1271 return;
1272
1273 /* If using the weighted method copy in the weights. */
1274 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1275 {
1276 int i;
0272a10d
VZ
1277 for (i = 0; i < num_weights; i++)
1278 {
9c0d9ce3 1279 if (filter_weights[i] <= 0.0)
0272a10d
VZ
1280 {
1281 png_ptr->inv_filter_weights[i] =
1282 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1283 }
9c0d9ce3 1284
0272a10d
VZ
1285 else
1286 {
1287 png_ptr->inv_filter_weights[i] =
9c0d9ce3
DS
1288 (png_uint_16)(PNG_WEIGHT_FACTOR*filter_weights[i]+.5);
1289
0272a10d 1290 png_ptr->filter_weights[i] =
9c0d9ce3 1291 (png_uint_16)(PNG_WEIGHT_FACTOR/filter_weights[i]+.5);
0272a10d
VZ
1292 }
1293 }
0272a10d 1294
9c0d9ce3
DS
1295 /* Here is where we set the relative costs of the different filters. We
1296 * should take the desired compression level into account when setting
1297 * the costs, so that Paeth, for instance, has a high relative cost at low
1298 * compression levels, while it has a lower relative cost at higher
1299 * compression settings. The filter types are in order of increasing
1300 * relative cost, so it would be possible to do this with an algorithm.
1301 */
1302 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) if (filter_costs[i] >= 1.0)
0272a10d
VZ
1303 {
1304 png_ptr->inv_filter_costs[i] =
9c0d9ce3
DS
1305 (png_uint_16)(PNG_COST_FACTOR / filter_costs[i] + .5);
1306
1307 png_ptr->filter_costs[i] =
1308 (png_uint_16)(PNG_COST_FACTOR * filter_costs[i] + .5);
0272a10d
VZ
1309 }
1310 }
9c0d9ce3
DS
1311}
1312#endif /* FLOATING_POINT */
0272a10d 1313
9c0d9ce3
DS
1314#ifdef PNG_FIXED_POINT_SUPPORTED
1315void PNGAPI
1316png_set_filter_heuristics_fixed(png_structp png_ptr, int heuristic_method,
1317 int num_weights, png_const_fixed_point_p filter_weights,
1318 png_const_fixed_point_p filter_costs)
1319{
1320 png_debug(1, "in png_set_filter_heuristics_fixed");
1321
1322 /* The internal API allocates all the arrays and ensures that the elements of
1323 * those arrays are set to the default value.
0272a10d 1324 */
9c0d9ce3
DS
1325 if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
1326 return;
1327
1328 /* If using the weighted method copy in the weights. */
1329 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
0272a10d 1330 {
9c0d9ce3
DS
1331 int i;
1332 for (i = 0; i < num_weights; i++)
0272a10d 1333 {
9c0d9ce3
DS
1334 if (filter_weights[i] <= 0)
1335 {
1336 png_ptr->inv_filter_weights[i] =
1337 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1338 }
1339
1340 else
1341 {
1342 png_ptr->inv_filter_weights[i] = (png_uint_16)
1343 ((PNG_WEIGHT_FACTOR*filter_weights[i]+PNG_FP_HALF)/PNG_FP_1);
1344
1345 png_ptr->filter_weights[i] = (png_uint_16)((PNG_WEIGHT_FACTOR*
1346 PNG_FP_1+(filter_weights[i]/2))/filter_weights[i]);
1347 }
0272a10d 1348 }
9c0d9ce3
DS
1349
1350 /* Here is where we set the relative costs of the different filters. We
1351 * should take the desired compression level into account when setting
1352 * the costs, so that Paeth, for instance, has a high relative cost at low
1353 * compression levels, while it has a lower relative cost at higher
1354 * compression settings. The filter types are in order of increasing
1355 * relative cost, so it would be possible to do this with an algorithm.
1356 */
1357 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1358 if (filter_costs[i] >= PNG_FP_1)
0272a10d 1359 {
9c0d9ce3
DS
1360 png_uint_32 tmp;
1361
1362 /* Use a 32 bit unsigned temporary here because otherwise the
1363 * intermediate value will be a 32 bit *signed* integer (ANSI rules)
1364 * and this will get the wrong answer on division.
1365 */
1366 tmp = PNG_COST_FACTOR*PNG_FP_1 + (filter_costs[i]/2);
1367 tmp /= filter_costs[i];
1368
1369 png_ptr->inv_filter_costs[i] = (png_uint_16)tmp;
1370
1371 tmp = PNG_COST_FACTOR * filter_costs[i] + PNG_FP_HALF;
1372 tmp /= PNG_FP_1;
1373
1374 png_ptr->filter_costs[i] = (png_uint_16)tmp;
0272a10d
VZ
1375 }
1376 }
1377}
9c0d9ce3 1378#endif /* FIXED_POINT */
0272a10d
VZ
1379#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1380
1381void PNGAPI
1382png_set_compression_level(png_structp png_ptr, int level)
1383{
970f6abe 1384 png_debug(1, "in png_set_compression_level");
b61cc19c 1385
0272a10d
VZ
1386 if (png_ptr == NULL)
1387 return;
9c0d9ce3 1388
0272a10d
VZ
1389 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
1390 png_ptr->zlib_level = level;
1391}
1392
1393void PNGAPI
1394png_set_compression_mem_level(png_structp png_ptr, int mem_level)
1395{
970f6abe 1396 png_debug(1, "in png_set_compression_mem_level");
b61cc19c 1397
0272a10d
VZ
1398 if (png_ptr == NULL)
1399 return;
9c0d9ce3 1400
0272a10d
VZ
1401 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
1402 png_ptr->zlib_mem_level = mem_level;
1403}
1404
1405void PNGAPI
1406png_set_compression_strategy(png_structp png_ptr, int strategy)
1407{
970f6abe 1408 png_debug(1, "in png_set_compression_strategy");
b61cc19c 1409
0272a10d
VZ
1410 if (png_ptr == NULL)
1411 return;
9c0d9ce3 1412
0272a10d
VZ
1413 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
1414 png_ptr->zlib_strategy = strategy;
1415}
1416
9c0d9ce3
DS
1417/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
1418 * smaller value of window_bits if it can do so safely.
1419 */
0272a10d
VZ
1420void PNGAPI
1421png_set_compression_window_bits(png_structp png_ptr, int window_bits)
1422{
1423 if (png_ptr == NULL)
1424 return;
9c0d9ce3 1425
0272a10d
VZ
1426 if (window_bits > 15)
1427 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
9c0d9ce3 1428
0272a10d
VZ
1429 else if (window_bits < 8)
1430 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
9c0d9ce3 1431
0272a10d 1432#ifndef WBITS_8_OK
b61cc19c 1433 /* Avoid libpng bug with 256-byte windows */
0272a10d 1434 if (window_bits == 8)
9c0d9ce3
DS
1435 {
1436 png_warning(png_ptr, "Compression window is being reset to 512");
1437 window_bits = 9;
1438 }
1439
0272a10d
VZ
1440#endif
1441 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
1442 png_ptr->zlib_window_bits = window_bits;
1443}
1444
1445void PNGAPI
1446png_set_compression_method(png_structp png_ptr, int method)
1447{
970f6abe 1448 png_debug(1, "in png_set_compression_method");
b61cc19c 1449
0272a10d
VZ
1450 if (png_ptr == NULL)
1451 return;
9c0d9ce3 1452
0272a10d
VZ
1453 if (method != 8)
1454 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
9c0d9ce3 1455
0272a10d
VZ
1456 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
1457 png_ptr->zlib_method = method;
1458}
1459
9c0d9ce3
DS
1460/* The following were added to libpng-1.5.4 */
1461#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
1462void PNGAPI
1463png_set_text_compression_level(png_structp png_ptr, int level)
1464{
1465 png_debug(1, "in png_set_text_compression_level");
1466
1467 if (png_ptr == NULL)
1468 return;
1469
1470 png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_LEVEL;
1471 png_ptr->zlib_text_level = level;
1472}
1473
1474void PNGAPI
1475png_set_text_compression_mem_level(png_structp png_ptr, int mem_level)
1476{
1477 png_debug(1, "in png_set_text_compression_mem_level");
1478
1479 if (png_ptr == NULL)
1480 return;
1481
1482 png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_MEM_LEVEL;
1483 png_ptr->zlib_text_mem_level = mem_level;
1484}
1485
1486void PNGAPI
1487png_set_text_compression_strategy(png_structp png_ptr, int strategy)
1488{
1489 png_debug(1, "in png_set_text_compression_strategy");
1490
1491 if (png_ptr == NULL)
1492 return;
1493
1494 png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_STRATEGY;
1495 png_ptr->zlib_text_strategy = strategy;
1496}
1497
1498/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
1499 * smaller value of window_bits if it can do so safely.
1500 */
1501void PNGAPI
1502png_set_text_compression_window_bits(png_structp png_ptr, int window_bits)
1503{
1504 if (png_ptr == NULL)
1505 return;
1506
1507 if (window_bits > 15)
1508 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
1509
1510 else if (window_bits < 8)
1511 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
1512
1513#ifndef WBITS_8_OK
1514 /* Avoid libpng bug with 256-byte windows */
1515 if (window_bits == 8)
1516 {
1517 png_warning(png_ptr, "Text compression window is being reset to 512");
1518 window_bits = 9;
1519 }
1520
1521#endif
1522 png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_WINDOW_BITS;
1523 png_ptr->zlib_text_window_bits = window_bits;
1524}
1525
1526void PNGAPI
1527png_set_text_compression_method(png_structp png_ptr, int method)
1528{
1529 png_debug(1, "in png_set_text_compression_method");
1530
1531 if (png_ptr == NULL)
1532 return;
1533
1534 if (method != 8)
1535 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1536
1537 png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_METHOD;
1538 png_ptr->zlib_text_method = method;
1539}
1540#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */
1541/* end of API added to libpng-1.5.4 */
1542
0272a10d
VZ
1543void PNGAPI
1544png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1545{
1546 if (png_ptr == NULL)
1547 return;
9c0d9ce3 1548
0272a10d
VZ
1549 png_ptr->write_row_fn = write_row_fn;
1550}
1551
b61cc19c 1552#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
0272a10d
VZ
1553void PNGAPI
1554png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
9c0d9ce3 1555 write_user_transform_fn)
0272a10d 1556{
970f6abe 1557 png_debug(1, "in png_set_write_user_transform_fn");
b61cc19c 1558
0272a10d
VZ
1559 if (png_ptr == NULL)
1560 return;
9c0d9ce3 1561
0272a10d
VZ
1562 png_ptr->transformations |= PNG_USER_TRANSFORM;
1563 png_ptr->write_user_transform_fn = write_user_transform_fn;
1564}
1565#endif
1566
1567
b61cc19c 1568#ifdef PNG_INFO_IMAGE_SUPPORTED
0272a10d
VZ
1569void PNGAPI
1570png_write_png(png_structp png_ptr, png_infop info_ptr,
9c0d9ce3 1571 int transforms, voidp params)
0272a10d
VZ
1572{
1573 if (png_ptr == NULL || info_ptr == NULL)
1574 return;
0272a10d
VZ
1575
1576 /* Write the file header information. */
1577 png_write_info(png_ptr, info_ptr);
1578
1579 /* ------ these transformations don't touch the info structure ------- */
1580
b61cc19c
PC
1581#ifdef PNG_WRITE_INVERT_SUPPORTED
1582 /* Invert monochrome pixels */
0272a10d 1583 if (transforms & PNG_TRANSFORM_INVERT_MONO)
b61cc19c 1584 png_set_invert_mono(png_ptr);
0272a10d
VZ
1585#endif
1586
b61cc19c 1587#ifdef PNG_WRITE_SHIFT_SUPPORTED
0272a10d
VZ
1588 /* Shift the pixels up to a legal bit depth and fill in
1589 * as appropriate to correctly scale the image.
1590 */
1591 if ((transforms & PNG_TRANSFORM_SHIFT)
9c0d9ce3 1592 && (info_ptr->valid & PNG_INFO_sBIT))
b61cc19c 1593 png_set_shift(png_ptr, &info_ptr->sig_bit);
0272a10d
VZ
1594#endif
1595
b61cc19c
PC
1596#ifdef PNG_WRITE_PACK_SUPPORTED
1597 /* Pack pixels into bytes */
0272a10d
VZ
1598 if (transforms & PNG_TRANSFORM_PACKING)
1599 png_set_packing(png_ptr);
1600#endif
1601
b61cc19c
PC
1602#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
1603 /* Swap location of alpha bytes from ARGB to RGBA */
0272a10d 1604 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
b61cc19c 1605 png_set_swap_alpha(png_ptr);
0272a10d
VZ
1606#endif
1607
b61cc19c 1608#ifdef PNG_WRITE_FILLER_SUPPORTED
9c0d9ce3 1609 /* Pack XRGB/RGBX/ARGB/RGBA into RGB (4 channels -> 3 channels) */
b61cc19c 1610 if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)
970f6abe 1611 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
9c0d9ce3 1612
b61cc19c 1613 else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
970f6abe 1614 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
0272a10d
VZ
1615#endif
1616
b61cc19c
PC
1617#ifdef PNG_WRITE_BGR_SUPPORTED
1618 /* Flip BGR pixels to RGB */
0272a10d 1619 if (transforms & PNG_TRANSFORM_BGR)
b61cc19c 1620 png_set_bgr(png_ptr);
0272a10d
VZ
1621#endif
1622
b61cc19c
PC
1623#ifdef PNG_WRITE_SWAP_SUPPORTED
1624 /* Swap bytes of 16-bit files to most significant byte first */
0272a10d 1625 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
b61cc19c 1626 png_set_swap(png_ptr);
0272a10d
VZ
1627#endif
1628
b61cc19c
PC
1629#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
1630 /* Swap bits of 1, 2, 4 bit packed pixel formats */
0272a10d 1631 if (transforms & PNG_TRANSFORM_PACKSWAP)
b61cc19c
PC
1632 png_set_packswap(png_ptr);
1633#endif
1634
1635#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
1636 /* Invert the alpha channel from opacity to transparency */
1637 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1638 png_set_invert_alpha(png_ptr);
0272a10d
VZ
1639#endif
1640
1641 /* ----------------------- end of transformations ------------------- */
1642
b61cc19c 1643 /* Write the bits */
0272a10d
VZ
1644 if (info_ptr->valid & PNG_INFO_IDAT)
1645 png_write_image(png_ptr, info_ptr->row_pointers);
1646
1647 /* It is REQUIRED to call this to finish writing the rest of the file */
1648 png_write_end(png_ptr, info_ptr);
1649
9c0d9ce3
DS
1650 PNG_UNUSED(transforms) /* Quiet compiler warnings */
1651 PNG_UNUSED(params)
0272a10d
VZ
1652}
1653#endif
1654#endif /* PNG_WRITE_SUPPORTED */