blob: b120df9838c136f03e4d0e3fb7c20d37754db1e7 [file] [log] [blame]
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001
Andreas Dilger47a0c421997-05-16 02:46:07 -05002/* pngwrite.c - general routines to write a PNG file
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004 * Last changed in libpng 1.5.6 [(PENDING RELEASE)]
Glenn Randers-Pehrson64b863c2011-01-04 09:57:06 -06005 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -05008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060012 */
Guy Schalnat0d580581995-07-20 02:43:20 -050013
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050014#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050015
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060016#ifdef PNG_WRITE_SUPPORTED
17
Andreas Dilger47a0c421997-05-16 02:46:07 -050018/* 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
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050021 * after the image data, put it in png_write_end(). I strongly encourage
Andreas Dilger47a0c421997-05-16 02:46:07 -050022 * 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 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050027void PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060028png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050029{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050030 png_debug(1, "in png_write_info_before_PLTE");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050031
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -060032 if (png_ptr == NULL || info_ptr == NULL)
33 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050034
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060035 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
36 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -050037 /* Write PNG signature */
38 png_write_sig(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050039
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050040#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060041 if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) && \
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060042 (png_ptr->mng_features_permitted))
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060043 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050044 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -050045 png_ptr->mng_features_permitted = 0;
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060046 }
47#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050048
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -050049 /* Write IHDR information. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060050 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060051 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
52 info_ptr->filter_type,
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050053#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060054 info_ptr->interlace_type);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060055#else
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060056 0);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060057#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050058 /* 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 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050061#ifdef PNG_WRITE_gAMA_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060062 if (info_ptr->valid & PNG_INFO_gAMA)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050063 png_write_gAMA_fixed(png_ptr, info_ptr->gamma);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050064#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050065#ifdef PNG_WRITE_sRGB_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060066 if (info_ptr->valid & PNG_INFO_sRGB)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060067 png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060068#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050069
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050070#ifdef PNG_WRITE_iCCP_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060071 if (info_ptr->valid & PNG_INFO_iCCP)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -060072 png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050073 (png_charp)info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060074#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050075#ifdef PNG_WRITE_sBIT_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060076 if (info_ptr->valid & PNG_INFO_sBIT)
77 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050078#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050079#ifdef PNG_WRITE_cHRM_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060080 if (info_ptr->valid & PNG_INFO_cHRM)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050081 png_write_cHRM_fixed(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060082 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);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060086#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050087
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050088#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060089 if (info_ptr->unknown_chunks_num)
90 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050091 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060092
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050093 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060094
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050095 for (up = info_ptr->unknown_chunks;
96 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
97 up++)
98 {
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -050099 int keep = png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500100
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500101 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson36fa2a02011-05-11 06:52:37 -0500102 up->location &&
103 !(up->location & PNG_HAVE_PLTE) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600104 !(up->location & PNG_HAVE_IDAT) &&
Glenn Randers-Pehrson36fa2a02011-05-11 06:52:37 -0500105 !(up->location & PNG_AFTER_IDAT) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600106 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
107 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600108 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500109 if (up->size == 0)
110 png_warning(png_ptr, "Writing zero-length unknown chunk");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500111
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600112 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600113 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500114 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600115 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500116#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600117 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
118 }
119}
120
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500121void PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600122png_write_info(png_structp png_ptr, png_infop info_ptr)
123{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600124#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600125 int i;
126#endif
127
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500128 png_debug(1, "in png_write_info");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600129
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600130 if (png_ptr == NULL || info_ptr == NULL)
131 return;
132
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600133 png_write_info_before_PLTE(png_ptr, info_ptr);
134
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600135 if (info_ptr->valid & PNG_INFO_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500136 png_write_PLTE(png_ptr, info_ptr->palette,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600137 (png_uint_32)info_ptr->num_palette);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500138
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600139 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600140 png_error(png_ptr, "Valid palette required for paletted images");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600141
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500142#ifdef PNG_WRITE_tRNS_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600143 if (info_ptr->valid & PNG_INFO_tRNS)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500144 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500145#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500146 /* Invert the alpha channel (in tRNS) */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500147 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600148 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500149 {
150 int j;
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500151 for (j = 0; j<(int)info_ptr->num_trans; j++)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500152 info_ptr->trans_alpha[j] =
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -0500153 (png_byte)(255 - info_ptr->trans_alpha[j]);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500154 }
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600155#endif
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500156 png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600157 info_ptr->num_trans, info_ptr->color_type);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500158 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500159#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500160#ifdef PNG_WRITE_bKGD_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600161 if (info_ptr->valid & PNG_INFO_bKGD)
162 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500163#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500164
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500165#ifdef PNG_WRITE_hIST_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600166 if (info_ptr->valid & PNG_INFO_hIST)
167 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500168#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500169
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500170#ifdef PNG_WRITE_oFFs_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600171 if (info_ptr->valid & PNG_INFO_oFFs)
172 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600173 info_ptr->offset_unit_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500174#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500175
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500176#ifdef PNG_WRITE_pCAL_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500177 if (info_ptr->valid & PNG_INFO_pCAL)
178 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600179 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
180 info_ptr->pcal_units, info_ptr->pcal_params);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500181#endif
Glenn Randers-Pehrson59020592009-05-18 10:52:12 -0500182
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500183#ifdef PNG_WRITE_sCAL_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500184 if (info_ptr->valid & PNG_INFO_sCAL)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600185 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600186 info_ptr->scal_s_width, info_ptr->scal_s_height);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500187#endif /* sCAL */
188
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500189#ifdef PNG_WRITE_pHYs_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500190 if (info_ptr->valid & PNG_INFO_pHYs)
191 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600192 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500193#endif /* pHYs */
194
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500195#ifdef PNG_WRITE_tIME_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600196 if (info_ptr->valid & PNG_INFO_tIME)
Guy Schalnate5a37791996-06-05 15:50:50 -0500197 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600198 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600199 png_ptr->mode |= PNG_WROTE_tIME;
Guy Schalnate5a37791996-06-05 15:50:50 -0500200 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500201#endif /* tIME */
202
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500203#ifdef PNG_WRITE_sPLT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600204 if (info_ptr->valid & PNG_INFO_sPLT)
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600205 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
206 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500207#endif /* sPLT */
208
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500209#ifdef PNG_WRITE_TEXT_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500210 /* Check to see if we need to write text chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500211 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500212 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500213 png_debug2(2, "Writing header text chunk %d, type %d", i,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600214 info_ptr->text[i].compression);
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500215 /* An internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600216 if (info_ptr->text[i].compression > 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600217 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500218#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600219 /* 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);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600226#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600227 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600228#endif
229 /* Mark this chunk as written */
230 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
231 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500232
Andreas Dilger47a0c421997-05-16 02:46:07 -0500233 /* If we want a compressed text chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600234 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500235 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500236#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500237 /* Write compressed chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500238 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600239 info_ptr->text[i].text, 0,
240 info_ptr->text[i].compression);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500241#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600242 png_warning(png_ptr, "Unable to write compressed text");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500243#endif
244 /* Mark this chunk as written */
245 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
246 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500247
Andreas Dilger47a0c421997-05-16 02:46:07 -0500248 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
249 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500250#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500251 /* Write uncompressed chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500252 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600253 info_ptr->text[i].text,
254 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500255 /* Mark this chunk as written */
256 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500257#else
258 /* Can't get here */
259 png_warning(png_ptr, "Unable to write uncompressed text");
260#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500261 }
262 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500263#endif /* tEXt */
264
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500265#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600266 if (info_ptr->unknown_chunks_num)
267 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500268 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600269
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500270 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600271
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500272 for (up = info_ptr->unknown_chunks;
273 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
274 up++)
275 {
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500276 int keep = png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500277 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson36fa2a02011-05-11 06:52:37 -0500278 up->location &&
279 (up->location & PNG_HAVE_PLTE) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600280 !(up->location & PNG_HAVE_IDAT) &&
Glenn Randers-Pehrson36fa2a02011-05-11 06:52:37 -0500281 !(up->location & PNG_AFTER_IDAT) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600282 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
283 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600284 {
285 png_write_chunk(png_ptr, up->name, up->data, up->size);
286 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500287 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600288 }
289#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500290}
Guy Schalnat0d580581995-07-20 02:43:20 -0500291
Andreas Dilger47a0c421997-05-16 02:46:07 -0500292/* Writes the end of the PNG file. If you don't want to write comments or
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600293 * 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 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500297void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500298png_write_end(png_structp png_ptr, png_infop info_ptr)
299{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500300 png_debug(1, "in png_write_end");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500301
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600302 if (png_ptr == NULL)
303 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500304
Andreas Dilger47a0c421997-05-16 02:46:07 -0500305 if (!(png_ptr->mode & PNG_HAVE_IDAT))
306 png_error(png_ptr, "No IDATs written into file");
307
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500308 /* See if user wants us to write information chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500309 if (info_ptr != NULL)
310 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500311#ifdef PNG_WRITE_TEXT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500312 int i; /* local index variable */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600313#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500314#ifdef PNG_WRITE_tIME_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500315 /* Check to see if user has supplied a time chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600316 if ((info_ptr->valid & PNG_INFO_tIME) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600317 !(png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500318 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500319
Andreas Dilger47a0c421997-05-16 02:46:07 -0500320#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500321#ifdef PNG_WRITE_TEXT_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500322 /* Loop through comment chunks */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600323 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500324 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500325 png_debug2(2, "Writing trailer text chunk %d, type %d", i,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500326 info_ptr->text[i].compression);
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500327 /* An internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600328 if (info_ptr->text[i].compression > 0)
329 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500330#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500331 /* Write international chunk */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500332 png_write_iTXt(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600333 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);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600338#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500339 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600340#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500341 /* Mark this chunk as written */
342 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600343 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500344
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600345 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
Guy Schalnat0d580581995-07-20 02:43:20 -0500346 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500347#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500348 /* Write compressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600349 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600350 info_ptr->text[i].text, 0,
351 info_ptr->text[i].compression);
Guy Schalnate5a37791996-06-05 15:50:50 -0500352#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600353 png_warning(png_ptr, "Unable to write compressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500354#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500355 /* Mark this chunk as written */
356 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500357 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500358
Andreas Dilger47a0c421997-05-16 02:46:07 -0500359 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500360 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500361#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500362 /* Write uncompressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600363 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600364 info_ptr->text[i].text, 0);
Guy Schalnate5a37791996-06-05 15:50:50 -0500365#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600366 png_warning(png_ptr, "Unable to write uncompressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500367#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500368
Andreas Dilger47a0c421997-05-16 02:46:07 -0500369 /* Mark this chunk as written */
370 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500371 }
372 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600373#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500374#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600375 if (info_ptr->unknown_chunks_num)
376 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500377 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600378
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500379 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600380
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500381 for (up = info_ptr->unknown_chunks;
382 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
383 up++)
384 {
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500385 int keep = png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500386 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson36fa2a02011-05-11 06:52:37 -0500387 up->location &&
388 (up->location & PNG_AFTER_IDAT) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600389 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
390 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600391 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600392 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600393 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500394 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600395 }
396#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500397 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500398
399 png_ptr->mode |= PNG_AFTER_IDAT;
400
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500401 /* Write end of PNG file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500402 png_write_IEND(png_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500403 /* 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
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600406 * experiences a problem, please try building libpng with
407 * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500408 * png-mng-implement at lists.sf.net .
409 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500410#ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600411# ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
Glenn Randers-Pehrson32fc5ce2000-07-24 06:34:14 -0500412 png_flush(png_ptr);
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600413# endif
Glenn Randers-Pehrsondbed41f2008-08-19 18:20:52 -0500414#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500415}
416
Glenn Randers-Pehrson65d235a2009-11-02 11:32:30 -0600417#ifdef PNG_CONVERT_tIME_SUPPORTED
Glenn Randers-Pehrson99106de2009-11-01 16:26:14 -0600418/* "tm" structure is not supported on WindowsCE */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500419void PNGAPI
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500420png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm FAR * ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500421{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500422 png_debug(1, "in png_convert_from_struct_tm");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500423
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600424 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;
Guy Schalnat0d580581995-07-20 02:43:20 -0500430}
431
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500432void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600433png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500434{
435 struct tm *tbuf;
436
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500437 png_debug(1, "in png_convert_from_time_t");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500438
Guy Schalnat0d580581995-07-20 02:43:20 -0500439 tbuf = gmtime(&ttime);
440 png_convert_from_struct_tm(ptime, tbuf);
441}
Guy Schalnat6d764711995-12-19 03:22:19 -0600442#endif
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500443
Andreas Dilger47a0c421997-05-16 02:46:07 -0500444/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500445PNG_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)
Guy Schalnat0d580581995-07-20 02:43:20 -0500448{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500449#ifdef PNG_USER_MEM_SUPPORTED
450 return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600451 warn_fn, NULL, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500452}
453
454/* Alternate initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500455static void png_reset_filter_heuristics(png_structp png_ptr); /* forward decl */
456
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500457PNG_FUNCTION(png_structp,PNGAPI
458png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600459 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500460 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500461{
462#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson62ca98e2009-12-20 15:14:57 -0600463 volatile int png_cleanup_needed = 0;
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600464#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500465 volatile
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600466#endif
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500467 png_structp png_ptr;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600468#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600469#ifdef USE_FAR_KEYWORD
John Bowlere6dc85b2011-04-27 14:47:15 -0500470 jmp_buf tmp_jmpbuf;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600471#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600472#endif
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500473
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500474 png_debug(1, "in png_create_write_struct");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500475
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500476#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600477 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600478 (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500479#else
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600480 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500481#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600482 if (png_ptr == NULL)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500483 return (NULL);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600484
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500485 /* Added at libpng-1.2.6 */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500486#ifdef PNG_SET_USER_LIMITS_SUPPORTED
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500487 png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
488 png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500489#endif
490
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500491#ifdef PNG_SETJMP_SUPPORTED
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. */
495#ifdef USE_FAR_KEYWORD
John Bowlere6dc85b2011-04-27 14:47:15 -0500496 if (setjmp(tmp_jmpbuf))
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500497#else
Glenn Randers-Pehrsonf4ea2242009-11-20 21:38:24 -0600498 if (setjmp(png_jmpbuf(png_ptr))) /* sets longjmp to match setjmp */
499#endif
500#ifdef USE_FAR_KEYWORD
John Bowlere6dc85b2011-04-27 14:47:15 -0500501 png_memcpy(png_jmpbuf(png_ptr), tmp_jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500502#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500503 PNG_ABORT();
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500504#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600505
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500506#ifdef PNG_USER_MEM_SUPPORTED
507 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
508#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600509 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnate5a37791996-06-05 15:50:50 -0500510
John Bowler88b77cc2011-05-05 06:49:55 -0500511 if (!png_user_version_check(png_ptr, user_png_ver))
512 png_cleanup_needed = 1;
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500513
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500514 /* Initialize zbuf - compression buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500515 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500516
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500517 if (!png_cleanup_needed)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500518 {
519 png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600520 png_ptr->zbuf_size);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500521 if (png_ptr->zbuf == NULL)
522 png_cleanup_needed = 1;
523 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500524
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500525 if (png_cleanup_needed)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500526 {
527 /* Clean up PNG structure and deallocate any memory. */
528 png_free(png_ptr, png_ptr->zbuf);
529 png_ptr->zbuf = NULL;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500530#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500531 png_destroy_struct_2((png_voidp)png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600532 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500533#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500534 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500535#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500536 return (NULL);
537 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500538
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500539 png_set_write_fn(png_ptr, NULL, NULL, NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500540
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500541#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500542 png_reset_filter_heuristics(png_ptr);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600543#endif
544
Glenn Randers-Pehrson03008a02002-04-27 10:11:25 -0500545 return (png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500546}
547
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500548
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600549/* 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 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500554void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600555png_write_rows(png_structp png_ptr, png_bytepp row,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600556 png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500557{
558 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600559 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500560
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500561 png_debug(1, "in png_write_rows");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600562
563 if (png_ptr == NULL)
564 return;
565
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500566 /* Loop through the rows */
Guy Schalnat0d580581995-07-20 02:43:20 -0500567 for (i = 0, rp = row; i < num_rows; i++, rp++)
568 {
569 png_write_row(png_ptr, *rp);
570 }
571}
572
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600573/* Write the image. You only need to call this function once, even
574 * if you are writing an interlaced image.
575 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500576void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600577png_write_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500578{
579 png_uint_32 i; /* row index */
580 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600581 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500582
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600583 if (png_ptr == NULL)
584 return;
585
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500586 png_debug(1, "in png_write_image");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500587
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500588#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500589 /* Initialize interlace handling. If image is not interlaced,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500590 * this will set pass to 1
591 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500592 num_pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600593#else
594 num_pass = 1;
595#endif
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500596 /* Loop through passes */
Guy Schalnat0d580581995-07-20 02:43:20 -0500597 for (pass = 0; pass < num_pass; pass++)
598 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500599 /* Loop through image */
Guy Schalnat0d580581995-07-20 02:43:20 -0500600 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
601 {
602 png_write_row(png_ptr, *rp);
603 }
604 }
605}
606
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500607/* Called by user to write a row of image data */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500608void PNGAPI
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500609png_write_row(png_structp png_ptr, png_const_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500610{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500611 /* 1.5.6: moved from png_struct to be a local structure: */
612 png_row_info row_info;
613
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600614 if (png_ptr == NULL)
615 return;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500616
Glenn Randers-Pehrson632a84e2010-03-09 22:28:33 -0600617 png_debug2(1, "in png_write_row (row %u, pass %d)",
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600618 png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600619
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500620 /* Initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600621 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500622 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500623 /* Make sure we wrote the header info */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500624 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
625 png_error(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600626 "png_write_info was never called before png_write_row");
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600627
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500628 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500629#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500630 if (png_ptr->transformations & PNG_INVERT_MONO)
631 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500632#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500633
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500634#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500635 if (png_ptr->transformations & PNG_FILLER)
636 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500637#endif
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600638#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
639 defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500640 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600641 png_warning(png_ptr,
642 "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500643#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500644
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500645#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500646 if (png_ptr->transformations & PNG_PACK)
647 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500648#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500649
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500650#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500651 if (png_ptr->transformations & PNG_SHIFT)
652 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500653#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500654
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500655#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500656 if (png_ptr->transformations & PNG_BGR)
657 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500658#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500659
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500660#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500661 if (png_ptr->transformations & PNG_SWAP_BYTES)
662 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500663#endif
664
Guy Schalnat0d580581995-07-20 02:43:20 -0500665 png_write_start_row(png_ptr);
666 }
667
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500668#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500669 /* If interlaced and not interested in row, return */
Guy Schalnat0d580581995-07-20 02:43:20 -0500670 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
671 {
672 switch (png_ptr->pass)
673 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600674 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600675 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500676 {
677 png_write_finish_row(png_ptr);
678 return;
679 }
680 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500681
Guy Schalnat0d580581995-07-20 02:43:20 -0500682 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600683 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500684 {
685 png_write_finish_row(png_ptr);
686 return;
687 }
688 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500689
Guy Schalnat0d580581995-07-20 02:43:20 -0500690 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600691 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500692 {
693 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600694 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500695 }
696 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500697
Guy Schalnat0d580581995-07-20 02:43:20 -0500698 case 3:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600699 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
Guy Schalnat0d580581995-07-20 02:43:20 -0500700 {
701 png_write_finish_row(png_ptr);
702 return;
703 }
704 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500705
Guy Schalnat0d580581995-07-20 02:43:20 -0500706 case 4:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600707 if ((png_ptr->row_number & 0x03) != 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500708 {
709 png_write_finish_row(png_ptr);
710 return;
711 }
712 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500713
Guy Schalnat0d580581995-07-20 02:43:20 -0500714 case 5:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600715 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500716 {
717 png_write_finish_row(png_ptr);
718 return;
719 }
720 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500721
Guy Schalnat0d580581995-07-20 02:43:20 -0500722 case 6:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600723 if (!(png_ptr->row_number & 0x01))
Guy Schalnat0d580581995-07-20 02:43:20 -0500724 {
725 png_write_finish_row(png_ptr);
726 return;
727 }
728 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500729
730 default: /* error: ignore it */
731 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500732 }
733 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600734#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500735
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500736 /* Set up row info for transformations */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500737 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);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600743
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500744 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);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500750
751 /* Copy user's row into buffer, leaving room for filter byte. */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500752 png_memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500753
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500754#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500755 /* Handle interlacing */
Guy Schalnat0d580581995-07-20 02:43:20 -0500756 if (png_ptr->interlaced && png_ptr->pass < 6 &&
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500757 (png_ptr->transformations & PNG_INTERLACE))
Guy Schalnat0d580581995-07-20 02:43:20 -0500758 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500759 png_do_write_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500760 /* This should always get caught above, but still ... */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500761 if (!(row_info.width))
Guy Schalnat0d580581995-07-20 02:43:20 -0500762 {
763 png_write_finish_row(png_ptr);
764 return;
765 }
766 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600767#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500768
John Bowler4a12f4a2011-04-17 18:34:22 -0500769#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500770 /* Handle other transformations */
Guy Schalnat0d580581995-07-20 02:43:20 -0500771 if (png_ptr->transformations)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500772 png_do_write_transformations(png_ptr, &row_info);
John Bowler4a12f4a2011-04-17 18:34:22 -0500773#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500774
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500775 /* 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");
781
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500782#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600783 /* 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 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500792 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500793 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600794 {
795 /* Intrapixel differencing */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500796 png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600797 }
798#endif
799
Andreas Dilger47a0c421997-05-16 02:46:07 -0500800 /* Find a filter if necessary, filter the row and write it out. */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500801 png_write_find_filter(png_ptr, &row_info);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600802
803 if (png_ptr->write_row_fn != NULL)
804 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500805}
806
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500807#ifdef PNG_WRITE_FLUSH_SUPPORTED
Guy Schalnat0f716451995-11-28 11:22:13 -0600808/* Set the automatic flush interval or 0 to turn flushing off */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500809void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600810png_set_flush(png_structp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600811{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500812 png_debug(1, "in png_set_flush");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500813
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600814 if (png_ptr == NULL)
815 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500816
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600817 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600818}
819
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500820/* Flush the current output buffers now */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500821void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600822png_write_flush(png_structp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600823{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600824 int wrote_IDAT;
Guy Schalnat0f716451995-11-28 11:22:13 -0600825
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500826 png_debug(1, "in png_write_flush");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500827
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600828 if (png_ptr == NULL)
829 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500830
Guy Schalnate5a37791996-06-05 15:50:50 -0500831 /* We have already written out all of the data */
832 if (png_ptr->row_number >= png_ptr->num_rows)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500833 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600834
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600835 do
836 {
837 int ret;
Guy Schalnat0f716451995-11-28 11:22:13 -0600838
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500839 /* Compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600840 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600841 wrote_IDAT = 0;
Guy Schalnat0f716451995-11-28 11:22:13 -0600842
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500843 /* Check for compression errors */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600844 if (ret != Z_OK)
845 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500846 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600847 png_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500848
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600849 else
850 png_error(png_ptr, "zlib error");
851 }
Guy Schalnat0f716451995-11-28 11:22:13 -0600852
Andreas Dilger47a0c421997-05-16 02:46:07 -0500853 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600854 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500855 /* Write the IDAT and reset the zlib output buffer */
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600856 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600857 wrote_IDAT = 1;
858 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500859 } while (wrote_IDAT == 1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600860
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600861 /* If there is any data left to be output, write it into a new IDAT */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600862 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600863 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500864 /* Write the IDAT and reset the zlib output buffer */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600865 png_write_IDAT(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600866 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600867 }
868 png_ptr->flush_rows = 0;
869 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600870}
871#endif /* PNG_WRITE_FLUSH_SUPPORTED */
872
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500873/* Free all memory used by the write */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500874void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600875png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500876{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600877 png_structp png_ptr = NULL;
878 png_infop info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500879#ifdef PNG_USER_MEM_SUPPORTED
880 png_free_ptr free_fn = NULL;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500881 png_voidp mem_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500882#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600883
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500884 png_debug(1, "in png_destroy_write_struct");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500885
Andreas Dilger47a0c421997-05-16 02:46:07 -0500886 if (png_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600887 png_ptr = *png_ptr_ptr;
888
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500889#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
Andreas Dilger47a0c421997-05-16 02:46:07 -0500897 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600898 info_ptr = *info_ptr_ptr;
899
Andreas Dilger47a0c421997-05-16 02:46:07 -0500900 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500901 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500902 if (png_ptr != NULL)
903 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500904 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600905
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600906#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500907 if (png_ptr->num_chunk_list)
908 {
909 png_free(png_ptr, png_ptr->chunk_list);
910 png_ptr->num_chunk_list = 0;
911 }
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500912#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500913 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600914
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500915#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500916 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500917 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500918#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600919 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500920#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500921 *info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500922 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600923
Andreas Dilger47a0c421997-05-16 02:46:07 -0500924 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500925 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600926 png_write_destroy(png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500927#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500928 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600929 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500930#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600931 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500932#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500933 *png_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500934 }
935}
936
937
Andreas Dilger47a0c421997-05-16 02:46:07 -0500938/* Free any memory used in png_ptr struct (old method) */
Glenn Randers-Pehrsonf64a06f2001-04-11 07:38:00 -0500939void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600940png_write_destroy(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500941{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600942#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500943 jmp_buf tmp_jmp; /* Save jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600944#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500945 png_error_ptr error_fn;
John Bowler88b77cc2011-05-05 06:49:55 -0500946#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500947 png_error_ptr warning_fn;
John Bowler88b77cc2011-05-05 06:49:55 -0500948#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500949 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500950#ifdef PNG_USER_MEM_SUPPORTED
951 png_free_ptr free_fn;
952#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500953
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500954 png_debug(1, "in png_write_destroy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500955
956 /* Free any memory zlib uses */
John Bowlerc5bef942011-05-05 17:35:39 -0500957 if (png_ptr->zlib_state != PNG_ZLIB_UNINITIALIZED)
958 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -0500959
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500960 /* Free our memory. png_free checks NULL for us. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600961 png_free(png_ptr, png_ptr->zbuf);
962 png_free(png_ptr, png_ptr->row_buf);
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500963#ifdef PNG_WRITE_FILTER_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600964 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);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500969#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600970
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500971#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500972 /* Use this to save a little code space, it doesn't free the filter_costs */
973 png_reset_filter_heuristics(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500974 png_free(png_ptr, png_ptr->filter_costs);
975 png_free(png_ptr, png_ptr->inv_filter_costs);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600976#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500977
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600978#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500979 /* Reset structure */
John Bowlere6dc85b2011-04-27 14:47:15 -0500980 png_memcpy(tmp_jmp, png_ptr->longjmp_buffer, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600981#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500982
983 error_fn = png_ptr->error_fn;
John Bowler88b77cc2011-05-05 06:49:55 -0500984#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500985 warning_fn = png_ptr->warning_fn;
John Bowler88b77cc2011-05-05 06:49:55 -0500986#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500987 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500988#ifdef PNG_USER_MEM_SUPPORTED
989 free_fn = png_ptr->free_fn;
990#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500991
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500992 png_memset(png_ptr, 0, png_sizeof(png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -0500993
994 png_ptr->error_fn = error_fn;
John Bowler88b77cc2011-05-05 06:49:55 -0500995#ifdef PNG_WARNINGS_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500996 png_ptr->warning_fn = warning_fn;
John Bowler88b77cc2011-05-05 06:49:55 -0500997#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500998 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500999#ifdef PNG_USER_MEM_SUPPORTED
1000 png_ptr->free_fn = free_fn;
1001#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001002
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001003#ifdef PNG_SETJMP_SUPPORTED
John Bowlere6dc85b2011-04-27 14:47:15 -05001004 png_memcpy(png_ptr->longjmp_buffer, tmp_jmp, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001005#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001006}
Guy Schalnate5a37791996-06-05 15:50:50 -05001007
Andreas Dilger47a0c421997-05-16 02:46:07 -05001008/* Allow the application to select one or more row filters to use. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001009void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -05001010png_set_filter(png_structp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001011{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001012 png_debug(1, "in png_set_filter");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001013
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001014 if (png_ptr == NULL)
1015 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001016
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001017#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001018 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001019 (method == PNG_INTRAPIXEL_DIFFERENCING))
1020 method = PNG_FILTER_TYPE_BASE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001021
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -06001022#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001023 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001024 {
1025 switch (filters & (PNG_ALL_FILTERS | 0x07))
1026 {
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001027#ifdef PNG_WRITE_FILTER_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05001028 case 5:
1029 case 6:
Andreas Dilger47a0c421997-05-16 02:46:07 -05001030 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001031#endif /* PNG_WRITE_FILTER_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001032 case PNG_FILTER_VALUE_NONE:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001033 png_ptr->do_filter = PNG_FILTER_NONE; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001034
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001035#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001036 case PNG_FILTER_VALUE_SUB:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001037 png_ptr->do_filter = PNG_FILTER_SUB; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001038
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001039 case PNG_FILTER_VALUE_UP:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001040 png_ptr->do_filter = PNG_FILTER_UP; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001041
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001042 case PNG_FILTER_VALUE_AVG:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001043 png_ptr->do_filter = PNG_FILTER_AVG; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001044
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001045 case PNG_FILTER_VALUE_PAETH:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001046 png_ptr->do_filter = PNG_FILTER_PAETH; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001047
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001048 default:
1049 png_ptr->do_filter = (png_byte)filters; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001050#else
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001051 default:
1052 png_warning(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001053#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -05001054 }
1055
Andreas Dilger47a0c421997-05-16 02:46:07 -05001056 /* 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.
Guy Schalnate5a37791996-06-05 15:50:50 -05001064 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001065 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001066 {
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001067#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001068 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001069 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001070 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001071 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001072 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001073 }
1074
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001075 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001076 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001077 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001078 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001079 png_warning(png_ptr, "Can't add Up filter after starting");
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001080 png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
1081 ~PNG_FILTER_UP);
Guy Schalnate5a37791996-06-05 15:50:50 -05001082 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001083
Guy Schalnate5a37791996-06-05 15:50:50 -05001084 else
1085 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001086 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001087 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001088 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001089 }
1090 }
1091
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001092 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001093 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001094 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001095 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001096 png_warning(png_ptr, "Can't add Average filter after starting");
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001097 png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
1098 ~PNG_FILTER_AVG);
Guy Schalnate5a37791996-06-05 15:50:50 -05001099 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001100
Guy Schalnate5a37791996-06-05 15:50:50 -05001101 else
1102 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001103 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001104 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001105 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001106 }
1107 }
1108
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001109 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
Andreas Dilger47a0c421997-05-16 02:46:07 -05001110 png_ptr->paeth_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001111 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001112 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001113 {
1114 png_warning(png_ptr, "Can't add Paeth filter after starting");
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001115 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
Guy Schalnate5a37791996-06-05 15:50:50 -05001116 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001117
Guy Schalnate5a37791996-06-05 15:50:50 -05001118 else
1119 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001120 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001121 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001122 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001123 }
1124 }
1125
1126 if (png_ptr->do_filter == PNG_NO_FILTERS)
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001127#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -05001128 png_ptr->do_filter = PNG_FILTER_NONE;
1129 }
1130 }
1131 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05001132 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001133}
1134
Andreas Dilger47a0c421997-05-16 02:46:07 -05001135/* 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
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001140 * better compression.
1141 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001142#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */
Glenn Randers-Pehrson47dc5f72011-03-30 09:59:02 -05001143/* Convenience reset API. */
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001144static void
1145png_reset_filter_heuristics(png_structp png_ptr)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001146{
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001147 /* 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)
1155 {
1156 png_bytep old = png_ptr->prev_filters;
1157 png_ptr->prev_filters = NULL;
1158 png_free(png_ptr, old);
1159 }
1160 if (png_ptr->filter_weights != NULL)
1161 {
1162 png_uint_16p old = png_ptr->filter_weights;
1163 png_ptr->filter_weights = NULL;
1164 png_free(png_ptr, old);
1165 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001166
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001167 if (png_ptr->inv_filter_weights != NULL)
1168 {
1169 png_uint_16p old = png_ptr->inv_filter_weights;
1170 png_ptr->inv_filter_weights = NULL;
1171 png_free(png_ptr, old);
1172 }
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001173
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001174 /* 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{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001181 if (png_ptr == NULL)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001182 return 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001183
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001184 /* 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)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001192 {
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001193 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001194
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001195 if (num_weights > 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001196 {
1197 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001198 (png_uint_32)(png_sizeof(png_byte) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001199
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001200 /* To make sure that the weighting starts out fairly */
1201 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001202 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001203 png_ptr->prev_filters[i] = 255;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001204 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001205
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001206 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
1207 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001208
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001209 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
1210 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001211
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001212 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 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001217
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001218 /* Safe to set this now */
1219 png_ptr->num_prev_filters = (png_byte)num_weights;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001220 }
1221
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001222 /* 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)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001226 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001227 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
1228 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001229
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001230 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));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001232 }
1233
Andreas Dilger47a0c421997-05-16 02:46:07 -05001234 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1235 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001236 png_ptr->inv_filter_costs[i] =
1237 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001238 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001239
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;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001245 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001246 else if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT ||
1247 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001248 {
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001249 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,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001262 int num_weights, png_const_doublep filter_weights,
1263 png_const_doublep filter_costs)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001264{
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;
1277 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001278 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001279 if (filter_weights[i] <= 0.0)
1280 {
1281 png_ptr->inv_filter_weights[i] =
1282 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1283 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001284
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001285 else
1286 {
1287 png_ptr->inv_filter_weights[i] =
1288 (png_uint_16)(PNG_WEIGHT_FACTOR*filter_weights[i]+.5);
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001289
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001290 png_ptr->filter_weights[i] =
1291 (png_uint_16)(PNG_WEIGHT_FACTOR/filter_weights[i]+.5);
1292 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001293 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001294
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001295 /* 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)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001303 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001304 png_ptr->inv_filter_costs[i] =
1305 (png_uint_16)(PNG_COST_FACTOR / filter_costs[i] + .5);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001306
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001307 png_ptr->filter_costs[i] =
1308 (png_uint_16)(PNG_COST_FACTOR * filter_costs[i] + .5);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001309 }
1310 }
1311}
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001312#endif /* FLOATING_POINT */
1313
1314#ifdef PNG_FIXED_POINT_SUPPORTED
1315void PNGAPI
1316png_set_filter_heuristics_fixed(png_structp png_ptr, int heuristic_method,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001317 int num_weights, png_const_fixed_point_p filter_weights,
1318 png_const_fixed_point_p filter_costs)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001319{
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.
1324 */
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)
1330 {
1331 int i;
1332 for (i = 0; i < num_weights; i++)
1333 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001334 if (filter_weights[i] <= 0)
1335 {
1336 png_ptr->inv_filter_weights[i] =
1337 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1338 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001339
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001340 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);
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001344
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001345 png_ptr->filter_weights[i] = (png_uint_16)((PNG_WEIGHT_FACTOR*
1346 PNG_FP_1+(filter_weights[i]/2))/filter_weights[i]);
1347 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001348 }
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)
1359 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001360 png_uint_32 tmp;
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001361
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001362 /* 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;
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001375 }
1376 }
1377}
1378#endif /* FIXED_POINT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001379#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1380
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001381void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001382png_set_compression_level(png_structp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001383{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001384 png_debug(1, "in png_set_compression_level");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001385
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001386 if (png_ptr == NULL)
1387 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001388
Guy Schalnate5a37791996-06-05 15:50:50 -05001389 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001390 png_ptr->zlib_level = level;
1391}
1392
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001393void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001394png_set_compression_mem_level(png_structp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001395{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001396 png_debug(1, "in png_set_compression_mem_level");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001397
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001398 if (png_ptr == NULL)
1399 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001400
Guy Schalnate5a37791996-06-05 15:50:50 -05001401 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001402 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001403}
1404
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001405void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001406png_set_compression_strategy(png_structp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001407{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001408 png_debug(1, "in png_set_compression_strategy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001409
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001410 if (png_ptr == NULL)
1411 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001412
Guy Schalnate5a37791996-06-05 15:50:50 -05001413 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001414 png_ptr->zlib_strategy = strategy;
1415}
1416
Glenn Randers-Pehrsonaf855e42011-05-07 10:52:49 -05001417/* 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 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001420void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001421png_set_compression_window_bits(png_structp png_ptr, int window_bits)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001422{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001423 if (png_ptr == NULL)
1424 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001425
Guy Schalnate5a37791996-06-05 15:50:50 -05001426 if (window_bits > 15)
1427 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001428
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001429 else if (window_bits < 8)
1430 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001431
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001432#ifndef WBITS_8_OK
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -05001433 /* Avoid libpng bug with 256-byte windows */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001434 if (window_bits == 8)
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001435 {
1436 png_warning(png_ptr, "Compression window is being reset to 512");
1437 window_bits = 9;
1438 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001439
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001440#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001441 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001442 png_ptr->zlib_window_bits = window_bits;
1443}
1444
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001445void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001446png_set_compression_method(png_structp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001447{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001448 png_debug(1, "in png_set_compression_method");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001449
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001450 if (png_ptr == NULL)
1451 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001452
Guy Schalnate5a37791996-06-05 15:50:50 -05001453 if (method != 8)
1454 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001455
Guy Schalnate5a37791996-06-05 15:50:50 -05001456 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001457 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -05001458}
1459
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001460/* The following were added to libpng-1.5.4 */
John Bowlerb2bee332011-06-10 23:24:58 -05001461#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001462void 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
Glenn Randers-Pehrsonaf855e42011-05-07 10:52:49 -05001498/* 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 */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001501void 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}
John Bowlerb2bee332011-06-10 23:24:58 -05001540#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001541/* end of API added to libpng-1.5.4 */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001542
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001543void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001544png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1545{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001546 if (png_ptr == NULL)
1547 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001548
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001549 png_ptr->write_row_fn = write_row_fn;
1550}
1551
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001552#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001553void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001554png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001555 write_user_transform_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001556{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001557 png_debug(1, "in png_set_write_user_transform_fn");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001558
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001559 if (png_ptr == NULL)
1560 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001561
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001562 png_ptr->transformations |= PNG_USER_TRANSFORM;
1563 png_ptr->write_user_transform_fn = write_user_transform_fn;
1564}
1565#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001566
1567
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001568#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001569void PNGAPI
1570png_write_png(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001571 int transforms, voidp params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001572{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001573 if (png_ptr == NULL || info_ptr == NULL)
1574 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001575
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
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001581#ifdef PNG_WRITE_INVERT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001582 /* Invert monochrome pixels */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001583 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001584 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001585#endif
1586
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001587#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001588 /* 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)
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001592 && (info_ptr->valid & PNG_INFO_sBIT))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001593 png_set_shift(png_ptr, &info_ptr->sig_bit);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001594#endif
1595
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001596#ifdef PNG_WRITE_PACK_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001597 /* Pack pixels into bytes */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001598 if (transforms & PNG_TRANSFORM_PACKING)
1599 png_set_packing(png_ptr);
1600#endif
1601
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001602#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001603 /* Swap location of alpha bytes from ARGB to RGBA */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001604 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001605 png_set_swap_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001606#endif
1607
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001608#ifdef PNG_WRITE_FILLER_SUPPORTED
Glenn Randers-Pehrson47539062011-05-05 07:32:30 -05001609 /* Pack XRGB/RGBX/ARGB/RGBA into RGB (4 channels -> 3 channels) */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001610 if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001611 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001612
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001613 else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001614 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001615#endif
1616
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001617#ifdef PNG_WRITE_BGR_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001618 /* Flip BGR pixels to RGB */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001619 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001620 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001621#endif
1622
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001623#ifdef PNG_WRITE_SWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001624 /* Swap bytes of 16-bit files to most significant byte first */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001625 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001626 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001627#endif
1628
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001629#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001630 /* Swap bits of 1, 2, 4 bit packed pixel formats */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001631 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001632 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001633#endif
1634
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001635#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001636 /* Invert the alpha channel from opacity to transparency */
1637 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1638 png_set_invert_alpha(png_ptr);
1639#endif
1640
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001641 /* ----------------------- end of transformations ------------------- */
1642
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001643 /* Write the bits */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001644 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);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001649
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001650 PNG_UNUSED(transforms) /* Quiet compiler warnings */
1651 PNG_UNUSED(params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001652}
1653#endif
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -06001654#endif /* PNG_WRITE_SUPPORTED */