blob: f7b0f5c502103534af1add3a6a5c8557f52cd4a4 [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-Pehrson13831bc2011-12-21 08:28:28 -06004 * Last changed in libpng 1.6.0 [(PENDING RELEASE)]
Glenn Randers-Pehrson1531bd62012-01-01 14:45:04 -06005 * Copyright (c) 1998-2012 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"
John Bowler7875d532011-11-07 22:33:49 -060015#if defined PNG_SIMPLIFIED_WRITE_SUPPORTED && defined PNG_STDIO_SUPPORTED
16# include <errno.h>
17#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050018
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060019#ifdef PNG_WRITE_SUPPORTED
20
Andreas Dilger47a0c421997-05-16 02:46:07 -050021/* Writes all the PNG information. This is the suggested way to use the
22 * library. If you have a new chunk to add, make a function to write it,
23 * and put it in the correct location here. If you want the chunk written
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050024 * after the image data, put it in png_write_end(). I strongly encourage
Andreas Dilger47a0c421997-05-16 02:46:07 -050025 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
26 * the chunk, as that will keep the code from breaking if you want to just
27 * write a plain PNG file. If you have long comments, I suggest writing
28 * them in png_write_end(), and compressing them.
29 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050030void PNGAPI
John Bowlerb11b31a2012-03-21 07:55:46 -050031png_write_info_before_PLTE(png_structrp png_ptr, png_const_inforp info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050032{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050033 png_debug(1, "in png_write_info_before_PLTE");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050034
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -060035 if (png_ptr == NULL || info_ptr == NULL)
36 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050037
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060038 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
39 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -050040 /* Write PNG signature */
41 png_write_sig(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050042
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050043#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060044 if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) && \
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060045 (png_ptr->mng_features_permitted))
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060046 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050047 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -050048 png_ptr->mng_features_permitted = 0;
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060049 }
50#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050051
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -050052 /* Write IHDR information. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060053 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060054 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
55 info_ptr->filter_type,
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050056#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060057 info_ptr->interlace_type);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060058#else
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060059 0);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060060#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050061 /* The rest of these check to see if the valid field has the appropriate
62 * flag set, and if it does, writes the chunk.
John Bowlerb11b31a2012-03-21 07:55:46 -050063 *
64 * 1.6.0: COLORSPACE support controls the writing of these chunks too, and
65 * the chunks will be written if the WRITE routine is there and information
66 * is available in the COLORSPACE. (See png_colorspace_sync_info in png.c
67 * for where the valid flags get set.)
68 *
69 * Under certain circumstances the colorspace can be invalidated without
70 * syncing the info_struct 'valid' flags; this happens if libpng detects and
71 * error and calls png_error while the color space is being set, yet the
72 * application continues writing the PNG. So check the 'invalid' flag here
73 * too.
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050074 */
John Bowlerb11b31a2012-03-21 07:55:46 -050075#ifdef PNG_GAMMA_SUPPORTED
76# ifdef PNG_WRITE_gAMA_SUPPORTED
77 if (!(info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) &&
78 (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_gAMA) &&
79 (info_ptr->valid & PNG_INFO_gAMA))
80 png_write_gAMA_fixed(png_ptr, info_ptr->colorspace.gamma);
81# endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060082#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050083
John Bowlerb11b31a2012-03-21 07:55:46 -050084#ifdef PNG_COLORSPACE_SUPPORTED
John Bowler921648a2012-03-28 23:36:12 -050085 /* Write only one of sRGB or an ICC profile. If a profile was supplied
86 * and it matches one of the known sRGB ones issue a warning.
John Bowlerb11b31a2012-03-21 07:55:46 -050087 */
John Bowler921648a2012-03-28 23:36:12 -050088# ifdef PNG_WRITE_iCCP_SUPPORTED
89 if (!(info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) &&
90 (info_ptr->valid & PNG_INFO_iCCP))
91 {
92# ifdef PNG_WRITE_sRGB_SUPPORTED
93 if (info_ptr->valid & PNG_INFO_sRGB)
94 png_app_warning(png_ptr,
95 "profile matches sRGB but writing iCCP instead");
96# endif
97
98 png_write_iCCP(png_ptr, info_ptr->iccp_name,
99 info_ptr->iccp_profile);
100 }
101# ifdef PNG_WRITE_sRGB_SUPPORTED
102 else
103# endif
104# endif
105
John Bowlerb11b31a2012-03-21 07:55:46 -0500106# ifdef PNG_WRITE_sRGB_SUPPORTED
107 if (!(info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) &&
108 (info_ptr->valid & PNG_INFO_sRGB))
109 png_write_sRGB(png_ptr, info_ptr->colorspace.rendering_intent);
John Bowlerb11b31a2012-03-21 07:55:46 -0500110# endif /* WRITE_sRGB */
John Bowlerb11b31a2012-03-21 07:55:46 -0500111#endif /* COLORSPACE */
112
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500113#ifdef PNG_WRITE_sBIT_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600114 if (info_ptr->valid & PNG_INFO_sBIT)
115 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500116#endif
John Bowlerb11b31a2012-03-21 07:55:46 -0500117
118#ifdef PNG_COLORSPACE_SUPPORTED
119# ifdef PNG_WRITE_cHRM_SUPPORTED
120 if (!(info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) &&
121 (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) &&
122 (info_ptr->valid & PNG_INFO_cHRM))
123 png_write_cHRM_fixed(png_ptr, &info_ptr->colorspace.end_points_xy);
124# endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600125#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500126
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500127#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600128 if (info_ptr->unknown_chunks_num)
129 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500130 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600131
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500132 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600133
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500134 for (up = info_ptr->unknown_chunks;
135 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
136 up++)
137 {
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500138 int keep = png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500139
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500140 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson36fa2a02011-05-11 06:52:37 -0500141 up->location &&
142 !(up->location & PNG_HAVE_PLTE) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600143 !(up->location & PNG_HAVE_IDAT) &&
Glenn Randers-Pehrson36fa2a02011-05-11 06:52:37 -0500144 !(up->location & PNG_AFTER_IDAT) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600145 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
146 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600147 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500148 if (up->size == 0)
149 png_warning(png_ptr, "Writing zero-length unknown chunk");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500150
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600151 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600152 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500153 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600154 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500155#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600156 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
157 }
158}
159
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500160void PNGAPI
John Bowlerb11b31a2012-03-21 07:55:46 -0500161png_write_info(png_structrp png_ptr, png_const_inforp info_ptr)
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600162{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600163#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600164 int i;
165#endif
166
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500167 png_debug(1, "in png_write_info");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600168
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600169 if (png_ptr == NULL || info_ptr == NULL)
170 return;
171
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600172 png_write_info_before_PLTE(png_ptr, info_ptr);
173
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600174 if (info_ptr->valid & PNG_INFO_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500175 png_write_PLTE(png_ptr, info_ptr->palette,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600176 (png_uint_32)info_ptr->num_palette);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500177
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600178 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600179 png_error(png_ptr, "Valid palette required for paletted images");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600180
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500181#ifdef PNG_WRITE_tRNS_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600182 if (info_ptr->valid & PNG_INFO_tRNS)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500183 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500184#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500185 /* Invert the alpha channel (in tRNS) */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500186 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600187 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500188 {
189 int j;
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500190 for (j = 0; j<(int)info_ptr->num_trans; j++)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500191 info_ptr->trans_alpha[j] =
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -0500192 (png_byte)(255 - info_ptr->trans_alpha[j]);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500193 }
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600194#endif
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500195 png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600196 info_ptr->num_trans, info_ptr->color_type);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500197 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500198#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500199#ifdef PNG_WRITE_bKGD_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600200 if (info_ptr->valid & PNG_INFO_bKGD)
201 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500202#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500203
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500204#ifdef PNG_WRITE_hIST_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600205 if (info_ptr->valid & PNG_INFO_hIST)
206 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500207#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500208
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500209#ifdef PNG_WRITE_oFFs_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600210 if (info_ptr->valid & PNG_INFO_oFFs)
211 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600212 info_ptr->offset_unit_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500213#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500214
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500215#ifdef PNG_WRITE_pCAL_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500216 if (info_ptr->valid & PNG_INFO_pCAL)
217 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600218 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
219 info_ptr->pcal_units, info_ptr->pcal_params);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500220#endif
Glenn Randers-Pehrson59020592009-05-18 10:52:12 -0500221
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500222#ifdef PNG_WRITE_sCAL_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500223 if (info_ptr->valid & PNG_INFO_sCAL)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600224 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600225 info_ptr->scal_s_width, info_ptr->scal_s_height);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500226#endif /* sCAL */
227
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500228#ifdef PNG_WRITE_pHYs_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500229 if (info_ptr->valid & PNG_INFO_pHYs)
230 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600231 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500232#endif /* pHYs */
233
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500234#ifdef PNG_WRITE_tIME_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600235 if (info_ptr->valid & PNG_INFO_tIME)
Guy Schalnate5a37791996-06-05 15:50:50 -0500236 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600237 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600238 png_ptr->mode |= PNG_WROTE_tIME;
Guy Schalnate5a37791996-06-05 15:50:50 -0500239 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500240#endif /* tIME */
241
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500242#ifdef PNG_WRITE_sPLT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600243 if (info_ptr->valid & PNG_INFO_sPLT)
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600244 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
245 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500246#endif /* sPLT */
247
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500248#ifdef PNG_WRITE_TEXT_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500249 /* Check to see if we need to write text chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500250 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500251 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500252 png_debug2(2, "Writing header text chunk %d, type %d", i,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600253 info_ptr->text[i].compression);
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500254 /* An internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600255 if (info_ptr->text[i].compression > 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600256 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500257#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600258 /* Write international chunk */
259 png_write_iTXt(png_ptr,
260 info_ptr->text[i].compression,
261 info_ptr->text[i].key,
262 info_ptr->text[i].lang,
263 info_ptr->text[i].lang_key,
264 info_ptr->text[i].text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600265#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600266 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600267#endif
268 /* Mark this chunk as written */
269 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
270 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500271
Andreas Dilger47a0c421997-05-16 02:46:07 -0500272 /* If we want a compressed text chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600273 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500274 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500275#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500276 /* Write compressed chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500277 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600278 info_ptr->text[i].text, 0,
279 info_ptr->text[i].compression);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500280#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600281 png_warning(png_ptr, "Unable to write compressed text");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500282#endif
283 /* Mark this chunk as written */
284 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
285 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500286
Andreas Dilger47a0c421997-05-16 02:46:07 -0500287 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
288 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500289#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500290 /* Write uncompressed chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500291 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600292 info_ptr->text[i].text,
293 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500294 /* Mark this chunk as written */
295 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500296#else
297 /* Can't get here */
298 png_warning(png_ptr, "Unable to write uncompressed text");
299#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500300 }
301 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500302#endif /* tEXt */
303
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500304#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600305 if (info_ptr->unknown_chunks_num)
306 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500307 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600308
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500309 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600310
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500311 for (up = info_ptr->unknown_chunks;
312 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
313 up++)
314 {
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500315 int keep = png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500316 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson36fa2a02011-05-11 06:52:37 -0500317 up->location &&
318 (up->location & PNG_HAVE_PLTE) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600319 !(up->location & PNG_HAVE_IDAT) &&
Glenn Randers-Pehrson36fa2a02011-05-11 06:52:37 -0500320 !(up->location & PNG_AFTER_IDAT) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600321 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
322 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600323 {
324 png_write_chunk(png_ptr, up->name, up->data, up->size);
325 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500326 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600327 }
328#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500329}
Guy Schalnat0d580581995-07-20 02:43:20 -0500330
Andreas Dilger47a0c421997-05-16 02:46:07 -0500331/* Writes the end of the PNG file. If you don't want to write comments or
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600332 * time information, you can pass NULL for info. If you already wrote these
333 * in png_write_info(), do not write them again here. If you have long
334 * comments, I suggest writing them here, and compressing them.
335 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500336void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600337png_write_end(png_structrp png_ptr, png_inforp info_ptr)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500338{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500339 png_debug(1, "in png_write_end");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500340
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600341 if (png_ptr == NULL)
342 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500343
Andreas Dilger47a0c421997-05-16 02:46:07 -0500344 if (!(png_ptr->mode & PNG_HAVE_IDAT))
345 png_error(png_ptr, "No IDATs written into file");
346
Glenn Randers-Pehrsoneeb1bb62012-03-02 22:10:15 -0600347#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
348 if (png_ptr->num_palette_max > png_ptr->num_palette)
Glenn Randers-Pehrson945cb1f2012-03-10 08:48:04 -0600349 png_benign_error(png_ptr, "Wrote palette index exceeding num_palette");
Glenn Randers-Pehrsoneeb1bb62012-03-02 22:10:15 -0600350#endif
351
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500352 /* See if user wants us to write information chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500353 if (info_ptr != NULL)
354 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500355#ifdef PNG_WRITE_TEXT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500356 int i; /* local index variable */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600357#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500358#ifdef PNG_WRITE_tIME_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500359 /* Check to see if user has supplied a time chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600360 if ((info_ptr->valid & PNG_INFO_tIME) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600361 !(png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500362 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500363
Andreas Dilger47a0c421997-05-16 02:46:07 -0500364#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500365#ifdef PNG_WRITE_TEXT_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500366 /* Loop through comment chunks */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600367 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500368 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500369 png_debug2(2, "Writing trailer text chunk %d, type %d", i,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500370 info_ptr->text[i].compression);
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500371 /* An internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600372 if (info_ptr->text[i].compression > 0)
373 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500374#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500375 /* Write international chunk */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500376 png_write_iTXt(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600377 info_ptr->text[i].compression,
378 info_ptr->text[i].key,
379 info_ptr->text[i].lang,
380 info_ptr->text[i].lang_key,
381 info_ptr->text[i].text);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600382#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500383 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600384#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500385 /* Mark this chunk as written */
386 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600387 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500388
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600389 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
Guy Schalnat0d580581995-07-20 02:43:20 -0500390 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500391#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500392 /* Write compressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600393 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600394 info_ptr->text[i].text, 0,
395 info_ptr->text[i].compression);
Guy Schalnate5a37791996-06-05 15:50:50 -0500396#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600397 png_warning(png_ptr, "Unable to write compressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500398#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500399 /* Mark this chunk as written */
400 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500401 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500402
Andreas Dilger47a0c421997-05-16 02:46:07 -0500403 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500404 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500405#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500406 /* Write uncompressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600407 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600408 info_ptr->text[i].text, 0);
Guy Schalnate5a37791996-06-05 15:50:50 -0500409#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600410 png_warning(png_ptr, "Unable to write uncompressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500411#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500412
Andreas Dilger47a0c421997-05-16 02:46:07 -0500413 /* Mark this chunk as written */
414 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500415 }
416 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600417#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500418#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600419 if (info_ptr->unknown_chunks_num)
420 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500421 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600422
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500423 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600424
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500425 for (up = info_ptr->unknown_chunks;
426 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
427 up++)
428 {
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500429 int keep = png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500430 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson36fa2a02011-05-11 06:52:37 -0500431 up->location &&
432 (up->location & PNG_AFTER_IDAT) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600433 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
434 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600435 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600436 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600437 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500438 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600439 }
440#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500441 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500442
443 png_ptr->mode |= PNG_AFTER_IDAT;
444
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500445 /* Write end of PNG file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500446 png_write_IEND(png_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500447 /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
448 * and restored again in libpng-1.2.30, may cause some applications that
449 * do not set png_ptr->output_flush_fn to crash. If your application
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600450 * experiences a problem, please try building libpng with
451 * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500452 * png-mng-implement at lists.sf.net .
453 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500454#ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600455# ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
Glenn Randers-Pehrson32fc5ce2000-07-24 06:34:14 -0500456 png_flush(png_ptr);
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600457# endif
Glenn Randers-Pehrsondbed41f2008-08-19 18:20:52 -0500458#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500459}
460
Glenn Randers-Pehrson65d235a2009-11-02 11:32:30 -0600461#ifdef PNG_CONVERT_tIME_SUPPORTED
Glenn Randers-Pehrson99106de2009-11-01 16:26:14 -0600462/* "tm" structure is not supported on WindowsCE */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500463void PNGAPI
John Bowlerbaeb6d12011-11-26 18:21:02 -0600464png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm * ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500465{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500466 png_debug(1, "in png_convert_from_struct_tm");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500467
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600468 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
469 ptime->month = (png_byte)(ttime->tm_mon + 1);
470 ptime->day = (png_byte)ttime->tm_mday;
471 ptime->hour = (png_byte)ttime->tm_hour;
472 ptime->minute = (png_byte)ttime->tm_min;
473 ptime->second = (png_byte)ttime->tm_sec;
Guy Schalnat0d580581995-07-20 02:43:20 -0500474}
475
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500476void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600477png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500478{
479 struct tm *tbuf;
480
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500481 png_debug(1, "in png_convert_from_time_t");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500482
Guy Schalnat0d580581995-07-20 02:43:20 -0500483 tbuf = gmtime(&ttime);
484 png_convert_from_struct_tm(ptime, tbuf);
485}
Guy Schalnat6d764711995-12-19 03:22:19 -0600486#endif
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500487
Andreas Dilger47a0c421997-05-16 02:46:07 -0500488/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500489PNG_FUNCTION(png_structp,PNGAPI
490png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
491 png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500492{
John Bowlerd332c672011-12-21 17:36:12 -0600493#ifndef PNG_USER_MEM_SUPPORTED
John Bowler5d567862011-12-24 09:12:00 -0600494 png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
John Bowlerd332c672011-12-21 17:36:12 -0600495 error_fn, warn_fn, NULL, NULL, NULL);
496#else
497 return png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
498 warn_fn, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500499}
500
501/* Alternate initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500502PNG_FUNCTION(png_structp,PNGAPI
503png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600504 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500505 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500506{
John Bowler5d567862011-12-24 09:12:00 -0600507 png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
John Bowlerd332c672011-12-21 17:36:12 -0600508 error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500509#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500510
John Bowler42a2b552012-03-05 20:57:40 -0600511 /* Set the zlib control values to defaults; they can be overridden by the
512 * application after the struct has been created.
513 */
John Bowlerb5d00512012-03-09 09:15:18 -0600514 png_ptr->zbuffer_size = PNG_ZBUF_SIZE;
515
John Bowler42a2b552012-03-05 20:57:40 -0600516 png_ptr->zlib_strategy = Z_FILTERED; /* may be overridden if no filters */
517 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
518 png_ptr->zlib_mem_level = 8;
519 png_ptr->zlib_window_bits = 15;
520 png_ptr->zlib_method = 8;
521
522#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
523 png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY;
524 png_ptr->zlib_text_level = Z_DEFAULT_COMPRESSION;
525 png_ptr->zlib_text_mem_level = 8;
526 png_ptr->zlib_text_window_bits = 15;
527 png_ptr->zlib_text_method = 8;
528#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
529
John Bowleraa816c42012-03-16 07:39:49 -0500530 /* This is a highly dubious configuration option; by default it is off, but
531 * it may be appropriate for private builds that are testing extensions not
532 * conformant to the current specification, or of applications that must not
533 * fail to write at all costs!
534 */
535# ifdef PNG_BENIGN_WRITE_ERRORS_SUPPORTED
536 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
John Bowler921648a2012-03-28 23:36:12 -0500537 /* In stable builds only warn if an application error can be completely
538 * handled.
539 */
540# endif
541
542 /* App warnings are warnings in release (or release candidate) builds but
543 * are errors during development.
544 */
545# if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
546 png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
John Bowleraa816c42012-03-16 07:39:49 -0500547# endif
548
John Bowlerd332c672011-12-21 17:36:12 -0600549 if (png_ptr != NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500550 {
John Bowlerd332c672011-12-21 17:36:12 -0600551 /* TODO: delay this, it can be done in png_init_io() (if the app doesn't
552 * do it itself) avoiding setting the default function if it is not
553 * required.
554 */
555 png_set_write_fn(png_ptr, NULL, NULL, NULL);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500556 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500557
John Bowlerd332c672011-12-21 17:36:12 -0600558 return png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500559}
560
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500561
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600562/* Write a few rows of image data. If the image is interlaced,
563 * either you will have to write the 7 sub images, or, if you
564 * have called png_set_interlace_handling(), you will have to
565 * "write" the image seven times.
566 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500567void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600568png_write_rows(png_structrp png_ptr, png_bytepp row,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600569 png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500570{
571 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600572 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500573
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500574 png_debug(1, "in png_write_rows");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600575
576 if (png_ptr == NULL)
577 return;
578
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500579 /* Loop through the rows */
Guy Schalnat0d580581995-07-20 02:43:20 -0500580 for (i = 0, rp = row; i < num_rows; i++, rp++)
581 {
582 png_write_row(png_ptr, *rp);
583 }
584}
585
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600586/* Write the image. You only need to call this function once, even
587 * if you are writing an interlaced image.
588 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500589void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600590png_write_image(png_structrp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500591{
592 png_uint_32 i; /* row index */
593 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600594 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500595
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600596 if (png_ptr == NULL)
597 return;
598
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500599 png_debug(1, "in png_write_image");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500600
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500601#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500602 /* Initialize interlace handling. If image is not interlaced,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500603 * this will set pass to 1
604 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500605 num_pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600606#else
607 num_pass = 1;
608#endif
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500609 /* Loop through passes */
Guy Schalnat0d580581995-07-20 02:43:20 -0500610 for (pass = 0; pass < num_pass; pass++)
611 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500612 /* Loop through image */
Guy Schalnat0d580581995-07-20 02:43:20 -0500613 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
614 {
615 png_write_row(png_ptr, *rp);
616 }
617 }
618}
619
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500620/* Called by user to write a row of image data */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500621void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600622png_write_row(png_structrp png_ptr, png_const_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500623{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500624 /* 1.5.6: moved from png_struct to be a local structure: */
625 png_row_info row_info;
626
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600627 if (png_ptr == NULL)
628 return;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500629
Glenn Randers-Pehrson632a84e2010-03-09 22:28:33 -0600630 png_debug2(1, "in png_write_row (row %u, pass %d)",
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600631 png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600632
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500633 /* Initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600634 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500635 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500636 /* Make sure we wrote the header info */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500637 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
638 png_error(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600639 "png_write_info was never called before png_write_row");
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600640
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500641 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500642#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500643 if (png_ptr->transformations & PNG_INVERT_MONO)
644 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500645#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500646
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500647#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500648 if (png_ptr->transformations & PNG_FILLER)
649 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500650#endif
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600651#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
652 defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500653 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600654 png_warning(png_ptr,
655 "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500656#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500657
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500658#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500659 if (png_ptr->transformations & PNG_PACK)
660 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500661#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500662
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500663#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500664 if (png_ptr->transformations & PNG_SHIFT)
665 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500666#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500667
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500668#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500669 if (png_ptr->transformations & PNG_BGR)
670 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500671#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500672
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500673#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500674 if (png_ptr->transformations & PNG_SWAP_BYTES)
675 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500676#endif
677
Guy Schalnat0d580581995-07-20 02:43:20 -0500678 png_write_start_row(png_ptr);
679 }
680
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500681#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500682 /* If interlaced and not interested in row, return */
Guy Schalnat0d580581995-07-20 02:43:20 -0500683 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
684 {
685 switch (png_ptr->pass)
686 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600687 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600688 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500689 {
690 png_write_finish_row(png_ptr);
691 return;
692 }
693 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500694
Guy Schalnat0d580581995-07-20 02:43:20 -0500695 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600696 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500697 {
698 png_write_finish_row(png_ptr);
699 return;
700 }
701 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500702
Guy Schalnat0d580581995-07-20 02:43:20 -0500703 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600704 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500705 {
706 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600707 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500708 }
709 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500710
Guy Schalnat0d580581995-07-20 02:43:20 -0500711 case 3:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600712 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
Guy Schalnat0d580581995-07-20 02:43:20 -0500713 {
714 png_write_finish_row(png_ptr);
715 return;
716 }
717 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500718
Guy Schalnat0d580581995-07-20 02:43:20 -0500719 case 4:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600720 if ((png_ptr->row_number & 0x03) != 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500721 {
722 png_write_finish_row(png_ptr);
723 return;
724 }
725 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500726
Guy Schalnat0d580581995-07-20 02:43:20 -0500727 case 5:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600728 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500729 {
730 png_write_finish_row(png_ptr);
731 return;
732 }
733 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500734
Guy Schalnat0d580581995-07-20 02:43:20 -0500735 case 6:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600736 if (!(png_ptr->row_number & 0x01))
Guy Schalnat0d580581995-07-20 02:43:20 -0500737 {
738 png_write_finish_row(png_ptr);
739 return;
740 }
741 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500742
743 default: /* error: ignore it */
744 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500745 }
746 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600747#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500748
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500749 /* Set up row info for transformations */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500750 row_info.color_type = png_ptr->color_type;
751 row_info.width = png_ptr->usr_width;
752 row_info.channels = png_ptr->usr_channels;
753 row_info.bit_depth = png_ptr->usr_bit_depth;
754 row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels);
755 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600756
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500757 png_debug1(3, "row_info->color_type = %d", row_info.color_type);
758 png_debug1(3, "row_info->width = %u", row_info.width);
759 png_debug1(3, "row_info->channels = %d", row_info.channels);
760 png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth);
761 png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth);
762 png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500763
764 /* Copy user's row into buffer, leaving room for filter byte. */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500765 png_memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500766
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500767#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500768 /* Handle interlacing */
Guy Schalnat0d580581995-07-20 02:43:20 -0500769 if (png_ptr->interlaced && png_ptr->pass < 6 &&
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500770 (png_ptr->transformations & PNG_INTERLACE))
Guy Schalnat0d580581995-07-20 02:43:20 -0500771 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500772 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 -0500773 /* This should always get caught above, but still ... */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500774 if (!(row_info.width))
Guy Schalnat0d580581995-07-20 02:43:20 -0500775 {
776 png_write_finish_row(png_ptr);
777 return;
778 }
779 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600780#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500781
John Bowler4a12f4a2011-04-17 18:34:22 -0500782#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500783 /* Handle other transformations */
Guy Schalnat0d580581995-07-20 02:43:20 -0500784 if (png_ptr->transformations)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500785 png_do_write_transformations(png_ptr, &row_info);
John Bowler4a12f4a2011-04-17 18:34:22 -0500786#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500787
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500788 /* At this point the row_info pixel depth must match the 'transformed' depth,
789 * which is also the output depth.
790 */
791 if (row_info.pixel_depth != png_ptr->pixel_depth ||
792 row_info.pixel_depth != png_ptr->transformed_pixel_depth)
793 png_error(png_ptr, "internal write transform logic error");
794
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500795#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600796 /* Write filter_method 64 (intrapixel differencing) only if
797 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
798 * 2. Libpng did not write a PNG signature (this filter_method is only
799 * used in PNG datastreams that are embedded in MNG datastreams) and
800 * 3. The application called png_permit_mng_features with a mask that
801 * included PNG_FLAG_MNG_FILTER_64 and
802 * 4. The filter_method is 64 and
803 * 5. The color_type is RGB or RGBA
804 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500805 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500806 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600807 {
808 /* Intrapixel differencing */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500809 png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600810 }
811#endif
812
Glenn Randers-Pehrson363ae652012-03-01 21:39:29 -0600813/* Added at libpng-1.5.10 */
814#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
John Bowlerdee75772012-03-01 18:55:54 -0600815 /* Check for out-of-range palette index */
Glenn Randers-Pehrson14ca47b2012-06-06 13:30:30 -0500816 if (row_info.color_type == PNG_COLOR_TYPE_PALETTE &&
817 png_ptr->num_palette_max >= 0)
Glenn Randers-Pehrsoneeb1bb62012-03-02 22:10:15 -0600818 png_do_check_palette_indexes(png_ptr, &row_info);
John Bowlerdee75772012-03-01 18:55:54 -0600819#endif
820
Andreas Dilger47a0c421997-05-16 02:46:07 -0500821 /* Find a filter if necessary, filter the row and write it out. */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500822 png_write_find_filter(png_ptr, &row_info);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600823
824 if (png_ptr->write_row_fn != NULL)
825 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500826}
827
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500828#ifdef PNG_WRITE_FLUSH_SUPPORTED
Guy Schalnat0f716451995-11-28 11:22:13 -0600829/* Set the automatic flush interval or 0 to turn flushing off */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500830void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600831png_set_flush(png_structrp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600832{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500833 png_debug(1, "in png_set_flush");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500834
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600835 if (png_ptr == NULL)
836 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500837
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600838 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600839}
840
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500841/* Flush the current output buffers now */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500842void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600843png_write_flush(png_structrp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600844{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500845 png_debug(1, "in png_write_flush");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500846
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600847 if (png_ptr == NULL)
848 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500849
Guy Schalnate5a37791996-06-05 15:50:50 -0500850 /* We have already written out all of the data */
851 if (png_ptr->row_number >= png_ptr->num_rows)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500852 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600853
John Bowlerb5d00512012-03-09 09:15:18 -0600854 png_compress_IDAT(png_ptr, NULL, 0, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600855 png_ptr->flush_rows = 0;
856 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600857}
858#endif /* PNG_WRITE_FLUSH_SUPPORTED */
859
John Bowlerd332c672011-12-21 17:36:12 -0600860#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
John Bowler5d567862011-12-24 09:12:00 -0600861static void png_reset_filter_heuristics(png_structrp png_ptr); /* forward decl */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500862#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600863
John Bowlerd332c672011-12-21 17:36:12 -0600864/* Free any memory used in png_ptr struct without freeing the struct itself. */
865static void
John Bowler5d567862011-12-24 09:12:00 -0600866png_write_destroy(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500867{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500868 png_debug(1, "in png_write_destroy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500869
870 /* Free any memory zlib uses */
John Bowler42a2b552012-03-05 20:57:40 -0600871 if (png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED)
John Bowlerc5bef942011-05-05 17:35:39 -0500872 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -0500873
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500874 /* Free our memory. png_free checks NULL for us. */
John Bowlerb5d00512012-03-09 09:15:18 -0600875 png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600876 png_free(png_ptr, png_ptr->row_buf);
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500877#ifdef PNG_WRITE_FILTER_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600878 png_free(png_ptr, png_ptr->prev_row);
879 png_free(png_ptr, png_ptr->sub_row);
880 png_free(png_ptr, png_ptr->up_row);
881 png_free(png_ptr, png_ptr->avg_row);
882 png_free(png_ptr, png_ptr->paeth_row);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500883#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600884
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500885#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500886 /* Use this to save a little code space, it doesn't free the filter_costs */
887 png_reset_filter_heuristics(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500888 png_free(png_ptr, png_ptr->filter_costs);
889 png_free(png_ptr, png_ptr->inv_filter_costs);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600890#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500891
John Bowlerd332c672011-12-21 17:36:12 -0600892#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
893 png_free(png_ptr, png_ptr->chunk_list);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600894#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500895
John Bowlerd332c672011-12-21 17:36:12 -0600896 /* The error handling and memory handling information is left intact at this
897 * point: the jmp_buf may still have to be freed. See png_destroy_png_struct
898 * for how this happens.
899 */
900}
Guy Schalnate5a37791996-06-05 15:50:50 -0500901
John Bowlerd332c672011-12-21 17:36:12 -0600902/* Free all memory used by the write.
903 * In libpng 1.6.0 this API changed quietly to no longer accept a NULL value for
904 * *png_ptr_ptr. Prior to 1.6.0 it would accept such a value and it would free
905 * the passed in info_structs but it would quietly fail to free any of the data
906 * inside them. In 1.6.0 it quietly does nothing (it has to be quiet because it
907 * has no png_ptr.)
908 */
909void PNGAPI
910png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
911{
912 png_debug(1, "in png_destroy_write_struct");
Guy Schalnate5a37791996-06-05 15:50:50 -0500913
John Bowlerd332c672011-12-21 17:36:12 -0600914 if (png_ptr_ptr != NULL)
915 {
John Bowler5d567862011-12-24 09:12:00 -0600916 png_structrp png_ptr = *png_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500917
John Bowlerd332c672011-12-21 17:36:12 -0600918 if (png_ptr != NULL) /* added in libpng 1.6.0 */
919 {
920 png_destroy_info_struct(png_ptr, info_ptr_ptr);
921
922 *png_ptr_ptr = NULL;
923 png_write_destroy(png_ptr);
924 png_destroy_png_struct(png_ptr);
925 }
926 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500927}
Guy Schalnate5a37791996-06-05 15:50:50 -0500928
Andreas Dilger47a0c421997-05-16 02:46:07 -0500929/* Allow the application to select one or more row filters to use. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500930void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600931png_set_filter(png_structrp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500932{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500933 png_debug(1, "in png_set_filter");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500934
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600935 if (png_ptr == NULL)
936 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500937
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500938#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500939 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600940 (method == PNG_INTRAPIXEL_DIFFERENCING))
941 method = PNG_FILTER_TYPE_BASE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500942
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600943#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500944 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500945 {
946 switch (filters & (PNG_ALL_FILTERS | 0x07))
947 {
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500948#ifdef PNG_WRITE_FILTER_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500949 case 5:
950 case 6:
Andreas Dilger47a0c421997-05-16 02:46:07 -0500951 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500952#endif /* PNG_WRITE_FILTER_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500953 case PNG_FILTER_VALUE_NONE:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600954 png_ptr->do_filter = PNG_FILTER_NONE; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500955
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500956#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500957 case PNG_FILTER_VALUE_SUB:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600958 png_ptr->do_filter = PNG_FILTER_SUB; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500959
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500960 case PNG_FILTER_VALUE_UP:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600961 png_ptr->do_filter = PNG_FILTER_UP; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500962
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500963 case PNG_FILTER_VALUE_AVG:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600964 png_ptr->do_filter = PNG_FILTER_AVG; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500965
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500966 case PNG_FILTER_VALUE_PAETH:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600967 png_ptr->do_filter = PNG_FILTER_PAETH; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500968
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600969 default:
970 png_ptr->do_filter = (png_byte)filters; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500971#else
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600972 default:
973 png_warning(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500974#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500975 }
976
Andreas Dilger47a0c421997-05-16 02:46:07 -0500977 /* If we have allocated the row_buf, this means we have already started
978 * with the image and we should have allocated all of the filter buffers
979 * that have been selected. If prev_row isn't already allocated, then
980 * it is too late to start using the filters that need it, since we
981 * will be missing the data in the previous row. If an application
982 * wants to start and stop using particular filters during compression,
983 * it should start out with all of the filters, and then add and
984 * remove them after the start of compression.
Guy Schalnate5a37791996-06-05 15:50:50 -0500985 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500986 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500987 {
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500988#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600989 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500990 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500991 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600992 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500993 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -0500994 }
995
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600996 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500997 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500998 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500999 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001000 png_warning(png_ptr, "Can't add Up filter after starting");
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001001 png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
1002 ~PNG_FILTER_UP);
Guy Schalnate5a37791996-06-05 15:50:50 -05001003 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001004
Guy Schalnate5a37791996-06-05 15:50:50 -05001005 else
1006 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001007 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001008 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001009 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001010 }
1011 }
1012
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001013 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001014 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001015 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001016 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001017 png_warning(png_ptr, "Can't add Average filter after starting");
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001018 png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
1019 ~PNG_FILTER_AVG);
Guy Schalnate5a37791996-06-05 15:50:50 -05001020 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001021
Guy Schalnate5a37791996-06-05 15:50:50 -05001022 else
1023 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001024 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001025 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001026 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001027 }
1028 }
1029
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001030 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
Andreas Dilger47a0c421997-05-16 02:46:07 -05001031 png_ptr->paeth_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001032 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001033 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001034 {
1035 png_warning(png_ptr, "Can't add Paeth filter after starting");
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001036 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
Guy Schalnate5a37791996-06-05 15:50:50 -05001037 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001038
Guy Schalnate5a37791996-06-05 15:50:50 -05001039 else
1040 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001041 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001042 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001043 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001044 }
1045 }
1046
1047 if (png_ptr->do_filter == PNG_NO_FILTERS)
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001048#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -05001049 png_ptr->do_filter = PNG_FILTER_NONE;
1050 }
1051 }
1052 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05001053 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001054}
1055
Andreas Dilger47a0c421997-05-16 02:46:07 -05001056/* This allows us to influence the way in which libpng chooses the "best"
1057 * filter for the current scanline. While the "minimum-sum-of-absolute-
1058 * differences metric is relatively fast and effective, there is some
1059 * question as to whether it can be improved upon by trying to keep the
1060 * filtered data going to zlib more consistent, hopefully resulting in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001061 * better compression.
1062 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001063#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */
Glenn Randers-Pehrson47dc5f72011-03-30 09:59:02 -05001064/* Convenience reset API. */
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001065static void
John Bowler5d567862011-12-24 09:12:00 -06001066png_reset_filter_heuristics(png_structrp png_ptr)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001067{
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001068 /* Clear out any old values in the 'weights' - this must be done because if
1069 * the app calls set_filter_heuristics multiple times with different
1070 * 'num_weights' values we would otherwise potentially have wrong sized
1071 * arrays.
1072 */
1073 png_ptr->num_prev_filters = 0;
1074 png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1075 if (png_ptr->prev_filters != NULL)
1076 {
1077 png_bytep old = png_ptr->prev_filters;
1078 png_ptr->prev_filters = NULL;
1079 png_free(png_ptr, old);
1080 }
1081 if (png_ptr->filter_weights != NULL)
1082 {
1083 png_uint_16p old = png_ptr->filter_weights;
1084 png_ptr->filter_weights = NULL;
1085 png_free(png_ptr, old);
1086 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001087
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001088 if (png_ptr->inv_filter_weights != NULL)
1089 {
1090 png_uint_16p old = png_ptr->inv_filter_weights;
1091 png_ptr->inv_filter_weights = NULL;
1092 png_free(png_ptr, old);
1093 }
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001094
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001095 /* Leave the filter_costs - this array is fixed size. */
1096}
1097
1098static int
John Bowler5d567862011-12-24 09:12:00 -06001099png_init_filter_heuristics(png_structrp png_ptr, int heuristic_method,
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001100 int num_weights)
1101{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001102 if (png_ptr == NULL)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001103 return 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001104
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001105 /* Clear out the arrays */
1106 png_reset_filter_heuristics(png_ptr);
1107
1108 /* Check arguments; the 'reset' function makes the correct settings for the
1109 * unweighted case, but we must handle the weight case by initializing the
1110 * arrays for the caller.
1111 */
1112 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001113 {
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001114 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001115
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001116 if (num_weights > 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001117 {
1118 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001119 (png_uint_32)((sizeof (png_byte)) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001120
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001121 /* To make sure that the weighting starts out fairly */
1122 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001123 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001124 png_ptr->prev_filters[i] = 255;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001125 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001126
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001127 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001128 (png_uint_32)((sizeof (png_uint_16)) * num_weights));
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001129
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001130 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001131 (png_uint_32)((sizeof (png_uint_16)) * num_weights));
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001132
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001133 for (i = 0; i < num_weights; i++)
1134 {
1135 png_ptr->inv_filter_weights[i] =
1136 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1137 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001138
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001139 /* Safe to set this now */
1140 png_ptr->num_prev_filters = (png_byte)num_weights;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001141 }
1142
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001143 /* If, in the future, there are other filter methods, this would
1144 * need to be based on png_ptr->filter.
1145 */
1146 if (png_ptr->filter_costs == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001147 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001148 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001149 (png_uint_32)((sizeof (png_uint_16)) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001150
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001151 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001152 (png_uint_32)((sizeof (png_uint_16)) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001153 }
1154
Andreas Dilger47a0c421997-05-16 02:46:07 -05001155 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1156 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001157 png_ptr->inv_filter_costs[i] =
1158 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001159 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001160
1161 /* All the arrays are inited, safe to set this: */
1162 png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_WEIGHTED;
1163
1164 /* Return the 'ok' code. */
1165 return 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001166 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001167 else if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT ||
1168 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001169 {
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001170 return 1;
1171 }
1172 else
1173 {
1174 png_warning(png_ptr, "Unknown filter heuristic method");
1175 return 0;
1176 }
1177}
1178
1179/* Provide floating and fixed point APIs */
1180#ifdef PNG_FLOATING_POINT_SUPPORTED
1181void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001182png_set_filter_heuristics(png_structrp png_ptr, int heuristic_method,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001183 int num_weights, png_const_doublep filter_weights,
1184 png_const_doublep filter_costs)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001185{
1186 png_debug(1, "in png_set_filter_heuristics");
1187
1188 /* The internal API allocates all the arrays and ensures that the elements of
1189 * those arrays are set to the default value.
1190 */
1191 if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
1192 return;
1193
1194 /* If using the weighted method copy in the weights. */
1195 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1196 {
1197 int i;
1198 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001199 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001200 if (filter_weights[i] <= 0.0)
1201 {
1202 png_ptr->inv_filter_weights[i] =
1203 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1204 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001205
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001206 else
1207 {
1208 png_ptr->inv_filter_weights[i] =
1209 (png_uint_16)(PNG_WEIGHT_FACTOR*filter_weights[i]+.5);
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001210
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001211 png_ptr->filter_weights[i] =
1212 (png_uint_16)(PNG_WEIGHT_FACTOR/filter_weights[i]+.5);
1213 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001214 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001215
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001216 /* Here is where we set the relative costs of the different filters. We
1217 * should take the desired compression level into account when setting
1218 * the costs, so that Paeth, for instance, has a high relative cost at low
1219 * compression levels, while it has a lower relative cost at higher
1220 * compression settings. The filter types are in order of increasing
1221 * relative cost, so it would be possible to do this with an algorithm.
1222 */
1223 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) if (filter_costs[i] >= 1.0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001224 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001225 png_ptr->inv_filter_costs[i] =
1226 (png_uint_16)(PNG_COST_FACTOR / filter_costs[i] + .5);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001227
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001228 png_ptr->filter_costs[i] =
1229 (png_uint_16)(PNG_COST_FACTOR * filter_costs[i] + .5);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001230 }
1231 }
1232}
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001233#endif /* FLOATING_POINT */
1234
1235#ifdef PNG_FIXED_POINT_SUPPORTED
1236void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001237png_set_filter_heuristics_fixed(png_structrp png_ptr, int heuristic_method,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001238 int num_weights, png_const_fixed_point_p filter_weights,
1239 png_const_fixed_point_p filter_costs)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001240{
1241 png_debug(1, "in png_set_filter_heuristics_fixed");
1242
1243 /* The internal API allocates all the arrays and ensures that the elements of
1244 * those arrays are set to the default value.
1245 */
1246 if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
1247 return;
1248
1249 /* If using the weighted method copy in the weights. */
1250 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1251 {
1252 int i;
1253 for (i = 0; i < num_weights; i++)
1254 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001255 if (filter_weights[i] <= 0)
1256 {
1257 png_ptr->inv_filter_weights[i] =
1258 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1259 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001260
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001261 else
1262 {
1263 png_ptr->inv_filter_weights[i] = (png_uint_16)
1264 ((PNG_WEIGHT_FACTOR*filter_weights[i]+PNG_FP_HALF)/PNG_FP_1);
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001265
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001266 png_ptr->filter_weights[i] = (png_uint_16)((PNG_WEIGHT_FACTOR*
1267 PNG_FP_1+(filter_weights[i]/2))/filter_weights[i]);
1268 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001269 }
1270
1271 /* Here is where we set the relative costs of the different filters. We
1272 * should take the desired compression level into account when setting
1273 * the costs, so that Paeth, for instance, has a high relative cost at low
1274 * compression levels, while it has a lower relative cost at higher
1275 * compression settings. The filter types are in order of increasing
1276 * relative cost, so it would be possible to do this with an algorithm.
1277 */
1278 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1279 if (filter_costs[i] >= PNG_FP_1)
1280 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001281 png_uint_32 tmp;
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001282
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001283 /* Use a 32 bit unsigned temporary here because otherwise the
1284 * intermediate value will be a 32 bit *signed* integer (ANSI rules)
1285 * and this will get the wrong answer on division.
1286 */
1287 tmp = PNG_COST_FACTOR*PNG_FP_1 + (filter_costs[i]/2);
1288 tmp /= filter_costs[i];
1289
1290 png_ptr->inv_filter_costs[i] = (png_uint_16)tmp;
1291
1292 tmp = PNG_COST_FACTOR * filter_costs[i] + PNG_FP_HALF;
1293 tmp /= PNG_FP_1;
1294
1295 png_ptr->filter_costs[i] = (png_uint_16)tmp;
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001296 }
1297 }
1298}
1299#endif /* FIXED_POINT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001300#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1301
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001302void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001303png_set_compression_level(png_structrp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001304{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001305 png_debug(1, "in png_set_compression_level");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001306
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001307 if (png_ptr == NULL)
1308 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001309
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001310 png_ptr->zlib_level = level;
1311}
1312
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001313void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001314png_set_compression_mem_level(png_structrp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001315{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001316 png_debug(1, "in png_set_compression_mem_level");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001317
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001318 if (png_ptr == NULL)
1319 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001320
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001321 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001322}
1323
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001324void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001325png_set_compression_strategy(png_structrp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001326{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001327 png_debug(1, "in png_set_compression_strategy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001328
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001329 if (png_ptr == NULL)
1330 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001331
John Bowler42a2b552012-03-05 20:57:40 -06001332 /* The flag setting here prevents the libpng dynamic selection of strategy.
1333 */
Guy Schalnate5a37791996-06-05 15:50:50 -05001334 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001335 png_ptr->zlib_strategy = strategy;
1336}
1337
Glenn Randers-Pehrsonaf855e42011-05-07 10:52:49 -05001338/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
1339 * smaller value of window_bits if it can do so safely.
1340 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001341void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001342png_set_compression_window_bits(png_structrp png_ptr, int window_bits)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001343{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001344 if (png_ptr == NULL)
1345 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001346
John Bowler42a2b552012-03-05 20:57:40 -06001347 /* Prior to 1.6.0 this would warn but then set the window_bits value, this
1348 * meant that negative window bits values could be selected which would cause
1349 * libpng to write a non-standard PNG file with raw deflate or gzip
1350 * compressed IDAT or ancilliary chunks. Such files can be read and there is
1351 * no warning on read, so this seems like a very bad idea.
1352 */
Guy Schalnate5a37791996-06-05 15:50:50 -05001353 if (window_bits > 15)
John Bowler42a2b552012-03-05 20:57:40 -06001354 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001355 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
John Bowler42a2b552012-03-05 20:57:40 -06001356 window_bits = 15;
1357 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001358
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001359 else if (window_bits < 8)
John Bowler42a2b552012-03-05 20:57:40 -06001360 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001361 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
John Bowler42a2b552012-03-05 20:57:40 -06001362 window_bits = 8;
1363 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001364
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001365 png_ptr->zlib_window_bits = window_bits;
1366}
1367
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001368void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001369png_set_compression_method(png_structrp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001370{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001371 png_debug(1, "in png_set_compression_method");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001372
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001373 if (png_ptr == NULL)
1374 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001375
John Bowler42a2b552012-03-05 20:57:40 -06001376 /* This would produce an invalid PNG file if it worked, but it doesn't and
1377 * deflate will fault it, so it is harmless to just warn here.
1378 */
Guy Schalnate5a37791996-06-05 15:50:50 -05001379 if (method != 8)
1380 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001381
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001382 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -05001383}
1384
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001385/* The following were added to libpng-1.5.4 */
John Bowlerb2bee332011-06-10 23:24:58 -05001386#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001387void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001388png_set_text_compression_level(png_structrp png_ptr, int level)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001389{
1390 png_debug(1, "in png_set_text_compression_level");
1391
1392 if (png_ptr == NULL)
1393 return;
1394
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001395 png_ptr->zlib_text_level = level;
1396}
1397
1398void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001399png_set_text_compression_mem_level(png_structrp png_ptr, int mem_level)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001400{
1401 png_debug(1, "in png_set_text_compression_mem_level");
1402
1403 if (png_ptr == NULL)
1404 return;
1405
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001406 png_ptr->zlib_text_mem_level = mem_level;
1407}
1408
1409void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001410png_set_text_compression_strategy(png_structrp png_ptr, int strategy)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001411{
1412 png_debug(1, "in png_set_text_compression_strategy");
1413
1414 if (png_ptr == NULL)
1415 return;
1416
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001417 png_ptr->zlib_text_strategy = strategy;
1418}
1419
Glenn Randers-Pehrsonaf855e42011-05-07 10:52:49 -05001420/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
1421 * smaller value of window_bits if it can do so safely.
1422 */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001423void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001424png_set_text_compression_window_bits(png_structrp png_ptr, int window_bits)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001425{
1426 if (png_ptr == NULL)
1427 return;
1428
1429 if (window_bits > 15)
John Bowler42a2b552012-03-05 20:57:40 -06001430 {
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001431 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
John Bowler42a2b552012-03-05 20:57:40 -06001432 window_bits = 15;
1433 }
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001434
1435 else if (window_bits < 8)
John Bowler42a2b552012-03-05 20:57:40 -06001436 {
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001437 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
John Bowler42a2b552012-03-05 20:57:40 -06001438 window_bits = 8;
1439 }
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001440
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001441 png_ptr->zlib_text_window_bits = window_bits;
1442}
1443
1444void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001445png_set_text_compression_method(png_structrp png_ptr, int method)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001446{
1447 png_debug(1, "in png_set_text_compression_method");
1448
1449 if (png_ptr == NULL)
1450 return;
1451
1452 if (method != 8)
1453 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1454
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001455 png_ptr->zlib_text_method = method;
1456}
John Bowlerb2bee332011-06-10 23:24:58 -05001457#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001458/* end of API added to libpng-1.5.4 */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001459
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001460void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001461png_set_write_status_fn(png_structrp png_ptr, png_write_status_ptr write_row_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001462{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001463 if (png_ptr == NULL)
1464 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001465
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001466 png_ptr->write_row_fn = write_row_fn;
1467}
1468
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001469#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001470void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001471png_set_write_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001472 write_user_transform_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001473{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001474 png_debug(1, "in png_set_write_user_transform_fn");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001475
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001476 if (png_ptr == NULL)
1477 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001478
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001479 png_ptr->transformations |= PNG_USER_TRANSFORM;
1480 png_ptr->write_user_transform_fn = write_user_transform_fn;
1481}
1482#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001483
1484
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001485#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001486void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001487png_write_png(png_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001488 int transforms, voidp params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001489{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001490 if (png_ptr == NULL || info_ptr == NULL)
1491 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001492
1493 /* Write the file header information. */
1494 png_write_info(png_ptr, info_ptr);
1495
1496 /* ------ these transformations don't touch the info structure ------- */
1497
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001498#ifdef PNG_WRITE_INVERT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001499 /* Invert monochrome pixels */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001500 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001501 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001502#endif
1503
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001504#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001505 /* Shift the pixels up to a legal bit depth and fill in
1506 * as appropriate to correctly scale the image.
1507 */
1508 if ((transforms & PNG_TRANSFORM_SHIFT)
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001509 && (info_ptr->valid & PNG_INFO_sBIT))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001510 png_set_shift(png_ptr, &info_ptr->sig_bit);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001511#endif
1512
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001513#ifdef PNG_WRITE_PACK_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001514 /* Pack pixels into bytes */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001515 if (transforms & PNG_TRANSFORM_PACKING)
1516 png_set_packing(png_ptr);
1517#endif
1518
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001519#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001520 /* Swap location of alpha bytes from ARGB to RGBA */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001521 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001522 png_set_swap_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001523#endif
1524
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001525#ifdef PNG_WRITE_FILLER_SUPPORTED
Glenn Randers-Pehrson47539062011-05-05 07:32:30 -05001526 /* Pack XRGB/RGBX/ARGB/RGBA into RGB (4 channels -> 3 channels) */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001527 if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001528 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001529
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001530 else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001531 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001532#endif
1533
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001534#ifdef PNG_WRITE_BGR_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001535 /* Flip BGR pixels to RGB */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001536 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001537 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001538#endif
1539
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001540#ifdef PNG_WRITE_SWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001541 /* Swap bytes of 16-bit files to most significant byte first */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001542 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001543 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001544#endif
1545
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001546#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001547 /* Swap bits of 1, 2, 4 bit packed pixel formats */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001548 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001549 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001550#endif
1551
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001552#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001553 /* Invert the alpha channel from opacity to transparency */
1554 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1555 png_set_invert_alpha(png_ptr);
1556#endif
1557
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001558 /* ----------------------- end of transformations ------------------- */
1559
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001560 /* Write the bits */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001561 if (info_ptr->valid & PNG_INFO_IDAT)
1562 png_write_image(png_ptr, info_ptr->row_pointers);
1563
1564 /* It is REQUIRED to call this to finish writing the rest of the file */
1565 png_write_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001566
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001567 PNG_UNUSED(transforms) /* Quiet compiler warnings */
1568 PNG_UNUSED(params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001569}
1570#endif
John Bowler7875d532011-11-07 22:33:49 -06001571
1572
1573#ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
1574#ifdef PNG_STDIO_SUPPORTED /* currently required for png_image_write_* */
1575/* Initialize the write structure - general purpose utility. */
1576static int
1577png_image_write_init(png_imagep image)
1578{
1579 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, image,
1580 png_safe_error, png_safe_warning);
1581
1582 if (png_ptr != NULL)
1583 {
1584 png_infop info_ptr = png_create_info_struct(png_ptr);
1585
1586 if (info_ptr != NULL)
1587 {
John Bowler4fa96a42011-11-16 16:39:16 -06001588 png_controlp control = png_voidcast(png_controlp,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001589 png_malloc_warn(png_ptr, (sizeof *control)));
John Bowler7875d532011-11-07 22:33:49 -06001590
1591 if (control != NULL)
1592 {
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001593 png_memset(control, 0, (sizeof *control));
John Bowler7875d532011-11-07 22:33:49 -06001594
1595 control->png_ptr = png_ptr;
1596 control->info_ptr = info_ptr;
1597 control->for_write = 1;
1598
1599 image->opaque = control;
1600 return 1;
1601 }
1602
1603 /* Error clean up */
1604 png_destroy_info_struct(png_ptr, &info_ptr);
1605 }
1606
1607 png_destroy_write_struct(&png_ptr, NULL);
1608 }
1609
1610 return png_image_error(image, "png_image_read: out of memory");
1611}
1612
1613/* Arguments to png_image_write_main: */
1614typedef struct
1615{
1616 /* Arguments: */
1617 png_imagep image;
1618 png_const_voidp buffer;
1619 png_int_32 row_stride;
John Bowler5bc90382012-01-23 22:43:22 -06001620 png_const_voidp colormap;
John Bowler7875d532011-11-07 22:33:49 -06001621 int convert_to_8bit;
1622 /* Local variables: */
1623 png_const_voidp first_row;
1624 ptrdiff_t row_bytes;
1625 png_voidp local_row;
1626} png_image_write_control;
1627
Glenn Randers-Pehrsonefc4b692011-11-07 23:31:34 -06001628/* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to
1629 * do any necessary byte swapping. The component order is defined by the
John Bowler7875d532011-11-07 22:33:49 -06001630 * png_image format value.
1631 */
1632static int
1633png_write_image_16bit(png_voidp argument)
1634{
John Bowler4fa96a42011-11-16 16:39:16 -06001635 png_image_write_control *display = png_voidcast(png_image_write_control*,
1636 argument);
John Bowler7875d532011-11-07 22:33:49 -06001637 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06001638 png_structrp png_ptr = image->opaque->png_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001639
John Bowler4fa96a42011-11-16 16:39:16 -06001640 png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
1641 display->first_row);
1642 png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row);
John Bowler7875d532011-11-07 22:33:49 -06001643 png_uint_16p row_end;
John Bowler1c25b9b2012-02-29 10:49:28 -06001644 const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
John Bowler7875d532011-11-07 22:33:49 -06001645 int aindex = 0;
1646 png_uint_32 y = image->height;
1647
1648 if (image->format & PNG_FORMAT_FLAG_ALPHA)
1649 {
1650 if (image->format & PNG_FORMAT_FLAG_AFIRST)
1651 {
1652 aindex = -1;
1653 ++input_row; /* To point to the first component */
1654 ++output_row;
1655 }
1656
1657 else
1658 aindex = channels;
1659 }
1660
1661 else
1662 png_error(png_ptr, "png_write_image: internal call error");
1663
1664 /* Work out the output row end and count over this, note that the increment
1665 * above to 'row' means that row_end can actually be beyond the end of the
Glenn Randers-Pehrsonefc4b692011-11-07 23:31:34 -06001666 * row; this is correct.
John Bowler7875d532011-11-07 22:33:49 -06001667 */
1668 row_end = output_row + image->width * (channels+1);
1669
1670 while (y-- > 0)
1671 {
1672 png_const_uint_16p in_ptr = input_row;
1673 png_uint_16p out_ptr = output_row;
1674
1675 while (out_ptr < row_end)
1676 {
John Bowler1c25b9b2012-02-29 10:49:28 -06001677 const png_uint_16 alpha = in_ptr[aindex];
John Bowler7875d532011-11-07 22:33:49 -06001678 png_uint_32 reciprocal = 0;
1679 int c;
1680
1681 out_ptr[aindex] = alpha;
1682
1683 /* Calculate a reciprocal. The correct calculation is simply
Glenn Randers-Pehrsonefc4b692011-11-07 23:31:34 -06001684 * component/alpha*65535 << 15. (I.e. 15 bits of precision); this
John Bowler7875d532011-11-07 22:33:49 -06001685 * allows correct rounding by adding .5 before the shift. 'reciprocal'
1686 * is only initialized when required.
1687 */
1688 if (alpha > 0 && alpha < 65535)
1689 reciprocal = ((0xffff<<15)+(alpha>>1))/alpha;
1690
1691 c = channels;
1692 do /* always at least one channel */
1693 {
1694 png_uint_16 component = *in_ptr++;
1695
1696 /* The following gives 65535 for an alpha of 0, which is fine,
1697 * otherwise if 0/0 is represented as some other value there is more
1698 * likely to be a discontinuity which will probably damage
1699 * compression when moving from a fully transparent area to a
1700 * nearly transparent one. (The assumption here is that opaque
1701 * areas tend not to be 0 intensity.)
1702 */
1703 if (component >= alpha)
1704 component = 65535;
1705
1706 /* component<alpha, so component/alpha is less than one and
1707 * component*reciprocal is less than 2^31.
1708 */
1709 else if (component > 0 && alpha < 65535)
1710 {
1711 png_uint_32 calc = component * reciprocal;
1712 calc += 16384; /* round to nearest */
1713 component = (png_uint_16)(calc >> 15);
1714 }
1715
1716 *out_ptr++ = component;
1717 }
1718 while (--c > 0);
1719
1720 /* Skip to next component (skip the intervening alpha channel) */
1721 ++in_ptr;
1722 ++out_ptr;
1723 }
1724
John Bowler4fa96a42011-11-16 16:39:16 -06001725 png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row));
John Bowler7875d532011-11-07 22:33:49 -06001726 input_row += display->row_bytes/(sizeof (png_uint_16));
1727 }
1728
1729 return 1;
1730}
1731
1732/* Given 16-bit input (1 to 4 channels) write 8-bit output. If an alpha channel
1733 * is present it must be removed from the components, the components are then
1734 * written in sRGB encoding. No components are added or removed.
John Bowler5bc90382012-01-23 22:43:22 -06001735 *
1736 * Calculate an alpha reciprocal to reverse pre-multiplication. As above the
1737 * calculation can be done to 15 bits of accuracy; however, the output needs to
1738 * be scaled in the range 0..255*65535, so include that scaling here.
John Bowler7875d532011-11-07 22:33:49 -06001739 */
John Bowler5bc90382012-01-23 22:43:22 -06001740#define UNP_RECIPROCAL(alpha) ((((0xffff*0xff)<<7)+(alpha>>1))/alpha)
1741
1742static png_byte
1743png_unpremultiply(png_uint_32 component, png_uint_32 alpha,
1744 png_uint_32 reciprocal/*from the above macro*/)
1745{
1746 /* The following gives 1.0 for an alpha of 0, which is fine, otherwise if 0/0
1747 * is represented as some other value there is more likely to be a
1748 * discontinuity which will probably damage compression when moving from a
1749 * fully transparent area to a nearly transparent one. (The assumption here
1750 * is that opaque areas tend not to be 0 intensity.)
1751 *
1752 * There is a rounding problem here; if alpha is less than 128 it will end up
1753 * as 0 when scaled to 8 bits. To avoid introducing spurious colors into the
1754 * output change for this too.
1755 */
1756 if (component >= alpha || alpha < 128)
1757 return 255;
1758
1759 /* component<alpha, so component/alpha is less than one and
1760 * component*reciprocal is less than 2^31.
1761 */
1762 else if (component > 0)
1763 {
1764 /* The test is that alpha/257 (rounded) is less than 255, the first value
1765 * that becomes 255 is 65407.
1766 * NOTE: this must agree with the PNG_DIV257 macro (which must, therefore,
1767 * be exact!) [Could also test reciprocal != 0]
1768 */
1769 if (alpha < 65407)
1770 {
1771 component *= reciprocal;
1772 component += 64; /* round to nearest */
1773 component >>= 7;
1774 }
1775
1776 else
1777 component *= 255;
1778
1779 /* Convert the component to sRGB. */
1780 return (png_byte)PNG_sRGB_FROM_LINEAR(component);
1781 }
1782
1783 else
1784 return 0;
1785}
1786
John Bowler7875d532011-11-07 22:33:49 -06001787static int
1788png_write_image_8bit(png_voidp argument)
1789{
John Bowler4fa96a42011-11-16 16:39:16 -06001790 png_image_write_control *display = png_voidcast(png_image_write_control*,
1791 argument);
John Bowler7875d532011-11-07 22:33:49 -06001792 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06001793 png_structrp png_ptr = image->opaque->png_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001794
John Bowler4fa96a42011-11-16 16:39:16 -06001795 png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
1796 display->first_row);
1797 png_bytep output_row = png_voidcast(png_bytep, display->local_row);
John Bowler7875d532011-11-07 22:33:49 -06001798 png_uint_32 y = image->height;
John Bowler1c25b9b2012-02-29 10:49:28 -06001799 const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
John Bowler7875d532011-11-07 22:33:49 -06001800
1801 if (image->format & PNG_FORMAT_FLAG_ALPHA)
1802 {
1803 png_bytep row_end;
1804 int aindex;
1805
1806 if (image->format & PNG_FORMAT_FLAG_AFIRST)
1807 {
1808 aindex = -1;
1809 ++input_row; /* To point to the first component */
1810 ++output_row;
1811 }
1812
1813 else
1814 aindex = channels;
1815
1816 /* Use row_end in place of a loop counter: */
1817 row_end = output_row + image->width * (channels+1);
1818
1819 while (y-- > 0)
1820 {
1821 png_const_uint_16p in_ptr = input_row;
1822 png_bytep out_ptr = output_row;
1823
John Bowler1c25b9b2012-02-29 10:49:28 -06001824 while (out_ptr < row_end)
John Bowler7875d532011-11-07 22:33:49 -06001825 {
1826 png_uint_16 alpha = in_ptr[aindex];
John Bowler5bc90382012-01-23 22:43:22 -06001827 png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
John Bowler7875d532011-11-07 22:33:49 -06001828 png_uint_32 reciprocal = 0;
1829 int c;
1830
John Bowler5bc90382012-01-23 22:43:22 -06001831 /* Scale and write the alpha channel. */
1832 out_ptr[aindex] = alphabyte;
John Bowler7875d532011-11-07 22:33:49 -06001833
John Bowler5bc90382012-01-23 22:43:22 -06001834 if (alphabyte > 0 && alphabyte < 255)
1835 reciprocal = UNP_RECIPROCAL(alpha);
John Bowler7875d532011-11-07 22:33:49 -06001836
1837 c = channels;
1838 do /* always at least one channel */
John Bowler5bc90382012-01-23 22:43:22 -06001839 *out_ptr++ = png_unpremultiply(*in_ptr++, alpha, reciprocal);
John Bowler7875d532011-11-07 22:33:49 -06001840 while (--c > 0);
1841
1842 /* Skip to next component (skip the intervening alpha channel) */
1843 ++in_ptr;
1844 ++out_ptr;
1845 } /* while out_ptr < row_end */
John Bowler3615d032011-11-08 10:38:09 -06001846
John Bowler4fa96a42011-11-16 16:39:16 -06001847 png_write_row(png_ptr, png_voidcast(png_const_bytep,
1848 display->local_row));
John Bowler3615d032011-11-08 10:38:09 -06001849 input_row += display->row_bytes/(sizeof (png_uint_16));
John Bowler7875d532011-11-07 22:33:49 -06001850 } /* while y */
1851 }
1852
1853 else
1854 {
1855 /* No alpha channel, so the row_end really is the end of the row and it
1856 * is sufficient to loop over the components one by one.
1857 */
1858 png_bytep row_end = output_row + image->width * channels;
1859
1860 while (y-- > 0)
1861 {
1862 png_const_uint_16p in_ptr = input_row;
1863 png_bytep out_ptr = output_row;
1864
1865 while (out_ptr < row_end)
1866 {
1867 png_uint_32 component = *in_ptr++;
1868
1869 component *= 255;
1870 *out_ptr++ = (png_byte)PNG_sRGB_FROM_LINEAR(component);
1871 }
1872
1873 png_write_row(png_ptr, output_row);
1874 input_row += display->row_bytes/(sizeof (png_uint_16));
1875 }
1876 }
1877
1878 return 1;
1879}
1880
John Bowler5bc90382012-01-23 22:43:22 -06001881static void
1882png_image_set_PLTE(png_image_write_control *display)
1883{
1884 const png_imagep image = display->image;
1885 const void *cmap = display->colormap;
1886 const int entries = image->colormap_entries > 256 ? 256 :
1887 (int)image->colormap_entries;
1888
1889 /* NOTE: the caller must check for cmap != NULL and entries != 0 */
1890 const png_uint_32 format = image->format;
1891 const int channels = PNG_IMAGE_SAMPLE_CHANNELS(format);
1892
1893# ifdef PNG_FORMAT_BGR_SUPPORTED
1894 const int afirst = (format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
1895 (format & PNG_FORMAT_FLAG_ALPHA) != 0;
1896# else
1897# define afirst 0
1898# endif
1899
1900# ifdef PNG_FORMAT_BGR_SUPPORTED
1901 const int bgr = (format & PNG_FORMAT_FLAG_BGR) ? 2 : 0;
1902# else
1903# define bgr 0
1904# endif
1905
1906 int i, num_trans;
1907 png_color palette[256];
1908 png_byte tRNS[256];
1909
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001910 memset(tRNS, 255, (sizeof tRNS));
1911 memset(palette, 0, (sizeof palette));
John Bowler5bc90382012-01-23 22:43:22 -06001912
1913 for (i=num_trans=0; i<entries; ++i)
1914 {
1915 /* This gets automatically converted to sRGB with reversal of the
1916 * pre-multiplication if the color-map has an alpha channel.
1917 */
1918 if (format & PNG_FORMAT_FLAG_LINEAR)
1919 {
1920 png_const_uint_16p entry = png_voidcast(png_const_uint_16p, cmap);
John Bowler6f237b62012-03-02 13:13:15 -06001921
John Bowler5bc90382012-01-23 22:43:22 -06001922 entry += i * channels;
1923
1924 if (channels & 1) /* no alpha */
1925 {
1926 if (channels >= 3) /* RGB */
1927 {
1928 palette[i].blue = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
1929 entry[(2 ^ bgr)]);
1930 palette[i].green = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
1931 entry[1]);
1932 palette[i].red = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
1933 entry[bgr]);
1934 }
1935
1936 else /* Gray */
1937 palette[i].blue = palette[i].red = palette[i].green =
1938 (png_byte)PNG_sRGB_FROM_LINEAR(255 * *entry);
1939 }
1940
1941 else /* alpha */
1942 {
1943 png_uint_16 alpha = entry[afirst ? 0 : channels-1];
1944 png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
1945 png_uint_32 reciprocal = 0;
1946
1947 /* Calculate a reciprocal, as in the png_write_image_8bit code above
1948 * this is designed to produce a value scaled to 255*65535 when
1949 * divided by 128 (i.e. asr 7).
1950 */
1951 if (alphabyte > 0 && alphabyte < 255)
1952 reciprocal = (((0xffff*0xff)<<7)+(alpha>>1))/alpha;
1953
1954 tRNS[i] = alphabyte;
1955 if (alphabyte < 255)
1956 num_trans = i+1;
1957
1958 if (channels >= 3) /* RGB */
1959 {
1960 palette[i].blue = png_unpremultiply(entry[afirst + (2 ^ bgr)],
1961 alpha, reciprocal);
1962 palette[i].green = png_unpremultiply(entry[afirst + 1], alpha,
1963 reciprocal);
1964 palette[i].red = png_unpremultiply(entry[afirst + bgr], alpha,
1965 reciprocal);
1966 }
1967
1968 else /* gray */
1969 palette[i].blue = palette[i].red = palette[i].green =
1970 png_unpremultiply(entry[afirst], alpha, reciprocal);
1971 }
1972 }
1973
1974 else /* Color-map has sRGB values */
1975 {
1976 png_const_bytep entry = png_voidcast(png_const_bytep, cmap);
John Bowler6f237b62012-03-02 13:13:15 -06001977
John Bowler5bc90382012-01-23 22:43:22 -06001978 entry += i * channels;
1979
1980 switch (channels)
1981 {
1982 case 4:
1983 tRNS[i] = entry[afirst ? 0 : 3];
1984 if (tRNS[i] < 255)
1985 num_trans = i+1;
1986 /* FALL THROUGH */
1987 case 3:
1988 palette[i].blue = entry[afirst + (2 ^ bgr)];
1989 palette[i].green = entry[afirst + 1];
1990 palette[i].red = entry[afirst + bgr];
1991 break;
1992
1993 case 2:
1994 tRNS[i] = entry[1 ^ afirst];
1995 if (tRNS[i] < 255)
1996 num_trans = i+1;
1997 /* FALL THROUGH */
1998 case 1:
1999 palette[i].blue = palette[i].red = palette[i].green =
2000 entry[afirst];
2001 break;
2002
2003 default:
2004 break;
2005 }
2006 }
2007 }
2008
2009# ifdef afirst
2010# undef afirst
2011# endif
2012# ifdef bgr
2013# undef bgr
2014# endif
2015
2016 png_set_PLTE(image->opaque->png_ptr, image->opaque->info_ptr, palette,
2017 entries);
2018
2019 if (num_trans > 0)
2020 png_set_tRNS(image->opaque->png_ptr, image->opaque->info_ptr, tRNS,
2021 num_trans, NULL);
2022
2023 image->colormap_entries = entries;
2024}
2025
John Bowler7875d532011-11-07 22:33:49 -06002026static int
2027png_image_write_main(png_voidp argument)
2028{
John Bowler4fa96a42011-11-16 16:39:16 -06002029 png_image_write_control *display = png_voidcast(png_image_write_control*,
2030 argument);
John Bowler7875d532011-11-07 22:33:49 -06002031 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06002032 png_structrp png_ptr = image->opaque->png_ptr;
2033 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler7875d532011-11-07 22:33:49 -06002034 png_uint_32 format = image->format;
2035
John Bowler5bc90382012-01-23 22:43:22 -06002036 int colormap = (format & PNG_FORMAT_FLAG_COLORMAP) != 0;
2037 int linear = !colormap && (format & PNG_FORMAT_FLAG_LINEAR) != 0; /* input */
2038 int alpha = !colormap && (format & PNG_FORMAT_FLAG_ALPHA) != 0;
2039 int write_16bit = linear && !colormap && !display->convert_to_8bit;
John Bowler7875d532011-11-07 22:33:49 -06002040
John Bowleraa816c42012-03-16 07:39:49 -05002041# ifdef PNG_BENIGN_ERRORS_SUPPORTED
2042 /* Make sure we error out on any bad situation */
2043 png_set_benign_errors(png_ptr, 0/*error*/);
2044# endif
2045
John Bowler3706d732011-11-21 10:28:06 -06002046 /* Default the 'row_stride' parameter if required. */
2047 if (display->row_stride == 0)
2048 display->row_stride = PNG_IMAGE_ROW_STRIDE(*image);
2049
John Bowler7875d532011-11-07 22:33:49 -06002050 /* Set the required transforms then write the rows in the correct order. */
John Bowler04336ba2012-01-16 07:48:36 -06002051 if (format & PNG_FORMAT_FLAG_COLORMAP)
John Bowler5bc90382012-01-23 22:43:22 -06002052 {
2053 if (display->colormap != NULL && image->colormap_entries > 0)
2054 {
2055 png_uint_32 entries = image->colormap_entries;
2056
2057 png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
2058 entries > 16 ? 8 : (entries > 4 ? 4 : (entries > 2 ? 2 : 1)),
2059 PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
2060 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2061
2062 png_image_set_PLTE(display);
2063 }
2064
2065 else
2066 png_error(image->opaque->png_ptr,
2067 "no color-map for color-mapped image");
2068 }
John Bowler04336ba2012-01-16 07:48:36 -06002069
2070 else
2071 png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
2072 write_16bit ? 16 : 8,
2073 ((format & PNG_FORMAT_FLAG_COLOR) ? PNG_COLOR_MASK_COLOR : 0) +
2074 ((format & PNG_FORMAT_FLAG_ALPHA) ? PNG_COLOR_MASK_ALPHA : 0),
2075 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
John Bowler7875d532011-11-07 22:33:49 -06002076
2077 /* Counter-intuitively the data transformations must be called *after*
2078 * png_write_info, not before as in the read code, but the 'set' functions
2079 * must still be called before. Just set the color space information, never
2080 * write an interlaced image.
2081 */
John Bowler5bc90382012-01-23 22:43:22 -06002082
John Bowler7875d532011-11-07 22:33:49 -06002083 if (write_16bit)
2084 {
2085 /* The gamma here is 1.0 (linear) and the cHRM chunk matches sRGB. */
2086 png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_LINEAR);
John Bowlerdd819152011-11-08 14:29:45 -06002087
2088 if (!(image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB))
2089 png_set_cHRM_fixed(png_ptr, info_ptr,
2090 /* color x y */
2091 /* white */ 31270, 32900,
2092 /* red */ 64000, 33000,
2093 /* green */ 30000, 60000,
2094 /* blue */ 15000, 6000
2095 );
John Bowler7875d532011-11-07 22:33:49 -06002096 }
2097
John Bowlerdd819152011-11-08 14:29:45 -06002098 else if (!(image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB))
John Bowler7875d532011-11-07 22:33:49 -06002099 png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL);
2100
John Bowlerdd819152011-11-08 14:29:45 -06002101 /* Else writing an 8-bit file and the *colors* aren't sRGB, but the 8-bit
2102 * space must still be gamma encoded.
2103 */
2104 else
2105 png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE);
2106
John Bowler7875d532011-11-07 22:33:49 -06002107 /* Write the file header. */
2108 png_write_info(png_ptr, info_ptr);
2109
2110 /* Now set up the data transformations (*after* the header is written),
2111 * remove the handled transformations from the 'format' flags for checking.
John Bowlere6fb6912011-11-08 21:35:16 -06002112 *
2113 * First check for a little endian system if writing 16 bit files.
John Bowler7875d532011-11-07 22:33:49 -06002114 */
John Bowler7875d532011-11-07 22:33:49 -06002115 if (write_16bit)
2116 {
2117 PNG_CONST png_uint_16 le = 0x0001;
2118
2119 if (*(png_const_bytep)&le)
2120 png_set_swap(png_ptr);
2121 }
2122
2123# ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
2124 if (format & PNG_FORMAT_FLAG_BGR)
2125 {
John Bowler5bc90382012-01-23 22:43:22 -06002126 if (!colormap && (format & PNG_FORMAT_FLAG_COLOR) != 0)
John Bowlere6fb6912011-11-08 21:35:16 -06002127 png_set_bgr(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06002128 format &= ~PNG_FORMAT_FLAG_BGR;
2129 }
2130# endif
2131
2132# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
2133 if (format & PNG_FORMAT_FLAG_AFIRST)
2134 {
John Bowler5bc90382012-01-23 22:43:22 -06002135 if (!colormap && (format & PNG_FORMAT_FLAG_ALPHA) != 0)
John Bowlere6fb6912011-11-08 21:35:16 -06002136 png_set_swap_alpha(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06002137 format &= ~PNG_FORMAT_FLAG_AFIRST;
2138 }
2139# endif
2140
John Bowler5bc90382012-01-23 22:43:22 -06002141 /* If there are 16 or fewer color-map entries we wrote a lower bit depth
2142 * above, but the application data is still byte packed.
2143 */
2144 if (colormap && image->colormap_entries <= 16)
2145 png_set_packing(png_ptr);
2146
John Bowlere6fb6912011-11-08 21:35:16 -06002147 /* That should have handled all (both) the transforms. */
John Bowler89c2f842011-11-16 12:04:39 -06002148 if ((format & ~(png_uint_32)(PNG_FORMAT_FLAG_COLOR | PNG_FORMAT_FLAG_LINEAR |
John Bowler5bc90382012-01-23 22:43:22 -06002149 PNG_FORMAT_FLAG_ALPHA | PNG_FORMAT_FLAG_COLORMAP)) != 0)
John Bowler7875d532011-11-07 22:33:49 -06002150 png_error(png_ptr, "png_write_image: unsupported transformation");
2151
2152 {
John Bowler4fa96a42011-11-16 16:39:16 -06002153 png_const_bytep row = png_voidcast(png_const_bytep, display->buffer);
John Bowler7875d532011-11-07 22:33:49 -06002154 ptrdiff_t row_bytes = display->row_stride;
2155
2156 if (linear)
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05002157 row_bytes *= (sizeof (png_uint_16));
John Bowler7875d532011-11-07 22:33:49 -06002158
2159 if (row_bytes < 0)
2160 row += (image->height-1) * (-row_bytes);
2161
2162 display->first_row = row;
2163 display->row_bytes = row_bytes;
2164 }
2165
John Bowlerdee75772012-03-01 18:55:54 -06002166 /* Apply 'fast' options if the flag is set. */
2167 if ((image->flags & PNG_IMAGE_FLAG_FAST) != 0)
2168 {
2169 png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_NO_FILTERS);
2170 /* NOTE: determined by experiment using pngstest, this reflects some
2171 * balance between the time to write the image once and the time to read
2172 * it about 50 times. The speed-up in pngstest was about 10-20% of the
2173 * total (user) time on a heavily loaded system.
2174 */
2175 png_set_compression_level(png_ptr, 3);
2176 }
2177
John Bowler7875d532011-11-07 22:33:49 -06002178 /* Check for the cases that currently require a pre-transform on the row
2179 * before it is written. This only applies when the input is 16-bit and
2180 * either there is an alpha channel or it is converted to 8-bit.
2181 */
John Bowler5bc90382012-01-23 22:43:22 -06002182 if ((linear && alpha) || (!colormap && display->convert_to_8bit))
John Bowler7875d532011-11-07 22:33:49 -06002183 {
John Bowler4fa96a42011-11-16 16:39:16 -06002184 png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
2185 png_get_rowbytes(png_ptr, info_ptr)));
John Bowler7875d532011-11-07 22:33:49 -06002186 int result;
2187
2188 display->local_row = row;
2189 if (write_16bit)
2190 result = png_safe_execute(image, png_write_image_16bit, display);
2191 else
2192 result = png_safe_execute(image, png_write_image_8bit, display);
2193 display->local_row = NULL;
2194
2195 png_free(png_ptr, row);
2196
2197 /* Skip the 'write_end' on error: */
2198 if (!result)
2199 return 0;
2200 }
2201
2202 /* Otherwise this is the case where the input is in a format currently
2203 * supported by the rest of the libpng write code; call it directly.
2204 */
2205 else
2206 {
John Bowler4fa96a42011-11-16 16:39:16 -06002207 png_const_bytep row = png_voidcast(png_const_bytep, display->first_row);
John Bowler7875d532011-11-07 22:33:49 -06002208 ptrdiff_t row_bytes = display->row_bytes;
2209 png_uint_32 y = image->height;
2210
2211 while (y-- > 0)
2212 {
2213 png_write_row(png_ptr, row);
2214 row += row_bytes;
2215 }
2216 }
2217
2218 png_write_end(png_ptr, info_ptr);
2219 return 1;
2220}
2221
2222int PNGAPI
John Bowlerdd819152011-11-08 14:29:45 -06002223png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit,
John Bowler5bc90382012-01-23 22:43:22 -06002224 const void *buffer, png_int_32 row_stride, const void *colormap)
John Bowler7875d532011-11-07 22:33:49 -06002225{
2226 /* Write the image to the given (FILE*). */
John Bowler5bc90382012-01-23 22:43:22 -06002227 if (image != NULL || image->version != PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06002228 {
2229 if (file != NULL)
2230 {
2231 if (png_image_write_init(image))
2232 {
2233 png_image_write_control display;
2234 int result;
2235
2236 /* This is slightly evil, but png_init_io doesn't do anything other
2237 * than this and we haven't changed the standard IO functions so
2238 * this saves a 'safe' function.
2239 */
2240 image->opaque->png_ptr->io_ptr = file;
2241
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05002242 png_memset(&display, 0, (sizeof display));
John Bowler7875d532011-11-07 22:33:49 -06002243 display.image = image;
2244 display.buffer = buffer;
2245 display.row_stride = row_stride;
John Bowler5bc90382012-01-23 22:43:22 -06002246 display.colormap = colormap;
John Bowler7875d532011-11-07 22:33:49 -06002247 display.convert_to_8bit = convert_to_8bit;
2248
2249 result = png_safe_execute(image, png_image_write_main, &display);
2250 png_image_free(image);
2251 return result;
2252 }
2253
2254 else
2255 return 0;
2256 }
2257
2258 else
2259 return png_image_error(image,
2260 "png_image_write_to_stdio: invalid argument");
2261 }
2262
2263 else
2264 return 0;
2265}
2266
2267int PNGAPI
John Bowlerdd819152011-11-08 14:29:45 -06002268png_image_write_to_file(png_imagep image, const char *file_name,
John Bowler5bc90382012-01-23 22:43:22 -06002269 int convert_to_8bit, const void *buffer, png_int_32 row_stride,
2270 const void *colormap)
John Bowler7875d532011-11-07 22:33:49 -06002271{
2272 /* Write the image to the named file. */
John Bowler5bc90382012-01-23 22:43:22 -06002273 if (image != NULL || image->version != PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06002274 {
2275 if (file_name != NULL)
2276 {
2277 FILE *fp = fopen(file_name, "wb");
2278
2279 if (fp != NULL)
2280 {
John Bowlerdd819152011-11-08 14:29:45 -06002281 if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer,
John Bowler5bc90382012-01-23 22:43:22 -06002282 row_stride, colormap))
John Bowler7875d532011-11-07 22:33:49 -06002283 {
John Bowlerdd819152011-11-08 14:29:45 -06002284 int error; /* from fflush/fclose */
John Bowler7875d532011-11-07 22:33:49 -06002285
John Bowlerdd819152011-11-08 14:29:45 -06002286 /* Make sure the file is flushed correctly. */
2287 if (fflush(fp) == 0 && ferror(fp) == 0)
John Bowler7875d532011-11-07 22:33:49 -06002288 {
John Bowlerdd819152011-11-08 14:29:45 -06002289 if (fclose(fp) == 0)
2290 return 1;
John Bowler7875d532011-11-07 22:33:49 -06002291
John Bowlerdd819152011-11-08 14:29:45 -06002292 error = errno; /* from fclose */
John Bowler7875d532011-11-07 22:33:49 -06002293 }
2294
John Bowlerdd819152011-11-08 14:29:45 -06002295 else
2296 {
2297 error = errno; /* from fflush or ferror */
2298 (void)fclose(fp);
2299 }
2300
2301 (void)remove(file_name);
2302 /* The image has already been cleaned up; this is just used to
2303 * set the error (because the original write succeeded).
2304 */
2305 return png_image_error(image, strerror(error));
John Bowler7875d532011-11-07 22:33:49 -06002306 }
2307
2308 else
2309 {
2310 /* Clean up: just the opened file. */
2311 (void)fclose(fp);
John Bowlerdd819152011-11-08 14:29:45 -06002312 (void)remove(file_name);
John Bowler7875d532011-11-07 22:33:49 -06002313 return 0;
2314 }
2315 }
2316
2317 else
2318 return png_image_error(image, strerror(errno));
2319 }
2320
2321 else
2322 return png_image_error(image,
2323 "png_image_write_to_file: invalid argument");
2324 }
2325
2326 else
2327 return 0;
2328}
2329#endif /* PNG_STDIO_SUPPORTED */
2330#endif /* SIMPLIFIED_WRITE */
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -06002331#endif /* PNG_WRITE_SUPPORTED */