blob: 098a6823027a9ca43c6570064417c10977500832 [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
John Bowlere9567512012-08-15 22:53:00 -050021#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
22/* Write out all the unknown chunks for the current given location */
23static void
24write_unknown_chunks(png_structrp png_ptr, png_const_inforp info_ptr,
25 unsigned int where)
26{
27 if (info_ptr->unknown_chunks_num)
28 {
29 png_const_unknown_chunkp up;
30
31 png_debug(5, "writing extra chunks");
32
33 for (up = info_ptr->unknown_chunks;
34 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
35 ++up)
36 if (up->location & where)
37 {
38 int keep = png_handle_as_unknown(png_ptr, up->name);
39
40 /* NOTE: this code is radically different from the read side in the
41 * matter of handling an ancilliary unknown chunk. In the read side
42 * the default behavior is to discard it, in the code below the default
43 * behavior is to write it. Critical chunks are, however, only
44 * written if explicitly listed or if the default is set to write all
45 * unknown chunks.
46 *
47 * The default handling is also slightly weird - it is not possible to
48 * stop the writing of all unsafe-to-copy chunks!
49 *
50 * TODO: REVIEW: this would seem to be a bug.
51 */
52 if (keep != PNG_HANDLE_CHUNK_NEVER &&
53 ((up->name[3] & 0x20) /* safe-to-copy overrides everything */ ||
54 keep == PNG_HANDLE_CHUNK_ALWAYS ||
55 (keep == PNG_HANDLE_CHUNK_AS_DEFAULT &&
56 png_ptr->unknown_default == PNG_HANDLE_CHUNK_ALWAYS)))
57 {
58 /* TODO: review, what is wrong with a zero length unknown chunk? */
59 if (up->size == 0)
60 png_warning(png_ptr, "Writing zero-length unknown chunk");
61
62 png_write_chunk(png_ptr, up->name, up->data, up->size);
63 }
64 }
65 }
66}
67#endif /* PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED */
68
Andreas Dilger47a0c421997-05-16 02:46:07 -050069/* Writes all the PNG information. This is the suggested way to use the
70 * library. If you have a new chunk to add, make a function to write it,
71 * and put it in the correct location here. If you want the chunk written
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050072 * after the image data, put it in png_write_end(). I strongly encourage
Andreas Dilger47a0c421997-05-16 02:46:07 -050073 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
74 * the chunk, as that will keep the code from breaking if you want to just
75 * write a plain PNG file. If you have long comments, I suggest writing
76 * them in png_write_end(), and compressing them.
77 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050078void PNGAPI
John Bowlerb11b31a2012-03-21 07:55:46 -050079png_write_info_before_PLTE(png_structrp png_ptr, png_const_inforp info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050080{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050081 png_debug(1, "in png_write_info_before_PLTE");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050082
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -060083 if (png_ptr == NULL || info_ptr == NULL)
84 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050085
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060086 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
87 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -050088 /* Write PNG signature */
89 png_write_sig(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050090
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050091#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060092 if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) && \
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060093 (png_ptr->mng_features_permitted))
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060094 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050095 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -050096 png_ptr->mng_features_permitted = 0;
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060097 }
98#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050099
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500100 /* Write IHDR information. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600101 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600102 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
103 info_ptr->filter_type,
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500104#ifdef PNG_WRITE_INTERLACING_SUPPORTED
John Bowlere9567512012-08-15 22:53:00 -0500105 info_ptr->interlace_type
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600106#else
John Bowlere9567512012-08-15 22:53:00 -0500107 0
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600108#endif
John Bowlere9567512012-08-15 22:53:00 -0500109 );
110
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500111 /* The rest of these check to see if the valid field has the appropriate
112 * flag set, and if it does, writes the chunk.
John Bowlerb11b31a2012-03-21 07:55:46 -0500113 *
114 * 1.6.0: COLORSPACE support controls the writing of these chunks too, and
115 * the chunks will be written if the WRITE routine is there and information
116 * is available in the COLORSPACE. (See png_colorspace_sync_info in png.c
117 * for where the valid flags get set.)
118 *
119 * Under certain circumstances the colorspace can be invalidated without
120 * syncing the info_struct 'valid' flags; this happens if libpng detects and
121 * error and calls png_error while the color space is being set, yet the
122 * application continues writing the PNG. So check the 'invalid' flag here
123 * too.
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500124 */
John Bowlerb11b31a2012-03-21 07:55:46 -0500125#ifdef PNG_GAMMA_SUPPORTED
126# ifdef PNG_WRITE_gAMA_SUPPORTED
127 if (!(info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) &&
128 (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_gAMA) &&
129 (info_ptr->valid & PNG_INFO_gAMA))
130 png_write_gAMA_fixed(png_ptr, info_ptr->colorspace.gamma);
131# endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600132#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500133
John Bowlerb11b31a2012-03-21 07:55:46 -0500134#ifdef PNG_COLORSPACE_SUPPORTED
John Bowler921648a2012-03-28 23:36:12 -0500135 /* Write only one of sRGB or an ICC profile. If a profile was supplied
136 * and it matches one of the known sRGB ones issue a warning.
John Bowlerb11b31a2012-03-21 07:55:46 -0500137 */
John Bowler921648a2012-03-28 23:36:12 -0500138# ifdef PNG_WRITE_iCCP_SUPPORTED
139 if (!(info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) &&
140 (info_ptr->valid & PNG_INFO_iCCP))
141 {
142# ifdef PNG_WRITE_sRGB_SUPPORTED
143 if (info_ptr->valid & PNG_INFO_sRGB)
144 png_app_warning(png_ptr,
145 "profile matches sRGB but writing iCCP instead");
146# endif
147
148 png_write_iCCP(png_ptr, info_ptr->iccp_name,
149 info_ptr->iccp_profile);
150 }
151# ifdef PNG_WRITE_sRGB_SUPPORTED
152 else
153# endif
154# endif
155
John Bowlerb11b31a2012-03-21 07:55:46 -0500156# ifdef PNG_WRITE_sRGB_SUPPORTED
157 if (!(info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) &&
158 (info_ptr->valid & PNG_INFO_sRGB))
159 png_write_sRGB(png_ptr, info_ptr->colorspace.rendering_intent);
John Bowlerb11b31a2012-03-21 07:55:46 -0500160# endif /* WRITE_sRGB */
John Bowlerb11b31a2012-03-21 07:55:46 -0500161#endif /* COLORSPACE */
162
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500163#ifdef PNG_WRITE_sBIT_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600164 if (info_ptr->valid & PNG_INFO_sBIT)
165 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500166#endif
John Bowlerb11b31a2012-03-21 07:55:46 -0500167
168#ifdef PNG_COLORSPACE_SUPPORTED
169# ifdef PNG_WRITE_cHRM_SUPPORTED
170 if (!(info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) &&
171 (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) &&
172 (info_ptr->valid & PNG_INFO_cHRM))
173 png_write_cHRM_fixed(png_ptr, &info_ptr->colorspace.end_points_xy);
174# endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600175#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500176
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500177#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
John Bowlere9567512012-08-15 22:53:00 -0500178 write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_IHDR);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500179#endif
John Bowlere9567512012-08-15 22:53:00 -0500180
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600181 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
182 }
183}
184
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500185void PNGAPI
John Bowlerb11b31a2012-03-21 07:55:46 -0500186png_write_info(png_structrp png_ptr, png_const_inforp info_ptr)
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600187{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600188#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600189 int i;
190#endif
191
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500192 png_debug(1, "in png_write_info");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600193
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600194 if (png_ptr == NULL || info_ptr == NULL)
195 return;
196
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600197 png_write_info_before_PLTE(png_ptr, info_ptr);
198
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600199 if (info_ptr->valid & PNG_INFO_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500200 png_write_PLTE(png_ptr, info_ptr->palette,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600201 (png_uint_32)info_ptr->num_palette);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500202
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600203 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600204 png_error(png_ptr, "Valid palette required for paletted images");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600205
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500206#ifdef PNG_WRITE_tRNS_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600207 if (info_ptr->valid & PNG_INFO_tRNS)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500208 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500209#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500210 /* Invert the alpha channel (in tRNS) */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500211 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600212 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500213 {
214 int j;
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500215 for (j = 0; j<(int)info_ptr->num_trans; j++)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500216 info_ptr->trans_alpha[j] =
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -0500217 (png_byte)(255 - info_ptr->trans_alpha[j]);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500218 }
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600219#endif
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500220 png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600221 info_ptr->num_trans, info_ptr->color_type);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500222 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500223#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500224#ifdef PNG_WRITE_bKGD_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600225 if (info_ptr->valid & PNG_INFO_bKGD)
226 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500227#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500228
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500229#ifdef PNG_WRITE_hIST_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600230 if (info_ptr->valid & PNG_INFO_hIST)
231 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500232#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500233
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500234#ifdef PNG_WRITE_oFFs_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600235 if (info_ptr->valid & PNG_INFO_oFFs)
236 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600237 info_ptr->offset_unit_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500238#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500239
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500240#ifdef PNG_WRITE_pCAL_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500241 if (info_ptr->valid & PNG_INFO_pCAL)
242 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600243 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
244 info_ptr->pcal_units, info_ptr->pcal_params);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500245#endif
Glenn Randers-Pehrson59020592009-05-18 10:52:12 -0500246
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500247#ifdef PNG_WRITE_sCAL_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500248 if (info_ptr->valid & PNG_INFO_sCAL)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600249 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600250 info_ptr->scal_s_width, info_ptr->scal_s_height);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500251#endif /* sCAL */
252
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500253#ifdef PNG_WRITE_pHYs_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500254 if (info_ptr->valid & PNG_INFO_pHYs)
255 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600256 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500257#endif /* pHYs */
258
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500259#ifdef PNG_WRITE_tIME_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600260 if (info_ptr->valid & PNG_INFO_tIME)
Guy Schalnate5a37791996-06-05 15:50:50 -0500261 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600262 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600263 png_ptr->mode |= PNG_WROTE_tIME;
Guy Schalnate5a37791996-06-05 15:50:50 -0500264 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500265#endif /* tIME */
266
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500267#ifdef PNG_WRITE_sPLT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600268 if (info_ptr->valid & PNG_INFO_sPLT)
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600269 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
270 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500271#endif /* sPLT */
272
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500273#ifdef PNG_WRITE_TEXT_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500274 /* Check to see if we need to write text chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500275 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500276 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500277 png_debug2(2, "Writing header text chunk %d, type %d", i,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600278 info_ptr->text[i].compression);
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500279 /* An internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600280 if (info_ptr->text[i].compression > 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600281 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500282#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600283 /* Write international chunk */
284 png_write_iTXt(png_ptr,
285 info_ptr->text[i].compression,
286 info_ptr->text[i].key,
287 info_ptr->text[i].lang,
288 info_ptr->text[i].lang_key,
289 info_ptr->text[i].text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600290#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600291 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600292#endif
293 /* Mark this chunk as written */
294 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
295 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500296
Andreas Dilger47a0c421997-05-16 02:46:07 -0500297 /* If we want a compressed text chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600298 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500299 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500300#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500301 /* Write compressed chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500302 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600303 info_ptr->text[i].text, 0,
304 info_ptr->text[i].compression);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500305#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600306 png_warning(png_ptr, "Unable to write compressed text");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500307#endif
308 /* Mark this chunk as written */
309 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
310 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500311
Andreas Dilger47a0c421997-05-16 02:46:07 -0500312 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
313 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500314#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500315 /* Write uncompressed chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500316 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600317 info_ptr->text[i].text,
318 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500319 /* Mark this chunk as written */
320 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500321#else
322 /* Can't get here */
323 png_warning(png_ptr, "Unable to write uncompressed text");
324#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500325 }
326 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500327#endif /* tEXt */
328
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500329#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
John Bowlere9567512012-08-15 22:53:00 -0500330 write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_PLTE);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600331#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500332}
Guy Schalnat0d580581995-07-20 02:43:20 -0500333
Andreas Dilger47a0c421997-05-16 02:46:07 -0500334/* Writes the end of the PNG file. If you don't want to write comments or
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600335 * time information, you can pass NULL for info. If you already wrote these
336 * in png_write_info(), do not write them again here. If you have long
337 * comments, I suggest writing them here, and compressing them.
338 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500339void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600340png_write_end(png_structrp png_ptr, png_inforp info_ptr)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500341{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500342 png_debug(1, "in png_write_end");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500343
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600344 if (png_ptr == NULL)
345 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500346
Andreas Dilger47a0c421997-05-16 02:46:07 -0500347 if (!(png_ptr->mode & PNG_HAVE_IDAT))
348 png_error(png_ptr, "No IDATs written into file");
349
Glenn Randers-Pehrsoneeb1bb62012-03-02 22:10:15 -0600350#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
351 if (png_ptr->num_palette_max > png_ptr->num_palette)
Glenn Randers-Pehrson945cb1f2012-03-10 08:48:04 -0600352 png_benign_error(png_ptr, "Wrote palette index exceeding num_palette");
Glenn Randers-Pehrsoneeb1bb62012-03-02 22:10:15 -0600353#endif
354
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500355 /* See if user wants us to write information chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500356 if (info_ptr != NULL)
357 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500358#ifdef PNG_WRITE_TEXT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500359 int i; /* local index variable */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600360#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500361#ifdef PNG_WRITE_tIME_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500362 /* Check to see if user has supplied a time chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600363 if ((info_ptr->valid & PNG_INFO_tIME) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600364 !(png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500365 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500366
Andreas Dilger47a0c421997-05-16 02:46:07 -0500367#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500368#ifdef PNG_WRITE_TEXT_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500369 /* Loop through comment chunks */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600370 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500371 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500372 png_debug2(2, "Writing trailer text chunk %d, type %d", i,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500373 info_ptr->text[i].compression);
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500374 /* An internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600375 if (info_ptr->text[i].compression > 0)
376 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500377#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500378 /* Write international chunk */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500379 png_write_iTXt(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600380 info_ptr->text[i].compression,
381 info_ptr->text[i].key,
382 info_ptr->text[i].lang,
383 info_ptr->text[i].lang_key,
384 info_ptr->text[i].text);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600385#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500386 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600387#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500388 /* Mark this chunk as written */
389 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600390 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500391
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600392 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
Guy Schalnat0d580581995-07-20 02:43:20 -0500393 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500394#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500395 /* Write compressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600396 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600397 info_ptr->text[i].text, 0,
398 info_ptr->text[i].compression);
Guy Schalnate5a37791996-06-05 15:50:50 -0500399#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600400 png_warning(png_ptr, "Unable to write compressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500401#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500402 /* Mark this chunk as written */
403 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500404 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500405
Andreas Dilger47a0c421997-05-16 02:46:07 -0500406 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500407 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500408#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500409 /* Write uncompressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600410 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600411 info_ptr->text[i].text, 0);
Guy Schalnate5a37791996-06-05 15:50:50 -0500412#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600413 png_warning(png_ptr, "Unable to write uncompressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500414#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500415
Andreas Dilger47a0c421997-05-16 02:46:07 -0500416 /* Mark this chunk as written */
417 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500418 }
419 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600420#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500421#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
John Bowlere9567512012-08-15 22:53:00 -0500422 write_unknown_chunks(png_ptr, info_ptr, PNG_AFTER_IDAT);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600423#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500424 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500425
426 png_ptr->mode |= PNG_AFTER_IDAT;
427
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500428 /* Write end of PNG file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500429 png_write_IEND(png_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500430 /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
431 * and restored again in libpng-1.2.30, may cause some applications that
432 * do not set png_ptr->output_flush_fn to crash. If your application
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600433 * experiences a problem, please try building libpng with
434 * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500435 * png-mng-implement at lists.sf.net .
436 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500437#ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600438# ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
Glenn Randers-Pehrson32fc5ce2000-07-24 06:34:14 -0500439 png_flush(png_ptr);
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600440# endif
Glenn Randers-Pehrsondbed41f2008-08-19 18:20:52 -0500441#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500442}
443
Glenn Randers-Pehrson65d235a2009-11-02 11:32:30 -0600444#ifdef PNG_CONVERT_tIME_SUPPORTED
Glenn Randers-Pehrson99106de2009-11-01 16:26:14 -0600445/* "tm" structure is not supported on WindowsCE */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500446void PNGAPI
John Bowlerbaeb6d12011-11-26 18:21:02 -0600447png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm * ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500448{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500449 png_debug(1, "in png_convert_from_struct_tm");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500450
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600451 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
452 ptime->month = (png_byte)(ttime->tm_mon + 1);
453 ptime->day = (png_byte)ttime->tm_mday;
454 ptime->hour = (png_byte)ttime->tm_hour;
455 ptime->minute = (png_byte)ttime->tm_min;
456 ptime->second = (png_byte)ttime->tm_sec;
Guy Schalnat0d580581995-07-20 02:43:20 -0500457}
458
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500459void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600460png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500461{
462 struct tm *tbuf;
463
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500464 png_debug(1, "in png_convert_from_time_t");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500465
Guy Schalnat0d580581995-07-20 02:43:20 -0500466 tbuf = gmtime(&ttime);
467 png_convert_from_struct_tm(ptime, tbuf);
468}
Guy Schalnat6d764711995-12-19 03:22:19 -0600469#endif
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500470
Andreas Dilger47a0c421997-05-16 02:46:07 -0500471/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500472PNG_FUNCTION(png_structp,PNGAPI
473png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
474 png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500475{
John Bowlerd332c672011-12-21 17:36:12 -0600476#ifndef PNG_USER_MEM_SUPPORTED
John Bowler5d567862011-12-24 09:12:00 -0600477 png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
John Bowlerd332c672011-12-21 17:36:12 -0600478 error_fn, warn_fn, NULL, NULL, NULL);
479#else
480 return png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
481 warn_fn, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500482}
483
484/* Alternate initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500485PNG_FUNCTION(png_structp,PNGAPI
486png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600487 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500488 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500489{
John Bowler5d567862011-12-24 09:12:00 -0600490 png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
John Bowlerd332c672011-12-21 17:36:12 -0600491 error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500492#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500493
John Bowler42a2b552012-03-05 20:57:40 -0600494 /* Set the zlib control values to defaults; they can be overridden by the
495 * application after the struct has been created.
496 */
John Bowlerb5d00512012-03-09 09:15:18 -0600497 png_ptr->zbuffer_size = PNG_ZBUF_SIZE;
498
John Bowler42a2b552012-03-05 20:57:40 -0600499 png_ptr->zlib_strategy = Z_FILTERED; /* may be overridden if no filters */
500 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
501 png_ptr->zlib_mem_level = 8;
502 png_ptr->zlib_window_bits = 15;
503 png_ptr->zlib_method = 8;
504
505#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
506 png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY;
507 png_ptr->zlib_text_level = Z_DEFAULT_COMPRESSION;
508 png_ptr->zlib_text_mem_level = 8;
509 png_ptr->zlib_text_window_bits = 15;
510 png_ptr->zlib_text_method = 8;
511#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
512
John Bowleraa816c42012-03-16 07:39:49 -0500513 /* This is a highly dubious configuration option; by default it is off, but
514 * it may be appropriate for private builds that are testing extensions not
515 * conformant to the current specification, or of applications that must not
516 * fail to write at all costs!
517 */
518# ifdef PNG_BENIGN_WRITE_ERRORS_SUPPORTED
519 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
John Bowler921648a2012-03-28 23:36:12 -0500520 /* In stable builds only warn if an application error can be completely
521 * handled.
522 */
523# endif
524
525 /* App warnings are warnings in release (or release candidate) builds but
526 * are errors during development.
527 */
528# if PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC
529 png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
John Bowleraa816c42012-03-16 07:39:49 -0500530# endif
531
John Bowlerd332c672011-12-21 17:36:12 -0600532 if (png_ptr != NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500533 {
John Bowlerd332c672011-12-21 17:36:12 -0600534 /* TODO: delay this, it can be done in png_init_io() (if the app doesn't
535 * do it itself) avoiding setting the default function if it is not
536 * required.
537 */
538 png_set_write_fn(png_ptr, NULL, NULL, NULL);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500539 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500540
John Bowlerd332c672011-12-21 17:36:12 -0600541 return png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500542}
543
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500544
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600545/* Write a few rows of image data. If the image is interlaced,
546 * either you will have to write the 7 sub images, or, if you
547 * have called png_set_interlace_handling(), you will have to
548 * "write" the image seven times.
549 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500550void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600551png_write_rows(png_structrp png_ptr, png_bytepp row,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600552 png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500553{
554 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600555 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500556
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500557 png_debug(1, "in png_write_rows");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600558
559 if (png_ptr == NULL)
560 return;
561
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500562 /* Loop through the rows */
Guy Schalnat0d580581995-07-20 02:43:20 -0500563 for (i = 0, rp = row; i < num_rows; i++, rp++)
564 {
565 png_write_row(png_ptr, *rp);
566 }
567}
568
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600569/* Write the image. You only need to call this function once, even
570 * if you are writing an interlaced image.
571 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500572void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600573png_write_image(png_structrp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500574{
575 png_uint_32 i; /* row index */
576 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600577 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500578
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600579 if (png_ptr == NULL)
580 return;
581
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500582 png_debug(1, "in png_write_image");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500583
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500584#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500585 /* Initialize interlace handling. If image is not interlaced,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500586 * this will set pass to 1
587 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500588 num_pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600589#else
590 num_pass = 1;
591#endif
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500592 /* Loop through passes */
Guy Schalnat0d580581995-07-20 02:43:20 -0500593 for (pass = 0; pass < num_pass; pass++)
594 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500595 /* Loop through image */
Guy Schalnat0d580581995-07-20 02:43:20 -0500596 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
597 {
598 png_write_row(png_ptr, *rp);
599 }
600 }
601}
602
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500603/* Called by user to write a row of image data */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500604void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600605png_write_row(png_structrp png_ptr, png_const_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500606{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500607 /* 1.5.6: moved from png_struct to be a local structure: */
608 png_row_info row_info;
609
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600610 if (png_ptr == NULL)
611 return;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500612
Glenn Randers-Pehrson632a84e2010-03-09 22:28:33 -0600613 png_debug2(1, "in png_write_row (row %u, pass %d)",
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600614 png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600615
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500616 /* Initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600617 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500618 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500619 /* Make sure we wrote the header info */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500620 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
621 png_error(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600622 "png_write_info was never called before png_write_row");
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600623
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500624 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500625#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500626 if (png_ptr->transformations & PNG_INVERT_MONO)
627 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500628#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500629
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500630#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500631 if (png_ptr->transformations & PNG_FILLER)
632 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500633#endif
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600634#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
635 defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500636 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600637 png_warning(png_ptr,
638 "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500639#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500640
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500641#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500642 if (png_ptr->transformations & PNG_PACK)
643 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500644#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500645
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500646#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500647 if (png_ptr->transformations & PNG_SHIFT)
648 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500649#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500650
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500651#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500652 if (png_ptr->transformations & PNG_BGR)
653 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500654#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500655
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500656#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500657 if (png_ptr->transformations & PNG_SWAP_BYTES)
658 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500659#endif
660
Guy Schalnat0d580581995-07-20 02:43:20 -0500661 png_write_start_row(png_ptr);
662 }
663
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500664#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500665 /* If interlaced and not interested in row, return */
Guy Schalnat0d580581995-07-20 02:43:20 -0500666 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
667 {
668 switch (png_ptr->pass)
669 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600670 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600671 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500672 {
673 png_write_finish_row(png_ptr);
674 return;
675 }
676 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500677
Guy Schalnat0d580581995-07-20 02:43:20 -0500678 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600679 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500680 {
681 png_write_finish_row(png_ptr);
682 return;
683 }
684 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500685
Guy Schalnat0d580581995-07-20 02:43:20 -0500686 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600687 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500688 {
689 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600690 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500691 }
692 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500693
Guy Schalnat0d580581995-07-20 02:43:20 -0500694 case 3:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600695 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
Guy Schalnat0d580581995-07-20 02:43:20 -0500696 {
697 png_write_finish_row(png_ptr);
698 return;
699 }
700 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500701
Guy Schalnat0d580581995-07-20 02:43:20 -0500702 case 4:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600703 if ((png_ptr->row_number & 0x03) != 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500704 {
705 png_write_finish_row(png_ptr);
706 return;
707 }
708 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500709
Guy Schalnat0d580581995-07-20 02:43:20 -0500710 case 5:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600711 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500712 {
713 png_write_finish_row(png_ptr);
714 return;
715 }
716 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500717
Guy Schalnat0d580581995-07-20 02:43:20 -0500718 case 6:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600719 if (!(png_ptr->row_number & 0x01))
Guy Schalnat0d580581995-07-20 02:43:20 -0500720 {
721 png_write_finish_row(png_ptr);
722 return;
723 }
724 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500725
726 default: /* error: ignore it */
727 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500728 }
729 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600730#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500731
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500732 /* Set up row info for transformations */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500733 row_info.color_type = png_ptr->color_type;
734 row_info.width = png_ptr->usr_width;
735 row_info.channels = png_ptr->usr_channels;
736 row_info.bit_depth = png_ptr->usr_bit_depth;
737 row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels);
738 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600739
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500740 png_debug1(3, "row_info->color_type = %d", row_info.color_type);
741 png_debug1(3, "row_info->width = %u", row_info.width);
742 png_debug1(3, "row_info->channels = %d", row_info.channels);
743 png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth);
744 png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth);
745 png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500746
747 /* Copy user's row into buffer, leaving room for filter byte. */
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -0500748 memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500749
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500750#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500751 /* Handle interlacing */
Guy Schalnat0d580581995-07-20 02:43:20 -0500752 if (png_ptr->interlaced && png_ptr->pass < 6 &&
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500753 (png_ptr->transformations & PNG_INTERLACE))
Guy Schalnat0d580581995-07-20 02:43:20 -0500754 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500755 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 -0500756 /* This should always get caught above, but still ... */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500757 if (!(row_info.width))
Guy Schalnat0d580581995-07-20 02:43:20 -0500758 {
759 png_write_finish_row(png_ptr);
760 return;
761 }
762 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600763#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500764
John Bowler4a12f4a2011-04-17 18:34:22 -0500765#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500766 /* Handle other transformations */
Guy Schalnat0d580581995-07-20 02:43:20 -0500767 if (png_ptr->transformations)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500768 png_do_write_transformations(png_ptr, &row_info);
John Bowler4a12f4a2011-04-17 18:34:22 -0500769#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500770
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500771 /* At this point the row_info pixel depth must match the 'transformed' depth,
772 * which is also the output depth.
773 */
774 if (row_info.pixel_depth != png_ptr->pixel_depth ||
775 row_info.pixel_depth != png_ptr->transformed_pixel_depth)
776 png_error(png_ptr, "internal write transform logic error");
777
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500778#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600779 /* Write filter_method 64 (intrapixel differencing) only if
780 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
781 * 2. Libpng did not write a PNG signature (this filter_method is only
782 * used in PNG datastreams that are embedded in MNG datastreams) and
783 * 3. The application called png_permit_mng_features with a mask that
784 * included PNG_FLAG_MNG_FILTER_64 and
785 * 4. The filter_method is 64 and
786 * 5. The color_type is RGB or RGBA
787 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500788 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500789 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600790 {
791 /* Intrapixel differencing */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500792 png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600793 }
794#endif
795
Glenn Randers-Pehrson363ae652012-03-01 21:39:29 -0600796/* Added at libpng-1.5.10 */
797#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
John Bowlerdee75772012-03-01 18:55:54 -0600798 /* Check for out-of-range palette index */
Glenn Randers-Pehrson14ca47b2012-06-06 13:30:30 -0500799 if (row_info.color_type == PNG_COLOR_TYPE_PALETTE &&
800 png_ptr->num_palette_max >= 0)
Glenn Randers-Pehrsoneeb1bb62012-03-02 22:10:15 -0600801 png_do_check_palette_indexes(png_ptr, &row_info);
John Bowlerdee75772012-03-01 18:55:54 -0600802#endif
803
Andreas Dilger47a0c421997-05-16 02:46:07 -0500804 /* Find a filter if necessary, filter the row and write it out. */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500805 png_write_find_filter(png_ptr, &row_info);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600806
807 if (png_ptr->write_row_fn != NULL)
808 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500809}
810
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500811#ifdef PNG_WRITE_FLUSH_SUPPORTED
Guy Schalnat0f716451995-11-28 11:22:13 -0600812/* Set the automatic flush interval or 0 to turn flushing off */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500813void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600814png_set_flush(png_structrp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600815{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500816 png_debug(1, "in png_set_flush");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500817
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600818 if (png_ptr == NULL)
819 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500820
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600821 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600822}
823
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500824/* Flush the current output buffers now */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500825void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600826png_write_flush(png_structrp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600827{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500828 png_debug(1, "in png_write_flush");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500829
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600830 if (png_ptr == NULL)
831 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500832
Guy Schalnate5a37791996-06-05 15:50:50 -0500833 /* We have already written out all of the data */
834 if (png_ptr->row_number >= png_ptr->num_rows)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500835 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600836
John Bowlerb5d00512012-03-09 09:15:18 -0600837 png_compress_IDAT(png_ptr, NULL, 0, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600838 png_ptr->flush_rows = 0;
839 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600840}
841#endif /* PNG_WRITE_FLUSH_SUPPORTED */
842
John Bowlerd332c672011-12-21 17:36:12 -0600843#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
John Bowler5d567862011-12-24 09:12:00 -0600844static void png_reset_filter_heuristics(png_structrp png_ptr); /* forward decl */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500845#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600846
John Bowlerd332c672011-12-21 17:36:12 -0600847/* Free any memory used in png_ptr struct without freeing the struct itself. */
848static void
John Bowler5d567862011-12-24 09:12:00 -0600849png_write_destroy(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500850{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500851 png_debug(1, "in png_write_destroy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500852
853 /* Free any memory zlib uses */
John Bowler42a2b552012-03-05 20:57:40 -0600854 if (png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED)
John Bowlerc5bef942011-05-05 17:35:39 -0500855 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -0500856
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500857 /* Free our memory. png_free checks NULL for us. */
John Bowlerb5d00512012-03-09 09:15:18 -0600858 png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600859 png_free(png_ptr, png_ptr->row_buf);
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500860#ifdef PNG_WRITE_FILTER_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600861 png_free(png_ptr, png_ptr->prev_row);
862 png_free(png_ptr, png_ptr->sub_row);
863 png_free(png_ptr, png_ptr->up_row);
864 png_free(png_ptr, png_ptr->avg_row);
865 png_free(png_ptr, png_ptr->paeth_row);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500866#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600867
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500868#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500869 /* Use this to save a little code space, it doesn't free the filter_costs */
870 png_reset_filter_heuristics(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500871 png_free(png_ptr, png_ptr->filter_costs);
872 png_free(png_ptr, png_ptr->inv_filter_costs);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600873#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500874
John Bowlere9567512012-08-15 22:53:00 -0500875#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
John Bowlerd332c672011-12-21 17:36:12 -0600876 png_free(png_ptr, png_ptr->chunk_list);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600877#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500878
John Bowlerd332c672011-12-21 17:36:12 -0600879 /* The error handling and memory handling information is left intact at this
880 * point: the jmp_buf may still have to be freed. See png_destroy_png_struct
881 * for how this happens.
882 */
883}
Guy Schalnate5a37791996-06-05 15:50:50 -0500884
John Bowlerd332c672011-12-21 17:36:12 -0600885/* Free all memory used by the write.
886 * In libpng 1.6.0 this API changed quietly to no longer accept a NULL value for
887 * *png_ptr_ptr. Prior to 1.6.0 it would accept such a value and it would free
888 * the passed in info_structs but it would quietly fail to free any of the data
889 * inside them. In 1.6.0 it quietly does nothing (it has to be quiet because it
890 * has no png_ptr.)
891 */
892void PNGAPI
893png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
894{
895 png_debug(1, "in png_destroy_write_struct");
Guy Schalnate5a37791996-06-05 15:50:50 -0500896
John Bowlerd332c672011-12-21 17:36:12 -0600897 if (png_ptr_ptr != NULL)
898 {
John Bowler5d567862011-12-24 09:12:00 -0600899 png_structrp png_ptr = *png_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500900
John Bowlerd332c672011-12-21 17:36:12 -0600901 if (png_ptr != NULL) /* added in libpng 1.6.0 */
902 {
903 png_destroy_info_struct(png_ptr, info_ptr_ptr);
904
905 *png_ptr_ptr = NULL;
906 png_write_destroy(png_ptr);
907 png_destroy_png_struct(png_ptr);
908 }
909 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500910}
Guy Schalnate5a37791996-06-05 15:50:50 -0500911
Andreas Dilger47a0c421997-05-16 02:46:07 -0500912/* Allow the application to select one or more row filters to use. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500913void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600914png_set_filter(png_structrp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500915{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500916 png_debug(1, "in png_set_filter");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500917
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600918 if (png_ptr == NULL)
919 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500920
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500921#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500922 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600923 (method == PNG_INTRAPIXEL_DIFFERENCING))
924 method = PNG_FILTER_TYPE_BASE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500925
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600926#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500927 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500928 {
929 switch (filters & (PNG_ALL_FILTERS | 0x07))
930 {
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500931#ifdef PNG_WRITE_FILTER_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500932 case 5:
933 case 6:
Andreas Dilger47a0c421997-05-16 02:46:07 -0500934 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500935#endif /* PNG_WRITE_FILTER_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500936 case PNG_FILTER_VALUE_NONE:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600937 png_ptr->do_filter = PNG_FILTER_NONE; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500938
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500939#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500940 case PNG_FILTER_VALUE_SUB:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600941 png_ptr->do_filter = PNG_FILTER_SUB; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500942
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500943 case PNG_FILTER_VALUE_UP:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600944 png_ptr->do_filter = PNG_FILTER_UP; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500945
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500946 case PNG_FILTER_VALUE_AVG:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600947 png_ptr->do_filter = PNG_FILTER_AVG; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500948
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500949 case PNG_FILTER_VALUE_PAETH:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600950 png_ptr->do_filter = PNG_FILTER_PAETH; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500951
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600952 default:
953 png_ptr->do_filter = (png_byte)filters; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500954#else
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600955 default:
956 png_warning(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500957#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500958 }
959
Andreas Dilger47a0c421997-05-16 02:46:07 -0500960 /* If we have allocated the row_buf, this means we have already started
961 * with the image and we should have allocated all of the filter buffers
962 * that have been selected. If prev_row isn't already allocated, then
963 * it is too late to start using the filters that need it, since we
964 * will be missing the data in the previous row. If an application
965 * wants to start and stop using particular filters during compression,
966 * it should start out with all of the filters, and then add and
967 * remove them after the start of compression.
Guy Schalnate5a37791996-06-05 15:50:50 -0500968 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500969 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500970 {
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500971#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600972 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500973 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500974 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600975 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500976 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -0500977 }
978
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600979 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500980 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500981 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500982 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500983 png_warning(png_ptr, "Can't add Up filter after starting");
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500984 png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
985 ~PNG_FILTER_UP);
Guy Schalnate5a37791996-06-05 15:50:50 -0500986 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500987
Guy Schalnate5a37791996-06-05 15:50:50 -0500988 else
989 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500990 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600991 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500992 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -0500993 }
994 }
995
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600996 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_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 Average 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_AVG);
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->avg_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->avg_row[0] = PNG_FILTER_VALUE_AVG;
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_PAETH) &&
Andreas Dilger47a0c421997-05-16 02:46:07 -05001014 png_ptr->paeth_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001015 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001016 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001017 {
1018 png_warning(png_ptr, "Can't add Paeth filter after starting");
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001019 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
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 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001024 png_ptr->paeth_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->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001027 }
1028 }
1029
1030 if (png_ptr->do_filter == PNG_NO_FILTERS)
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001031#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -05001032 png_ptr->do_filter = PNG_FILTER_NONE;
1033 }
1034 }
1035 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05001036 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001037}
1038
Andreas Dilger47a0c421997-05-16 02:46:07 -05001039/* This allows us to influence the way in which libpng chooses the "best"
1040 * filter for the current scanline. While the "minimum-sum-of-absolute-
1041 * differences metric is relatively fast and effective, there is some
1042 * question as to whether it can be improved upon by trying to keep the
1043 * filtered data going to zlib more consistent, hopefully resulting in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001044 * better compression.
1045 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001046#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */
Glenn Randers-Pehrson47dc5f72011-03-30 09:59:02 -05001047/* Convenience reset API. */
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001048static void
John Bowler5d567862011-12-24 09:12:00 -06001049png_reset_filter_heuristics(png_structrp png_ptr)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001050{
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001051 /* Clear out any old values in the 'weights' - this must be done because if
1052 * the app calls set_filter_heuristics multiple times with different
1053 * 'num_weights' values we would otherwise potentially have wrong sized
1054 * arrays.
1055 */
1056 png_ptr->num_prev_filters = 0;
1057 png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1058 if (png_ptr->prev_filters != NULL)
1059 {
1060 png_bytep old = png_ptr->prev_filters;
1061 png_ptr->prev_filters = NULL;
1062 png_free(png_ptr, old);
1063 }
1064 if (png_ptr->filter_weights != NULL)
1065 {
1066 png_uint_16p old = png_ptr->filter_weights;
1067 png_ptr->filter_weights = NULL;
1068 png_free(png_ptr, old);
1069 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001070
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001071 if (png_ptr->inv_filter_weights != NULL)
1072 {
1073 png_uint_16p old = png_ptr->inv_filter_weights;
1074 png_ptr->inv_filter_weights = NULL;
1075 png_free(png_ptr, old);
1076 }
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001077
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001078 /* Leave the filter_costs - this array is fixed size. */
1079}
1080
1081static int
John Bowler5d567862011-12-24 09:12:00 -06001082png_init_filter_heuristics(png_structrp png_ptr, int heuristic_method,
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001083 int num_weights)
1084{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001085 if (png_ptr == NULL)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001086 return 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001087
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001088 /* Clear out the arrays */
1089 png_reset_filter_heuristics(png_ptr);
1090
1091 /* Check arguments; the 'reset' function makes the correct settings for the
1092 * unweighted case, but we must handle the weight case by initializing the
1093 * arrays for the caller.
1094 */
1095 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001096 {
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001097 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001098
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001099 if (num_weights > 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001100 {
1101 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001102 (png_uint_32)((sizeof (png_byte)) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001103
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001104 /* To make sure that the weighting starts out fairly */
1105 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001106 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001107 png_ptr->prev_filters[i] = 255;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001108 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001109
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001110 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001111 (png_uint_32)((sizeof (png_uint_16)) * num_weights));
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001112
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001113 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001114 (png_uint_32)((sizeof (png_uint_16)) * num_weights));
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001115
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001116 for (i = 0; i < num_weights; i++)
1117 {
1118 png_ptr->inv_filter_weights[i] =
1119 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1120 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001121
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001122 /* Safe to set this now */
1123 png_ptr->num_prev_filters = (png_byte)num_weights;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001124 }
1125
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001126 /* If, in the future, there are other filter methods, this would
1127 * need to be based on png_ptr->filter.
1128 */
1129 if (png_ptr->filter_costs == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001130 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001131 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001132 (png_uint_32)((sizeof (png_uint_16)) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001133
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001134 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001135 (png_uint_32)((sizeof (png_uint_16)) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001136 }
1137
Andreas Dilger47a0c421997-05-16 02:46:07 -05001138 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1139 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001140 png_ptr->inv_filter_costs[i] =
1141 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001142 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001143
1144 /* All the arrays are inited, safe to set this: */
1145 png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_WEIGHTED;
1146
1147 /* Return the 'ok' code. */
1148 return 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001149 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001150 else if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT ||
1151 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001152 {
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001153 return 1;
1154 }
1155 else
1156 {
1157 png_warning(png_ptr, "Unknown filter heuristic method");
1158 return 0;
1159 }
1160}
1161
1162/* Provide floating and fixed point APIs */
1163#ifdef PNG_FLOATING_POINT_SUPPORTED
1164void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001165png_set_filter_heuristics(png_structrp png_ptr, int heuristic_method,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001166 int num_weights, png_const_doublep filter_weights,
1167 png_const_doublep filter_costs)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001168{
1169 png_debug(1, "in png_set_filter_heuristics");
1170
1171 /* The internal API allocates all the arrays and ensures that the elements of
1172 * those arrays are set to the default value.
1173 */
1174 if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
1175 return;
1176
1177 /* If using the weighted method copy in the weights. */
1178 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1179 {
1180 int i;
1181 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001182 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001183 if (filter_weights[i] <= 0.0)
1184 {
1185 png_ptr->inv_filter_weights[i] =
1186 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1187 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001188
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001189 else
1190 {
1191 png_ptr->inv_filter_weights[i] =
1192 (png_uint_16)(PNG_WEIGHT_FACTOR*filter_weights[i]+.5);
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001193
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001194 png_ptr->filter_weights[i] =
1195 (png_uint_16)(PNG_WEIGHT_FACTOR/filter_weights[i]+.5);
1196 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001197 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001198
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001199 /* Here is where we set the relative costs of the different filters. We
1200 * should take the desired compression level into account when setting
1201 * the costs, so that Paeth, for instance, has a high relative cost at low
1202 * compression levels, while it has a lower relative cost at higher
1203 * compression settings. The filter types are in order of increasing
1204 * relative cost, so it would be possible to do this with an algorithm.
1205 */
1206 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) if (filter_costs[i] >= 1.0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001207 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001208 png_ptr->inv_filter_costs[i] =
1209 (png_uint_16)(PNG_COST_FACTOR / filter_costs[i] + .5);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001210
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001211 png_ptr->filter_costs[i] =
1212 (png_uint_16)(PNG_COST_FACTOR * filter_costs[i] + .5);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001213 }
1214 }
1215}
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001216#endif /* FLOATING_POINT */
1217
1218#ifdef PNG_FIXED_POINT_SUPPORTED
1219void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001220png_set_filter_heuristics_fixed(png_structrp png_ptr, int heuristic_method,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001221 int num_weights, png_const_fixed_point_p filter_weights,
1222 png_const_fixed_point_p filter_costs)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001223{
1224 png_debug(1, "in png_set_filter_heuristics_fixed");
1225
1226 /* The internal API allocates all the arrays and ensures that the elements of
1227 * those arrays are set to the default value.
1228 */
1229 if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
1230 return;
1231
1232 /* If using the weighted method copy in the weights. */
1233 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1234 {
1235 int i;
1236 for (i = 0; i < num_weights; i++)
1237 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001238 if (filter_weights[i] <= 0)
1239 {
1240 png_ptr->inv_filter_weights[i] =
1241 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1242 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001243
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001244 else
1245 {
1246 png_ptr->inv_filter_weights[i] = (png_uint_16)
1247 ((PNG_WEIGHT_FACTOR*filter_weights[i]+PNG_FP_HALF)/PNG_FP_1);
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001248
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001249 png_ptr->filter_weights[i] = (png_uint_16)((PNG_WEIGHT_FACTOR*
1250 PNG_FP_1+(filter_weights[i]/2))/filter_weights[i]);
1251 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001252 }
1253
1254 /* Here is where we set the relative costs of the different filters. We
1255 * should take the desired compression level into account when setting
1256 * the costs, so that Paeth, for instance, has a high relative cost at low
1257 * compression levels, while it has a lower relative cost at higher
1258 * compression settings. The filter types are in order of increasing
1259 * relative cost, so it would be possible to do this with an algorithm.
1260 */
1261 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1262 if (filter_costs[i] >= PNG_FP_1)
1263 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001264 png_uint_32 tmp;
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001265
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001266 /* Use a 32 bit unsigned temporary here because otherwise the
1267 * intermediate value will be a 32 bit *signed* integer (ANSI rules)
1268 * and this will get the wrong answer on division.
1269 */
1270 tmp = PNG_COST_FACTOR*PNG_FP_1 + (filter_costs[i]/2);
1271 tmp /= filter_costs[i];
1272
1273 png_ptr->inv_filter_costs[i] = (png_uint_16)tmp;
1274
1275 tmp = PNG_COST_FACTOR * filter_costs[i] + PNG_FP_HALF;
1276 tmp /= PNG_FP_1;
1277
1278 png_ptr->filter_costs[i] = (png_uint_16)tmp;
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001279 }
1280 }
1281}
1282#endif /* FIXED_POINT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001283#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1284
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001285void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001286png_set_compression_level(png_structrp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001287{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001288 png_debug(1, "in png_set_compression_level");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001289
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001290 if (png_ptr == NULL)
1291 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001292
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001293 png_ptr->zlib_level = level;
1294}
1295
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001296void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001297png_set_compression_mem_level(png_structrp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001298{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001299 png_debug(1, "in png_set_compression_mem_level");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001300
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001301 if (png_ptr == NULL)
1302 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001303
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001304 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001305}
1306
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001307void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001308png_set_compression_strategy(png_structrp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001309{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001310 png_debug(1, "in png_set_compression_strategy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001311
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001312 if (png_ptr == NULL)
1313 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001314
John Bowler42a2b552012-03-05 20:57:40 -06001315 /* The flag setting here prevents the libpng dynamic selection of strategy.
1316 */
Guy Schalnate5a37791996-06-05 15:50:50 -05001317 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001318 png_ptr->zlib_strategy = strategy;
1319}
1320
Glenn Randers-Pehrsonaf855e42011-05-07 10:52:49 -05001321/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
1322 * smaller value of window_bits if it can do so safely.
1323 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001324void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001325png_set_compression_window_bits(png_structrp png_ptr, int window_bits)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001326{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001327 if (png_ptr == NULL)
1328 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001329
John Bowler42a2b552012-03-05 20:57:40 -06001330 /* Prior to 1.6.0 this would warn but then set the window_bits value, this
1331 * meant that negative window bits values could be selected which would cause
1332 * libpng to write a non-standard PNG file with raw deflate or gzip
1333 * compressed IDAT or ancilliary chunks. Such files can be read and there is
1334 * no warning on read, so this seems like a very bad idea.
1335 */
Guy Schalnate5a37791996-06-05 15:50:50 -05001336 if (window_bits > 15)
John Bowler42a2b552012-03-05 20:57:40 -06001337 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001338 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
John Bowler42a2b552012-03-05 20:57:40 -06001339 window_bits = 15;
1340 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001341
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001342 else if (window_bits < 8)
John Bowler42a2b552012-03-05 20:57:40 -06001343 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001344 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
John Bowler42a2b552012-03-05 20:57:40 -06001345 window_bits = 8;
1346 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001347
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001348 png_ptr->zlib_window_bits = window_bits;
1349}
1350
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001351void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001352png_set_compression_method(png_structrp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001353{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001354 png_debug(1, "in png_set_compression_method");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001355
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001356 if (png_ptr == NULL)
1357 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001358
John Bowler42a2b552012-03-05 20:57:40 -06001359 /* This would produce an invalid PNG file if it worked, but it doesn't and
1360 * deflate will fault it, so it is harmless to just warn here.
1361 */
Guy Schalnate5a37791996-06-05 15:50:50 -05001362 if (method != 8)
1363 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001364
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001365 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -05001366}
1367
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001368/* The following were added to libpng-1.5.4 */
John Bowlerb2bee332011-06-10 23:24:58 -05001369#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001370void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001371png_set_text_compression_level(png_structrp png_ptr, int level)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001372{
1373 png_debug(1, "in png_set_text_compression_level");
1374
1375 if (png_ptr == NULL)
1376 return;
1377
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001378 png_ptr->zlib_text_level = level;
1379}
1380
1381void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001382png_set_text_compression_mem_level(png_structrp png_ptr, int mem_level)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001383{
1384 png_debug(1, "in png_set_text_compression_mem_level");
1385
1386 if (png_ptr == NULL)
1387 return;
1388
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001389 png_ptr->zlib_text_mem_level = mem_level;
1390}
1391
1392void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001393png_set_text_compression_strategy(png_structrp png_ptr, int strategy)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001394{
1395 png_debug(1, "in png_set_text_compression_strategy");
1396
1397 if (png_ptr == NULL)
1398 return;
1399
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001400 png_ptr->zlib_text_strategy = strategy;
1401}
1402
Glenn Randers-Pehrsonaf855e42011-05-07 10:52:49 -05001403/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
1404 * smaller value of window_bits if it can do so safely.
1405 */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001406void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001407png_set_text_compression_window_bits(png_structrp png_ptr, int window_bits)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001408{
1409 if (png_ptr == NULL)
1410 return;
1411
1412 if (window_bits > 15)
John Bowler42a2b552012-03-05 20:57:40 -06001413 {
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001414 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
John Bowler42a2b552012-03-05 20:57:40 -06001415 window_bits = 15;
1416 }
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001417
1418 else if (window_bits < 8)
John Bowler42a2b552012-03-05 20:57:40 -06001419 {
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001420 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
John Bowler42a2b552012-03-05 20:57:40 -06001421 window_bits = 8;
1422 }
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001423
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001424 png_ptr->zlib_text_window_bits = window_bits;
1425}
1426
1427void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001428png_set_text_compression_method(png_structrp png_ptr, int method)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001429{
1430 png_debug(1, "in png_set_text_compression_method");
1431
1432 if (png_ptr == NULL)
1433 return;
1434
1435 if (method != 8)
1436 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1437
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001438 png_ptr->zlib_text_method = method;
1439}
John Bowlerb2bee332011-06-10 23:24:58 -05001440#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001441/* end of API added to libpng-1.5.4 */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001442
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001443void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001444png_set_write_status_fn(png_structrp png_ptr, png_write_status_ptr write_row_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001445{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001446 if (png_ptr == NULL)
1447 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001448
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001449 png_ptr->write_row_fn = write_row_fn;
1450}
1451
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001452#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001453void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001454png_set_write_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001455 write_user_transform_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001456{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001457 png_debug(1, "in png_set_write_user_transform_fn");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001458
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001459 if (png_ptr == NULL)
1460 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001461
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001462 png_ptr->transformations |= PNG_USER_TRANSFORM;
1463 png_ptr->write_user_transform_fn = write_user_transform_fn;
1464}
1465#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001466
1467
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001468#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001469void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001470png_write_png(png_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001471 int transforms, voidp params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001472{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001473 if (png_ptr == NULL || info_ptr == NULL)
1474 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001475
1476 /* Write the file header information. */
1477 png_write_info(png_ptr, info_ptr);
1478
1479 /* ------ these transformations don't touch the info structure ------- */
1480
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001481#ifdef PNG_WRITE_INVERT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001482 /* Invert monochrome pixels */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001483 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001484 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001485#endif
1486
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001487#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001488 /* Shift the pixels up to a legal bit depth and fill in
1489 * as appropriate to correctly scale the image.
1490 */
1491 if ((transforms & PNG_TRANSFORM_SHIFT)
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001492 && (info_ptr->valid & PNG_INFO_sBIT))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001493 png_set_shift(png_ptr, &info_ptr->sig_bit);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001494#endif
1495
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001496#ifdef PNG_WRITE_PACK_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001497 /* Pack pixels into bytes */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001498 if (transforms & PNG_TRANSFORM_PACKING)
1499 png_set_packing(png_ptr);
1500#endif
1501
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001502#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001503 /* Swap location of alpha bytes from ARGB to RGBA */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001504 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001505 png_set_swap_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001506#endif
1507
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001508#ifdef PNG_WRITE_FILLER_SUPPORTED
Glenn Randers-Pehrson47539062011-05-05 07:32:30 -05001509 /* Pack XRGB/RGBX/ARGB/RGBA into RGB (4 channels -> 3 channels) */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001510 if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001511 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001512
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001513 else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001514 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001515#endif
1516
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001517#ifdef PNG_WRITE_BGR_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001518 /* Flip BGR pixels to RGB */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001519 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001520 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001521#endif
1522
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001523#ifdef PNG_WRITE_SWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001524 /* Swap bytes of 16-bit files to most significant byte first */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001525 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001526 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001527#endif
1528
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001529#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001530 /* Swap bits of 1, 2, 4 bit packed pixel formats */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001531 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001532 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001533#endif
1534
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001535#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001536 /* Invert the alpha channel from opacity to transparency */
1537 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1538 png_set_invert_alpha(png_ptr);
1539#endif
1540
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001541 /* ----------------------- end of transformations ------------------- */
1542
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001543 /* Write the bits */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001544 if (info_ptr->valid & PNG_INFO_IDAT)
1545 png_write_image(png_ptr, info_ptr->row_pointers);
1546
1547 /* It is REQUIRED to call this to finish writing the rest of the file */
1548 png_write_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001549
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001550 PNG_UNUSED(transforms) /* Quiet compiler warnings */
1551 PNG_UNUSED(params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001552}
1553#endif
John Bowler7875d532011-11-07 22:33:49 -06001554
1555
1556#ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
1557#ifdef PNG_STDIO_SUPPORTED /* currently required for png_image_write_* */
1558/* Initialize the write structure - general purpose utility. */
1559static int
1560png_image_write_init(png_imagep image)
1561{
1562 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, image,
1563 png_safe_error, png_safe_warning);
1564
1565 if (png_ptr != NULL)
1566 {
1567 png_infop info_ptr = png_create_info_struct(png_ptr);
1568
1569 if (info_ptr != NULL)
1570 {
John Bowler4fa96a42011-11-16 16:39:16 -06001571 png_controlp control = png_voidcast(png_controlp,
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05001572 png_malloc_warn(png_ptr, (sizeof *control)));
John Bowler7875d532011-11-07 22:33:49 -06001573
1574 if (control != NULL)
1575 {
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05001576 memset(control, 0, (sizeof *control));
John Bowler7875d532011-11-07 22:33:49 -06001577
1578 control->png_ptr = png_ptr;
1579 control->info_ptr = info_ptr;
1580 control->for_write = 1;
1581
1582 image->opaque = control;
1583 return 1;
1584 }
1585
1586 /* Error clean up */
1587 png_destroy_info_struct(png_ptr, &info_ptr);
1588 }
1589
1590 png_destroy_write_struct(&png_ptr, NULL);
1591 }
1592
1593 return png_image_error(image, "png_image_read: out of memory");
1594}
1595
1596/* Arguments to png_image_write_main: */
1597typedef struct
1598{
1599 /* Arguments: */
1600 png_imagep image;
1601 png_const_voidp buffer;
1602 png_int_32 row_stride;
John Bowler5bc90382012-01-23 22:43:22 -06001603 png_const_voidp colormap;
John Bowler7875d532011-11-07 22:33:49 -06001604 int convert_to_8bit;
1605 /* Local variables: */
1606 png_const_voidp first_row;
1607 ptrdiff_t row_bytes;
1608 png_voidp local_row;
1609} png_image_write_control;
1610
Glenn Randers-Pehrsonefc4b692011-11-07 23:31:34 -06001611/* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to
1612 * do any necessary byte swapping. The component order is defined by the
John Bowler7875d532011-11-07 22:33:49 -06001613 * png_image format value.
1614 */
1615static int
1616png_write_image_16bit(png_voidp argument)
1617{
John Bowler4fa96a42011-11-16 16:39:16 -06001618 png_image_write_control *display = png_voidcast(png_image_write_control*,
1619 argument);
John Bowler7875d532011-11-07 22:33:49 -06001620 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06001621 png_structrp png_ptr = image->opaque->png_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001622
John Bowler4fa96a42011-11-16 16:39:16 -06001623 png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
1624 display->first_row);
1625 png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row);
John Bowler7875d532011-11-07 22:33:49 -06001626 png_uint_16p row_end;
John Bowler1c25b9b2012-02-29 10:49:28 -06001627 const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
John Bowler7875d532011-11-07 22:33:49 -06001628 int aindex = 0;
1629 png_uint_32 y = image->height;
1630
1631 if (image->format & PNG_FORMAT_FLAG_ALPHA)
1632 {
1633 if (image->format & PNG_FORMAT_FLAG_AFIRST)
1634 {
1635 aindex = -1;
1636 ++input_row; /* To point to the first component */
1637 ++output_row;
1638 }
1639
1640 else
1641 aindex = channels;
1642 }
1643
1644 else
1645 png_error(png_ptr, "png_write_image: internal call error");
1646
1647 /* Work out the output row end and count over this, note that the increment
1648 * above to 'row' means that row_end can actually be beyond the end of the
Glenn Randers-Pehrsonefc4b692011-11-07 23:31:34 -06001649 * row; this is correct.
John Bowler7875d532011-11-07 22:33:49 -06001650 */
1651 row_end = output_row + image->width * (channels+1);
1652
1653 while (y-- > 0)
1654 {
1655 png_const_uint_16p in_ptr = input_row;
1656 png_uint_16p out_ptr = output_row;
1657
1658 while (out_ptr < row_end)
1659 {
John Bowler1c25b9b2012-02-29 10:49:28 -06001660 const png_uint_16 alpha = in_ptr[aindex];
John Bowler7875d532011-11-07 22:33:49 -06001661 png_uint_32 reciprocal = 0;
1662 int c;
1663
1664 out_ptr[aindex] = alpha;
1665
1666 /* Calculate a reciprocal. The correct calculation is simply
Glenn Randers-Pehrsonefc4b692011-11-07 23:31:34 -06001667 * component/alpha*65535 << 15. (I.e. 15 bits of precision); this
John Bowler7875d532011-11-07 22:33:49 -06001668 * allows correct rounding by adding .5 before the shift. 'reciprocal'
1669 * is only initialized when required.
1670 */
1671 if (alpha > 0 && alpha < 65535)
1672 reciprocal = ((0xffff<<15)+(alpha>>1))/alpha;
1673
1674 c = channels;
1675 do /* always at least one channel */
1676 {
1677 png_uint_16 component = *in_ptr++;
1678
1679 /* The following gives 65535 for an alpha of 0, which is fine,
1680 * otherwise if 0/0 is represented as some other value there is more
1681 * likely to be a discontinuity which will probably damage
1682 * compression when moving from a fully transparent area to a
1683 * nearly transparent one. (The assumption here is that opaque
1684 * areas tend not to be 0 intensity.)
1685 */
1686 if (component >= alpha)
1687 component = 65535;
1688
1689 /* component<alpha, so component/alpha is less than one and
1690 * component*reciprocal is less than 2^31.
1691 */
1692 else if (component > 0 && alpha < 65535)
1693 {
1694 png_uint_32 calc = component * reciprocal;
1695 calc += 16384; /* round to nearest */
1696 component = (png_uint_16)(calc >> 15);
1697 }
1698
1699 *out_ptr++ = component;
1700 }
1701 while (--c > 0);
1702
1703 /* Skip to next component (skip the intervening alpha channel) */
1704 ++in_ptr;
1705 ++out_ptr;
1706 }
1707
John Bowler4fa96a42011-11-16 16:39:16 -06001708 png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row));
John Bowler7875d532011-11-07 22:33:49 -06001709 input_row += display->row_bytes/(sizeof (png_uint_16));
1710 }
1711
1712 return 1;
1713}
1714
1715/* Given 16-bit input (1 to 4 channels) write 8-bit output. If an alpha channel
1716 * is present it must be removed from the components, the components are then
1717 * written in sRGB encoding. No components are added or removed.
John Bowler5bc90382012-01-23 22:43:22 -06001718 *
1719 * Calculate an alpha reciprocal to reverse pre-multiplication. As above the
1720 * calculation can be done to 15 bits of accuracy; however, the output needs to
1721 * be scaled in the range 0..255*65535, so include that scaling here.
John Bowler7875d532011-11-07 22:33:49 -06001722 */
John Bowler5bc90382012-01-23 22:43:22 -06001723#define UNP_RECIPROCAL(alpha) ((((0xffff*0xff)<<7)+(alpha>>1))/alpha)
1724
1725static png_byte
1726png_unpremultiply(png_uint_32 component, png_uint_32 alpha,
1727 png_uint_32 reciprocal/*from the above macro*/)
1728{
1729 /* The following gives 1.0 for an alpha of 0, which is fine, otherwise if 0/0
1730 * is represented as some other value there is more likely to be a
1731 * discontinuity which will probably damage compression when moving from a
1732 * fully transparent area to a nearly transparent one. (The assumption here
1733 * is that opaque areas tend not to be 0 intensity.)
1734 *
1735 * There is a rounding problem here; if alpha is less than 128 it will end up
1736 * as 0 when scaled to 8 bits. To avoid introducing spurious colors into the
1737 * output change for this too.
1738 */
1739 if (component >= alpha || alpha < 128)
1740 return 255;
1741
1742 /* component<alpha, so component/alpha is less than one and
1743 * component*reciprocal is less than 2^31.
1744 */
1745 else if (component > 0)
1746 {
1747 /* The test is that alpha/257 (rounded) is less than 255, the first value
1748 * that becomes 255 is 65407.
1749 * NOTE: this must agree with the PNG_DIV257 macro (which must, therefore,
1750 * be exact!) [Could also test reciprocal != 0]
1751 */
1752 if (alpha < 65407)
1753 {
1754 component *= reciprocal;
1755 component += 64; /* round to nearest */
1756 component >>= 7;
1757 }
1758
1759 else
1760 component *= 255;
1761
1762 /* Convert the component to sRGB. */
1763 return (png_byte)PNG_sRGB_FROM_LINEAR(component);
1764 }
1765
1766 else
1767 return 0;
1768}
1769
John Bowler7875d532011-11-07 22:33:49 -06001770static int
1771png_write_image_8bit(png_voidp argument)
1772{
John Bowler4fa96a42011-11-16 16:39:16 -06001773 png_image_write_control *display = png_voidcast(png_image_write_control*,
1774 argument);
John Bowler7875d532011-11-07 22:33:49 -06001775 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06001776 png_structrp png_ptr = image->opaque->png_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001777
John Bowler4fa96a42011-11-16 16:39:16 -06001778 png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
1779 display->first_row);
1780 png_bytep output_row = png_voidcast(png_bytep, display->local_row);
John Bowler7875d532011-11-07 22:33:49 -06001781 png_uint_32 y = image->height;
John Bowler1c25b9b2012-02-29 10:49:28 -06001782 const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
John Bowler7875d532011-11-07 22:33:49 -06001783
1784 if (image->format & PNG_FORMAT_FLAG_ALPHA)
1785 {
1786 png_bytep row_end;
1787 int aindex;
1788
1789 if (image->format & PNG_FORMAT_FLAG_AFIRST)
1790 {
1791 aindex = -1;
1792 ++input_row; /* To point to the first component */
1793 ++output_row;
1794 }
1795
1796 else
1797 aindex = channels;
1798
1799 /* Use row_end in place of a loop counter: */
1800 row_end = output_row + image->width * (channels+1);
1801
1802 while (y-- > 0)
1803 {
1804 png_const_uint_16p in_ptr = input_row;
1805 png_bytep out_ptr = output_row;
1806
John Bowler1c25b9b2012-02-29 10:49:28 -06001807 while (out_ptr < row_end)
John Bowler7875d532011-11-07 22:33:49 -06001808 {
1809 png_uint_16 alpha = in_ptr[aindex];
John Bowler5bc90382012-01-23 22:43:22 -06001810 png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
John Bowler7875d532011-11-07 22:33:49 -06001811 png_uint_32 reciprocal = 0;
1812 int c;
1813
John Bowler5bc90382012-01-23 22:43:22 -06001814 /* Scale and write the alpha channel. */
1815 out_ptr[aindex] = alphabyte;
John Bowler7875d532011-11-07 22:33:49 -06001816
John Bowler5bc90382012-01-23 22:43:22 -06001817 if (alphabyte > 0 && alphabyte < 255)
1818 reciprocal = UNP_RECIPROCAL(alpha);
John Bowler7875d532011-11-07 22:33:49 -06001819
1820 c = channels;
1821 do /* always at least one channel */
John Bowler5bc90382012-01-23 22:43:22 -06001822 *out_ptr++ = png_unpremultiply(*in_ptr++, alpha, reciprocal);
John Bowler7875d532011-11-07 22:33:49 -06001823 while (--c > 0);
1824
1825 /* Skip to next component (skip the intervening alpha channel) */
1826 ++in_ptr;
1827 ++out_ptr;
1828 } /* while out_ptr < row_end */
John Bowler3615d032011-11-08 10:38:09 -06001829
John Bowler4fa96a42011-11-16 16:39:16 -06001830 png_write_row(png_ptr, png_voidcast(png_const_bytep,
1831 display->local_row));
John Bowler3615d032011-11-08 10:38:09 -06001832 input_row += display->row_bytes/(sizeof (png_uint_16));
John Bowler7875d532011-11-07 22:33:49 -06001833 } /* while y */
1834 }
1835
1836 else
1837 {
1838 /* No alpha channel, so the row_end really is the end of the row and it
1839 * is sufficient to loop over the components one by one.
1840 */
1841 png_bytep row_end = output_row + image->width * channels;
1842
1843 while (y-- > 0)
1844 {
1845 png_const_uint_16p in_ptr = input_row;
1846 png_bytep out_ptr = output_row;
1847
1848 while (out_ptr < row_end)
1849 {
1850 png_uint_32 component = *in_ptr++;
1851
1852 component *= 255;
1853 *out_ptr++ = (png_byte)PNG_sRGB_FROM_LINEAR(component);
1854 }
1855
1856 png_write_row(png_ptr, output_row);
1857 input_row += display->row_bytes/(sizeof (png_uint_16));
1858 }
1859 }
1860
1861 return 1;
1862}
1863
John Bowler5bc90382012-01-23 22:43:22 -06001864static void
1865png_image_set_PLTE(png_image_write_control *display)
1866{
1867 const png_imagep image = display->image;
1868 const void *cmap = display->colormap;
1869 const int entries = image->colormap_entries > 256 ? 256 :
1870 (int)image->colormap_entries;
1871
1872 /* NOTE: the caller must check for cmap != NULL and entries != 0 */
1873 const png_uint_32 format = image->format;
1874 const int channels = PNG_IMAGE_SAMPLE_CHANNELS(format);
1875
1876# ifdef PNG_FORMAT_BGR_SUPPORTED
1877 const int afirst = (format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
1878 (format & PNG_FORMAT_FLAG_ALPHA) != 0;
1879# else
1880# define afirst 0
1881# endif
1882
1883# ifdef PNG_FORMAT_BGR_SUPPORTED
1884 const int bgr = (format & PNG_FORMAT_FLAG_BGR) ? 2 : 0;
1885# else
1886# define bgr 0
1887# endif
1888
1889 int i, num_trans;
1890 png_color palette[256];
1891 png_byte tRNS[256];
1892
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05001893 memset(tRNS, 255, (sizeof tRNS));
1894 memset(palette, 0, (sizeof palette));
John Bowler5bc90382012-01-23 22:43:22 -06001895
1896 for (i=num_trans=0; i<entries; ++i)
1897 {
1898 /* This gets automatically converted to sRGB with reversal of the
1899 * pre-multiplication if the color-map has an alpha channel.
1900 */
1901 if (format & PNG_FORMAT_FLAG_LINEAR)
1902 {
1903 png_const_uint_16p entry = png_voidcast(png_const_uint_16p, cmap);
John Bowler6f237b62012-03-02 13:13:15 -06001904
John Bowler5bc90382012-01-23 22:43:22 -06001905 entry += i * channels;
1906
1907 if (channels & 1) /* no alpha */
1908 {
1909 if (channels >= 3) /* RGB */
1910 {
1911 palette[i].blue = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
1912 entry[(2 ^ bgr)]);
1913 palette[i].green = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
1914 entry[1]);
1915 palette[i].red = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
1916 entry[bgr]);
1917 }
1918
1919 else /* Gray */
1920 palette[i].blue = palette[i].red = palette[i].green =
1921 (png_byte)PNG_sRGB_FROM_LINEAR(255 * *entry);
1922 }
1923
1924 else /* alpha */
1925 {
1926 png_uint_16 alpha = entry[afirst ? 0 : channels-1];
1927 png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
1928 png_uint_32 reciprocal = 0;
1929
1930 /* Calculate a reciprocal, as in the png_write_image_8bit code above
1931 * this is designed to produce a value scaled to 255*65535 when
1932 * divided by 128 (i.e. asr 7).
1933 */
1934 if (alphabyte > 0 && alphabyte < 255)
1935 reciprocal = (((0xffff*0xff)<<7)+(alpha>>1))/alpha;
1936
1937 tRNS[i] = alphabyte;
1938 if (alphabyte < 255)
1939 num_trans = i+1;
1940
1941 if (channels >= 3) /* RGB */
1942 {
1943 palette[i].blue = png_unpremultiply(entry[afirst + (2 ^ bgr)],
1944 alpha, reciprocal);
1945 palette[i].green = png_unpremultiply(entry[afirst + 1], alpha,
1946 reciprocal);
1947 palette[i].red = png_unpremultiply(entry[afirst + bgr], alpha,
1948 reciprocal);
1949 }
1950
1951 else /* gray */
1952 palette[i].blue = palette[i].red = palette[i].green =
1953 png_unpremultiply(entry[afirst], alpha, reciprocal);
1954 }
1955 }
1956
1957 else /* Color-map has sRGB values */
1958 {
1959 png_const_bytep entry = png_voidcast(png_const_bytep, cmap);
John Bowler6f237b62012-03-02 13:13:15 -06001960
John Bowler5bc90382012-01-23 22:43:22 -06001961 entry += i * channels;
1962
1963 switch (channels)
1964 {
1965 case 4:
1966 tRNS[i] = entry[afirst ? 0 : 3];
1967 if (tRNS[i] < 255)
1968 num_trans = i+1;
1969 /* FALL THROUGH */
1970 case 3:
1971 palette[i].blue = entry[afirst + (2 ^ bgr)];
1972 palette[i].green = entry[afirst + 1];
1973 palette[i].red = entry[afirst + bgr];
1974 break;
1975
1976 case 2:
1977 tRNS[i] = entry[1 ^ afirst];
1978 if (tRNS[i] < 255)
1979 num_trans = i+1;
1980 /* FALL THROUGH */
1981 case 1:
1982 palette[i].blue = palette[i].red = palette[i].green =
1983 entry[afirst];
1984 break;
1985
1986 default:
1987 break;
1988 }
1989 }
1990 }
1991
1992# ifdef afirst
1993# undef afirst
1994# endif
1995# ifdef bgr
1996# undef bgr
1997# endif
1998
1999 png_set_PLTE(image->opaque->png_ptr, image->opaque->info_ptr, palette,
2000 entries);
2001
2002 if (num_trans > 0)
2003 png_set_tRNS(image->opaque->png_ptr, image->opaque->info_ptr, tRNS,
2004 num_trans, NULL);
2005
2006 image->colormap_entries = entries;
2007}
2008
John Bowler7875d532011-11-07 22:33:49 -06002009static int
2010png_image_write_main(png_voidp argument)
2011{
John Bowler4fa96a42011-11-16 16:39:16 -06002012 png_image_write_control *display = png_voidcast(png_image_write_control*,
2013 argument);
John Bowler7875d532011-11-07 22:33:49 -06002014 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06002015 png_structrp png_ptr = image->opaque->png_ptr;
2016 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler7875d532011-11-07 22:33:49 -06002017 png_uint_32 format = image->format;
2018
John Bowler5bc90382012-01-23 22:43:22 -06002019 int colormap = (format & PNG_FORMAT_FLAG_COLORMAP) != 0;
2020 int linear = !colormap && (format & PNG_FORMAT_FLAG_LINEAR) != 0; /* input */
2021 int alpha = !colormap && (format & PNG_FORMAT_FLAG_ALPHA) != 0;
2022 int write_16bit = linear && !colormap && !display->convert_to_8bit;
John Bowler7875d532011-11-07 22:33:49 -06002023
John Bowleraa816c42012-03-16 07:39:49 -05002024# ifdef PNG_BENIGN_ERRORS_SUPPORTED
2025 /* Make sure we error out on any bad situation */
2026 png_set_benign_errors(png_ptr, 0/*error*/);
2027# endif
2028
John Bowler3706d732011-11-21 10:28:06 -06002029 /* Default the 'row_stride' parameter if required. */
2030 if (display->row_stride == 0)
2031 display->row_stride = PNG_IMAGE_ROW_STRIDE(*image);
2032
John Bowler7875d532011-11-07 22:33:49 -06002033 /* Set the required transforms then write the rows in the correct order. */
John Bowler04336ba2012-01-16 07:48:36 -06002034 if (format & PNG_FORMAT_FLAG_COLORMAP)
John Bowler5bc90382012-01-23 22:43:22 -06002035 {
2036 if (display->colormap != NULL && image->colormap_entries > 0)
2037 {
2038 png_uint_32 entries = image->colormap_entries;
2039
2040 png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
2041 entries > 16 ? 8 : (entries > 4 ? 4 : (entries > 2 ? 2 : 1)),
2042 PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
2043 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2044
2045 png_image_set_PLTE(display);
2046 }
2047
2048 else
2049 png_error(image->opaque->png_ptr,
2050 "no color-map for color-mapped image");
2051 }
John Bowler04336ba2012-01-16 07:48:36 -06002052
2053 else
2054 png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
2055 write_16bit ? 16 : 8,
2056 ((format & PNG_FORMAT_FLAG_COLOR) ? PNG_COLOR_MASK_COLOR : 0) +
2057 ((format & PNG_FORMAT_FLAG_ALPHA) ? PNG_COLOR_MASK_ALPHA : 0),
2058 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
John Bowler7875d532011-11-07 22:33:49 -06002059
2060 /* Counter-intuitively the data transformations must be called *after*
2061 * png_write_info, not before as in the read code, but the 'set' functions
2062 * must still be called before. Just set the color space information, never
2063 * write an interlaced image.
2064 */
John Bowler5bc90382012-01-23 22:43:22 -06002065
John Bowler7875d532011-11-07 22:33:49 -06002066 if (write_16bit)
2067 {
2068 /* The gamma here is 1.0 (linear) and the cHRM chunk matches sRGB. */
2069 png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_LINEAR);
John Bowlerdd819152011-11-08 14:29:45 -06002070
2071 if (!(image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB))
2072 png_set_cHRM_fixed(png_ptr, info_ptr,
2073 /* color x y */
2074 /* white */ 31270, 32900,
2075 /* red */ 64000, 33000,
2076 /* green */ 30000, 60000,
2077 /* blue */ 15000, 6000
2078 );
John Bowler7875d532011-11-07 22:33:49 -06002079 }
2080
John Bowlerdd819152011-11-08 14:29:45 -06002081 else if (!(image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB))
John Bowler7875d532011-11-07 22:33:49 -06002082 png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL);
2083
John Bowlerdd819152011-11-08 14:29:45 -06002084 /* Else writing an 8-bit file and the *colors* aren't sRGB, but the 8-bit
2085 * space must still be gamma encoded.
2086 */
2087 else
2088 png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE);
2089
John Bowler7875d532011-11-07 22:33:49 -06002090 /* Write the file header. */
2091 png_write_info(png_ptr, info_ptr);
2092
2093 /* Now set up the data transformations (*after* the header is written),
2094 * remove the handled transformations from the 'format' flags for checking.
John Bowlere6fb6912011-11-08 21:35:16 -06002095 *
2096 * First check for a little endian system if writing 16 bit files.
John Bowler7875d532011-11-07 22:33:49 -06002097 */
John Bowler7875d532011-11-07 22:33:49 -06002098 if (write_16bit)
2099 {
2100 PNG_CONST png_uint_16 le = 0x0001;
2101
2102 if (*(png_const_bytep)&le)
2103 png_set_swap(png_ptr);
2104 }
2105
2106# ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
2107 if (format & PNG_FORMAT_FLAG_BGR)
2108 {
John Bowler5bc90382012-01-23 22:43:22 -06002109 if (!colormap && (format & PNG_FORMAT_FLAG_COLOR) != 0)
John Bowlere6fb6912011-11-08 21:35:16 -06002110 png_set_bgr(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06002111 format &= ~PNG_FORMAT_FLAG_BGR;
2112 }
2113# endif
2114
2115# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
2116 if (format & PNG_FORMAT_FLAG_AFIRST)
2117 {
John Bowler5bc90382012-01-23 22:43:22 -06002118 if (!colormap && (format & PNG_FORMAT_FLAG_ALPHA) != 0)
John Bowlere6fb6912011-11-08 21:35:16 -06002119 png_set_swap_alpha(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06002120 format &= ~PNG_FORMAT_FLAG_AFIRST;
2121 }
2122# endif
2123
John Bowler5bc90382012-01-23 22:43:22 -06002124 /* If there are 16 or fewer color-map entries we wrote a lower bit depth
2125 * above, but the application data is still byte packed.
2126 */
2127 if (colormap && image->colormap_entries <= 16)
2128 png_set_packing(png_ptr);
2129
John Bowlere6fb6912011-11-08 21:35:16 -06002130 /* That should have handled all (both) the transforms. */
John Bowler89c2f842011-11-16 12:04:39 -06002131 if ((format & ~(png_uint_32)(PNG_FORMAT_FLAG_COLOR | PNG_FORMAT_FLAG_LINEAR |
John Bowler5bc90382012-01-23 22:43:22 -06002132 PNG_FORMAT_FLAG_ALPHA | PNG_FORMAT_FLAG_COLORMAP)) != 0)
John Bowler7875d532011-11-07 22:33:49 -06002133 png_error(png_ptr, "png_write_image: unsupported transformation");
2134
2135 {
John Bowler4fa96a42011-11-16 16:39:16 -06002136 png_const_bytep row = png_voidcast(png_const_bytep, display->buffer);
John Bowler7875d532011-11-07 22:33:49 -06002137 ptrdiff_t row_bytes = display->row_stride;
2138
2139 if (linear)
Glenn Randers-Pehrson432c1742012-08-09 20:14:48 -05002140 row_bytes *= (sizeof (png_uint_16));
John Bowler7875d532011-11-07 22:33:49 -06002141
2142 if (row_bytes < 0)
2143 row += (image->height-1) * (-row_bytes);
2144
2145 display->first_row = row;
2146 display->row_bytes = row_bytes;
2147 }
2148
John Bowlerdee75772012-03-01 18:55:54 -06002149 /* Apply 'fast' options if the flag is set. */
2150 if ((image->flags & PNG_IMAGE_FLAG_FAST) != 0)
2151 {
2152 png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_NO_FILTERS);
2153 /* NOTE: determined by experiment using pngstest, this reflects some
2154 * balance between the time to write the image once and the time to read
2155 * it about 50 times. The speed-up in pngstest was about 10-20% of the
2156 * total (user) time on a heavily loaded system.
2157 */
2158 png_set_compression_level(png_ptr, 3);
2159 }
2160
John Bowler7875d532011-11-07 22:33:49 -06002161 /* Check for the cases that currently require a pre-transform on the row
2162 * before it is written. This only applies when the input is 16-bit and
2163 * either there is an alpha channel or it is converted to 8-bit.
2164 */
John Bowler5bc90382012-01-23 22:43:22 -06002165 if ((linear && alpha) || (!colormap && display->convert_to_8bit))
John Bowler7875d532011-11-07 22:33:49 -06002166 {
John Bowler4fa96a42011-11-16 16:39:16 -06002167 png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
2168 png_get_rowbytes(png_ptr, info_ptr)));
John Bowler7875d532011-11-07 22:33:49 -06002169 int result;
2170
2171 display->local_row = row;
2172 if (write_16bit)
2173 result = png_safe_execute(image, png_write_image_16bit, display);
2174 else
2175 result = png_safe_execute(image, png_write_image_8bit, display);
2176 display->local_row = NULL;
2177
2178 png_free(png_ptr, row);
2179
2180 /* Skip the 'write_end' on error: */
2181 if (!result)
2182 return 0;
2183 }
2184
2185 /* Otherwise this is the case where the input is in a format currently
2186 * supported by the rest of the libpng write code; call it directly.
2187 */
2188 else
2189 {
John Bowler4fa96a42011-11-16 16:39:16 -06002190 png_const_bytep row = png_voidcast(png_const_bytep, display->first_row);
John Bowler7875d532011-11-07 22:33:49 -06002191 ptrdiff_t row_bytes = display->row_bytes;
2192 png_uint_32 y = image->height;
2193
2194 while (y-- > 0)
2195 {
2196 png_write_row(png_ptr, row);
2197 row += row_bytes;
2198 }
2199 }
2200
2201 png_write_end(png_ptr, info_ptr);
2202 return 1;
2203}
2204
2205int PNGAPI
John Bowlerdd819152011-11-08 14:29:45 -06002206png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit,
John Bowler5bc90382012-01-23 22:43:22 -06002207 const void *buffer, png_int_32 row_stride, const void *colormap)
John Bowler7875d532011-11-07 22:33:49 -06002208{
2209 /* Write the image to the given (FILE*). */
John Bowler5bc90382012-01-23 22:43:22 -06002210 if (image != NULL || image->version != PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06002211 {
2212 if (file != NULL)
2213 {
2214 if (png_image_write_init(image))
2215 {
2216 png_image_write_control display;
2217 int result;
2218
2219 /* This is slightly evil, but png_init_io doesn't do anything other
2220 * than this and we haven't changed the standard IO functions so
2221 * this saves a 'safe' function.
2222 */
2223 image->opaque->png_ptr->io_ptr = file;
2224
Glenn Randers-Pehrsondbb7e192012-08-10 17:27:42 -05002225 memset(&display, 0, (sizeof display));
John Bowler7875d532011-11-07 22:33:49 -06002226 display.image = image;
2227 display.buffer = buffer;
2228 display.row_stride = row_stride;
John Bowler5bc90382012-01-23 22:43:22 -06002229 display.colormap = colormap;
John Bowler7875d532011-11-07 22:33:49 -06002230 display.convert_to_8bit = convert_to_8bit;
2231
2232 result = png_safe_execute(image, png_image_write_main, &display);
2233 png_image_free(image);
2234 return result;
2235 }
2236
2237 else
2238 return 0;
2239 }
2240
2241 else
2242 return png_image_error(image,
2243 "png_image_write_to_stdio: invalid argument");
2244 }
2245
2246 else
2247 return 0;
2248}
2249
2250int PNGAPI
John Bowlerdd819152011-11-08 14:29:45 -06002251png_image_write_to_file(png_imagep image, const char *file_name,
John Bowler5bc90382012-01-23 22:43:22 -06002252 int convert_to_8bit, const void *buffer, png_int_32 row_stride,
2253 const void *colormap)
John Bowler7875d532011-11-07 22:33:49 -06002254{
2255 /* Write the image to the named file. */
John Bowler5bc90382012-01-23 22:43:22 -06002256 if (image != NULL || image->version != PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06002257 {
2258 if (file_name != NULL)
2259 {
2260 FILE *fp = fopen(file_name, "wb");
2261
2262 if (fp != NULL)
2263 {
John Bowlerdd819152011-11-08 14:29:45 -06002264 if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer,
John Bowler5bc90382012-01-23 22:43:22 -06002265 row_stride, colormap))
John Bowler7875d532011-11-07 22:33:49 -06002266 {
John Bowlerdd819152011-11-08 14:29:45 -06002267 int error; /* from fflush/fclose */
John Bowler7875d532011-11-07 22:33:49 -06002268
John Bowlerdd819152011-11-08 14:29:45 -06002269 /* Make sure the file is flushed correctly. */
2270 if (fflush(fp) == 0 && ferror(fp) == 0)
John Bowler7875d532011-11-07 22:33:49 -06002271 {
John Bowlerdd819152011-11-08 14:29:45 -06002272 if (fclose(fp) == 0)
2273 return 1;
John Bowler7875d532011-11-07 22:33:49 -06002274
John Bowlerdd819152011-11-08 14:29:45 -06002275 error = errno; /* from fclose */
John Bowler7875d532011-11-07 22:33:49 -06002276 }
2277
John Bowlerdd819152011-11-08 14:29:45 -06002278 else
2279 {
2280 error = errno; /* from fflush or ferror */
2281 (void)fclose(fp);
2282 }
2283
2284 (void)remove(file_name);
2285 /* The image has already been cleaned up; this is just used to
2286 * set the error (because the original write succeeded).
2287 */
2288 return png_image_error(image, strerror(error));
John Bowler7875d532011-11-07 22:33:49 -06002289 }
2290
2291 else
2292 {
2293 /* Clean up: just the opened file. */
2294 (void)fclose(fp);
John Bowlerdd819152011-11-08 14:29:45 -06002295 (void)remove(file_name);
John Bowler7875d532011-11-07 22:33:49 -06002296 return 0;
2297 }
2298 }
2299
2300 else
2301 return png_image_error(image, strerror(errno));
2302 }
2303
2304 else
2305 return png_image_error(image,
2306 "png_image_write_to_file: invalid argument");
2307 }
2308
2309 else
2310 return 0;
2311}
2312#endif /* PNG_STDIO_SUPPORTED */
2313#endif /* SIMPLIFIED_WRITE */
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -06002314#endif /* PNG_WRITE_SUPPORTED */