blob: 59377a4ddea2e43188b44d12dd69c2640c7fc04d [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 *
Cosmin Truta70d122a2019-02-03 19:51:18 -05004 * Copyright (c) 2018-2019 Cosmin Truta
Cosmin Truta46aedd82018-07-15 23:58:00 -04005 * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
Cosmin Trutaa8738932018-07-28 18:47:21 -04006 * Copyright (c) 1996-1997 Andreas Dilger
7 * 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 Bowler175a1262016-01-18 09:53:38 -080015#ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED
John Bowler7875d532011-11-07 22:33:49 -060016# include <errno.h>
John Bowler175a1262016-01-18 09:53:38 -080017#endif /* SIMPLIFIED_WRITE_STDIO */
Guy Schalnat0d580581995-07-20 02:43:20 -050018
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060019#ifdef PNG_WRITE_SUPPORTED
20
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060021#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,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -050025 unsigned int where)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060026{
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -050027 if (info_ptr->unknown_chunks_num != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060028 {
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)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -050036 if ((up->location & where) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060037 {
38 /* If per-chunk unknown chunk handling is enabled use it, otherwise
39 * just write the chunks the application has set.
40 */
41#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
42 int keep = png_handle_as_unknown(png_ptr, up->name);
43
44 /* NOTE: this code is radically different from the read side in the
45 * matter of handling an ancillary unknown chunk. In the read side
46 * the default behavior is to discard it, in the code below the default
47 * behavior is to write it. Critical chunks are, however, only
48 * written if explicitly listed or if the default is set to write all
49 * unknown chunks.
50 *
51 * The default handling is also slightly weird - it is not possible to
52 * stop the writing of all unsafe-to-copy chunks!
53 *
54 * TODO: REVIEW: this would seem to be a bug.
55 */
56 if (keep != PNG_HANDLE_CHUNK_NEVER &&
57 ((up->name[3] & 0x20) /* safe-to-copy overrides everything */ ||
58 keep == PNG_HANDLE_CHUNK_ALWAYS ||
59 (keep == PNG_HANDLE_CHUNK_AS_DEFAULT &&
60 png_ptr->unknown_default == PNG_HANDLE_CHUNK_ALWAYS)))
61#endif
62 {
63 /* TODO: review, what is wrong with a zero length unknown chunk? */
64 if (up->size == 0)
65 png_warning(png_ptr, "Writing zero-length unknown chunk");
66
67 png_write_chunk(png_ptr, up->name, up->data, up->size);
68 }
69 }
70 }
71}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -060072#endif /* WRITE_UNKNOWN_CHUNKS */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060073
Andreas Dilger47a0c421997-05-16 02:46:07 -050074/* Writes all the PNG information. This is the suggested way to use the
75 * library. If you have a new chunk to add, make a function to write it,
76 * and put it in the correct location here. If you want the chunk written
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050077 * after the image data, put it in png_write_end(). I strongly encourage
Andreas Dilger47a0c421997-05-16 02:46:07 -050078 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
79 * the chunk, as that will keep the code from breaking if you want to just
80 * write a plain PNG file. If you have long comments, I suggest writing
81 * them in png_write_end(), and compressing them.
82 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050083void PNGAPI
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -060084png_write_info_before_PLTE(png_structrp png_ptr, png_const_inforp info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050085{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050086 png_debug(1, "in png_write_info_before_PLTE");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050087
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -060088 if (png_ptr == NULL || info_ptr == NULL)
89 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050090
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -050091 if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0)
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060092 {
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -050093 /* Write PNG signature */
94 png_write_sig(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050095
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050096#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -050097 if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) != 0 && \
98 png_ptr->mng_features_permitted != 0)
99 {
100 png_warning(png_ptr,
101 "MNG features are not allowed in a PNG datastream");
102 png_ptr->mng_features_permitted = 0;
103 }
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600104#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500105
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -0500106 /* Write IHDR information. */
107 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
108 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
109 info_ptr->filter_type,
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500110#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -0500111 info_ptr->interlace_type
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600112#else
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -0500113 0
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600114#endif
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -0500115 );
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600116
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -0500117 /* The rest of these check to see if the valid field has the appropriate
118 * flag set, and if it does, writes the chunk.
119 *
120 * 1.6.0: COLORSPACE support controls the writing of these chunks too, and
121 * the chunks will be written if the WRITE routine is there and
122 * information * is available in the COLORSPACE. (See
123 * png_colorspace_sync_info in png.c for where the valid flags get set.)
124 *
125 * Under certain circumstances the colorspace can be invalidated without
126 * syncing the info_struct 'valid' flags; this happens if libpng detects
127 * an error and calls png_error while the color space is being set, yet
Glenn Randers-Pehrsone6172802015-07-22 22:36:43 -0500128 * the application continues writing the PNG. So check the 'invalid'
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -0500129 * flag here too.
130 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600131#ifdef PNG_GAMMA_SUPPORTED
132# ifdef PNG_WRITE_gAMA_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500133 if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
134 (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_gAMA) != 0 &&
135 (info_ptr->valid & PNG_INFO_gAMA) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600136 png_write_gAMA_fixed(png_ptr, info_ptr->colorspace.gamma);
137# endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600138#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500139
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600140#ifdef PNG_COLORSPACE_SUPPORTED
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -0500141 /* Write only one of sRGB or an ICC profile. If a profile was supplied
142 * and it matches one of the known sRGB ones issue a warning.
143 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600144# ifdef PNG_WRITE_iCCP_SUPPORTED
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -0500145 if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
146 (info_ptr->valid & PNG_INFO_iCCP) != 0)
147 {
148# ifdef PNG_WRITE_sRGB_SUPPORTED
149 if ((info_ptr->valid & PNG_INFO_sRGB) != 0)
150 png_app_warning(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500151 "profile matches sRGB but writing iCCP instead");
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -0500152# endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600153
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -0500154 png_write_iCCP(png_ptr, info_ptr->iccp_name,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500155 info_ptr->iccp_profile);
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -0500156 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600157# ifdef PNG_WRITE_sRGB_SUPPORTED
158 else
159# endif
160# endif
161
162# ifdef PNG_WRITE_sRGB_SUPPORTED
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -0500163 if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
164 (info_ptr->valid & PNG_INFO_sRGB) != 0)
165 png_write_sRGB(png_ptr, info_ptr->colorspace.rendering_intent);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600166# endif /* WRITE_sRGB */
167#endif /* COLORSPACE */
168
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500169#ifdef PNG_WRITE_sBIT_SUPPORTED
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -0500170 if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
171 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500172#endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600173
174#ifdef PNG_COLORSPACE_SUPPORTED
175# ifdef PNG_WRITE_cHRM_SUPPORTED
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -0500176 if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
177 (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0 &&
178 (info_ptr->valid & PNG_INFO_cHRM) != 0)
179 png_write_cHRM_fixed(png_ptr, &info_ptr->colorspace.end_points_xy);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600180# endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600181#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500182
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500183#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -0500184 write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_IHDR);
Glenn Randers-Pehrsonf3af7062012-02-02 23:11:45 -0600185#endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600186
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600187 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
188 }
189}
190
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500191void PNGAPI
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600192png_write_info(png_structrp png_ptr, png_const_inforp info_ptr)
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600193{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600194#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600195 int i;
196#endif
197
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500198 png_debug(1, "in png_write_info");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600199
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600200 if (png_ptr == NULL || info_ptr == NULL)
201 return;
202
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600203 png_write_info_before_PLTE(png_ptr, info_ptr);
204
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500205 if ((info_ptr->valid & PNG_INFO_PLTE) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500206 png_write_PLTE(png_ptr, info_ptr->palette,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600207 (png_uint_32)info_ptr->num_palette);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500208
Glenn Randers-Pehrson81f44662015-10-29 09:26:41 -0500209 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600210 png_error(png_ptr, "Valid palette required for paletted images");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600211
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500212#ifdef PNG_WRITE_tRNS_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500213 if ((info_ptr->valid & PNG_INFO_tRNS) !=0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500214 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500215#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500216 /* Invert the alpha channel (in tRNS) */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500217 if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0 &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600218 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500219 {
Glenn Randers-Pehrsond8d18fb2015-05-06 14:57:02 -0500220 int j, jend;
221
222 jend = info_ptr->num_trans;
223 if (jend > PNG_MAX_PALETTE_LENGTH)
224 jend = PNG_MAX_PALETTE_LENGTH;
225
226 for (j = 0; j<jend; ++j)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500227 info_ptr->trans_alpha[j] =
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -0500228 (png_byte)(255 - info_ptr->trans_alpha[j]);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500229 }
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600230#endif
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500231 png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600232 info_ptr->num_trans, info_ptr->color_type);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500233 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500234#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500235#ifdef PNG_WRITE_bKGD_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500236 if ((info_ptr->valid & PNG_INFO_bKGD) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600237 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500238#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500239
Glenn Randers-Pehrson40afb682017-07-31 14:20:40 -0500240#ifdef PNG_WRITE_eXIf_SUPPORTED
241 if ((info_ptr->valid & PNG_INFO_eXIf) != 0)
242 png_write_eXIf(png_ptr, info_ptr->exif, info_ptr->num_exif);
243#endif
244
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500245#ifdef PNG_WRITE_hIST_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500246 if ((info_ptr->valid & PNG_INFO_hIST) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600247 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500248#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500249
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500250#ifdef PNG_WRITE_oFFs_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500251 if ((info_ptr->valid & PNG_INFO_oFFs) != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600252 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600253 info_ptr->offset_unit_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500254#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500255
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500256#ifdef PNG_WRITE_pCAL_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500257 if ((info_ptr->valid & PNG_INFO_pCAL) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500258 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600259 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
260 info_ptr->pcal_units, info_ptr->pcal_params);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500261#endif
Glenn Randers-Pehrson59020592009-05-18 10:52:12 -0500262
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500263#ifdef PNG_WRITE_sCAL_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500264 if ((info_ptr->valid & PNG_INFO_sCAL) != 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600265 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600266 info_ptr->scal_s_width, info_ptr->scal_s_height);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500267#endif /* sCAL */
268
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500269#ifdef PNG_WRITE_pHYs_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500270 if ((info_ptr->valid & PNG_INFO_pHYs) != 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500271 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600272 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500273#endif /* pHYs */
274
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500275#ifdef PNG_WRITE_tIME_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500276 if ((info_ptr->valid & PNG_INFO_tIME) != 0)
Guy Schalnate5a37791996-06-05 15:50:50 -0500277 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600278 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600279 png_ptr->mode |= PNG_WROTE_tIME;
Guy Schalnate5a37791996-06-05 15:50:50 -0500280 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500281#endif /* tIME */
282
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500283#ifdef PNG_WRITE_sPLT_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500284 if ((info_ptr->valid & PNG_INFO_sPLT) != 0)
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600285 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
286 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500287#endif /* sPLT */
288
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500289#ifdef PNG_WRITE_TEXT_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500290 /* Check to see if we need to write text chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500291 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500292 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500293 png_debug2(2, "Writing header text chunk %d, type %d", i,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600294 info_ptr->text[i].compression);
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500295 /* An internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600296 if (info_ptr->text[i].compression > 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600297 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500298#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600299 /* Write international chunk */
300 png_write_iTXt(png_ptr,
301 info_ptr->text[i].compression,
302 info_ptr->text[i].key,
303 info_ptr->text[i].lang,
304 info_ptr->text[i].lang_key,
305 info_ptr->text[i].text);
Glenn Randers-Pehrsona11cd842014-10-05 13:45:38 -0500306 /* Mark this chunk as written */
307 if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
308 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
309 else
310 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600311#else
Glenn Randers-Pehrsona11cd842014-10-05 13:45:38 -0500312 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600313#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600314 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500315
Andreas Dilger47a0c421997-05-16 02:46:07 -0500316 /* If we want a compressed text chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600317 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500318 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500319#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500320 /* Write compressed chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500321 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrsond7522252014-10-03 17:00:38 -0500322 info_ptr->text[i].text, info_ptr->text[i].compression);
Glenn Randers-Pehrsona11cd842014-10-05 13:45:38 -0500323 /* Mark this chunk as written */
324 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500325#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600326 png_warning(png_ptr, "Unable to write compressed text");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500327#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500328 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500329
Andreas Dilger47a0c421997-05-16 02:46:07 -0500330 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
331 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500332#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500333 /* Write uncompressed chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500334 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600335 info_ptr->text[i].text,
336 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500337 /* Mark this chunk as written */
338 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500339#else
340 /* Can't get here */
341 png_warning(png_ptr, "Unable to write uncompressed text");
342#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500343 }
344 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500345#endif /* tEXt */
346
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500347#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600348 write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_PLTE);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600349#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500350}
Guy Schalnat0d580581995-07-20 02:43:20 -0500351
Andreas Dilger47a0c421997-05-16 02:46:07 -0500352/* Writes the end of the PNG file. If you don't want to write comments or
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600353 * time information, you can pass NULL for info. If you already wrote these
354 * in png_write_info(), do not write them again here. If you have long
355 * comments, I suggest writing them here, and compressing them.
356 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500357void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600358png_write_end(png_structrp png_ptr, png_inforp info_ptr)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500359{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500360 png_debug(1, "in png_write_end");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500361
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600362 if (png_ptr == NULL)
363 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500364
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500365 if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500366 png_error(png_ptr, "No IDATs written into file");
367
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600368#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
369 if (png_ptr->num_palette_max > png_ptr->num_palette)
370 png_benign_error(png_ptr, "Wrote palette index exceeding num_palette");
371#endif
372
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500373 /* See if user wants us to write information chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500374 if (info_ptr != NULL)
375 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500376#ifdef PNG_WRITE_TEXT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500377 int i; /* local index variable */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600378#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500379#ifdef PNG_WRITE_tIME_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500380 /* Check to see if user has supplied a time chunk */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500381 if ((info_ptr->valid & PNG_INFO_tIME) != 0 &&
382 (png_ptr->mode & PNG_WROTE_tIME) == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500383 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500384
Andreas Dilger47a0c421997-05-16 02:46:07 -0500385#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500386#ifdef PNG_WRITE_TEXT_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500387 /* Loop through comment chunks */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600388 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500389 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500390 png_debug2(2, "Writing trailer text chunk %d, type %d", i,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500391 info_ptr->text[i].compression);
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500392 /* An internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600393 if (info_ptr->text[i].compression > 0)
394 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500395#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500396 /* Write international chunk */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500397 png_write_iTXt(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600398 info_ptr->text[i].compression,
399 info_ptr->text[i].key,
400 info_ptr->text[i].lang,
401 info_ptr->text[i].lang_key,
402 info_ptr->text[i].text);
Glenn Randers-Pehrson470d64c2014-10-05 18:08:39 -0500403 /* Mark this chunk as written */
404 if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
405 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
406 else
407 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600408#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500409 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600410#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600411 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500412
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600413 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
Guy Schalnat0d580581995-07-20 02:43:20 -0500414 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500415#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500416 /* Write compressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600417 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrsond7522252014-10-03 17:00:38 -0500418 info_ptr->text[i].text, info_ptr->text[i].compression);
Glenn Randers-Pehrson470d64c2014-10-05 18:08:39 -0500419 /* Mark this chunk as written */
420 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Guy Schalnate5a37791996-06-05 15:50:50 -0500421#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600422 png_warning(png_ptr, "Unable to write compressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500423#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500424 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500425
Andreas Dilger47a0c421997-05-16 02:46:07 -0500426 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500427 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500428#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500429 /* Write uncompressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600430 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600431 info_ptr->text[i].text, 0);
Glenn Randers-Pehrson470d64c2014-10-05 18:08:39 -0500432 /* Mark this chunk as written */
433 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Guy Schalnate5a37791996-06-05 15:50:50 -0500434#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600435 png_warning(png_ptr, "Unable to write uncompressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500436#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500437 }
438 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600439#endif
Glenn Randers-Pehrson40afb682017-07-31 14:20:40 -0500440
441#ifdef PNG_WRITE_eXIf_SUPPORTED
442 if ((info_ptr->valid & PNG_INFO_eXIf) != 0)
443 png_write_eXIf(png_ptr, info_ptr->exif, info_ptr->num_exif);
444#endif
445
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500446#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600447 write_unknown_chunks(png_ptr, info_ptr, PNG_AFTER_IDAT);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600448#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500449 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500450
451 png_ptr->mode |= PNG_AFTER_IDAT;
452
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500453 /* Write end of PNG file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500454 png_write_IEND(png_ptr);
Glenn Randers-Pehrson149eea22014-03-17 10:58:02 -0500455
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500456 /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
457 * and restored again in libpng-1.2.30, may cause some applications that
458 * do not set png_ptr->output_flush_fn to crash. If your application
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600459 * experiences a problem, please try building libpng with
460 * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500461 * png-mng-implement at lists.sf.net .
462 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500463#ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600464# ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
Glenn Randers-Pehrson32fc5ce2000-07-24 06:34:14 -0500465 png_flush(png_ptr);
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600466# endif
Glenn Randers-Pehrsondbed41f2008-08-19 18:20:52 -0500467#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500468}
469
Glenn Randers-Pehrson65d235a2009-11-02 11:32:30 -0600470#ifdef PNG_CONVERT_tIME_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500471void PNGAPI
Cosmin Truta1ef88822018-08-18 21:01:02 -0400472png_convert_from_struct_tm(png_timep ptime, const struct tm * ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500473{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500474 png_debug(1, "in png_convert_from_struct_tm");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500475
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600476 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -0500477 ptime->month = (png_byte)(ttime->tm_mon + 1);
478 ptime->day = (png_byte)ttime->tm_mday;
479 ptime->hour = (png_byte)ttime->tm_hour;
480 ptime->minute = (png_byte)ttime->tm_min;
481 ptime->second = (png_byte)ttime->tm_sec;
Guy Schalnat0d580581995-07-20 02:43:20 -0500482}
483
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500484void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600485png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500486{
487 struct tm *tbuf;
488
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500489 png_debug(1, "in png_convert_from_time_t");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500490
Guy Schalnat0d580581995-07-20 02:43:20 -0500491 tbuf = gmtime(&ttime);
492 png_convert_from_struct_tm(ptime, tbuf);
493}
Guy Schalnat6d764711995-12-19 03:22:19 -0600494#endif
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500495
Andreas Dilger47a0c421997-05-16 02:46:07 -0500496/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500497PNG_FUNCTION(png_structp,PNGAPI
498png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
499 png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500500{
John Bowlerd332c672011-12-21 17:36:12 -0600501#ifndef PNG_USER_MEM_SUPPORTED
John Bowler5d567862011-12-24 09:12:00 -0600502 png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
Glenn Randers-Pehrson149eea22014-03-17 10:58:02 -0500503 error_fn, warn_fn, NULL, NULL, NULL);
John Bowlerd332c672011-12-21 17:36:12 -0600504#else
505 return png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
506 warn_fn, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500507}
508
509/* Alternate initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500510PNG_FUNCTION(png_structp,PNGAPI
511png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600512 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500513 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500514{
John Bowler5d567862011-12-24 09:12:00 -0600515 png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
Glenn Randers-Pehrson149eea22014-03-17 10:58:02 -0500516 error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600517#endif /* USER_MEM */
Glenn Randers-Pehrson56d6bc22013-04-29 08:57:14 -0500518 if (png_ptr != NULL)
519 {
520 /* Set the zlib control values to defaults; they can be overridden by the
521 * application after the struct has been created.
522 */
523 png_ptr->zbuffer_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500524
Glenn Randers-Pehrson56d6bc22013-04-29 08:57:14 -0500525 /* The 'zlib_strategy' setting is irrelevant because png_default_claim in
526 * pngwutil.c defaults it according to whether or not filters will be
527 * used, and ignores this setting.
528 */
529 png_ptr->zlib_strategy = PNG_Z_DEFAULT_STRATEGY;
530 png_ptr->zlib_level = PNG_Z_DEFAULT_COMPRESSION;
531 png_ptr->zlib_mem_level = 8;
532 png_ptr->zlib_window_bits = 15;
533 png_ptr->zlib_method = 8;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600534
535#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
Glenn Randers-Pehrson56d6bc22013-04-29 08:57:14 -0500536 png_ptr->zlib_text_strategy = PNG_TEXT_Z_DEFAULT_STRATEGY;
537 png_ptr->zlib_text_level = PNG_TEXT_Z_DEFAULT_COMPRESSION;
538 png_ptr->zlib_text_mem_level = 8;
539 png_ptr->zlib_text_window_bits = 15;
540 png_ptr->zlib_text_method = 8;
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600541#endif /* WRITE_COMPRESSED_TEXT */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600542
Glenn Randers-Pehrson56d6bc22013-04-29 08:57:14 -0500543 /* This is a highly dubious configuration option; by default it is off,
544 * but it may be appropriate for private builds that are testing
545 * extensions not conformant to the current specification, or of
546 * applications that must not fail to write at all costs!
547 */
548#ifdef PNG_BENIGN_WRITE_ERRORS_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600549 /* In stable builds only warn if an application error can be completely
550 * handled.
551 */
Glenn Randers-Pehrson149eea22014-03-17 10:58:02 -0500552 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
Glenn Randers-Pehrson56d6bc22013-04-29 08:57:14 -0500553#endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600554
Glenn Randers-Pehrson56d6bc22013-04-29 08:57:14 -0500555 /* App warnings are warnings in release (or release candidate) builds but
556 * are errors during development.
557 */
John Bowler8ee821e2015-05-06 20:03:14 -0500558#if PNG_RELEASE_BUILD
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600559 png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
Glenn Randers-Pehrson56d6bc22013-04-29 08:57:14 -0500560#endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600561
John Bowlerd332c672011-12-21 17:36:12 -0600562 /* TODO: delay this, it can be done in png_init_io() (if the app doesn't
563 * do it itself) avoiding setting the default function if it is not
564 * required.
565 */
566 png_set_write_fn(png_ptr, NULL, NULL, NULL);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500567 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500568
John Bowlerd332c672011-12-21 17:36:12 -0600569 return png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500570}
571
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500572
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600573/* Write a few rows of image data. If the image is interlaced,
574 * either you will have to write the 7 sub images, or, if you
575 * have called png_set_interlace_handling(), you will have to
576 * "write" the image seven times.
577 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500578void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600579png_write_rows(png_structrp png_ptr, png_bytepp row,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600580 png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500581{
582 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600583 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500584
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500585 png_debug(1, "in png_write_rows");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600586
587 if (png_ptr == NULL)
588 return;
589
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500590 /* Loop through the rows */
Guy Schalnat0d580581995-07-20 02:43:20 -0500591 for (i = 0, rp = row; i < num_rows; i++, rp++)
592 {
593 png_write_row(png_ptr, *rp);
594 }
595}
596
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600597/* Write the image. You only need to call this function once, even
598 * if you are writing an interlaced image.
599 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500600void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600601png_write_image(png_structrp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500602{
603 png_uint_32 i; /* row index */
604 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600605 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500606
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600607 if (png_ptr == NULL)
608 return;
609
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500610 png_debug(1, "in png_write_image");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500611
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500612#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500613 /* Initialize interlace handling. If image is not interlaced,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500614 * this will set pass to 1
615 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500616 num_pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600617#else
618 num_pass = 1;
619#endif
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500620 /* Loop through passes */
Guy Schalnat0d580581995-07-20 02:43:20 -0500621 for (pass = 0; pass < num_pass; pass++)
622 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500623 /* Loop through image */
Guy Schalnat0d580581995-07-20 02:43:20 -0500624 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
625 {
626 png_write_row(png_ptr, *rp);
627 }
628 }
629}
630
John Bowlerc10930a2013-12-19 15:24:06 -0600631#ifdef PNG_MNG_FEATURES_SUPPORTED
632/* Performs intrapixel differencing */
633static void
634png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
635{
636 png_debug(1, "in png_do_write_intrapixel");
637
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500638 if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -0600639 {
640 int bytes_per_pixel;
641 png_uint_32 row_width = row_info->width;
642 if (row_info->bit_depth == 8)
643 {
644 png_bytep rp;
645 png_uint_32 i;
646
647 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
648 bytes_per_pixel = 3;
649
650 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
651 bytes_per_pixel = 4;
652
653 else
654 return;
655
656 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
657 {
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -0500658 *(rp) = (png_byte)(*rp - *(rp + 1));
659 *(rp + 2) = (png_byte)(*(rp + 2) - *(rp + 1));
John Bowlerc10930a2013-12-19 15:24:06 -0600660 }
661 }
662
663#ifdef PNG_WRITE_16BIT_SUPPORTED
664 else if (row_info->bit_depth == 16)
665 {
666 png_bytep rp;
667 png_uint_32 i;
668
669 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
670 bytes_per_pixel = 6;
671
672 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
673 bytes_per_pixel = 8;
674
675 else
676 return;
677
678 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
679 {
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -0500680 png_uint_32 s0 = (png_uint_32)(*(rp ) << 8) | *(rp + 1);
681 png_uint_32 s1 = (png_uint_32)(*(rp + 2) << 8) | *(rp + 3);
682 png_uint_32 s2 = (png_uint_32)(*(rp + 4) << 8) | *(rp + 5);
Glenn Randers-Pehrsona8242fe2015-08-17 20:46:27 -0500683 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
684 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -0500685 *(rp ) = (png_byte)(red >> 8);
686 *(rp + 1) = (png_byte)red;
687 *(rp + 4) = (png_byte)(blue >> 8);
688 *(rp + 5) = (png_byte)blue;
John Bowlerc10930a2013-12-19 15:24:06 -0600689 }
690 }
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600691#endif /* WRITE_16BIT */
John Bowlerc10930a2013-12-19 15:24:06 -0600692 }
693}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600694#endif /* MNG_FEATURES */
John Bowlerc10930a2013-12-19 15:24:06 -0600695
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500696/* Called by user to write a row of image data */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500697void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600698png_write_row(png_structrp png_ptr, png_const_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500699{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500700 /* 1.5.6: moved from png_struct to be a local structure: */
701 png_row_info row_info;
702
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600703 if (png_ptr == NULL)
704 return;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500705
Glenn Randers-Pehrson632a84e2010-03-09 22:28:33 -0600706 png_debug2(1, "in png_write_row (row %u, pass %d)",
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500707 png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600708
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500709 /* Initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600710 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500711 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500712 /* Make sure we wrote the header info */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500713 if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500714 png_error(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600715 "png_write_info was never called before png_write_row");
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600716
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500717 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500718#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500719 if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500720 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500721#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500722
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500723#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500724 if ((png_ptr->transformations & PNG_FILLER) != 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500725 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500726#endif
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600727#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
728 defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500729 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600730 png_warning(png_ptr,
731 "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500732#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500733
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500734#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500735 if ((png_ptr->transformations & PNG_PACK) != 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500736 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500737#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500738
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500739#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500740 if ((png_ptr->transformations & PNG_SHIFT) != 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500741 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500742#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500743
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500744#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500745 if ((png_ptr->transformations & PNG_BGR) != 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500746 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500747#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500748
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500749#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500750 if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500751 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500752#endif
753
Guy Schalnat0d580581995-07-20 02:43:20 -0500754 png_write_start_row(png_ptr);
755 }
756
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500757#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500758 /* If interlaced and not interested in row, return */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500759 if (png_ptr->interlaced != 0 &&
760 (png_ptr->transformations & PNG_INTERLACE) != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500761 {
762 switch (png_ptr->pass)
763 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600764 case 0:
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500765 if ((png_ptr->row_number & 0x07) != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500766 {
767 png_write_finish_row(png_ptr);
768 return;
769 }
770 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500771
Guy Schalnat0d580581995-07-20 02:43:20 -0500772 case 1:
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500773 if ((png_ptr->row_number & 0x07) != 0 || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500774 {
775 png_write_finish_row(png_ptr);
776 return;
777 }
778 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500779
Guy Schalnat0d580581995-07-20 02:43:20 -0500780 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600781 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500782 {
783 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600784 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500785 }
786 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500787
Guy Schalnat0d580581995-07-20 02:43:20 -0500788 case 3:
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500789 if ((png_ptr->row_number & 0x03) != 0 || png_ptr->width < 3)
Guy Schalnat0d580581995-07-20 02:43:20 -0500790 {
791 png_write_finish_row(png_ptr);
792 return;
793 }
794 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500795
Guy Schalnat0d580581995-07-20 02:43:20 -0500796 case 4:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600797 if ((png_ptr->row_number & 0x03) != 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500798 {
799 png_write_finish_row(png_ptr);
800 return;
801 }
802 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500803
Guy Schalnat0d580581995-07-20 02:43:20 -0500804 case 5:
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500805 if ((png_ptr->row_number & 0x01) != 0 || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500806 {
807 png_write_finish_row(png_ptr);
808 return;
809 }
810 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500811
Guy Schalnat0d580581995-07-20 02:43:20 -0500812 case 6:
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500813 if ((png_ptr->row_number & 0x01) == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500814 {
815 png_write_finish_row(png_ptr);
816 return;
817 }
818 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500819
820 default: /* error: ignore it */
821 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500822 }
823 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600824#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500825
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500826 /* Set up row info for transformations */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500827 row_info.color_type = png_ptr->color_type;
828 row_info.width = png_ptr->usr_width;
829 row_info.channels = png_ptr->usr_channels;
830 row_info.bit_depth = png_ptr->usr_bit_depth;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -0500831 row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500832 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600833
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500834 png_debug1(3, "row_info->color_type = %d", row_info.color_type);
835 png_debug1(3, "row_info->width = %u", row_info.width);
836 png_debug1(3, "row_info->channels = %d", row_info.channels);
837 png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth);
838 png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth);
839 png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500840
841 /* Copy user's row into buffer, leaving room for filter byte. */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600842 memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500843
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500844#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500845 /* Handle interlacing */
Guy Schalnat0d580581995-07-20 02:43:20 -0500846 if (png_ptr->interlaced && png_ptr->pass < 6 &&
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500847 (png_ptr->transformations & PNG_INTERLACE) != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500848 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500849 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 -0500850 /* This should always get caught above, but still ... */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600851 if (row_info.width == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500852 {
853 png_write_finish_row(png_ptr);
854 return;
855 }
856 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600857#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500858
John Bowler4a12f4a2011-04-17 18:34:22 -0500859#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500860 /* Handle other transformations */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500861 if (png_ptr->transformations != 0)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500862 png_do_write_transformations(png_ptr, &row_info);
John Bowler4a12f4a2011-04-17 18:34:22 -0500863#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500864
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500865 /* At this point the row_info pixel depth must match the 'transformed' depth,
866 * which is also the output depth.
867 */
868 if (row_info.pixel_depth != png_ptr->pixel_depth ||
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -0500869 row_info.pixel_depth != png_ptr->transformed_pixel_depth)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500870 png_error(png_ptr, "internal write transform logic error");
871
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500872#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600873 /* Write filter_method 64 (intrapixel differencing) only if
874 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
875 * 2. Libpng did not write a PNG signature (this filter_method is only
876 * used in PNG datastreams that are embedded in MNG datastreams) and
877 * 3. The application called png_permit_mng_features with a mask that
878 * included PNG_FLAG_MNG_FILTER_64 and
879 * 4. The filter_method is 64 and
880 * 5. The color_type is RGB or RGBA
881 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500882 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500883 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600884 {
885 /* Intrapixel differencing */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500886 png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600887 }
888#endif
889
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600890/* Added at libpng-1.5.10 */
891#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
892 /* Check for out-of-range palette index */
893 if (row_info.color_type == PNG_COLOR_TYPE_PALETTE &&
894 png_ptr->num_palette_max >= 0)
895 png_do_check_palette_indexes(png_ptr, &row_info);
896#endif
897
Andreas Dilger47a0c421997-05-16 02:46:07 -0500898 /* Find a filter if necessary, filter the row and write it out. */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500899 png_write_find_filter(png_ptr, &row_info);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600900
901 if (png_ptr->write_row_fn != NULL)
902 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500903}
904
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500905#ifdef PNG_WRITE_FLUSH_SUPPORTED
Guy Schalnat0f716451995-11-28 11:22:13 -0600906/* Set the automatic flush interval or 0 to turn flushing off */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500907void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600908png_set_flush(png_structrp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600909{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500910 png_debug(1, "in png_set_flush");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500911
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600912 if (png_ptr == NULL)
913 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500914
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -0500915 png_ptr->flush_dist = (nrows < 0 ? 0 : (png_uint_32)nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600916}
917
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500918/* Flush the current output buffers now */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500919void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600920png_write_flush(png_structrp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600921{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500922 png_debug(1, "in png_write_flush");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500923
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600924 if (png_ptr == NULL)
925 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500926
Guy Schalnate5a37791996-06-05 15:50:50 -0500927 /* We have already written out all of the data */
928 if (png_ptr->row_number >= png_ptr->num_rows)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500929 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600930
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600931 png_compress_IDAT(png_ptr, NULL, 0, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600932 png_ptr->flush_rows = 0;
933 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600934}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600935#endif /* WRITE_FLUSH */
Guy Schalnat0f716451995-11-28 11:22:13 -0600936
John Bowlerd332c672011-12-21 17:36:12 -0600937/* Free any memory used in png_ptr struct without freeing the struct itself. */
938static void
John Bowler5d567862011-12-24 09:12:00 -0600939png_write_destroy(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500940{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500941 png_debug(1, "in png_write_destroy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500942
943 /* Free any memory zlib uses */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500944 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
John Bowlerc5bef942011-05-05 17:35:39 -0500945 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -0500946
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500947 /* Free our memory. png_free checks NULL for us. */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600948 png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600949 png_free(png_ptr, png_ptr->row_buf);
Glenn Randers-Pehrsonc6a8cb72014-10-29 08:27:34 -0500950 png_ptr->row_buf = NULL;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500951#ifdef PNG_WRITE_FILTER_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600952 png_free(png_ptr, png_ptr->prev_row);
Glenn Randers-Pehrsone6877672015-05-30 11:54:45 -0500953 png_free(png_ptr, png_ptr->try_row);
954 png_free(png_ptr, png_ptr->tst_row);
Glenn Randers-Pehrsonc6a8cb72014-10-29 08:27:34 -0500955 png_ptr->prev_row = NULL;
Glenn Randers-Pehrsone6877672015-05-30 11:54:45 -0500956 png_ptr->try_row = NULL;
957 png_ptr->tst_row = NULL;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500958#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600959
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600960#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
John Bowlerd332c672011-12-21 17:36:12 -0600961 png_free(png_ptr, png_ptr->chunk_list);
Glenn Randers-Pehrsonc6a8cb72014-10-29 08:27:34 -0500962 png_ptr->chunk_list = NULL;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600963#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500964
John Bowlerd332c672011-12-21 17:36:12 -0600965 /* The error handling and memory handling information is left intact at this
966 * point: the jmp_buf may still have to be freed. See png_destroy_png_struct
967 * for how this happens.
968 */
969}
Guy Schalnate5a37791996-06-05 15:50:50 -0500970
John Bowlerd332c672011-12-21 17:36:12 -0600971/* Free all memory used by the write.
972 * In libpng 1.6.0 this API changed quietly to no longer accept a NULL value for
973 * *png_ptr_ptr. Prior to 1.6.0 it would accept such a value and it would free
974 * the passed in info_structs but it would quietly fail to free any of the data
975 * inside them. In 1.6.0 it quietly does nothing (it has to be quiet because it
976 * has no png_ptr.)
977 */
978void PNGAPI
979png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
980{
981 png_debug(1, "in png_destroy_write_struct");
Guy Schalnate5a37791996-06-05 15:50:50 -0500982
John Bowlerd332c672011-12-21 17:36:12 -0600983 if (png_ptr_ptr != NULL)
984 {
John Bowler5d567862011-12-24 09:12:00 -0600985 png_structrp png_ptr = *png_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500986
John Bowlerd332c672011-12-21 17:36:12 -0600987 if (png_ptr != NULL) /* added in libpng 1.6.0 */
988 {
989 png_destroy_info_struct(png_ptr, info_ptr_ptr);
990
991 *png_ptr_ptr = NULL;
992 png_write_destroy(png_ptr);
993 png_destroy_png_struct(png_ptr);
994 }
995 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500996}
Guy Schalnate5a37791996-06-05 15:50:50 -0500997
Andreas Dilger47a0c421997-05-16 02:46:07 -0500998/* Allow the application to select one or more row filters to use. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500999void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001000png_set_filter(png_structrp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001001{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001002 png_debug(1, "in png_set_filter");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001003
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001004 if (png_ptr == NULL)
1005 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001006
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001007#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001008 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001009 (method == PNG_INTRAPIXEL_DIFFERENCING))
1010 method = PNG_FILTER_TYPE_BASE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001011
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -06001012#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001013 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001014 {
1015 switch (filters & (PNG_ALL_FILTERS | 0x07))
1016 {
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001017#ifdef PNG_WRITE_FILTER_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05001018 case 5:
1019 case 6:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001020 case 7: png_app_error(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001021#endif /* WRITE_FILTER */
John Bowler72d07d32017-07-11 07:50:35 -05001022 /* FALLTHROUGH */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001023 case PNG_FILTER_VALUE_NONE:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001024 png_ptr->do_filter = PNG_FILTER_NONE; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001025
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001026#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001027 case PNG_FILTER_VALUE_SUB:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001028 png_ptr->do_filter = PNG_FILTER_SUB; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001029
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001030 case PNG_FILTER_VALUE_UP:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001031 png_ptr->do_filter = PNG_FILTER_UP; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001032
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001033 case PNG_FILTER_VALUE_AVG:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001034 png_ptr->do_filter = PNG_FILTER_AVG; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001035
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001036 case PNG_FILTER_VALUE_PAETH:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001037 png_ptr->do_filter = PNG_FILTER_PAETH; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001038
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001039 default:
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05001040 png_ptr->do_filter = (png_byte)filters; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001041#else
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001042 default:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001043 png_app_error(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001044#endif /* WRITE_FILTER */
Guy Schalnate5a37791996-06-05 15:50:50 -05001045 }
1046
John Bowler2dfcf652015-06-03 14:58:18 -05001047#ifdef PNG_WRITE_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001048 /* If we have allocated the row_buf, this means we have already started
1049 * with the image and we should have allocated all of the filter buffers
1050 * that have been selected. If prev_row isn't already allocated, then
1051 * it is too late to start using the filters that need it, since we
1052 * will be missing the data in the previous row. If an application
1053 * wants to start and stop using particular filters during compression,
Glenn Randers-Pehrson3e0bef62015-03-04 09:45:57 -06001054 * it should start out with all of the filters, and then remove them
1055 * or add them back after the start of compression.
Glenn Randers-Pehrsone6877672015-05-30 11:54:45 -05001056 *
1057 * NOTE: this is a nasty constraint on the code, because it means that the
1058 * prev_row buffer must be maintained even if there are currently no
1059 * 'prev_row' requiring filters active.
Guy Schalnate5a37791996-06-05 15:50:50 -05001060 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001061 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001062 {
Glenn Randers-Pehrsone6877672015-05-30 11:54:45 -05001063 int num_filters;
1064 png_alloc_size_t buf_size;
1065
1066 /* Repeat the checks in png_write_start_row; 1 pixel high or wide
1067 * images cannot benefit from certain filters. If this isn't done here
1068 * the check below will fire on 1 pixel high images.
1069 */
1070 if (png_ptr->height == 1)
1071 filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH);
1072
1073 if (png_ptr->width == 1)
1074 filters &= ~(PNG_FILTER_SUB|PNG_FILTER_AVG|PNG_FILTER_PAETH);
1075
1076 if ((filters & (PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH)) != 0
1077 && png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001078 {
Glenn Randers-Pehrsone6877672015-05-30 11:54:45 -05001079 /* This is the error case, however it is benign - the previous row
1080 * is not available so the filter can't be used. Just warn here.
1081 */
1082 png_app_warning(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001083 "png_set_filter: UP/AVG/PAETH cannot be added after start");
Glenn Randers-Pehrsone6877672015-05-30 11:54:45 -05001084 filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH);
Guy Schalnate5a37791996-06-05 15:50:50 -05001085 }
1086
Glenn Randers-Pehrsone6877672015-05-30 11:54:45 -05001087 num_filters = 0;
1088
1089 if (filters & PNG_FILTER_SUB)
1090 num_filters++;
1091
1092 if (filters & PNG_FILTER_UP)
1093 num_filters++;
1094
1095 if (filters & PNG_FILTER_AVG)
1096 num_filters++;
1097
1098 if (filters & PNG_FILTER_PAETH)
1099 num_filters++;
1100
1101 /* Allocate needed row buffers if they have not already been
1102 * allocated.
1103 */
1104 buf_size = PNG_ROWBYTES(png_ptr->usr_channels * png_ptr->usr_bit_depth,
1105 png_ptr->width) + 1;
1106
1107 if (png_ptr->try_row == NULL)
John Bowler7023d872015-06-03 15:04:03 -05001108 png_ptr->try_row = png_voidcast(png_bytep,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001109 png_malloc(png_ptr, buf_size));
Glenn Randers-Pehrsone6877672015-05-30 11:54:45 -05001110
1111 if (num_filters > 1)
Guy Schalnate5a37791996-06-05 15:50:50 -05001112 {
Glenn Randers-Pehrsone6877672015-05-30 11:54:45 -05001113 if (png_ptr->tst_row == NULL)
John Bowler7023d872015-06-03 15:04:03 -05001114 png_ptr->tst_row = png_voidcast(png_bytep,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001115 png_malloc(png_ptr, buf_size));
Guy Schalnate5a37791996-06-05 15:50:50 -05001116 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001117 }
Glenn Randers-Pehrsone6877672015-05-30 11:54:45 -05001118 png_ptr->do_filter = (png_byte)filters;
John Bowler2dfcf652015-06-03 14:58:18 -05001119#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001120 }
1121 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05001122 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001123}
1124
John Bowler2dfcf652015-06-03 14:58:18 -05001125#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* DEPRECATED */
1126/* Provide floating and fixed point APIs */
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001127#ifdef PNG_FLOATING_POINT_SUPPORTED
1128void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001129png_set_filter_heuristics(png_structrp png_ptr, int heuristic_method,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001130 int num_weights, png_const_doublep filter_weights,
1131 png_const_doublep filter_costs)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001132{
Glenn Randers-Pehrson89edbf82015-05-31 22:40:36 -05001133 PNG_UNUSED(png_ptr)
1134 PNG_UNUSED(heuristic_method)
1135 PNG_UNUSED(num_weights)
1136 PNG_UNUSED(filter_weights)
1137 PNG_UNUSED(filter_costs)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001138}
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001139#endif /* FLOATING_POINT */
1140
1141#ifdef PNG_FIXED_POINT_SUPPORTED
1142void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001143png_set_filter_heuristics_fixed(png_structrp png_ptr, int heuristic_method,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001144 int num_weights, png_const_fixed_point_p filter_weights,
1145 png_const_fixed_point_p filter_costs)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001146{
Glenn Randers-Pehrson89edbf82015-05-31 22:40:36 -05001147 PNG_UNUSED(png_ptr)
1148 PNG_UNUSED(heuristic_method)
1149 PNG_UNUSED(num_weights)
1150 PNG_UNUSED(filter_weights)
1151 PNG_UNUSED(filter_costs)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001152}
1153#endif /* FIXED_POINT */
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001154#endif /* WRITE_WEIGHTED_FILTER */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001155
Glenn Randers-Pehrsond3ff44f2015-02-17 21:06:23 -06001156#ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001157void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001158png_set_compression_level(png_structrp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001159{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001160 png_debug(1, "in png_set_compression_level");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001161
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001162 if (png_ptr == NULL)
1163 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001164
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001165 png_ptr->zlib_level = level;
1166}
1167
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001168void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001169png_set_compression_mem_level(png_structrp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001170{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001171 png_debug(1, "in png_set_compression_mem_level");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001172
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001173 if (png_ptr == NULL)
1174 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001175
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001176 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001177}
1178
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001179void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001180png_set_compression_strategy(png_structrp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001181{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001182 png_debug(1, "in png_set_compression_strategy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001183
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001184 if (png_ptr == NULL)
1185 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001186
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001187 /* The flag setting here prevents the libpng dynamic selection of strategy.
1188 */
Guy Schalnate5a37791996-06-05 15:50:50 -05001189 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001190 png_ptr->zlib_strategy = strategy;
1191}
1192
Glenn Randers-Pehrsonaf855e42011-05-07 10:52:49 -05001193/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
1194 * smaller value of window_bits if it can do so safely.
1195 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001196void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001197png_set_compression_window_bits(png_structrp png_ptr, int window_bits)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001198{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001199 if (png_ptr == NULL)
1200 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001201
Glenn Randers-Pehrson3e0bef62015-03-04 09:45:57 -06001202 /* Prior to 1.6.0 this would warn but then set the window_bits value. This
1203 * meant that negative window bits values could be selected that would cause
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001204 * libpng to write a non-standard PNG file with raw deflate or gzip
1205 * compressed IDAT or ancillary chunks. Such files can be read and there is
1206 * no warning on read, so this seems like a very bad idea.
1207 */
Guy Schalnate5a37791996-06-05 15:50:50 -05001208 if (window_bits > 15)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001209 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001210 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001211 window_bits = 15;
1212 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001213
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001214 else if (window_bits < 8)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001215 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001216 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001217 window_bits = 8;
1218 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001219
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001220 png_ptr->zlib_window_bits = window_bits;
1221}
1222
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001223void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001224png_set_compression_method(png_structrp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001225{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001226 png_debug(1, "in png_set_compression_method");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001227
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001228 if (png_ptr == NULL)
1229 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001230
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001231 /* This would produce an invalid PNG file if it worked, but it doesn't and
1232 * deflate will fault it, so it is harmless to just warn here.
1233 */
Guy Schalnate5a37791996-06-05 15:50:50 -05001234 if (method != 8)
1235 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001236
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001237 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -05001238}
Glenn Randers-Pehrsond3ff44f2015-02-17 21:06:23 -06001239#endif /* WRITE_CUSTOMIZE_COMPRESSION */
Guy Schalnat0d580581995-07-20 02:43:20 -05001240
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001241/* The following were added to libpng-1.5.4 */
John Bowlerb2bee332011-06-10 23:24:58 -05001242#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001243void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001244png_set_text_compression_level(png_structrp png_ptr, int level)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001245{
1246 png_debug(1, "in png_set_text_compression_level");
1247
1248 if (png_ptr == NULL)
1249 return;
1250
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001251 png_ptr->zlib_text_level = level;
1252}
1253
1254void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001255png_set_text_compression_mem_level(png_structrp png_ptr, int mem_level)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001256{
1257 png_debug(1, "in png_set_text_compression_mem_level");
1258
1259 if (png_ptr == NULL)
1260 return;
1261
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001262 png_ptr->zlib_text_mem_level = mem_level;
1263}
1264
1265void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001266png_set_text_compression_strategy(png_structrp png_ptr, int strategy)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001267{
1268 png_debug(1, "in png_set_text_compression_strategy");
1269
1270 if (png_ptr == NULL)
1271 return;
1272
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001273 png_ptr->zlib_text_strategy = strategy;
1274}
1275
Glenn Randers-Pehrsonaf855e42011-05-07 10:52:49 -05001276/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
1277 * smaller value of window_bits if it can do so safely.
1278 */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001279void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001280png_set_text_compression_window_bits(png_structrp png_ptr, int window_bits)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001281{
1282 if (png_ptr == NULL)
1283 return;
1284
1285 if (window_bits > 15)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001286 {
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001287 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001288 window_bits = 15;
1289 }
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001290
1291 else if (window_bits < 8)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001292 {
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001293 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001294 window_bits = 8;
1295 }
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001296
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001297 png_ptr->zlib_text_window_bits = window_bits;
1298}
1299
1300void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001301png_set_text_compression_method(png_structrp png_ptr, int method)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001302{
1303 png_debug(1, "in png_set_text_compression_method");
1304
1305 if (png_ptr == NULL)
1306 return;
1307
1308 if (method != 8)
1309 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1310
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001311 png_ptr->zlib_text_method = method;
1312}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001313#endif /* WRITE_CUSTOMIZE_ZTXT_COMPRESSION */
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001314/* end of API added to libpng-1.5.4 */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001315
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001316void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001317png_set_write_status_fn(png_structrp png_ptr, png_write_status_ptr write_row_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001318{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001319 if (png_ptr == NULL)
1320 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001321
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001322 png_ptr->write_row_fn = write_row_fn;
1323}
1324
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001325#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001326void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001327png_set_write_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001328 write_user_transform_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001329{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001330 png_debug(1, "in png_set_write_user_transform_fn");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001331
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001332 if (png_ptr == NULL)
1333 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001334
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001335 png_ptr->transformations |= PNG_USER_TRANSFORM;
1336 png_ptr->write_user_transform_fn = write_user_transform_fn;
1337}
1338#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001339
1340
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001341#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001342void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001343png_write_png(png_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001344 int transforms, voidp params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001345{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001346 if (png_ptr == NULL || info_ptr == NULL)
1347 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001348
John Bowler414d7b52014-02-06 11:32:57 -06001349 if ((info_ptr->valid & PNG_INFO_IDAT) == 0)
1350 {
1351 png_app_error(png_ptr, "no rows for png_write_image to write");
1352 return;
1353 }
1354
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001355 /* Write the file header information. */
1356 png_write_info(png_ptr, info_ptr);
1357
1358 /* ------ these transformations don't touch the info structure ------- */
1359
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001360 /* Invert monochrome pixels */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001361 if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001362#ifdef PNG_WRITE_INVERT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001363 png_set_invert_mono(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001364#else
1365 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001366#endif
1367
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001368 /* Shift the pixels up to a legal bit depth and fill in
1369 * as appropriate to correctly scale the image.
1370 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001371 if ((transforms & PNG_TRANSFORM_SHIFT) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001372#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001373 if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001374 png_set_shift(png_ptr, &info_ptr->sig_bit);
1375#else
1376 png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001377#endif
1378
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001379 /* Pack pixels into bytes */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001380 if ((transforms & PNG_TRANSFORM_PACKING) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001381#ifdef PNG_WRITE_PACK_SUPPORTED
1382 png_set_packing(png_ptr);
1383#else
1384 png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001385#endif
1386
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001387 /* Swap location of alpha bytes from ARGB to RGBA */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001388 if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001389#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001390 png_set_swap_alpha(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001391#else
1392 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001393#endif
1394
John Bowler414d7b52014-02-06 11:32:57 -06001395 /* Remove a filler (X) from XRGB/RGBX/AG/GA into to convert it into
1396 * RGB, note that the code expects the input color type to be G or RGB; no
1397 * alpha channel.
1398 */
Glenn Randers-Pehrsonf2d41672014-10-31 21:17:11 -05001399 if ((transforms & (PNG_TRANSFORM_STRIP_FILLER_AFTER|
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001400 PNG_TRANSFORM_STRIP_FILLER_BEFORE)) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001401 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001402#ifdef PNG_WRITE_FILLER_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001403 if ((transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001404 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001405 if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001406 png_app_error(png_ptr,
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001407 "PNG_TRANSFORM_STRIP_FILLER: BEFORE+AFTER not supported");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001408
John Bowler414d7b52014-02-06 11:32:57 -06001409 /* Continue if ignored - this is the pre-1.6.10 behavior */
Glenn Randers-Pehrson01a0e802015-09-24 22:39:53 -05001410 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
John Bowler414d7b52014-02-06 11:32:57 -06001411 }
1412
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001413 else if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0)
Glenn Randers-Pehrson01a0e802015-09-24 22:39:53 -05001414 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
John Bowler414d7b52014-02-06 11:32:57 -06001415#else
1416 png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_FILLER not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001417#endif
John Bowler414d7b52014-02-06 11:32:57 -06001418 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001419
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001420 /* Flip BGR pixels to RGB */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001421 if ((transforms & PNG_TRANSFORM_BGR) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001422#ifdef PNG_WRITE_BGR_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001423 png_set_bgr(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001424#else
1425 png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001426#endif
1427
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001428 /* Swap bytes of 16-bit files to most significant byte first */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001429 if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001430#ifdef PNG_WRITE_SWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001431 png_set_swap(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001432#else
1433 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001434#endif
1435
Glenn Randers-Pehrson8b83ff32015-08-13 20:57:18 -05001436 /* Swap bits of 1-bit, 2-bit, 4-bit packed pixel formats */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001437 if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001438#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001439 png_set_packswap(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001440#else
1441 png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001442#endif
1443
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001444 /* Invert the alpha channel from opacity to transparency */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001445 if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06001446#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001447 png_set_invert_alpha(png_ptr);
John Bowler414d7b52014-02-06 11:32:57 -06001448#else
1449 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001450#endif
1451
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001452 /* ----------------------- end of transformations ------------------- */
1453
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001454 /* Write the bits */
John Bowler414d7b52014-02-06 11:32:57 -06001455 png_write_image(png_ptr, info_ptr->row_pointers);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001456
1457 /* It is REQUIRED to call this to finish writing the rest of the file */
1458 png_write_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001459
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001460 PNG_UNUSED(params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001461}
1462#endif
John Bowler7875d532011-11-07 22:33:49 -06001463
1464
1465#ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
John Bowler7875d532011-11-07 22:33:49 -06001466/* Initialize the write structure - general purpose utility. */
1467static int
1468png_image_write_init(png_imagep image)
1469{
1470 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, image,
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001471 png_safe_error, png_safe_warning);
John Bowler7875d532011-11-07 22:33:49 -06001472
1473 if (png_ptr != NULL)
1474 {
1475 png_infop info_ptr = png_create_info_struct(png_ptr);
1476
1477 if (info_ptr != NULL)
1478 {
John Bowler4fa96a42011-11-16 16:39:16 -06001479 png_controlp control = png_voidcast(png_controlp,
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001480 png_malloc_warn(png_ptr, (sizeof *control)));
John Bowler7875d532011-11-07 22:33:49 -06001481
1482 if (control != NULL)
1483 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001484 memset(control, 0, (sizeof *control));
John Bowler7875d532011-11-07 22:33:49 -06001485
1486 control->png_ptr = png_ptr;
1487 control->info_ptr = info_ptr;
1488 control->for_write = 1;
1489
1490 image->opaque = control;
1491 return 1;
1492 }
1493
1494 /* Error clean up */
1495 png_destroy_info_struct(png_ptr, &info_ptr);
1496 }
1497
1498 png_destroy_write_struct(&png_ptr, NULL);
1499 }
1500
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001501 return png_image_error(image, "png_image_write_: out of memory");
John Bowler7875d532011-11-07 22:33:49 -06001502}
1503
1504/* Arguments to png_image_write_main: */
1505typedef struct
1506{
1507 /* Arguments: */
1508 png_imagep image;
1509 png_const_voidp buffer;
1510 png_int_32 row_stride;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001511 png_const_voidp colormap;
John Bowler7875d532011-11-07 22:33:49 -06001512 int convert_to_8bit;
1513 /* Local variables: */
1514 png_const_voidp first_row;
1515 ptrdiff_t row_bytes;
1516 png_voidp local_row;
John Bowler175a1262016-01-18 09:53:38 -08001517 /* Byte count for memory writing */
1518 png_bytep memory;
1519 png_alloc_size_t memory_bytes; /* not used for STDIO */
1520 png_alloc_size_t output_bytes; /* running total */
John Bowler7875d532011-11-07 22:33:49 -06001521} png_image_write_control;
1522
Glenn Randers-Pehrsonefc4b692011-11-07 23:31:34 -06001523/* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to
1524 * do any necessary byte swapping. The component order is defined by the
John Bowler7875d532011-11-07 22:33:49 -06001525 * png_image format value.
1526 */
1527static int
1528png_write_image_16bit(png_voidp argument)
1529{
John Bowler4fa96a42011-11-16 16:39:16 -06001530 png_image_write_control *display = png_voidcast(png_image_write_control*,
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001531 argument);
John Bowler7875d532011-11-07 22:33:49 -06001532 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06001533 png_structrp png_ptr = image->opaque->png_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001534
John Bowler4fa96a42011-11-16 16:39:16 -06001535 png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001536 display->first_row);
John Bowler4fa96a42011-11-16 16:39:16 -06001537 png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row);
John Bowler7875d532011-11-07 22:33:49 -06001538 png_uint_16p row_end;
Cosmin Trutaceb32772018-08-18 22:47:16 -04001539 unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ?
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -05001540 3 : 1;
John Bowler7875d532011-11-07 22:33:49 -06001541 int aindex = 0;
1542 png_uint_32 y = image->height;
1543
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001544 if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
John Bowler7875d532011-11-07 22:33:49 -06001545 {
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001546# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
1547 if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
1548 {
1549 aindex = -1;
1550 ++input_row; /* To point to the first component */
1551 ++output_row;
1552 }
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001553 else
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -05001554 aindex = (int)channels;
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001555# else
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -05001556 aindex = (int)channels;
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001557# endif
John Bowler7875d532011-11-07 22:33:49 -06001558 }
1559
1560 else
1561 png_error(png_ptr, "png_write_image: internal call error");
1562
1563 /* Work out the output row end and count over this, note that the increment
1564 * above to 'row' means that row_end can actually be beyond the end of the
Glenn Randers-Pehrsonefc4b692011-11-07 23:31:34 -06001565 * row; this is correct.
John Bowler7875d532011-11-07 22:33:49 -06001566 */
1567 row_end = output_row + image->width * (channels+1);
1568
John Bowler319c9852016-09-30 18:37:22 -07001569 for (; y > 0; --y)
John Bowler7875d532011-11-07 22:33:49 -06001570 {
1571 png_const_uint_16p in_ptr = input_row;
1572 png_uint_16p out_ptr = output_row;
1573
1574 while (out_ptr < row_end)
1575 {
Cosmin Trutaceb32772018-08-18 22:47:16 -04001576 png_uint_16 alpha = in_ptr[aindex];
John Bowler7875d532011-11-07 22:33:49 -06001577 png_uint_32 reciprocal = 0;
1578 int c;
1579
1580 out_ptr[aindex] = alpha;
1581
1582 /* Calculate a reciprocal. The correct calculation is simply
Glenn Randers-Pehrsonefc4b692011-11-07 23:31:34 -06001583 * component/alpha*65535 << 15. (I.e. 15 bits of precision); this
John Bowler7875d532011-11-07 22:33:49 -06001584 * allows correct rounding by adding .5 before the shift. 'reciprocal'
1585 * is only initialized when required.
1586 */
1587 if (alpha > 0 && alpha < 65535)
Glenn Randers-Pehrsona8242fe2015-08-17 20:46:27 -05001588 reciprocal = ((0xffff<<15)+(alpha>>1))/alpha;
John Bowler7875d532011-11-07 22:33:49 -06001589
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -05001590 c = (int)channels;
John Bowler7875d532011-11-07 22:33:49 -06001591 do /* always at least one channel */
1592 {
1593 png_uint_16 component = *in_ptr++;
1594
1595 /* The following gives 65535 for an alpha of 0, which is fine,
1596 * otherwise if 0/0 is represented as some other value there is more
1597 * likely to be a discontinuity which will probably damage
1598 * compression when moving from a fully transparent area to a
1599 * nearly transparent one. (The assumption here is that opaque
1600 * areas tend not to be 0 intensity.)
1601 */
1602 if (component >= alpha)
1603 component = 65535;
1604
1605 /* component<alpha, so component/alpha is less than one and
1606 * component*reciprocal is less than 2^31.
1607 */
1608 else if (component > 0 && alpha < 65535)
1609 {
1610 png_uint_32 calc = component * reciprocal;
1611 calc += 16384; /* round to nearest */
1612 component = (png_uint_16)(calc >> 15);
1613 }
1614
1615 *out_ptr++ = component;
1616 }
1617 while (--c > 0);
1618
1619 /* Skip to next component (skip the intervening alpha channel) */
1620 ++in_ptr;
1621 ++out_ptr;
1622 }
1623
John Bowler4fa96a42011-11-16 16:39:16 -06001624 png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row));
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -05001625 input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16));
John Bowler7875d532011-11-07 22:33:49 -06001626 }
1627
1628 return 1;
1629}
1630
1631/* Given 16-bit input (1 to 4 channels) write 8-bit output. If an alpha channel
1632 * is present it must be removed from the components, the components are then
1633 * written in sRGB encoding. No components are added or removed.
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001634 *
1635 * Calculate an alpha reciprocal to reverse pre-multiplication. As above the
1636 * calculation can be done to 15 bits of accuracy; however, the output needs to
1637 * be scaled in the range 0..255*65535, so include that scaling here.
John Bowler7875d532011-11-07 22:33:49 -06001638 */
Cosmin Truta24714342018-07-01 23:32:08 -04001639# define UNP_RECIPROCAL(alpha) ((((0xffff*0xff)<<7)+((alpha)>>1))/(alpha))
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001640
1641static png_byte
1642png_unpremultiply(png_uint_32 component, png_uint_32 alpha,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001643 png_uint_32 reciprocal/*from the above macro*/)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001644{
1645 /* The following gives 1.0 for an alpha of 0, which is fine, otherwise if 0/0
1646 * is represented as some other value there is more likely to be a
1647 * discontinuity which will probably damage compression when moving from a
1648 * fully transparent area to a nearly transparent one. (The assumption here
1649 * is that opaque areas tend not to be 0 intensity.)
1650 *
1651 * There is a rounding problem here; if alpha is less than 128 it will end up
1652 * as 0 when scaled to 8 bits. To avoid introducing spurious colors into the
1653 * output change for this too.
1654 */
1655 if (component >= alpha || alpha < 128)
1656 return 255;
1657
1658 /* component<alpha, so component/alpha is less than one and
1659 * component*reciprocal is less than 2^31.
1660 */
1661 else if (component > 0)
1662 {
1663 /* The test is that alpha/257 (rounded) is less than 255, the first value
1664 * that becomes 255 is 65407.
1665 * NOTE: this must agree with the PNG_DIV257 macro (which must, therefore,
1666 * be exact!) [Could also test reciprocal != 0]
1667 */
1668 if (alpha < 65407)
1669 {
1670 component *= reciprocal;
1671 component += 64; /* round to nearest */
1672 component >>= 7;
1673 }
1674
1675 else
1676 component *= 255;
1677
1678 /* Convert the component to sRGB. */
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05001679 return (png_byte)PNG_sRGB_FROM_LINEAR(component);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001680 }
1681
1682 else
1683 return 0;
1684}
1685
John Bowler7875d532011-11-07 22:33:49 -06001686static int
1687png_write_image_8bit(png_voidp argument)
1688{
John Bowler4fa96a42011-11-16 16:39:16 -06001689 png_image_write_control *display = png_voidcast(png_image_write_control*,
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001690 argument);
John Bowler7875d532011-11-07 22:33:49 -06001691 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06001692 png_structrp png_ptr = image->opaque->png_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001693
John Bowler4fa96a42011-11-16 16:39:16 -06001694 png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001695 display->first_row);
John Bowler4fa96a42011-11-16 16:39:16 -06001696 png_bytep output_row = png_voidcast(png_bytep, display->local_row);
John Bowler7875d532011-11-07 22:33:49 -06001697 png_uint_32 y = image->height;
Cosmin Trutaceb32772018-08-18 22:47:16 -04001698 unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ?
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -05001699 3 : 1;
John Bowler7875d532011-11-07 22:33:49 -06001700
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001701 if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
John Bowler7875d532011-11-07 22:33:49 -06001702 {
1703 png_bytep row_end;
1704 int aindex;
1705
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001706# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
1707 if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
1708 {
1709 aindex = -1;
1710 ++input_row; /* To point to the first component */
1711 ++output_row;
1712 }
John Bowler7875d532011-11-07 22:33:49 -06001713
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001714 else
1715# endif
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -05001716 aindex = (int)channels;
John Bowler7875d532011-11-07 22:33:49 -06001717
1718 /* Use row_end in place of a loop counter: */
1719 row_end = output_row + image->width * (channels+1);
1720
John Bowler319c9852016-09-30 18:37:22 -07001721 for (; y > 0; --y)
John Bowler7875d532011-11-07 22:33:49 -06001722 {
1723 png_const_uint_16p in_ptr = input_row;
1724 png_bytep out_ptr = output_row;
1725
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001726 while (out_ptr < row_end)
John Bowler7875d532011-11-07 22:33:49 -06001727 {
1728 png_uint_16 alpha = in_ptr[aindex];
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05001729 png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
John Bowler7875d532011-11-07 22:33:49 -06001730 png_uint_32 reciprocal = 0;
1731 int c;
1732
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001733 /* Scale and write the alpha channel. */
1734 out_ptr[aindex] = alphabyte;
John Bowler7875d532011-11-07 22:33:49 -06001735
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001736 if (alphabyte > 0 && alphabyte < 255)
1737 reciprocal = UNP_RECIPROCAL(alpha);
John Bowler7875d532011-11-07 22:33:49 -06001738
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -05001739 c = (int)channels;
John Bowler7875d532011-11-07 22:33:49 -06001740 do /* always at least one channel */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001741 *out_ptr++ = png_unpremultiply(*in_ptr++, alpha, reciprocal);
John Bowler7875d532011-11-07 22:33:49 -06001742 while (--c > 0);
1743
1744 /* Skip to next component (skip the intervening alpha channel) */
1745 ++in_ptr;
1746 ++out_ptr;
1747 } /* while out_ptr < row_end */
John Bowler3615d032011-11-08 10:38:09 -06001748
John Bowler4fa96a42011-11-16 16:39:16 -06001749 png_write_row(png_ptr, png_voidcast(png_const_bytep,
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001750 display->local_row));
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -05001751 input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16));
John Bowler7875d532011-11-07 22:33:49 -06001752 } /* while y */
1753 }
1754
1755 else
1756 {
1757 /* No alpha channel, so the row_end really is the end of the row and it
1758 * is sufficient to loop over the components one by one.
1759 */
1760 png_bytep row_end = output_row + image->width * channels;
1761
John Bowler319c9852016-09-30 18:37:22 -07001762 for (; y > 0; --y)
John Bowler7875d532011-11-07 22:33:49 -06001763 {
1764 png_const_uint_16p in_ptr = input_row;
1765 png_bytep out_ptr = output_row;
1766
1767 while (out_ptr < row_end)
1768 {
1769 png_uint_32 component = *in_ptr++;
1770
1771 component *= 255;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05001772 *out_ptr++ = (png_byte)PNG_sRGB_FROM_LINEAR(component);
John Bowler7875d532011-11-07 22:33:49 -06001773 }
1774
1775 png_write_row(png_ptr, output_row);
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -05001776 input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16));
John Bowler7875d532011-11-07 22:33:49 -06001777 }
1778 }
1779
1780 return 1;
1781}
1782
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001783static void
1784png_image_set_PLTE(png_image_write_control *display)
1785{
Cosmin Trutaceb32772018-08-18 22:47:16 -04001786 png_imagep image = display->image;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001787 const void *cmap = display->colormap;
Cosmin Trutaceb32772018-08-18 22:47:16 -04001788 int entries = image->colormap_entries > 256 ? 256 :
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001789 (int)image->colormap_entries;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001790
1791 /* NOTE: the caller must check for cmap != NULL and entries != 0 */
Cosmin Trutaceb32772018-08-18 22:47:16 -04001792 png_uint_32 format = image->format;
1793 unsigned int channels = PNG_IMAGE_SAMPLE_CHANNELS(format);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001794
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001795# if defined(PNG_FORMAT_BGR_SUPPORTED) &&\
Glenn Randers-Pehrsonc9120502013-11-22 14:58:04 -06001796 defined(PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED)
Cosmin Trutaceb32772018-08-18 22:47:16 -04001797 int afirst = (format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001798 (format & PNG_FORMAT_FLAG_ALPHA) != 0;
1799# else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001800# define afirst 0
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001801# endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001802
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001803# ifdef PNG_FORMAT_BGR_SUPPORTED
Cosmin Trutaceb32772018-08-18 22:47:16 -04001804 int bgr = (format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0;
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001805# else
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001806# define bgr 0
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001807# endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001808
1809 int i, num_trans;
1810 png_color palette[256];
1811 png_byte tRNS[256];
1812
1813 memset(tRNS, 255, (sizeof tRNS));
1814 memset(palette, 0, (sizeof palette));
1815
1816 for (i=num_trans=0; i<entries; ++i)
1817 {
1818 /* This gets automatically converted to sRGB with reversal of the
1819 * pre-multiplication if the color-map has an alpha channel.
1820 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001821 if ((format & PNG_FORMAT_FLAG_LINEAR) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001822 {
1823 png_const_uint_16p entry = png_voidcast(png_const_uint_16p, cmap);
1824
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -05001825 entry += (unsigned int)i * channels;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001826
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001827 if ((channels & 1) != 0) /* no alpha */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001828 {
1829 if (channels >= 3) /* RGB */
1830 {
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05001831 palette[i].blue = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001832 entry[(2 ^ bgr)]);
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05001833 palette[i].green = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001834 entry[1]);
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05001835 palette[i].red = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001836 entry[bgr]);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001837 }
1838
1839 else /* Gray */
1840 palette[i].blue = palette[i].red = palette[i].green =
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05001841 (png_byte)PNG_sRGB_FROM_LINEAR(255 * *entry);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001842 }
1843
1844 else /* alpha */
1845 {
1846 png_uint_16 alpha = entry[afirst ? 0 : channels-1];
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05001847 png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001848 png_uint_32 reciprocal = 0;
1849
1850 /* Calculate a reciprocal, as in the png_write_image_8bit code above
1851 * this is designed to produce a value scaled to 255*65535 when
1852 * divided by 128 (i.e. asr 7).
1853 */
1854 if (alphabyte > 0 && alphabyte < 255)
Glenn Randers-Pehrsona8242fe2015-08-17 20:46:27 -05001855 reciprocal = (((0xffff*0xff)<<7)+(alpha>>1))/alpha;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001856
1857 tRNS[i] = alphabyte;
1858 if (alphabyte < 255)
1859 num_trans = i+1;
1860
1861 if (channels >= 3) /* RGB */
1862 {
1863 palette[i].blue = png_unpremultiply(entry[afirst + (2 ^ bgr)],
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001864 alpha, reciprocal);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001865 palette[i].green = png_unpremultiply(entry[afirst + 1], alpha,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001866 reciprocal);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001867 palette[i].red = png_unpremultiply(entry[afirst + bgr], alpha,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001868 reciprocal);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001869 }
1870
1871 else /* gray */
1872 palette[i].blue = palette[i].red = palette[i].green =
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001873 png_unpremultiply(entry[afirst], alpha, reciprocal);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001874 }
1875 }
1876
1877 else /* Color-map has sRGB values */
1878 {
1879 png_const_bytep entry = png_voidcast(png_const_bytep, cmap);
1880
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -05001881 entry += (unsigned int)i * channels;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001882
1883 switch (channels)
1884 {
1885 case 4:
1886 tRNS[i] = entry[afirst ? 0 : 3];
1887 if (tRNS[i] < 255)
1888 num_trans = i+1;
John Bowler72d07d32017-07-11 07:50:35 -05001889 /* FALLTHROUGH */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001890 case 3:
1891 palette[i].blue = entry[afirst + (2 ^ bgr)];
1892 palette[i].green = entry[afirst + 1];
1893 palette[i].red = entry[afirst + bgr];
1894 break;
1895
1896 case 2:
1897 tRNS[i] = entry[1 ^ afirst];
1898 if (tRNS[i] < 255)
1899 num_trans = i+1;
John Bowler72d07d32017-07-11 07:50:35 -05001900 /* FALLTHROUGH */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001901 case 1:
1902 palette[i].blue = palette[i].red = palette[i].green =
1903 entry[afirst];
1904 break;
1905
1906 default:
1907 break;
1908 }
1909 }
1910 }
1911
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001912# ifdef afirst
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001913# undef afirst
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001914# endif
1915# ifdef bgr
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001916# undef bgr
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001917# endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001918
1919 png_set_PLTE(image->opaque->png_ptr, image->opaque->info_ptr, palette,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001920 entries);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001921
1922 if (num_trans > 0)
1923 png_set_tRNS(image->opaque->png_ptr, image->opaque->info_ptr, tRNS,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001924 num_trans, NULL);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001925
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -05001926 image->colormap_entries = (png_uint_32)entries;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001927}
1928
John Bowler7875d532011-11-07 22:33:49 -06001929static int
1930png_image_write_main(png_voidp argument)
1931{
John Bowler4fa96a42011-11-16 16:39:16 -06001932 png_image_write_control *display = png_voidcast(png_image_write_control*,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001933 argument);
John Bowler7875d532011-11-07 22:33:49 -06001934 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06001935 png_structrp png_ptr = image->opaque->png_ptr;
1936 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001937 png_uint_32 format = image->format;
1938
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001939 /* The following four ints are actually booleans */
1940 int colormap = (format & PNG_FORMAT_FLAG_COLORMAP);
1941 int linear = !colormap && (format & PNG_FORMAT_FLAG_LINEAR); /* input */
1942 int alpha = !colormap && (format & PNG_FORMAT_FLAG_ALPHA);
Glenn Randers-Pehrson90dd1852017-09-22 17:29:59 -05001943 int write_16bit = linear && (display->convert_to_8bit == 0);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001944
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001945# ifdef PNG_BENIGN_ERRORS_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001946 /* Make sure we error out on any bad situation */
1947 png_set_benign_errors(png_ptr, 0/*error*/);
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05001948# endif
John Bowleraa816c42012-03-16 07:39:49 -05001949
John Bowler175a1262016-01-18 09:53:38 -08001950 /* Default the 'row_stride' parameter if required, also check the row stride
1951 * and total image size to ensure that they are within the system limits.
1952 */
1953 {
Cosmin Trutaceb32772018-08-18 22:47:16 -04001954 unsigned int channels = PNG_IMAGE_PIXEL_CHANNELS(image->format);
John Bowler175a1262016-01-18 09:53:38 -08001955
Glenn Randers-Pehrson13bdd8b2016-10-05 19:34:09 -05001956 if (image->width <= 0x7fffffffU/channels) /* no overflow */
John Bowler175a1262016-01-18 09:53:38 -08001957 {
1958 png_uint_32 check;
Cosmin Trutaceb32772018-08-18 22:47:16 -04001959 png_uint_32 png_row_stride = image->width * channels;
John Bowler175a1262016-01-18 09:53:38 -08001960
1961 if (display->row_stride == 0)
1962 display->row_stride = (png_int_32)/*SAFE*/png_row_stride;
1963
1964 if (display->row_stride < 0)
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -05001965 check = (png_uint_32)(-display->row_stride);
John Bowler175a1262016-01-18 09:53:38 -08001966
1967 else
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -05001968 check = (png_uint_32)display->row_stride;
John Bowler175a1262016-01-18 09:53:38 -08001969
1970 if (check >= png_row_stride)
1971 {
1972 /* Now check for overflow of the image buffer calculation; this
1973 * limits the whole image size to 32 bits for API compatibility with
1974 * the current, 32-bit, PNG_IMAGE_BUFFER_SIZE macro.
1975 */
Glenn Randers-Pehrson13bdd8b2016-10-05 19:34:09 -05001976 if (image->height > 0xffffffffU/png_row_stride)
John Bowler175a1262016-01-18 09:53:38 -08001977 png_error(image->opaque->png_ptr, "memory image too large");
1978 }
1979
1980 else
1981 png_error(image->opaque->png_ptr, "supplied row stride too small");
1982 }
1983
1984 else
1985 png_error(image->opaque->png_ptr, "image row stride too large");
1986 }
John Bowler3706d732011-11-21 10:28:06 -06001987
John Bowler7875d532011-11-07 22:33:49 -06001988 /* Set the required transforms then write the rows in the correct order. */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001989 if ((format & PNG_FORMAT_FLAG_COLORMAP) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001990 {
1991 if (display->colormap != NULL && image->colormap_entries > 0)
1992 {
1993 png_uint_32 entries = image->colormap_entries;
1994
1995 png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001996 entries > 16 ? 8 : (entries > 4 ? 4 : (entries > 2 ? 2 : 1)),
1997 PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
1998 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001999
2000 png_image_set_PLTE(display);
2001 }
2002
2003 else
2004 png_error(image->opaque->png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002005 "no color-map for color-mapped image");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002006 }
2007
2008 else
2009 png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002010 write_16bit ? 16 : 8,
2011 ((format & PNG_FORMAT_FLAG_COLOR) ? PNG_COLOR_MASK_COLOR : 0) +
2012 ((format & PNG_FORMAT_FLAG_ALPHA) ? PNG_COLOR_MASK_ALPHA : 0),
2013 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
John Bowler7875d532011-11-07 22:33:49 -06002014
2015 /* Counter-intuitively the data transformations must be called *after*
2016 * png_write_info, not before as in the read code, but the 'set' functions
2017 * must still be called before. Just set the color space information, never
2018 * write an interlaced image.
2019 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002020
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06002021 if (write_16bit != 0)
John Bowler7875d532011-11-07 22:33:49 -06002022 {
2023 /* The gamma here is 1.0 (linear) and the cHRM chunk matches sRGB. */
2024 png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_LINEAR);
John Bowlerdd819152011-11-08 14:29:45 -06002025
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002026 if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0)
John Bowlerdd819152011-11-08 14:29:45 -06002027 png_set_cHRM_fixed(png_ptr, info_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002028 /* color x y */
2029 /* white */ 31270, 32900,
2030 /* red */ 64000, 33000,
2031 /* green */ 30000, 60000,
2032 /* blue */ 15000, 6000
John Bowlerdd819152011-11-08 14:29:45 -06002033 );
John Bowler7875d532011-11-07 22:33:49 -06002034 }
2035
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002036 else if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0)
John Bowler7875d532011-11-07 22:33:49 -06002037 png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL);
2038
John Bowlerdd819152011-11-08 14:29:45 -06002039 /* Else writing an 8-bit file and the *colors* aren't sRGB, but the 8-bit
2040 * space must still be gamma encoded.
2041 */
2042 else
2043 png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE);
2044
John Bowler7875d532011-11-07 22:33:49 -06002045 /* Write the file header. */
2046 png_write_info(png_ptr, info_ptr);
2047
2048 /* Now set up the data transformations (*after* the header is written),
2049 * remove the handled transformations from the 'format' flags for checking.
John Bowlere6fb6912011-11-08 21:35:16 -06002050 *
Glenn Randers-Pehrson8b83ff32015-08-13 20:57:18 -05002051 * First check for a little endian system if writing 16-bit files.
John Bowler7875d532011-11-07 22:33:49 -06002052 */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06002053 if (write_16bit != 0)
John Bowler7875d532011-11-07 22:33:49 -06002054 {
Cosmin Trutaceb32772018-08-18 22:47:16 -04002055 png_uint_16 le = 0x0001;
John Bowler7875d532011-11-07 22:33:49 -06002056
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002057 if ((*(png_const_bytep) & le) != 0)
John Bowler7875d532011-11-07 22:33:49 -06002058 png_set_swap(png_ptr);
2059 }
2060
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05002061# ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002062 if ((format & PNG_FORMAT_FLAG_BGR) != 0)
John Bowler7875d532011-11-07 22:33:49 -06002063 {
Glenn Randers-Pehrsonebba0742014-10-25 12:22:39 -05002064 if (colormap == 0 && (format & PNG_FORMAT_FLAG_COLOR) != 0)
John Bowlere6fb6912011-11-08 21:35:16 -06002065 png_set_bgr(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06002066 format &= ~PNG_FORMAT_FLAG_BGR;
2067 }
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05002068# endif
John Bowler7875d532011-11-07 22:33:49 -06002069
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05002070# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002071 if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
John Bowler7875d532011-11-07 22:33:49 -06002072 {
Glenn Randers-Pehrsonebba0742014-10-25 12:22:39 -05002073 if (colormap == 0 && (format & PNG_FORMAT_FLAG_ALPHA) != 0)
John Bowlere6fb6912011-11-08 21:35:16 -06002074 png_set_swap_alpha(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06002075 format &= ~PNG_FORMAT_FLAG_AFIRST;
2076 }
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05002077# endif
John Bowler7875d532011-11-07 22:33:49 -06002078
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002079 /* If there are 16 or fewer color-map entries we wrote a lower bit depth
2080 * above, but the application data is still byte packed.
2081 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002082 if (colormap != 0 && image->colormap_entries <= 16)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002083 png_set_packing(png_ptr);
2084
John Bowlere6fb6912011-11-08 21:35:16 -06002085 /* That should have handled all (both) the transforms. */
John Bowler89c2f842011-11-16 12:04:39 -06002086 if ((format & ~(png_uint_32)(PNG_FORMAT_FLAG_COLOR | PNG_FORMAT_FLAG_LINEAR |
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002087 PNG_FORMAT_FLAG_ALPHA | PNG_FORMAT_FLAG_COLORMAP)) != 0)
John Bowler7875d532011-11-07 22:33:49 -06002088 png_error(png_ptr, "png_write_image: unsupported transformation");
2089
2090 {
John Bowler4fa96a42011-11-16 16:39:16 -06002091 png_const_bytep row = png_voidcast(png_const_bytep, display->buffer);
John Bowler7875d532011-11-07 22:33:49 -06002092 ptrdiff_t row_bytes = display->row_stride;
2093
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06002094 if (linear != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002095 row_bytes *= (sizeof (png_uint_16));
John Bowler7875d532011-11-07 22:33:49 -06002096
2097 if (row_bytes < 0)
2098 row += (image->height-1) * (-row_bytes);
2099
2100 display->first_row = row;
2101 display->row_bytes = row_bytes;
2102 }
2103
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002104 /* Apply 'fast' options if the flag is set. */
2105 if ((image->flags & PNG_IMAGE_FLAG_FAST) != 0)
2106 {
2107 png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_NO_FILTERS);
2108 /* NOTE: determined by experiment using pngstest, this reflects some
2109 * balance between the time to write the image once and the time to read
2110 * it about 50 times. The speed-up in pngstest was about 10-20% of the
2111 * total (user) time on a heavily loaded system.
2112 */
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05002113# ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002114 png_set_compression_level(png_ptr, 3);
Glenn Randers-Pehrsonf86720c2015-06-11 22:00:14 -05002115# endif
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002116 }
2117
John Bowler7875d532011-11-07 22:33:49 -06002118 /* Check for the cases that currently require a pre-transform on the row
2119 * before it is written. This only applies when the input is 16-bit and
2120 * either there is an alpha channel or it is converted to 8-bit.
2121 */
Glenn Randers-Pehrsonebba0742014-10-25 12:22:39 -05002122 if ((linear != 0 && alpha != 0 ) ||
2123 (colormap == 0 && display->convert_to_8bit != 0))
John Bowler7875d532011-11-07 22:33:49 -06002124 {
John Bowler4fa96a42011-11-16 16:39:16 -06002125 png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002126 png_get_rowbytes(png_ptr, info_ptr)));
John Bowler7875d532011-11-07 22:33:49 -06002127 int result;
2128
2129 display->local_row = row;
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06002130 if (write_16bit != 0)
John Bowler7875d532011-11-07 22:33:49 -06002131 result = png_safe_execute(image, png_write_image_16bit, display);
2132 else
2133 result = png_safe_execute(image, png_write_image_8bit, display);
2134 display->local_row = NULL;
2135
2136 png_free(png_ptr, row);
2137
2138 /* Skip the 'write_end' on error: */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06002139 if (result == 0)
John Bowler7875d532011-11-07 22:33:49 -06002140 return 0;
2141 }
2142
2143 /* Otherwise this is the case where the input is in a format currently
2144 * supported by the rest of the libpng write code; call it directly.
2145 */
2146 else
2147 {
John Bowler4fa96a42011-11-16 16:39:16 -06002148 png_const_bytep row = png_voidcast(png_const_bytep, display->first_row);
John Bowler7875d532011-11-07 22:33:49 -06002149 ptrdiff_t row_bytes = display->row_bytes;
2150 png_uint_32 y = image->height;
2151
John Bowler319c9852016-09-30 18:37:22 -07002152 for (; y > 0; --y)
John Bowler7875d532011-11-07 22:33:49 -06002153 {
2154 png_write_row(png_ptr, row);
2155 row += row_bytes;
2156 }
2157 }
2158
2159 png_write_end(png_ptr, info_ptr);
2160 return 1;
2161}
2162
John Bowler175a1262016-01-18 09:53:38 -08002163
2164static void (PNGCBAPI
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04002165image_memory_write)(png_structp png_ptr, png_bytep/*const*/ data, size_t size)
John Bowler175a1262016-01-18 09:53:38 -08002166{
2167 png_image_write_control *display = png_voidcast(png_image_write_control*,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002168 png_ptr->io_ptr/*backdoor: png_get_io_ptr(png_ptr)*/);
Cosmin Trutaceb32772018-08-18 22:47:16 -04002169 png_alloc_size_t ob = display->output_bytes;
John Bowler175a1262016-01-18 09:53:38 -08002170
2171 /* Check for overflow; this should never happen: */
2172 if (size <= ((png_alloc_size_t)-1) - ob)
2173 {
2174 /* I don't think libpng ever does this, but just in case: */
2175 if (size > 0)
2176 {
2177 if (display->memory_bytes >= ob+size) /* writing */
2178 memcpy(display->memory+ob, data, size);
2179
2180 /* Always update the size: */
2181 display->output_bytes = ob+size;
2182 }
2183 }
2184
2185 else
2186 png_error(png_ptr, "png_image_write_to_memory: PNG too big");
2187}
2188
2189static void (PNGCBAPI
2190image_memory_flush)(png_structp png_ptr)
2191{
2192 PNG_UNUSED(png_ptr)
2193}
2194
2195static int
2196png_image_write_memory(png_voidp argument)
2197{
2198 png_image_write_control *display = png_voidcast(png_image_write_control*,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002199 argument);
John Bowler175a1262016-01-18 09:53:38 -08002200
2201 /* The rest of the memory-specific init and write_main in an error protected
2202 * environment. This case needs to use callbacks for the write operations
2203 * since libpng has no built in support for writing to memory.
2204 */
2205 png_set_write_fn(display->image->opaque->png_ptr, display/*io_ptr*/,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002206 image_memory_write, image_memory_flush);
John Bowler175a1262016-01-18 09:53:38 -08002207
2208 return png_image_write_main(display);
2209}
2210
2211int PNGAPI
2212png_image_write_to_memory(png_imagep image, void *memory,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002213 png_alloc_size_t * PNG_RESTRICT memory_bytes, int convert_to_8bit,
2214 const void *buffer, png_int_32 row_stride, const void *colormap)
John Bowler175a1262016-01-18 09:53:38 -08002215{
2216 /* Write the image to the given buffer, or count the bytes if it is NULL */
2217 if (image != NULL && image->version == PNG_IMAGE_VERSION)
2218 {
2219 if (memory_bytes != NULL && buffer != NULL)
2220 {
2221 /* This is to give the caller an easier error detection in the NULL
2222 * case and guard against uninitialized variable problems:
2223 */
2224 if (memory == NULL)
2225 *memory_bytes = 0;
2226
2227 if (png_image_write_init(image) != 0)
2228 {
2229 png_image_write_control display;
2230 int result;
2231
2232 memset(&display, 0, (sizeof display));
2233 display.image = image;
2234 display.buffer = buffer;
2235 display.row_stride = row_stride;
2236 display.colormap = colormap;
2237 display.convert_to_8bit = convert_to_8bit;
2238 display.memory = png_voidcast(png_bytep, memory);
2239 display.memory_bytes = *memory_bytes;
2240 display.output_bytes = 0;
2241
2242 result = png_safe_execute(image, png_image_write_memory, &display);
2243 png_image_free(image);
2244
2245 /* write_memory returns true even if we ran out of buffer. */
2246 if (result)
2247 {
2248 /* On out-of-buffer this function returns '0' but still updates
2249 * memory_bytes:
2250 */
2251 if (memory != NULL && display.output_bytes > *memory_bytes)
2252 result = 0;
2253
2254 *memory_bytes = display.output_bytes;
2255 }
2256
2257 return result;
2258 }
2259
2260 else
2261 return 0;
2262 }
2263
2264 else
2265 return png_image_error(image,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002266 "png_image_write_to_memory: invalid argument");
John Bowler175a1262016-01-18 09:53:38 -08002267 }
2268
2269 else if (image != NULL)
2270 return png_image_error(image,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002271 "png_image_write_to_memory: incorrect PNG_IMAGE_VERSION");
John Bowler175a1262016-01-18 09:53:38 -08002272
2273 else
2274 return 0;
2275}
2276
2277#ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED
John Bowler7875d532011-11-07 22:33:49 -06002278int PNGAPI
John Bowlerdd819152011-11-08 14:29:45 -06002279png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002280 const void *buffer, png_int_32 row_stride, const void *colormap)
John Bowler7875d532011-11-07 22:33:49 -06002281{
2282 /* Write the image to the given (FILE*). */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002283 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06002284 {
John Bowler175a1262016-01-18 09:53:38 -08002285 if (file != NULL && buffer != NULL)
John Bowler7875d532011-11-07 22:33:49 -06002286 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002287 if (png_image_write_init(image) != 0)
John Bowler7875d532011-11-07 22:33:49 -06002288 {
2289 png_image_write_control display;
2290 int result;
2291
2292 /* This is slightly evil, but png_init_io doesn't do anything other
2293 * than this and we haven't changed the standard IO functions so
2294 * this saves a 'safe' function.
2295 */
2296 image->opaque->png_ptr->io_ptr = file;
2297
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002298 memset(&display, 0, (sizeof display));
John Bowler7875d532011-11-07 22:33:49 -06002299 display.image = image;
2300 display.buffer = buffer;
2301 display.row_stride = row_stride;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002302 display.colormap = colormap;
John Bowler7875d532011-11-07 22:33:49 -06002303 display.convert_to_8bit = convert_to_8bit;
2304
2305 result = png_safe_execute(image, png_image_write_main, &display);
2306 png_image_free(image);
2307 return result;
2308 }
2309
2310 else
2311 return 0;
2312 }
2313
2314 else
2315 return png_image_error(image,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002316 "png_image_write_to_stdio: invalid argument");
John Bowler7875d532011-11-07 22:33:49 -06002317 }
2318
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002319 else if (image != NULL)
2320 return png_image_error(image,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002321 "png_image_write_to_stdio: incorrect PNG_IMAGE_VERSION");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002322
John Bowler7875d532011-11-07 22:33:49 -06002323 else
2324 return 0;
2325}
2326
2327int PNGAPI
John Bowlerdd819152011-11-08 14:29:45 -06002328png_image_write_to_file(png_imagep image, const char *file_name,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002329 int convert_to_8bit, const void *buffer, png_int_32 row_stride,
2330 const void *colormap)
John Bowler7875d532011-11-07 22:33:49 -06002331{
2332 /* Write the image to the named file. */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002333 if (image != NULL && image->version == PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06002334 {
John Bowler175a1262016-01-18 09:53:38 -08002335 if (file_name != NULL && buffer != NULL)
John Bowler7875d532011-11-07 22:33:49 -06002336 {
2337 FILE *fp = fopen(file_name, "wb");
2338
2339 if (fp != NULL)
2340 {
John Bowlerdd819152011-11-08 14:29:45 -06002341 if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002342 row_stride, colormap) != 0)
John Bowler7875d532011-11-07 22:33:49 -06002343 {
John Bowlerdd819152011-11-08 14:29:45 -06002344 int error; /* from fflush/fclose */
John Bowler7875d532011-11-07 22:33:49 -06002345
John Bowlerdd819152011-11-08 14:29:45 -06002346 /* Make sure the file is flushed correctly. */
2347 if (fflush(fp) == 0 && ferror(fp) == 0)
John Bowler7875d532011-11-07 22:33:49 -06002348 {
John Bowlerdd819152011-11-08 14:29:45 -06002349 if (fclose(fp) == 0)
2350 return 1;
John Bowler7875d532011-11-07 22:33:49 -06002351
John Bowlerdd819152011-11-08 14:29:45 -06002352 error = errno; /* from fclose */
John Bowler7875d532011-11-07 22:33:49 -06002353 }
2354
John Bowlerdd819152011-11-08 14:29:45 -06002355 else
2356 {
2357 error = errno; /* from fflush or ferror */
2358 (void)fclose(fp);
2359 }
2360
2361 (void)remove(file_name);
2362 /* The image has already been cleaned up; this is just used to
2363 * set the error (because the original write succeeded).
2364 */
2365 return png_image_error(image, strerror(error));
John Bowler7875d532011-11-07 22:33:49 -06002366 }
2367
2368 else
2369 {
2370 /* Clean up: just the opened file. */
2371 (void)fclose(fp);
John Bowlerdd819152011-11-08 14:29:45 -06002372 (void)remove(file_name);
John Bowler7875d532011-11-07 22:33:49 -06002373 return 0;
2374 }
2375 }
2376
2377 else
2378 return png_image_error(image, strerror(errno));
2379 }
2380
2381 else
2382 return png_image_error(image,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002383 "png_image_write_to_file: invalid argument");
John Bowler7875d532011-11-07 22:33:49 -06002384 }
2385
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002386 else if (image != NULL)
2387 return png_image_error(image,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05002388 "png_image_write_to_file: incorrect PNG_IMAGE_VERSION");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06002389
John Bowler7875d532011-11-07 22:33:49 -06002390 else
2391 return 0;
2392}
John Bowler175a1262016-01-18 09:53:38 -08002393#endif /* SIMPLIFIED_WRITE_STDIO */
John Bowler7875d532011-11-07 22:33:49 -06002394#endif /* SIMPLIFIED_WRITE */
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06002395#endif /* WRITE */