blob: 0d4ee9f7ae0ea156f0757d444232ce4df8e2b796 [file] [log] [blame]
The Android Open Source Project893912b2009-03-03 19:30:05 -08001
2/* pngwrite.c - general routines to write a PNG file
3 *
Matt Sarett9ea75692016-01-08 13:00:42 -05004 * Last changed in libpng 1.6.19 [November 12, 2015]
5 * Copyright (c) 1998-2015 Glenn Randers-Pehrson
The Android Open Source Project893912b2009-03-03 19:30:05 -08006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04008 *
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
The Android Open Source Project893912b2009-03-03 19:30:05 -080012 */
13
Chris Craikb50c2172013-07-29 15:28:30 -070014#include "pngpriv.h"
15#if defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
16# include <errno.h>
17#endif
18
The Android Open Source Project893912b2009-03-03 19:30:05 -080019#ifdef PNG_WRITE_SUPPORTED
20
Chris Craikb50c2172013-07-29 15:28:30 -070021#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
22/* Write out all the unknown chunks for the current given location */
23static void
24write_unknown_chunks(png_structrp png_ptr, png_const_inforp info_ptr,
25 unsigned int where)
26{
Matt Sarett9ea75692016-01-08 13:00:42 -050027 if (info_ptr->unknown_chunks_num != 0)
Chris Craikb50c2172013-07-29 15:28:30 -070028 {
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)
Matt Sarett9ea75692016-01-08 13:00:42 -050036 if ((up->location & where) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -070037 {
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}
Matt Sarett9ea75692016-01-08 13:00:42 -050072#endif /* WRITE_UNKNOWN_CHUNKS */
Chris Craikb50c2172013-07-29 15:28:30 -070073
The Android Open Source Project893912b2009-03-03 19:30:05 -080074/* 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
77 * after the image data, put it in png_write_end(). I strongly encourage
78 * 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 */
83void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -070084png_write_info_before_PLTE(png_structrp png_ptr, png_const_inforp info_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -080085{
The Android Open Source Project4215dd12009-03-09 11:52:12 -070086 png_debug(1, "in png_write_info_before_PLTE");
Patrick Scott5f6bd842010-06-28 16:55:16 -040087
The Android Open Source Project893912b2009-03-03 19:30:05 -080088 if (png_ptr == NULL || info_ptr == NULL)
89 return;
Chris Craikb50c2172013-07-29 15:28:30 -070090
Matt Sarett9ea75692016-01-08 13:00:42 -050091 if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -080092 {
Matt Sarett9ea75692016-01-08 13:00:42 -050093 /* Write PNG signature */
94 png_write_sig(png_ptr);
Chris Craikb50c2172013-07-29 15:28:30 -070095
Patrick Scott5f6bd842010-06-28 16:55:16 -040096#ifdef PNG_MNG_FEATURES_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -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 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800104#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700105
Matt Sarett9ea75692016-01-08 13:00:42 -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,
Patrick Scott5f6bd842010-06-28 16:55:16 -0400110#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -0500111 info_ptr->interlace_type
The Android Open Source Project893912b2009-03-03 19:30:05 -0800112#else
Matt Sarett9ea75692016-01-08 13:00:42 -0500113 0
The Android Open Source Project893912b2009-03-03 19:30:05 -0800114#endif
Matt Sarett9ea75692016-01-08 13:00:42 -0500115 );
Chris Craikb50c2172013-07-29 15:28:30 -0700116
Matt Sarett9ea75692016-01-08 13:00:42 -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
128 * the application continues writing the PNG. So check the 'invalid'
129 * flag here too.
130 */
Chris Craikb50c2172013-07-29 15:28:30 -0700131#ifdef PNG_GAMMA_SUPPORTED
132# ifdef PNG_WRITE_gAMA_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -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)
Chris Craikb50c2172013-07-29 15:28:30 -0700136 png_write_gAMA_fixed(png_ptr, info_ptr->colorspace.gamma);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800137# endif
138#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700139
140#ifdef PNG_COLORSPACE_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -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 */
Chris Craikb50c2172013-07-29 15:28:30 -0700144# ifdef PNG_WRITE_iCCP_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -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,
151 "profile matches sRGB but writing iCCP instead");
152# endif
Chris Craikb50c2172013-07-29 15:28:30 -0700153
Matt Sarett9ea75692016-01-08 13:00:42 -0500154 png_write_iCCP(png_ptr, info_ptr->iccp_name,
155 info_ptr->iccp_profile);
156 }
Chris Craikb50c2172013-07-29 15:28:30 -0700157# ifdef PNG_WRITE_sRGB_SUPPORTED
158 else
159# endif
160# endif
161
162# ifdef PNG_WRITE_sRGB_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -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);
Chris Craikb50c2172013-07-29 15:28:30 -0700166# endif /* WRITE_sRGB */
167#endif /* COLORSPACE */
168
Patrick Scott5f6bd842010-06-28 16:55:16 -0400169#ifdef PNG_WRITE_sBIT_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -0500170 if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
171 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800172#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700173
174#ifdef PNG_COLORSPACE_SUPPORTED
175# ifdef PNG_WRITE_cHRM_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -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);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800180# endif
181#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700182
Patrick Scott5f6bd842010-06-28 16:55:16 -0400183#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -0500184 write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_IHDR);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800185#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700186
The Android Open Source Project893912b2009-03-03 19:30:05 -0800187 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
188 }
189}
190
191void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700192png_write_info(png_structrp png_ptr, png_const_inforp info_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800193{
194#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
195 int i;
196#endif
197
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700198 png_debug(1, "in png_write_info");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800199
200 if (png_ptr == NULL || info_ptr == NULL)
201 return;
202
203 png_write_info_before_PLTE(png_ptr, info_ptr);
204
Matt Sarett9ea75692016-01-08 13:00:42 -0500205 if ((info_ptr->valid & PNG_INFO_PLTE) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800206 png_write_PLTE(png_ptr, info_ptr->palette,
Chris Craikb50c2172013-07-29 15:28:30 -0700207 (png_uint_32)info_ptr->num_palette);
208
The Android Open Source Project893912b2009-03-03 19:30:05 -0800209 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
210 png_error(png_ptr, "Valid palette required for paletted images");
211
Patrick Scott5f6bd842010-06-28 16:55:16 -0400212#ifdef PNG_WRITE_tRNS_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -0500213 if ((info_ptr->valid & PNG_INFO_tRNS) !=0)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400214 {
Patrick Scott5f6bd842010-06-28 16:55:16 -0400215#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400216 /* Invert the alpha channel (in tRNS) */
Matt Sarett9ea75692016-01-08 13:00:42 -0500217 if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0 &&
Chris Craikb50c2172013-07-29 15:28:30 -0700218 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400219 {
Matt Sarett9ea75692016-01-08 13:00:42 -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)
Chris Craikb50c2172013-07-29 15:28:30 -0700227 info_ptr->trans_alpha[j] =
228 (png_byte)(255 - info_ptr->trans_alpha[j]);
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400229 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800230#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700231 png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
232 info_ptr->num_trans, info_ptr->color_type);
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400233 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800234#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -0400235#ifdef PNG_WRITE_bKGD_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -0500236 if ((info_ptr->valid & PNG_INFO_bKGD) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800237 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
238#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700239
Patrick Scott5f6bd842010-06-28 16:55:16 -0400240#ifdef PNG_WRITE_hIST_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -0500241 if ((info_ptr->valid & PNG_INFO_hIST) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800242 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
243#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700244
Patrick Scott5f6bd842010-06-28 16:55:16 -0400245#ifdef PNG_WRITE_oFFs_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -0500246 if ((info_ptr->valid & PNG_INFO_oFFs) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800247 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
Chris Craikb50c2172013-07-29 15:28:30 -0700248 info_ptr->offset_unit_type);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800249#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700250
Patrick Scott5f6bd842010-06-28 16:55:16 -0400251#ifdef PNG_WRITE_pCAL_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -0500252 if ((info_ptr->valid & PNG_INFO_pCAL) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800253 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
Chris Craikb50c2172013-07-29 15:28:30 -0700254 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
255 info_ptr->pcal_units, info_ptr->pcal_params);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800256#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400257
Patrick Scott5f6bd842010-06-28 16:55:16 -0400258#ifdef PNG_WRITE_sCAL_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -0500259 if ((info_ptr->valid & PNG_INFO_sCAL) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800260 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
261 info_ptr->scal_s_width, info_ptr->scal_s_height);
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400262#endif /* sCAL */
263
Patrick Scott5f6bd842010-06-28 16:55:16 -0400264#ifdef PNG_WRITE_pHYs_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -0500265 if ((info_ptr->valid & PNG_INFO_pHYs) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800266 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
Chris Craikb50c2172013-07-29 15:28:30 -0700267 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400268#endif /* pHYs */
269
Patrick Scott5f6bd842010-06-28 16:55:16 -0400270#ifdef PNG_WRITE_tIME_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -0500271 if ((info_ptr->valid & PNG_INFO_tIME) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800272 {
273 png_write_tIME(png_ptr, &(info_ptr->mod_time));
274 png_ptr->mode |= PNG_WROTE_tIME;
275 }
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400276#endif /* tIME */
277
Patrick Scott5f6bd842010-06-28 16:55:16 -0400278#ifdef PNG_WRITE_sPLT_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -0500279 if ((info_ptr->valid & PNG_INFO_sPLT) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700280 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
281 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400282#endif /* sPLT */
283
Patrick Scott5f6bd842010-06-28 16:55:16 -0400284#ifdef PNG_WRITE_TEXT_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800285 /* Check to see if we need to write text chunks */
286 for (i = 0; i < info_ptr->num_text; i++)
287 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700288 png_debug2(2, "Writing header text chunk %d, type %d", i,
Chris Craikb50c2172013-07-29 15:28:30 -0700289 info_ptr->text[i].compression);
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400290 /* An internationalized chunk? */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800291 if (info_ptr->text[i].compression > 0)
292 {
Patrick Scott5f6bd842010-06-28 16:55:16 -0400293#ifdef PNG_WRITE_iTXt_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700294 /* Write international chunk */
295 png_write_iTXt(png_ptr,
296 info_ptr->text[i].compression,
297 info_ptr->text[i].key,
298 info_ptr->text[i].lang,
299 info_ptr->text[i].lang_key,
300 info_ptr->text[i].text);
Matt Sarett9ea75692016-01-08 13:00:42 -0500301 /* Mark this chunk as written */
302 if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
303 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
304 else
305 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800306#else
Matt Sarett9ea75692016-01-08 13:00:42 -0500307 png_warning(png_ptr, "Unable to write international text");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800308#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800309 }
Chris Craikb50c2172013-07-29 15:28:30 -0700310
The Android Open Source Project893912b2009-03-03 19:30:05 -0800311 /* If we want a compressed text chunk */
312 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
313 {
Patrick Scott5f6bd842010-06-28 16:55:16 -0400314#ifdef PNG_WRITE_zTXt_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400315 /* Write compressed chunk */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800316 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Matt Sarett9ea75692016-01-08 13:00:42 -0500317 info_ptr->text[i].text, info_ptr->text[i].compression);
318 /* Mark this chunk as written */
319 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800320#else
321 png_warning(png_ptr, "Unable to write compressed text");
322#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800323 }
Chris Craikb50c2172013-07-29 15:28:30 -0700324
The Android Open Source Project893912b2009-03-03 19:30:05 -0800325 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
326 {
Patrick Scott5f6bd842010-06-28 16:55:16 -0400327#ifdef PNG_WRITE_tEXt_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400328 /* Write uncompressed chunk */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800329 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Chris Craikb50c2172013-07-29 15:28:30 -0700330 info_ptr->text[i].text,
331 0);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800332 /* Mark this chunk as written */
333 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400334#else
335 /* Can't get here */
336 png_warning(png_ptr, "Unable to write uncompressed text");
337#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800338 }
339 }
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400340#endif /* tEXt */
341
Patrick Scott5f6bd842010-06-28 16:55:16 -0400342#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700343 write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_PLTE);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800344#endif
345}
346
347/* Writes the end of the PNG file. If you don't want to write comments or
348 * time information, you can pass NULL for info. If you already wrote these
349 * in png_write_info(), do not write them again here. If you have long
350 * comments, I suggest writing them here, and compressing them.
351 */
352void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700353png_write_end(png_structrp png_ptr, png_inforp info_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800354{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700355 png_debug(1, "in png_write_end");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400356
The Android Open Source Project893912b2009-03-03 19:30:05 -0800357 if (png_ptr == NULL)
358 return;
Chris Craikb50c2172013-07-29 15:28:30 -0700359
Matt Sarett9ea75692016-01-08 13:00:42 -0500360 if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800361 png_error(png_ptr, "No IDATs written into file");
362
Chris Craikb50c2172013-07-29 15:28:30 -0700363#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
364 if (png_ptr->num_palette_max > png_ptr->num_palette)
365 png_benign_error(png_ptr, "Wrote palette index exceeding num_palette");
366#endif
367
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400368 /* See if user wants us to write information chunks */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800369 if (info_ptr != NULL)
370 {
Patrick Scott5f6bd842010-06-28 16:55:16 -0400371#ifdef PNG_WRITE_TEXT_SUPPORTED
372 int i; /* local index variable */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800373#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -0400374#ifdef PNG_WRITE_tIME_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400375 /* Check to see if user has supplied a time chunk */
Matt Sarett9ea75692016-01-08 13:00:42 -0500376 if ((info_ptr->valid & PNG_INFO_tIME) != 0 &&
377 (png_ptr->mode & PNG_WROTE_tIME) == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800378 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Chris Craikb50c2172013-07-29 15:28:30 -0700379
The Android Open Source Project893912b2009-03-03 19:30:05 -0800380#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -0400381#ifdef PNG_WRITE_TEXT_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400382 /* Loop through comment chunks */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800383 for (i = 0; i < info_ptr->num_text; i++)
384 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700385 png_debug2(2, "Writing trailer text chunk %d, type %d", i,
The Android Open Source Project893912b2009-03-03 19:30:05 -0800386 info_ptr->text[i].compression);
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400387 /* An internationalized chunk? */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800388 if (info_ptr->text[i].compression > 0)
389 {
Patrick Scott5f6bd842010-06-28 16:55:16 -0400390#ifdef PNG_WRITE_iTXt_SUPPORTED
391 /* Write international chunk */
392 png_write_iTXt(png_ptr,
Chris Craikb50c2172013-07-29 15:28:30 -0700393 info_ptr->text[i].compression,
394 info_ptr->text[i].key,
395 info_ptr->text[i].lang,
396 info_ptr->text[i].lang_key,
397 info_ptr->text[i].text);
Matt Sarett9ea75692016-01-08 13:00:42 -0500398 /* Mark this chunk as written */
399 if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
400 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
401 else
402 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800403#else
Patrick Scott5f6bd842010-06-28 16:55:16 -0400404 png_warning(png_ptr, "Unable to write international text");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800405#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800406 }
Chris Craikb50c2172013-07-29 15:28:30 -0700407
The Android Open Source Project893912b2009-03-03 19:30:05 -0800408 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
409 {
Patrick Scott5f6bd842010-06-28 16:55:16 -0400410#ifdef PNG_WRITE_zTXt_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400411 /* Write compressed chunk */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800412 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Matt Sarett9ea75692016-01-08 13:00:42 -0500413 info_ptr->text[i].text, info_ptr->text[i].compression);
414 /* Mark this chunk as written */
415 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800416#else
417 png_warning(png_ptr, "Unable to write compressed text");
418#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800419 }
Chris Craikb50c2172013-07-29 15:28:30 -0700420
The Android Open Source Project893912b2009-03-03 19:30:05 -0800421 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
422 {
Patrick Scott5f6bd842010-06-28 16:55:16 -0400423#ifdef PNG_WRITE_tEXt_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400424 /* Write uncompressed chunk */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800425 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Chris Craikb50c2172013-07-29 15:28:30 -0700426 info_ptr->text[i].text, 0);
Matt Sarett9ea75692016-01-08 13:00:42 -0500427 /* Mark this chunk as written */
428 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800429#else
430 png_warning(png_ptr, "Unable to write uncompressed text");
431#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800432 }
433 }
434#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -0400435#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700436 write_unknown_chunks(png_ptr, info_ptr, PNG_AFTER_IDAT);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800437#endif
438 }
439
440 png_ptr->mode |= PNG_AFTER_IDAT;
441
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400442 /* Write end of PNG file */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800443 png_write_IEND(png_ptr);
Matt Sarett9ea75692016-01-08 13:00:42 -0500444
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700445 /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
446 * and restored again in libpng-1.2.30, may cause some applications that
447 * do not set png_ptr->output_flush_fn to crash. If your application
448 * experiences a problem, please try building libpng with
449 * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
Patrick Scott5f6bd842010-06-28 16:55:16 -0400450 * png-mng-implement at lists.sf.net .
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700451 */
Patrick Scott5f6bd842010-06-28 16:55:16 -0400452#ifdef PNG_WRITE_FLUSH_SUPPORTED
453# ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700454 png_flush(png_ptr);
Patrick Scott5f6bd842010-06-28 16:55:16 -0400455# endif
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700456#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800457}
458
Patrick Scott5f6bd842010-06-28 16:55:16 -0400459#ifdef PNG_CONVERT_tIME_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800460void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700461png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm * ttime)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800462{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700463 png_debug(1, "in png_convert_from_struct_tm");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400464
The Android Open Source Project893912b2009-03-03 19:30:05 -0800465 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
466 ptime->month = (png_byte)(ttime->tm_mon + 1);
467 ptime->day = (png_byte)ttime->tm_mday;
468 ptime->hour = (png_byte)ttime->tm_hour;
469 ptime->minute = (png_byte)ttime->tm_min;
470 ptime->second = (png_byte)ttime->tm_sec;
471}
472
473void PNGAPI
474png_convert_from_time_t(png_timep ptime, time_t ttime)
475{
476 struct tm *tbuf;
477
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700478 png_debug(1, "in png_convert_from_time_t");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400479
The Android Open Source Project893912b2009-03-03 19:30:05 -0800480 tbuf = gmtime(&ttime);
481 png_convert_from_struct_tm(ptime, tbuf);
482}
483#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800484
485/* Initialize png_ptr structure, and allocate any memory needed */
Chris Craikb50c2172013-07-29 15:28:30 -0700486PNG_FUNCTION(png_structp,PNGAPI
487png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
488 png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800489{
Chris Craikb50c2172013-07-29 15:28:30 -0700490#ifndef PNG_USER_MEM_SUPPORTED
491 png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
Matt Sarett9ea75692016-01-08 13:00:42 -0500492 error_fn, warn_fn, NULL, NULL, NULL);
Chris Craikb50c2172013-07-29 15:28:30 -0700493#else
494 return png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
495 warn_fn, NULL, NULL, NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800496}
497
498/* Alternate initialize png_ptr structure, and allocate any memory needed */
Chris Craikb50c2172013-07-29 15:28:30 -0700499PNG_FUNCTION(png_structp,PNGAPI
500png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
501 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
502 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800503{
Chris Craikb50c2172013-07-29 15:28:30 -0700504 png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
Matt Sarett9ea75692016-01-08 13:00:42 -0500505 error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
506#endif /* USER_MEM */
Chris Craikb50c2172013-07-29 15:28:30 -0700507 if (png_ptr != NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800508 {
Chris Craikb50c2172013-07-29 15:28:30 -0700509 /* Set the zlib control values to defaults; they can be overridden by the
510 * application after the struct has been created.
511 */
512 png_ptr->zbuffer_size = PNG_ZBUF_SIZE;
513
514 /* The 'zlib_strategy' setting is irrelevant because png_default_claim in
515 * pngwutil.c defaults it according to whether or not filters will be
516 * used, and ignores this setting.
517 */
518 png_ptr->zlib_strategy = PNG_Z_DEFAULT_STRATEGY;
519 png_ptr->zlib_level = PNG_Z_DEFAULT_COMPRESSION;
520 png_ptr->zlib_mem_level = 8;
521 png_ptr->zlib_window_bits = 15;
522 png_ptr->zlib_method = 8;
523
524#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
525 png_ptr->zlib_text_strategy = PNG_TEXT_Z_DEFAULT_STRATEGY;
526 png_ptr->zlib_text_level = PNG_TEXT_Z_DEFAULT_COMPRESSION;
527 png_ptr->zlib_text_mem_level = 8;
528 png_ptr->zlib_text_window_bits = 15;
529 png_ptr->zlib_text_method = 8;
Matt Sarett9ea75692016-01-08 13:00:42 -0500530#endif /* WRITE_COMPRESSED_TEXT */
Chris Craikb50c2172013-07-29 15:28:30 -0700531
532 /* This is a highly dubious configuration option; by default it is off,
533 * but it may be appropriate for private builds that are testing
534 * extensions not conformant to the current specification, or of
535 * applications that must not fail to write at all costs!
536 */
537#ifdef PNG_BENIGN_WRITE_ERRORS_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -0700538 /* In stable builds only warn if an application error can be completely
539 * handled.
540 */
Matt Sarett9ea75692016-01-08 13:00:42 -0500541 png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800542#endif
543
Chris Craikb50c2172013-07-29 15:28:30 -0700544 /* App warnings are warnings in release (or release candidate) builds but
545 * are errors during development.
546 */
Matt Sarett9ea75692016-01-08 13:00:42 -0500547#if PNG_RELEASE_BUILD
Chris Craikb50c2172013-07-29 15:28:30 -0700548 png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
549#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800550
Chris Craikb50c2172013-07-29 15:28:30 -0700551 /* TODO: delay this, it can be done in png_init_io() (if the app doesn't
552 * do it itself) avoiding setting the default function if it is not
553 * required.
554 */
555 png_set_write_fn(png_ptr, NULL, NULL, NULL);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800556 }
557
Chris Craikb50c2172013-07-29 15:28:30 -0700558 return png_ptr;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800559}
560
The Android Open Source Project893912b2009-03-03 19:30:05 -0800561
562/* Write a few rows of image data. If the image is interlaced,
563 * either you will have to write the 7 sub images, or, if you
564 * have called png_set_interlace_handling(), you will have to
565 * "write" the image seven times.
566 */
567void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700568png_write_rows(png_structrp png_ptr, png_bytepp row,
569 png_uint_32 num_rows)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800570{
Patrick Scott5f6bd842010-06-28 16:55:16 -0400571 png_uint_32 i; /* row counter */
572 png_bytepp rp; /* row pointer */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800573
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700574 png_debug(1, "in png_write_rows");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800575
576 if (png_ptr == NULL)
577 return;
578
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400579 /* Loop through the rows */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800580 for (i = 0, rp = row; i < num_rows; i++, rp++)
581 {
582 png_write_row(png_ptr, *rp);
583 }
584}
585
586/* Write the image. You only need to call this function once, even
587 * if you are writing an interlaced image.
588 */
589void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700590png_write_image(png_structrp png_ptr, png_bytepp image)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800591{
Patrick Scott5f6bd842010-06-28 16:55:16 -0400592 png_uint_32 i; /* row index */
593 int pass, num_pass; /* pass variables */
594 png_bytepp rp; /* points to current row */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800595
596 if (png_ptr == NULL)
597 return;
598
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700599 png_debug(1, "in png_write_image");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400600
601#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400602 /* Initialize interlace handling. If image is not interlaced,
603 * this will set pass to 1
604 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800605 num_pass = png_set_interlace_handling(png_ptr);
606#else
607 num_pass = 1;
608#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400609 /* Loop through passes */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800610 for (pass = 0; pass < num_pass; pass++)
611 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400612 /* Loop through image */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800613 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
614 {
615 png_write_row(png_ptr, *rp);
616 }
617 }
618}
619
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530620#ifdef PNG_MNG_FEATURES_SUPPORTED
621/* Performs intrapixel differencing */
622static void
623png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
624{
625 png_debug(1, "in png_do_write_intrapixel");
626
Matt Sarett9ea75692016-01-08 13:00:42 -0500627 if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530628 {
629 int bytes_per_pixel;
630 png_uint_32 row_width = row_info->width;
631 if (row_info->bit_depth == 8)
632 {
633 png_bytep rp;
634 png_uint_32 i;
635
636 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
637 bytes_per_pixel = 3;
638
639 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
640 bytes_per_pixel = 4;
641
642 else
643 return;
644
645 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
646 {
Matt Sarett9ea75692016-01-08 13:00:42 -0500647 *(rp) = (png_byte)(*rp - *(rp + 1));
648 *(rp + 2) = (png_byte)(*(rp + 2) - *(rp + 1));
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530649 }
650 }
651
652#ifdef PNG_WRITE_16BIT_SUPPORTED
653 else if (row_info->bit_depth == 16)
654 {
655 png_bytep rp;
656 png_uint_32 i;
657
658 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
659 bytes_per_pixel = 6;
660
661 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
662 bytes_per_pixel = 8;
663
664 else
665 return;
666
667 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
668 {
669 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
670 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
671 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
672 png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
673 png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
Matt Sarett9ea75692016-01-08 13:00:42 -0500674 *(rp ) = (png_byte)(red >> 8);
675 *(rp + 1) = (png_byte)red;
676 *(rp + 4) = (png_byte)(blue >> 8);
677 *(rp + 5) = (png_byte)blue;
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530678 }
679 }
Matt Sarett9ea75692016-01-08 13:00:42 -0500680#endif /* WRITE_16BIT */
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530681 }
682}
Matt Sarett9ea75692016-01-08 13:00:42 -0500683#endif /* MNG_FEATURES */
Sireesh Tripurarib478e662014-05-09 15:15:10 +0530684
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400685/* Called by user to write a row of image data */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800686void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700687png_write_row(png_structrp png_ptr, png_const_bytep row)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800688{
Chris Craikb50c2172013-07-29 15:28:30 -0700689 /* 1.5.6: moved from png_struct to be a local structure: */
690 png_row_info row_info;
691
The Android Open Source Project893912b2009-03-03 19:30:05 -0800692 if (png_ptr == NULL)
693 return;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400694
Chris Craikb50c2172013-07-29 15:28:30 -0700695 png_debug2(1, "in png_write_row (row %u, pass %d)",
The Android Open Source Project893912b2009-03-03 19:30:05 -0800696 png_ptr->row_number, png_ptr->pass);
697
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400698 /* Initialize transformations and other stuff if first time */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800699 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
700 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400701 /* Make sure we wrote the header info */
Matt Sarett9ea75692016-01-08 13:00:42 -0500702 if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400703 png_error(png_ptr,
Chris Craikb50c2172013-07-29 15:28:30 -0700704 "png_write_info was never called before png_write_row");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800705
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400706 /* Check for transforms that have been set but were defined out */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800707#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
Matt Sarett9ea75692016-01-08 13:00:42 -0500708 if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700709 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800710#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700711
The Android Open Source Project893912b2009-03-03 19:30:05 -0800712#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
Matt Sarett9ea75692016-01-08 13:00:42 -0500713 if ((png_ptr->transformations & PNG_FILLER) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700714 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800715#endif
Patrick Scott5f6bd842010-06-28 16:55:16 -0400716#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
717 defined(PNG_READ_PACKSWAP_SUPPORTED)
Matt Sarett9ea75692016-01-08 13:00:42 -0500718 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
Patrick Scott5f6bd842010-06-28 16:55:16 -0400719 png_warning(png_ptr,
Chris Craikb50c2172013-07-29 15:28:30 -0700720 "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800721#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700722
The Android Open Source Project893912b2009-03-03 19:30:05 -0800723#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
Matt Sarett9ea75692016-01-08 13:00:42 -0500724 if ((png_ptr->transformations & PNG_PACK) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700725 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800726#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700727
The Android Open Source Project893912b2009-03-03 19:30:05 -0800728#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
Matt Sarett9ea75692016-01-08 13:00:42 -0500729 if ((png_ptr->transformations & PNG_SHIFT) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700730 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800731#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700732
The Android Open Source Project893912b2009-03-03 19:30:05 -0800733#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
Matt Sarett9ea75692016-01-08 13:00:42 -0500734 if ((png_ptr->transformations & PNG_BGR) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700735 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800736#endif
Chris Craikb50c2172013-07-29 15:28:30 -0700737
The Android Open Source Project893912b2009-03-03 19:30:05 -0800738#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
Matt Sarett9ea75692016-01-08 13:00:42 -0500739 if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700740 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800741#endif
742
743 png_write_start_row(png_ptr);
744 }
745
Patrick Scott5f6bd842010-06-28 16:55:16 -0400746#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400747 /* If interlaced and not interested in row, return */
Matt Sarett9ea75692016-01-08 13:00:42 -0500748 if (png_ptr->interlaced != 0 &&
749 (png_ptr->transformations & PNG_INTERLACE) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800750 {
751 switch (png_ptr->pass)
752 {
753 case 0:
Matt Sarett9ea75692016-01-08 13:00:42 -0500754 if ((png_ptr->row_number & 0x07) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800755 {
756 png_write_finish_row(png_ptr);
757 return;
758 }
759 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700760
The Android Open Source Project893912b2009-03-03 19:30:05 -0800761 case 1:
Matt Sarett9ea75692016-01-08 13:00:42 -0500762 if ((png_ptr->row_number & 0x07) != 0 || png_ptr->width < 5)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800763 {
764 png_write_finish_row(png_ptr);
765 return;
766 }
767 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700768
The Android Open Source Project893912b2009-03-03 19:30:05 -0800769 case 2:
770 if ((png_ptr->row_number & 0x07) != 4)
771 {
772 png_write_finish_row(png_ptr);
773 return;
774 }
775 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700776
The Android Open Source Project893912b2009-03-03 19:30:05 -0800777 case 3:
Matt Sarett9ea75692016-01-08 13:00:42 -0500778 if ((png_ptr->row_number & 0x03) != 0 || png_ptr->width < 3)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800779 {
780 png_write_finish_row(png_ptr);
781 return;
782 }
783 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700784
The Android Open Source Project893912b2009-03-03 19:30:05 -0800785 case 4:
786 if ((png_ptr->row_number & 0x03) != 2)
787 {
788 png_write_finish_row(png_ptr);
789 return;
790 }
791 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700792
The Android Open Source Project893912b2009-03-03 19:30:05 -0800793 case 5:
Matt Sarett9ea75692016-01-08 13:00:42 -0500794 if ((png_ptr->row_number & 0x01) != 0 || png_ptr->width < 2)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800795 {
796 png_write_finish_row(png_ptr);
797 return;
798 }
799 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700800
The Android Open Source Project893912b2009-03-03 19:30:05 -0800801 case 6:
Matt Sarett9ea75692016-01-08 13:00:42 -0500802 if ((png_ptr->row_number & 0x01) == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800803 {
804 png_write_finish_row(png_ptr);
805 return;
806 }
807 break;
Chris Craikb50c2172013-07-29 15:28:30 -0700808
809 default: /* error: ignore it */
810 break;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800811 }
812 }
813#endif
814
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400815 /* Set up row info for transformations */
Chris Craikb50c2172013-07-29 15:28:30 -0700816 row_info.color_type = png_ptr->color_type;
817 row_info.width = png_ptr->usr_width;
818 row_info.channels = png_ptr->usr_channels;
819 row_info.bit_depth = png_ptr->usr_bit_depth;
820 row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels);
821 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800822
Chris Craikb50c2172013-07-29 15:28:30 -0700823 png_debug1(3, "row_info->color_type = %d", row_info.color_type);
824 png_debug1(3, "row_info->width = %u", row_info.width);
825 png_debug1(3, "row_info->channels = %d", row_info.channels);
826 png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth);
827 png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth);
828 png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800829
830 /* Copy user's row into buffer, leaving room for filter byte. */
Chris Craikb50c2172013-07-29 15:28:30 -0700831 memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800832
Patrick Scott5f6bd842010-06-28 16:55:16 -0400833#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400834 /* Handle interlacing */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800835 if (png_ptr->interlaced && png_ptr->pass < 6 &&
Matt Sarett9ea75692016-01-08 13:00:42 -0500836 (png_ptr->transformations & PNG_INTERLACE) != 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800837 {
Chris Craikb50c2172013-07-29 15:28:30 -0700838 png_do_write_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass);
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400839 /* This should always get caught above, but still ... */
Matt Sarett9ea75692016-01-08 13:00:42 -0500840 if (row_info.width == 0)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800841 {
842 png_write_finish_row(png_ptr);
843 return;
844 }
845 }
846#endif
847
Chris Craikb50c2172013-07-29 15:28:30 -0700848#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400849 /* Handle other transformations */
Matt Sarett9ea75692016-01-08 13:00:42 -0500850 if (png_ptr->transformations != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700851 png_do_write_transformations(png_ptr, &row_info);
852#endif
853
854 /* At this point the row_info pixel depth must match the 'transformed' depth,
855 * which is also the output depth.
856 */
857 if (row_info.pixel_depth != png_ptr->pixel_depth ||
Matt Sarett9ea75692016-01-08 13:00:42 -0500858 row_info.pixel_depth != png_ptr->transformed_pixel_depth)
Chris Craikb50c2172013-07-29 15:28:30 -0700859 png_error(png_ptr, "internal write transform logic error");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800860
Patrick Scott5f6bd842010-06-28 16:55:16 -0400861#ifdef PNG_MNG_FEATURES_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800862 /* Write filter_method 64 (intrapixel differencing) only if
863 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
864 * 2. Libpng did not write a PNG signature (this filter_method is only
865 * used in PNG datastreams that are embedded in MNG datastreams) and
866 * 3. The application called png_permit_mng_features with a mask that
867 * included PNG_FLAG_MNG_FILTER_64 and
868 * 4. The filter_method is 64 and
869 * 5. The color_type is RGB or RGBA
870 */
Matt Sarett9ea75692016-01-08 13:00:42 -0500871 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
Chris Craikb50c2172013-07-29 15:28:30 -0700872 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
The Android Open Source Project893912b2009-03-03 19:30:05 -0800873 {
874 /* Intrapixel differencing */
Chris Craikb50c2172013-07-29 15:28:30 -0700875 png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800876 }
877#endif
878
Chris Craikb50c2172013-07-29 15:28:30 -0700879/* Added at libpng-1.5.10 */
880#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
881 /* Check for out-of-range palette index */
882 if (row_info.color_type == PNG_COLOR_TYPE_PALETTE &&
883 png_ptr->num_palette_max >= 0)
884 png_do_check_palette_indexes(png_ptr, &row_info);
885#endif
886
The Android Open Source Project893912b2009-03-03 19:30:05 -0800887 /* Find a filter if necessary, filter the row and write it out. */
Chris Craikb50c2172013-07-29 15:28:30 -0700888 png_write_find_filter(png_ptr, &row_info);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800889
890 if (png_ptr->write_row_fn != NULL)
891 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
892}
893
Patrick Scott5f6bd842010-06-28 16:55:16 -0400894#ifdef PNG_WRITE_FLUSH_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800895/* Set the automatic flush interval or 0 to turn flushing off */
896void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700897png_set_flush(png_structrp png_ptr, int nrows)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800898{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700899 png_debug(1, "in png_set_flush");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400900
The Android Open Source Project893912b2009-03-03 19:30:05 -0800901 if (png_ptr == NULL)
902 return;
Chris Craikb50c2172013-07-29 15:28:30 -0700903
The Android Open Source Project893912b2009-03-03 19:30:05 -0800904 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
905}
906
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400907/* Flush the current output buffers now */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800908void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700909png_write_flush(png_structrp png_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800910{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700911 png_debug(1, "in png_write_flush");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400912
The Android Open Source Project893912b2009-03-03 19:30:05 -0800913 if (png_ptr == NULL)
914 return;
Chris Craikb50c2172013-07-29 15:28:30 -0700915
The Android Open Source Project893912b2009-03-03 19:30:05 -0800916 /* We have already written out all of the data */
917 if (png_ptr->row_number >= png_ptr->num_rows)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400918 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800919
Chris Craikb50c2172013-07-29 15:28:30 -0700920 png_compress_IDAT(png_ptr, NULL, 0, Z_SYNC_FLUSH);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800921 png_ptr->flush_rows = 0;
922 png_flush(png_ptr);
923}
Matt Sarett9ea75692016-01-08 13:00:42 -0500924#endif /* WRITE_FLUSH */
Chris Craikb50c2172013-07-29 15:28:30 -0700925
926/* Free any memory used in png_ptr struct without freeing the struct itself. */
927static void
928png_write_destroy(png_structrp png_ptr)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800929{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700930 png_debug(1, "in png_write_destroy");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400931
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400932 /* Free any memory zlib uses */
Matt Sarett9ea75692016-01-08 13:00:42 -0500933 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -0700934 deflateEnd(&png_ptr->zstream);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800935
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400936 /* Free our memory. png_free checks NULL for us. */
Chris Craikb50c2172013-07-29 15:28:30 -0700937 png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800938 png_free(png_ptr, png_ptr->row_buf);
Matt Sarett9ea75692016-01-08 13:00:42 -0500939 png_ptr->row_buf = NULL;
Patrick Scott5f6bd842010-06-28 16:55:16 -0400940#ifdef PNG_WRITE_FILTER_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -0800941 png_free(png_ptr, png_ptr->prev_row);
Matt Sarett9ea75692016-01-08 13:00:42 -0500942 png_free(png_ptr, png_ptr->try_row);
943 png_free(png_ptr, png_ptr->tst_row);
944 png_ptr->prev_row = NULL;
945 png_ptr->try_row = NULL;
946 png_ptr->tst_row = NULL;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800947#endif
948
Chris Craikb50c2172013-07-29 15:28:30 -0700949#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
950 png_free(png_ptr, png_ptr->chunk_list);
Matt Sarett9ea75692016-01-08 13:00:42 -0500951 png_ptr->chunk_list = NULL;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800952#endif
953
Chris Craikb50c2172013-07-29 15:28:30 -0700954 /* The error handling and memory handling information is left intact at this
955 * point: the jmp_buf may still have to be freed. See png_destroy_png_struct
956 * for how this happens.
957 */
958}
The Android Open Source Project893912b2009-03-03 19:30:05 -0800959
Chris Craikb50c2172013-07-29 15:28:30 -0700960/* Free all memory used by the write.
961 * In libpng 1.6.0 this API changed quietly to no longer accept a NULL value for
962 * *png_ptr_ptr. Prior to 1.6.0 it would accept such a value and it would free
963 * the passed in info_structs but it would quietly fail to free any of the data
964 * inside them. In 1.6.0 it quietly does nothing (it has to be quiet because it
965 * has no png_ptr.)
966 */
967void PNGAPI
968png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
969{
970 png_debug(1, "in png_destroy_write_struct");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800971
Chris Craikb50c2172013-07-29 15:28:30 -0700972 if (png_ptr_ptr != NULL)
973 {
974 png_structrp png_ptr = *png_ptr_ptr;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800975
Chris Craikb50c2172013-07-29 15:28:30 -0700976 if (png_ptr != NULL) /* added in libpng 1.6.0 */
977 {
978 png_destroy_info_struct(png_ptr, info_ptr_ptr);
979
980 *png_ptr_ptr = NULL;
981 png_write_destroy(png_ptr);
982 png_destroy_png_struct(png_ptr);
983 }
984 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800985}
986
987/* Allow the application to select one or more row filters to use. */
988void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -0700989png_set_filter(png_structrp png_ptr, int method, int filters)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800990{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700991 png_debug(1, "in png_set_filter");
Patrick Scott5f6bd842010-06-28 16:55:16 -0400992
The Android Open Source Project893912b2009-03-03 19:30:05 -0800993 if (png_ptr == NULL)
994 return;
Chris Craikb50c2172013-07-29 15:28:30 -0700995
Patrick Scott5f6bd842010-06-28 16:55:16 -0400996#ifdef PNG_MNG_FEATURES_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -0500997 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
Chris Craikb50c2172013-07-29 15:28:30 -0700998 (method == PNG_INTRAPIXEL_DIFFERENCING))
999 method = PNG_FILTER_TYPE_BASE;
1000
The Android Open Source Project893912b2009-03-03 19:30:05 -08001001#endif
1002 if (method == PNG_FILTER_TYPE_BASE)
1003 {
1004 switch (filters & (PNG_ALL_FILTERS | 0x07))
1005 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04001006#ifdef PNG_WRITE_FILTER_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001007 case 5:
1008 case 6:
Chris Craikb50c2172013-07-29 15:28:30 -07001009 case 7: png_app_error(png_ptr, "Unknown row filter for method 0");
1010 /* FALL THROUGH */
Matt Sarett9ea75692016-01-08 13:00:42 -05001011#endif /* WRITE_FILTER */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001012 case PNG_FILTER_VALUE_NONE:
Chris Craikb50c2172013-07-29 15:28:30 -07001013 png_ptr->do_filter = PNG_FILTER_NONE; break;
1014
Patrick Scott5f6bd842010-06-28 16:55:16 -04001015#ifdef PNG_WRITE_FILTER_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001016 case PNG_FILTER_VALUE_SUB:
Chris Craikb50c2172013-07-29 15:28:30 -07001017 png_ptr->do_filter = PNG_FILTER_SUB; break;
1018
The Android Open Source Project893912b2009-03-03 19:30:05 -08001019 case PNG_FILTER_VALUE_UP:
Chris Craikb50c2172013-07-29 15:28:30 -07001020 png_ptr->do_filter = PNG_FILTER_UP; break;
1021
The Android Open Source Project893912b2009-03-03 19:30:05 -08001022 case PNG_FILTER_VALUE_AVG:
Chris Craikb50c2172013-07-29 15:28:30 -07001023 png_ptr->do_filter = PNG_FILTER_AVG; break;
1024
The Android Open Source Project893912b2009-03-03 19:30:05 -08001025 case PNG_FILTER_VALUE_PAETH:
Chris Craikb50c2172013-07-29 15:28:30 -07001026 png_ptr->do_filter = PNG_FILTER_PAETH; break;
1027
1028 default:
1029 png_ptr->do_filter = (png_byte)filters; break;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001030#else
Chris Craikb50c2172013-07-29 15:28:30 -07001031 default:
1032 png_app_error(png_ptr, "Unknown row filter for method 0");
Matt Sarett9ea75692016-01-08 13:00:42 -05001033#endif /* WRITE_FILTER */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001034 }
1035
Matt Sarett9ea75692016-01-08 13:00:42 -05001036#ifdef PNG_WRITE_FILTER_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001037 /* If we have allocated the row_buf, this means we have already started
1038 * with the image and we should have allocated all of the filter buffers
1039 * that have been selected. If prev_row isn't already allocated, then
1040 * it is too late to start using the filters that need it, since we
1041 * will be missing the data in the previous row. If an application
1042 * wants to start and stop using particular filters during compression,
Matt Sarett9ea75692016-01-08 13:00:42 -05001043 * it should start out with all of the filters, and then remove them
1044 * or add them back after the start of compression.
1045 *
1046 * NOTE: this is a nasty constraint on the code, because it means that the
1047 * prev_row buffer must be maintained even if there are currently no
1048 * 'prev_row' requiring filters active.
The Android Open Source Project893912b2009-03-03 19:30:05 -08001049 */
1050 if (png_ptr->row_buf != NULL)
1051 {
Matt Sarett9ea75692016-01-08 13:00:42 -05001052 int num_filters;
1053 png_alloc_size_t buf_size;
1054
1055 /* Repeat the checks in png_write_start_row; 1 pixel high or wide
1056 * images cannot benefit from certain filters. If this isn't done here
1057 * the check below will fire on 1 pixel high images.
1058 */
1059 if (png_ptr->height == 1)
1060 filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH);
1061
1062 if (png_ptr->width == 1)
1063 filters &= ~(PNG_FILTER_SUB|PNG_FILTER_AVG|PNG_FILTER_PAETH);
1064
1065 if ((filters & (PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH)) != 0
1066 && png_ptr->prev_row == NULL)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001067 {
Matt Sarett9ea75692016-01-08 13:00:42 -05001068 /* This is the error case, however it is benign - the previous row
1069 * is not available so the filter can't be used. Just warn here.
1070 */
1071 png_app_warning(png_ptr,
1072 "png_set_filter: UP/AVG/PAETH cannot be added after start");
1073 filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001074 }
1075
Matt Sarett9ea75692016-01-08 13:00:42 -05001076 num_filters = 0;
1077
1078 if (filters & PNG_FILTER_SUB)
1079 num_filters++;
1080
1081 if (filters & PNG_FILTER_UP)
1082 num_filters++;
1083
1084 if (filters & PNG_FILTER_AVG)
1085 num_filters++;
1086
1087 if (filters & PNG_FILTER_PAETH)
1088 num_filters++;
1089
1090 /* Allocate needed row buffers if they have not already been
1091 * allocated.
1092 */
1093 buf_size = PNG_ROWBYTES(png_ptr->usr_channels * png_ptr->usr_bit_depth,
1094 png_ptr->width) + 1;
1095
1096 if (png_ptr->try_row == NULL)
1097 png_ptr->try_row = png_voidcast(png_bytep,
1098 png_malloc(png_ptr, buf_size));
1099
1100 if (num_filters > 1)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001101 {
Matt Sarett9ea75692016-01-08 13:00:42 -05001102 if (png_ptr->tst_row == NULL)
1103 png_ptr->tst_row = png_voidcast(png_bytep,
1104 png_malloc(png_ptr, buf_size));
The Android Open Source Project893912b2009-03-03 19:30:05 -08001105 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08001106 }
Matt Sarett9ea75692016-01-08 13:00:42 -05001107 png_ptr->do_filter = (png_byte)filters;
1108#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -08001109 }
1110 else
1111 png_error(png_ptr, "Unknown custom filter method");
1112}
1113
Matt Sarett9ea75692016-01-08 13:00:42 -05001114#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* DEPRECATED */
Chris Craikb50c2172013-07-29 15:28:30 -07001115/* Provide floating and fixed point APIs */
1116#ifdef PNG_FLOATING_POINT_SUPPORTED
1117void PNGAPI
1118png_set_filter_heuristics(png_structrp png_ptr, int heuristic_method,
1119 int num_weights, png_const_doublep filter_weights,
1120 png_const_doublep filter_costs)
1121{
Matt Sarett9ea75692016-01-08 13:00:42 -05001122 PNG_UNUSED(png_ptr)
1123 PNG_UNUSED(heuristic_method)
1124 PNG_UNUSED(num_weights)
1125 PNG_UNUSED(filter_weights)
1126 PNG_UNUSED(filter_costs)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001127}
Chris Craikb50c2172013-07-29 15:28:30 -07001128#endif /* FLOATING_POINT */
1129
1130#ifdef PNG_FIXED_POINT_SUPPORTED
1131void PNGAPI
1132png_set_filter_heuristics_fixed(png_structrp png_ptr, int heuristic_method,
1133 int num_weights, png_const_fixed_point_p filter_weights,
1134 png_const_fixed_point_p filter_costs)
1135{
Matt Sarett9ea75692016-01-08 13:00:42 -05001136 PNG_UNUSED(png_ptr)
1137 PNG_UNUSED(heuristic_method)
1138 PNG_UNUSED(num_weights)
1139 PNG_UNUSED(filter_weights)
1140 PNG_UNUSED(filter_costs)
Chris Craikb50c2172013-07-29 15:28:30 -07001141}
1142#endif /* FIXED_POINT */
Matt Sarett9ea75692016-01-08 13:00:42 -05001143#endif /* WRITE_WEIGHTED_FILTER */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001144
Matt Sarett9ea75692016-01-08 13:00:42 -05001145#ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001146void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -07001147png_set_compression_level(png_structrp png_ptr, int level)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001148{
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001149 png_debug(1, "in png_set_compression_level");
Patrick Scott5f6bd842010-06-28 16:55:16 -04001150
The Android Open Source Project893912b2009-03-03 19:30:05 -08001151 if (png_ptr == NULL)
1152 return;
Chris Craikb50c2172013-07-29 15:28:30 -07001153
The Android Open Source Project893912b2009-03-03 19:30:05 -08001154 png_ptr->zlib_level = level;
1155}
1156
1157void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -07001158png_set_compression_mem_level(png_structrp png_ptr, int mem_level)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001159{
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001160 png_debug(1, "in png_set_compression_mem_level");
Patrick Scott5f6bd842010-06-28 16:55:16 -04001161
The Android Open Source Project893912b2009-03-03 19:30:05 -08001162 if (png_ptr == NULL)
1163 return;
Chris Craikb50c2172013-07-29 15:28:30 -07001164
The Android Open Source Project893912b2009-03-03 19:30:05 -08001165 png_ptr->zlib_mem_level = mem_level;
1166}
1167
1168void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -07001169png_set_compression_strategy(png_structrp png_ptr, int strategy)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001170{
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001171 png_debug(1, "in png_set_compression_strategy");
Patrick Scott5f6bd842010-06-28 16:55:16 -04001172
The Android Open Source Project893912b2009-03-03 19:30:05 -08001173 if (png_ptr == NULL)
1174 return;
Chris Craikb50c2172013-07-29 15:28:30 -07001175
1176 /* The flag setting here prevents the libpng dynamic selection of strategy.
1177 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001178 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
1179 png_ptr->zlib_strategy = strategy;
1180}
1181
Chris Craikb50c2172013-07-29 15:28:30 -07001182/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
1183 * smaller value of window_bits if it can do so safely.
1184 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001185void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -07001186png_set_compression_window_bits(png_structrp png_ptr, int window_bits)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001187{
1188 if (png_ptr == NULL)
1189 return;
Chris Craikb50c2172013-07-29 15:28:30 -07001190
Matt Sarett9ea75692016-01-08 13:00:42 -05001191 /* Prior to 1.6.0 this would warn but then set the window_bits value. This
1192 * meant that negative window bits values could be selected that would cause
Chris Craikb50c2172013-07-29 15:28:30 -07001193 * libpng to write a non-standard PNG file with raw deflate or gzip
1194 * compressed IDAT or ancillary chunks. Such files can be read and there is
1195 * no warning on read, so this seems like a very bad idea.
1196 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001197 if (window_bits > 15)
Chris Craikb50c2172013-07-29 15:28:30 -07001198 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001199 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
Chris Craikb50c2172013-07-29 15:28:30 -07001200 window_bits = 15;
1201 }
1202
The Android Open Source Project893912b2009-03-03 19:30:05 -08001203 else if (window_bits < 8)
Chris Craikb50c2172013-07-29 15:28:30 -07001204 {
The Android Open Source Project893912b2009-03-03 19:30:05 -08001205 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
Chris Craikb50c2172013-07-29 15:28:30 -07001206 window_bits = 8;
1207 }
1208
The Android Open Source Project893912b2009-03-03 19:30:05 -08001209 png_ptr->zlib_window_bits = window_bits;
1210}
1211
1212void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -07001213png_set_compression_method(png_structrp png_ptr, int method)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001214{
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001215 png_debug(1, "in png_set_compression_method");
Patrick Scott5f6bd842010-06-28 16:55:16 -04001216
The Android Open Source Project893912b2009-03-03 19:30:05 -08001217 if (png_ptr == NULL)
1218 return;
Chris Craikb50c2172013-07-29 15:28:30 -07001219
1220 /* This would produce an invalid PNG file if it worked, but it doesn't and
1221 * deflate will fault it, so it is harmless to just warn here.
1222 */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001223 if (method != 8)
1224 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
Chris Craikb50c2172013-07-29 15:28:30 -07001225
The Android Open Source Project893912b2009-03-03 19:30:05 -08001226 png_ptr->zlib_method = method;
1227}
Matt Sarett9ea75692016-01-08 13:00:42 -05001228#endif /* WRITE_CUSTOMIZE_COMPRESSION */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001229
Chris Craikb50c2172013-07-29 15:28:30 -07001230/* The following were added to libpng-1.5.4 */
1231#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001232void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -07001233png_set_text_compression_level(png_structrp png_ptr, int level)
1234{
1235 png_debug(1, "in png_set_text_compression_level");
1236
1237 if (png_ptr == NULL)
1238 return;
1239
1240 png_ptr->zlib_text_level = level;
1241}
1242
1243void PNGAPI
1244png_set_text_compression_mem_level(png_structrp png_ptr, int mem_level)
1245{
1246 png_debug(1, "in png_set_text_compression_mem_level");
1247
1248 if (png_ptr == NULL)
1249 return;
1250
1251 png_ptr->zlib_text_mem_level = mem_level;
1252}
1253
1254void PNGAPI
1255png_set_text_compression_strategy(png_structrp png_ptr, int strategy)
1256{
1257 png_debug(1, "in png_set_text_compression_strategy");
1258
1259 if (png_ptr == NULL)
1260 return;
1261
1262 png_ptr->zlib_text_strategy = strategy;
1263}
1264
1265/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
1266 * smaller value of window_bits if it can do so safely.
1267 */
1268void PNGAPI
1269png_set_text_compression_window_bits(png_structrp png_ptr, int window_bits)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001270{
1271 if (png_ptr == NULL)
1272 return;
Chris Craikb50c2172013-07-29 15:28:30 -07001273
1274 if (window_bits > 15)
1275 {
1276 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
1277 window_bits = 15;
1278 }
1279
1280 else if (window_bits < 8)
1281 {
1282 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
1283 window_bits = 8;
1284 }
1285
1286 png_ptr->zlib_text_window_bits = window_bits;
1287}
1288
1289void PNGAPI
1290png_set_text_compression_method(png_structrp png_ptr, int method)
1291{
1292 png_debug(1, "in png_set_text_compression_method");
1293
1294 if (png_ptr == NULL)
1295 return;
1296
1297 if (method != 8)
1298 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1299
1300 png_ptr->zlib_text_method = method;
1301}
Matt Sarett9ea75692016-01-08 13:00:42 -05001302#endif /* WRITE_CUSTOMIZE_ZTXT_COMPRESSION */
Chris Craikb50c2172013-07-29 15:28:30 -07001303/* end of API added to libpng-1.5.4 */
1304
1305void PNGAPI
1306png_set_write_status_fn(png_structrp png_ptr, png_write_status_ptr write_row_fn)
1307{
1308 if (png_ptr == NULL)
1309 return;
1310
The Android Open Source Project893912b2009-03-03 19:30:05 -08001311 png_ptr->write_row_fn = write_row_fn;
1312}
1313
Patrick Scott5f6bd842010-06-28 16:55:16 -04001314#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001315void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -07001316png_set_write_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr
1317 write_user_transform_fn)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001318{
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001319 png_debug(1, "in png_set_write_user_transform_fn");
Patrick Scott5f6bd842010-06-28 16:55:16 -04001320
The Android Open Source Project893912b2009-03-03 19:30:05 -08001321 if (png_ptr == NULL)
1322 return;
Chris Craikb50c2172013-07-29 15:28:30 -07001323
The Android Open Source Project893912b2009-03-03 19:30:05 -08001324 png_ptr->transformations |= PNG_USER_TRANSFORM;
1325 png_ptr->write_user_transform_fn = write_user_transform_fn;
1326}
1327#endif
1328
1329
Patrick Scott5f6bd842010-06-28 16:55:16 -04001330#ifdef PNG_INFO_IMAGE_SUPPORTED
The Android Open Source Project893912b2009-03-03 19:30:05 -08001331void PNGAPI
Chris Craikb50c2172013-07-29 15:28:30 -07001332png_write_png(png_structrp png_ptr, png_inforp info_ptr,
1333 int transforms, voidp params)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001334{
1335 if (png_ptr == NULL || info_ptr == NULL)
1336 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001337
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301338 if ((info_ptr->valid & PNG_INFO_IDAT) == 0)
1339 {
1340 png_app_error(png_ptr, "no rows for png_write_image to write");
1341 return;
1342 }
1343
The Android Open Source Project893912b2009-03-03 19:30:05 -08001344 /* Write the file header information. */
1345 png_write_info(png_ptr, info_ptr);
1346
1347 /* ------ these transformations don't touch the info structure ------- */
1348
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001349 /* Invert monochrome pixels */
Matt Sarett9ea75692016-01-08 13:00:42 -05001350 if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301351#ifdef PNG_WRITE_INVERT_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001352 png_set_invert_mono(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301353#else
1354 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001355#endif
1356
The Android Open Source Project893912b2009-03-03 19:30:05 -08001357 /* Shift the pixels up to a legal bit depth and fill in
1358 * as appropriate to correctly scale the image.
1359 */
Matt Sarett9ea75692016-01-08 13:00:42 -05001360 if ((transforms & PNG_TRANSFORM_SHIFT) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301361#ifdef PNG_WRITE_SHIFT_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -05001362 if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301363 png_set_shift(png_ptr, &info_ptr->sig_bit);
1364#else
1365 png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001366#endif
1367
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001368 /* Pack pixels into bytes */
Matt Sarett9ea75692016-01-08 13:00:42 -05001369 if ((transforms & PNG_TRANSFORM_PACKING) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301370#ifdef PNG_WRITE_PACK_SUPPORTED
1371 png_set_packing(png_ptr);
1372#else
1373 png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001374#endif
1375
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001376 /* Swap location of alpha bytes from ARGB to RGBA */
Matt Sarett9ea75692016-01-08 13:00:42 -05001377 if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301378#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001379 png_set_swap_alpha(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301380#else
1381 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001382#endif
1383
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301384 /* Remove a filler (X) from XRGB/RGBX/AG/GA into to convert it into
1385 * RGB, note that the code expects the input color type to be G or RGB; no
1386 * alpha channel.
1387 */
Matt Sarett9ea75692016-01-08 13:00:42 -05001388 if ((transforms & (PNG_TRANSFORM_STRIP_FILLER_AFTER|
1389 PNG_TRANSFORM_STRIP_FILLER_BEFORE)) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301390 {
Patrick Scott5f6bd842010-06-28 16:55:16 -04001391#ifdef PNG_WRITE_FILLER_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -05001392 if ((transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301393 {
Matt Sarett9ea75692016-01-08 13:00:42 -05001394 if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301395 png_app_error(png_ptr,
Matt Sarett9ea75692016-01-08 13:00:42 -05001396 "PNG_TRANSFORM_STRIP_FILLER: BEFORE+AFTER not supported");
Chris Craikb50c2172013-07-29 15:28:30 -07001397
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301398 /* Continue if ignored - this is the pre-1.6.10 behavior */
1399 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
1400 }
1401
Matt Sarett9ea75692016-01-08 13:00:42 -05001402 else if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301403 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
1404#else
1405 png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_FILLER not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001406#endif
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301407 }
The Android Open Source Project893912b2009-03-03 19:30:05 -08001408
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001409 /* Flip BGR pixels to RGB */
Matt Sarett9ea75692016-01-08 13:00:42 -05001410 if ((transforms & PNG_TRANSFORM_BGR) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301411#ifdef PNG_WRITE_BGR_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001412 png_set_bgr(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301413#else
1414 png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001415#endif
1416
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001417 /* Swap bytes of 16-bit files to most significant byte first */
Matt Sarett9ea75692016-01-08 13:00:42 -05001418 if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301419#ifdef PNG_WRITE_SWAP_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001420 png_set_swap(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301421#else
1422 png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001423#endif
1424
Matt Sarett9ea75692016-01-08 13:00:42 -05001425 /* Swap bits of 1-bit, 2-bit, 4-bit packed pixel formats */
1426 if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301427#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001428 png_set_packswap(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301429#else
1430 png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001431#endif
1432
Patrick Scott5f6bd842010-06-28 16:55:16 -04001433 /* Invert the alpha channel from opacity to transparency */
Matt Sarett9ea75692016-01-08 13:00:42 -05001434 if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0)
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301435#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Patrick Scott5f6bd842010-06-28 16:55:16 -04001436 png_set_invert_alpha(png_ptr);
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301437#else
1438 png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
Patrick Scott5f6bd842010-06-28 16:55:16 -04001439#endif
1440
The Android Open Source Project893912b2009-03-03 19:30:05 -08001441 /* ----------------------- end of transformations ------------------- */
1442
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001443 /* Write the bits */
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301444 png_write_image(png_ptr, info_ptr->row_pointers);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001445
1446 /* It is REQUIRED to call this to finish writing the rest of the file */
1447 png_write_end(png_ptr, info_ptr);
1448
Chris Craikb50c2172013-07-29 15:28:30 -07001449 PNG_UNUSED(params)
The Android Open Source Project893912b2009-03-03 19:30:05 -08001450}
1451#endif
Chris Craikb50c2172013-07-29 15:28:30 -07001452
1453
1454#ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
Matt Sarett9ea75692016-01-08 13:00:42 -05001455# ifdef PNG_STDIO_SUPPORTED /* currently required for png_image_write_* */
Chris Craikb50c2172013-07-29 15:28:30 -07001456/* Initialize the write structure - general purpose utility. */
1457static int
1458png_image_write_init(png_imagep image)
1459{
1460 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, image,
Matt Sarett9ea75692016-01-08 13:00:42 -05001461 png_safe_error, png_safe_warning);
Chris Craikb50c2172013-07-29 15:28:30 -07001462
1463 if (png_ptr != NULL)
1464 {
1465 png_infop info_ptr = png_create_info_struct(png_ptr);
1466
1467 if (info_ptr != NULL)
1468 {
1469 png_controlp control = png_voidcast(png_controlp,
Matt Sarett9ea75692016-01-08 13:00:42 -05001470 png_malloc_warn(png_ptr, (sizeof *control)));
Chris Craikb50c2172013-07-29 15:28:30 -07001471
1472 if (control != NULL)
1473 {
1474 memset(control, 0, (sizeof *control));
1475
1476 control->png_ptr = png_ptr;
1477 control->info_ptr = info_ptr;
1478 control->for_write = 1;
1479
1480 image->opaque = control;
1481 return 1;
1482 }
1483
1484 /* Error clean up */
1485 png_destroy_info_struct(png_ptr, &info_ptr);
1486 }
1487
1488 png_destroy_write_struct(&png_ptr, NULL);
1489 }
1490
1491 return png_image_error(image, "png_image_write_: out of memory");
1492}
1493
1494/* Arguments to png_image_write_main: */
1495typedef struct
1496{
1497 /* Arguments: */
1498 png_imagep image;
1499 png_const_voidp buffer;
1500 png_int_32 row_stride;
1501 png_const_voidp colormap;
1502 int convert_to_8bit;
1503 /* Local variables: */
1504 png_const_voidp first_row;
1505 ptrdiff_t row_bytes;
1506 png_voidp local_row;
1507} png_image_write_control;
1508
1509/* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to
1510 * do any necessary byte swapping. The component order is defined by the
1511 * png_image format value.
1512 */
1513static int
1514png_write_image_16bit(png_voidp argument)
1515{
1516 png_image_write_control *display = png_voidcast(png_image_write_control*,
Matt Sarett9ea75692016-01-08 13:00:42 -05001517 argument);
Chris Craikb50c2172013-07-29 15:28:30 -07001518 png_imagep image = display->image;
1519 png_structrp png_ptr = image->opaque->png_ptr;
1520
1521 png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
Matt Sarett9ea75692016-01-08 13:00:42 -05001522 display->first_row);
Chris Craikb50c2172013-07-29 15:28:30 -07001523 png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row);
1524 png_uint_16p row_end;
Matt Sarett9ea75692016-01-08 13:00:42 -05001525 const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1;
Chris Craikb50c2172013-07-29 15:28:30 -07001526 int aindex = 0;
1527 png_uint_32 y = image->height;
1528
Matt Sarett9ea75692016-01-08 13:00:42 -05001529 if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001530 {
Matt Sarett9ea75692016-01-08 13:00:42 -05001531# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
1532 if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
1533 {
1534 aindex = -1;
1535 ++input_row; /* To point to the first component */
1536 ++output_row;
1537 }
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301538 else
Matt Sarett9ea75692016-01-08 13:00:42 -05001539 aindex = channels;
1540# else
Chris Craikb50c2172013-07-29 15:28:30 -07001541 aindex = channels;
Matt Sarett9ea75692016-01-08 13:00:42 -05001542# endif
Chris Craikb50c2172013-07-29 15:28:30 -07001543 }
1544
1545 else
1546 png_error(png_ptr, "png_write_image: internal call error");
1547
1548 /* Work out the output row end and count over this, note that the increment
1549 * above to 'row' means that row_end can actually be beyond the end of the
1550 * row; this is correct.
1551 */
1552 row_end = output_row + image->width * (channels+1);
1553
1554 while (y-- > 0)
1555 {
1556 png_const_uint_16p in_ptr = input_row;
1557 png_uint_16p out_ptr = output_row;
1558
1559 while (out_ptr < row_end)
1560 {
1561 const png_uint_16 alpha = in_ptr[aindex];
1562 png_uint_32 reciprocal = 0;
1563 int c;
1564
1565 out_ptr[aindex] = alpha;
1566
1567 /* Calculate a reciprocal. The correct calculation is simply
1568 * component/alpha*65535 << 15. (I.e. 15 bits of precision); this
1569 * allows correct rounding by adding .5 before the shift. 'reciprocal'
1570 * is only initialized when required.
1571 */
1572 if (alpha > 0 && alpha < 65535)
1573 reciprocal = ((0xffff<<15)+(alpha>>1))/alpha;
1574
1575 c = channels;
1576 do /* always at least one channel */
1577 {
1578 png_uint_16 component = *in_ptr++;
1579
1580 /* The following gives 65535 for an alpha of 0, which is fine,
1581 * otherwise if 0/0 is represented as some other value there is more
1582 * likely to be a discontinuity which will probably damage
1583 * compression when moving from a fully transparent area to a
1584 * nearly transparent one. (The assumption here is that opaque
1585 * areas tend not to be 0 intensity.)
1586 */
1587 if (component >= alpha)
1588 component = 65535;
1589
1590 /* component<alpha, so component/alpha is less than one and
1591 * component*reciprocal is less than 2^31.
1592 */
1593 else if (component > 0 && alpha < 65535)
1594 {
1595 png_uint_32 calc = component * reciprocal;
1596 calc += 16384; /* round to nearest */
1597 component = (png_uint_16)(calc >> 15);
1598 }
1599
1600 *out_ptr++ = component;
1601 }
1602 while (--c > 0);
1603
1604 /* Skip to next component (skip the intervening alpha channel) */
1605 ++in_ptr;
1606 ++out_ptr;
1607 }
1608
1609 png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row));
1610 input_row += display->row_bytes/(sizeof (png_uint_16));
1611 }
1612
1613 return 1;
1614}
1615
1616/* Given 16-bit input (1 to 4 channels) write 8-bit output. If an alpha channel
1617 * is present it must be removed from the components, the components are then
1618 * written in sRGB encoding. No components are added or removed.
1619 *
1620 * Calculate an alpha reciprocal to reverse pre-multiplication. As above the
1621 * calculation can be done to 15 bits of accuracy; however, the output needs to
1622 * be scaled in the range 0..255*65535, so include that scaling here.
1623 */
Matt Sarett9ea75692016-01-08 13:00:42 -05001624# define UNP_RECIPROCAL(alpha) ((((0xffff*0xff)<<7)+(alpha>>1))/alpha)
Chris Craikb50c2172013-07-29 15:28:30 -07001625
1626static png_byte
1627png_unpremultiply(png_uint_32 component, png_uint_32 alpha,
1628 png_uint_32 reciprocal/*from the above macro*/)
1629{
1630 /* The following gives 1.0 for an alpha of 0, which is fine, otherwise if 0/0
1631 * is represented as some other value there is more likely to be a
1632 * discontinuity which will probably damage compression when moving from a
1633 * fully transparent area to a nearly transparent one. (The assumption here
1634 * is that opaque areas tend not to be 0 intensity.)
1635 *
1636 * There is a rounding problem here; if alpha is less than 128 it will end up
1637 * as 0 when scaled to 8 bits. To avoid introducing spurious colors into the
1638 * output change for this too.
1639 */
1640 if (component >= alpha || alpha < 128)
1641 return 255;
1642
1643 /* component<alpha, so component/alpha is less than one and
1644 * component*reciprocal is less than 2^31.
1645 */
1646 else if (component > 0)
1647 {
1648 /* The test is that alpha/257 (rounded) is less than 255, the first value
1649 * that becomes 255 is 65407.
1650 * NOTE: this must agree with the PNG_DIV257 macro (which must, therefore,
1651 * be exact!) [Could also test reciprocal != 0]
1652 */
1653 if (alpha < 65407)
1654 {
1655 component *= reciprocal;
1656 component += 64; /* round to nearest */
1657 component >>= 7;
1658 }
1659
1660 else
1661 component *= 255;
1662
1663 /* Convert the component to sRGB. */
1664 return (png_byte)PNG_sRGB_FROM_LINEAR(component);
1665 }
1666
1667 else
1668 return 0;
1669}
1670
1671static int
1672png_write_image_8bit(png_voidp argument)
1673{
1674 png_image_write_control *display = png_voidcast(png_image_write_control*,
Matt Sarett9ea75692016-01-08 13:00:42 -05001675 argument);
Chris Craikb50c2172013-07-29 15:28:30 -07001676 png_imagep image = display->image;
1677 png_structrp png_ptr = image->opaque->png_ptr;
1678
1679 png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
Matt Sarett9ea75692016-01-08 13:00:42 -05001680 display->first_row);
Chris Craikb50c2172013-07-29 15:28:30 -07001681 png_bytep output_row = png_voidcast(png_bytep, display->local_row);
1682 png_uint_32 y = image->height;
Matt Sarett9ea75692016-01-08 13:00:42 -05001683 const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1;
Chris Craikb50c2172013-07-29 15:28:30 -07001684
Matt Sarett9ea75692016-01-08 13:00:42 -05001685 if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001686 {
1687 png_bytep row_end;
1688 int aindex;
1689
Matt Sarett9ea75692016-01-08 13:00:42 -05001690# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
1691 if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
1692 {
1693 aindex = -1;
1694 ++input_row; /* To point to the first component */
1695 ++output_row;
1696 }
Chris Craikb50c2172013-07-29 15:28:30 -07001697
Matt Sarett9ea75692016-01-08 13:00:42 -05001698 else
1699# endif
1700 aindex = channels;
Chris Craikb50c2172013-07-29 15:28:30 -07001701
1702 /* Use row_end in place of a loop counter: */
1703 row_end = output_row + image->width * (channels+1);
1704
1705 while (y-- > 0)
1706 {
1707 png_const_uint_16p in_ptr = input_row;
1708 png_bytep out_ptr = output_row;
1709
1710 while (out_ptr < row_end)
1711 {
1712 png_uint_16 alpha = in_ptr[aindex];
1713 png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
1714 png_uint_32 reciprocal = 0;
1715 int c;
1716
1717 /* Scale and write the alpha channel. */
1718 out_ptr[aindex] = alphabyte;
1719
1720 if (alphabyte > 0 && alphabyte < 255)
1721 reciprocal = UNP_RECIPROCAL(alpha);
1722
1723 c = channels;
1724 do /* always at least one channel */
1725 *out_ptr++ = png_unpremultiply(*in_ptr++, alpha, reciprocal);
1726 while (--c > 0);
1727
1728 /* Skip to next component (skip the intervening alpha channel) */
1729 ++in_ptr;
1730 ++out_ptr;
1731 } /* while out_ptr < row_end */
1732
1733 png_write_row(png_ptr, png_voidcast(png_const_bytep,
Matt Sarett9ea75692016-01-08 13:00:42 -05001734 display->local_row));
Chris Craikb50c2172013-07-29 15:28:30 -07001735 input_row += display->row_bytes/(sizeof (png_uint_16));
1736 } /* while y */
1737 }
1738
1739 else
1740 {
1741 /* No alpha channel, so the row_end really is the end of the row and it
1742 * is sufficient to loop over the components one by one.
1743 */
1744 png_bytep row_end = output_row + image->width * channels;
1745
1746 while (y-- > 0)
1747 {
1748 png_const_uint_16p in_ptr = input_row;
1749 png_bytep out_ptr = output_row;
1750
1751 while (out_ptr < row_end)
1752 {
1753 png_uint_32 component = *in_ptr++;
1754
1755 component *= 255;
1756 *out_ptr++ = (png_byte)PNG_sRGB_FROM_LINEAR(component);
1757 }
1758
1759 png_write_row(png_ptr, output_row);
1760 input_row += display->row_bytes/(sizeof (png_uint_16));
1761 }
1762 }
1763
1764 return 1;
1765}
1766
1767static void
1768png_image_set_PLTE(png_image_write_control *display)
1769{
1770 const png_imagep image = display->image;
1771 const void *cmap = display->colormap;
1772 const int entries = image->colormap_entries > 256 ? 256 :
Matt Sarett9ea75692016-01-08 13:00:42 -05001773 (int)image->colormap_entries;
Chris Craikb50c2172013-07-29 15:28:30 -07001774
1775 /* NOTE: the caller must check for cmap != NULL and entries != 0 */
1776 const png_uint_32 format = image->format;
1777 const int channels = PNG_IMAGE_SAMPLE_CHANNELS(format);
1778
Matt Sarett9ea75692016-01-08 13:00:42 -05001779# if defined(PNG_FORMAT_BGR_SUPPORTED) &&\
Sireesh Tripurarib478e662014-05-09 15:15:10 +05301780 defined(PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED)
Chris Craikb50c2172013-07-29 15:28:30 -07001781 const int afirst = (format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
Matt Sarett9ea75692016-01-08 13:00:42 -05001782 (format & PNG_FORMAT_FLAG_ALPHA) != 0;
1783# else
Chris Craikb50c2172013-07-29 15:28:30 -07001784# define afirst 0
Matt Sarett9ea75692016-01-08 13:00:42 -05001785# endif
Chris Craikb50c2172013-07-29 15:28:30 -07001786
Matt Sarett9ea75692016-01-08 13:00:42 -05001787# ifdef PNG_FORMAT_BGR_SUPPORTED
1788 const int bgr = (format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0;
1789# else
Chris Craikb50c2172013-07-29 15:28:30 -07001790# define bgr 0
Matt Sarett9ea75692016-01-08 13:00:42 -05001791# endif
Chris Craikb50c2172013-07-29 15:28:30 -07001792
1793 int i, num_trans;
1794 png_color palette[256];
1795 png_byte tRNS[256];
1796
1797 memset(tRNS, 255, (sizeof tRNS));
1798 memset(palette, 0, (sizeof palette));
1799
1800 for (i=num_trans=0; i<entries; ++i)
1801 {
1802 /* This gets automatically converted to sRGB with reversal of the
1803 * pre-multiplication if the color-map has an alpha channel.
1804 */
Matt Sarett9ea75692016-01-08 13:00:42 -05001805 if ((format & PNG_FORMAT_FLAG_LINEAR) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001806 {
1807 png_const_uint_16p entry = png_voidcast(png_const_uint_16p, cmap);
1808
1809 entry += i * channels;
1810
Matt Sarett9ea75692016-01-08 13:00:42 -05001811 if ((channels & 1) != 0) /* no alpha */
Chris Craikb50c2172013-07-29 15:28:30 -07001812 {
1813 if (channels >= 3) /* RGB */
1814 {
1815 palette[i].blue = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
Matt Sarett9ea75692016-01-08 13:00:42 -05001816 entry[(2 ^ bgr)]);
Chris Craikb50c2172013-07-29 15:28:30 -07001817 palette[i].green = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
Matt Sarett9ea75692016-01-08 13:00:42 -05001818 entry[1]);
Chris Craikb50c2172013-07-29 15:28:30 -07001819 palette[i].red = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
Matt Sarett9ea75692016-01-08 13:00:42 -05001820 entry[bgr]);
Chris Craikb50c2172013-07-29 15:28:30 -07001821 }
1822
1823 else /* Gray */
1824 palette[i].blue = palette[i].red = palette[i].green =
1825 (png_byte)PNG_sRGB_FROM_LINEAR(255 * *entry);
1826 }
1827
1828 else /* alpha */
1829 {
1830 png_uint_16 alpha = entry[afirst ? 0 : channels-1];
1831 png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
1832 png_uint_32 reciprocal = 0;
1833
1834 /* Calculate a reciprocal, as in the png_write_image_8bit code above
1835 * this is designed to produce a value scaled to 255*65535 when
1836 * divided by 128 (i.e. asr 7).
1837 */
1838 if (alphabyte > 0 && alphabyte < 255)
1839 reciprocal = (((0xffff*0xff)<<7)+(alpha>>1))/alpha;
1840
1841 tRNS[i] = alphabyte;
1842 if (alphabyte < 255)
1843 num_trans = i+1;
1844
1845 if (channels >= 3) /* RGB */
1846 {
1847 palette[i].blue = png_unpremultiply(entry[afirst + (2 ^ bgr)],
1848 alpha, reciprocal);
1849 palette[i].green = png_unpremultiply(entry[afirst + 1], alpha,
1850 reciprocal);
1851 palette[i].red = png_unpremultiply(entry[afirst + bgr], alpha,
1852 reciprocal);
1853 }
1854
1855 else /* gray */
1856 palette[i].blue = palette[i].red = palette[i].green =
1857 png_unpremultiply(entry[afirst], alpha, reciprocal);
1858 }
1859 }
1860
1861 else /* Color-map has sRGB values */
1862 {
1863 png_const_bytep entry = png_voidcast(png_const_bytep, cmap);
1864
1865 entry += i * channels;
1866
1867 switch (channels)
1868 {
1869 case 4:
1870 tRNS[i] = entry[afirst ? 0 : 3];
1871 if (tRNS[i] < 255)
1872 num_trans = i+1;
1873 /* FALL THROUGH */
1874 case 3:
1875 palette[i].blue = entry[afirst + (2 ^ bgr)];
1876 palette[i].green = entry[afirst + 1];
1877 palette[i].red = entry[afirst + bgr];
1878 break;
1879
1880 case 2:
1881 tRNS[i] = entry[1 ^ afirst];
1882 if (tRNS[i] < 255)
1883 num_trans = i+1;
1884 /* FALL THROUGH */
1885 case 1:
1886 palette[i].blue = palette[i].red = palette[i].green =
1887 entry[afirst];
1888 break;
1889
1890 default:
1891 break;
1892 }
1893 }
1894 }
1895
Matt Sarett9ea75692016-01-08 13:00:42 -05001896# ifdef afirst
Chris Craikb50c2172013-07-29 15:28:30 -07001897# undef afirst
Matt Sarett9ea75692016-01-08 13:00:42 -05001898# endif
1899# ifdef bgr
Chris Craikb50c2172013-07-29 15:28:30 -07001900# undef bgr
Matt Sarett9ea75692016-01-08 13:00:42 -05001901# endif
Chris Craikb50c2172013-07-29 15:28:30 -07001902
1903 png_set_PLTE(image->opaque->png_ptr, image->opaque->info_ptr, palette,
1904 entries);
1905
1906 if (num_trans > 0)
1907 png_set_tRNS(image->opaque->png_ptr, image->opaque->info_ptr, tRNS,
1908 num_trans, NULL);
1909
1910 image->colormap_entries = entries;
1911}
1912
1913static int
1914png_image_write_main(png_voidp argument)
1915{
1916 png_image_write_control *display = png_voidcast(png_image_write_control*,
1917 argument);
1918 png_imagep image = display->image;
1919 png_structrp png_ptr = image->opaque->png_ptr;
1920 png_inforp info_ptr = image->opaque->info_ptr;
1921 png_uint_32 format = image->format;
1922
Matt Sarett9ea75692016-01-08 13:00:42 -05001923 /* The following four ints are actually booleans */
1924 int colormap = (format & PNG_FORMAT_FLAG_COLORMAP);
1925 int linear = !colormap && (format & PNG_FORMAT_FLAG_LINEAR); /* input */
1926 int alpha = !colormap && (format & PNG_FORMAT_FLAG_ALPHA);
1927 int write_16bit = linear && !colormap && (display->convert_to_8bit == 0);
Chris Craikb50c2172013-07-29 15:28:30 -07001928
Matt Sarett9ea75692016-01-08 13:00:42 -05001929# ifdef PNG_BENIGN_ERRORS_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07001930 /* Make sure we error out on any bad situation */
1931 png_set_benign_errors(png_ptr, 0/*error*/);
Matt Sarett9ea75692016-01-08 13:00:42 -05001932# endif
Chris Craikb50c2172013-07-29 15:28:30 -07001933
1934 /* Default the 'row_stride' parameter if required. */
1935 if (display->row_stride == 0)
1936 display->row_stride = PNG_IMAGE_ROW_STRIDE(*image);
1937
1938 /* Set the required transforms then write the rows in the correct order. */
Matt Sarett9ea75692016-01-08 13:00:42 -05001939 if ((format & PNG_FORMAT_FLAG_COLORMAP) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001940 {
1941 if (display->colormap != NULL && image->colormap_entries > 0)
1942 {
1943 png_uint_32 entries = image->colormap_entries;
1944
1945 png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
1946 entries > 16 ? 8 : (entries > 4 ? 4 : (entries > 2 ? 2 : 1)),
1947 PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
1948 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
1949
1950 png_image_set_PLTE(display);
1951 }
1952
1953 else
1954 png_error(image->opaque->png_ptr,
1955 "no color-map for color-mapped image");
1956 }
1957
1958 else
1959 png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
1960 write_16bit ? 16 : 8,
1961 ((format & PNG_FORMAT_FLAG_COLOR) ? PNG_COLOR_MASK_COLOR : 0) +
1962 ((format & PNG_FORMAT_FLAG_ALPHA) ? PNG_COLOR_MASK_ALPHA : 0),
1963 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
1964
1965 /* Counter-intuitively the data transformations must be called *after*
1966 * png_write_info, not before as in the read code, but the 'set' functions
1967 * must still be called before. Just set the color space information, never
1968 * write an interlaced image.
1969 */
1970
Matt Sarett9ea75692016-01-08 13:00:42 -05001971 if (write_16bit != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001972 {
1973 /* The gamma here is 1.0 (linear) and the cHRM chunk matches sRGB. */
1974 png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_LINEAR);
1975
Matt Sarett9ea75692016-01-08 13:00:42 -05001976 if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001977 png_set_cHRM_fixed(png_ptr, info_ptr,
1978 /* color x y */
1979 /* white */ 31270, 32900,
1980 /* red */ 64000, 33000,
1981 /* green */ 30000, 60000,
1982 /* blue */ 15000, 6000
1983 );
1984 }
1985
Matt Sarett9ea75692016-01-08 13:00:42 -05001986 else if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07001987 png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL);
1988
1989 /* Else writing an 8-bit file and the *colors* aren't sRGB, but the 8-bit
1990 * space must still be gamma encoded.
1991 */
1992 else
1993 png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE);
1994
1995 /* Write the file header. */
1996 png_write_info(png_ptr, info_ptr);
1997
1998 /* Now set up the data transformations (*after* the header is written),
1999 * remove the handled transformations from the 'format' flags for checking.
2000 *
Matt Sarett9ea75692016-01-08 13:00:42 -05002001 * First check for a little endian system if writing 16-bit files.
Chris Craikb50c2172013-07-29 15:28:30 -07002002 */
Matt Sarett9ea75692016-01-08 13:00:42 -05002003 if (write_16bit != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002004 {
2005 PNG_CONST png_uint_16 le = 0x0001;
2006
Matt Sarett9ea75692016-01-08 13:00:42 -05002007 if ((*(png_const_bytep) & le) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002008 png_set_swap(png_ptr);
2009 }
2010
Matt Sarett9ea75692016-01-08 13:00:42 -05002011# ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
2012 if ((format & PNG_FORMAT_FLAG_BGR) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002013 {
Matt Sarett9ea75692016-01-08 13:00:42 -05002014 if (colormap == 0 && (format & PNG_FORMAT_FLAG_COLOR) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002015 png_set_bgr(png_ptr);
2016 format &= ~PNG_FORMAT_FLAG_BGR;
2017 }
Matt Sarett9ea75692016-01-08 13:00:42 -05002018# endif
Chris Craikb50c2172013-07-29 15:28:30 -07002019
Matt Sarett9ea75692016-01-08 13:00:42 -05002020# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
2021 if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002022 {
Matt Sarett9ea75692016-01-08 13:00:42 -05002023 if (colormap == 0 && (format & PNG_FORMAT_FLAG_ALPHA) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002024 png_set_swap_alpha(png_ptr);
2025 format &= ~PNG_FORMAT_FLAG_AFIRST;
2026 }
Matt Sarett9ea75692016-01-08 13:00:42 -05002027# endif
Chris Craikb50c2172013-07-29 15:28:30 -07002028
2029 /* If there are 16 or fewer color-map entries we wrote a lower bit depth
2030 * above, but the application data is still byte packed.
2031 */
Matt Sarett9ea75692016-01-08 13:00:42 -05002032 if (colormap != 0 && image->colormap_entries <= 16)
Chris Craikb50c2172013-07-29 15:28:30 -07002033 png_set_packing(png_ptr);
2034
2035 /* That should have handled all (both) the transforms. */
2036 if ((format & ~(png_uint_32)(PNG_FORMAT_FLAG_COLOR | PNG_FORMAT_FLAG_LINEAR |
2037 PNG_FORMAT_FLAG_ALPHA | PNG_FORMAT_FLAG_COLORMAP)) != 0)
2038 png_error(png_ptr, "png_write_image: unsupported transformation");
2039
2040 {
2041 png_const_bytep row = png_voidcast(png_const_bytep, display->buffer);
2042 ptrdiff_t row_bytes = display->row_stride;
2043
Matt Sarett9ea75692016-01-08 13:00:42 -05002044 if (linear != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002045 row_bytes *= (sizeof (png_uint_16));
2046
2047 if (row_bytes < 0)
2048 row += (image->height-1) * (-row_bytes);
2049
2050 display->first_row = row;
2051 display->row_bytes = row_bytes;
2052 }
2053
2054 /* Apply 'fast' options if the flag is set. */
2055 if ((image->flags & PNG_IMAGE_FLAG_FAST) != 0)
2056 {
2057 png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_NO_FILTERS);
2058 /* NOTE: determined by experiment using pngstest, this reflects some
2059 * balance between the time to write the image once and the time to read
2060 * it about 50 times. The speed-up in pngstest was about 10-20% of the
2061 * total (user) time on a heavily loaded system.
2062 */
Matt Sarett9ea75692016-01-08 13:00:42 -05002063# ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED
Chris Craikb50c2172013-07-29 15:28:30 -07002064 png_set_compression_level(png_ptr, 3);
Matt Sarett9ea75692016-01-08 13:00:42 -05002065# endif
Chris Craikb50c2172013-07-29 15:28:30 -07002066 }
2067
2068 /* Check for the cases that currently require a pre-transform on the row
2069 * before it is written. This only applies when the input is 16-bit and
2070 * either there is an alpha channel or it is converted to 8-bit.
2071 */
Matt Sarett9ea75692016-01-08 13:00:42 -05002072 if ((linear != 0 && alpha != 0 ) ||
2073 (colormap == 0 && display->convert_to_8bit != 0))
Chris Craikb50c2172013-07-29 15:28:30 -07002074 {
2075 png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
2076 png_get_rowbytes(png_ptr, info_ptr)));
2077 int result;
2078
2079 display->local_row = row;
Matt Sarett9ea75692016-01-08 13:00:42 -05002080 if (write_16bit != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002081 result = png_safe_execute(image, png_write_image_16bit, display);
2082 else
2083 result = png_safe_execute(image, png_write_image_8bit, display);
2084 display->local_row = NULL;
2085
2086 png_free(png_ptr, row);
2087
2088 /* Skip the 'write_end' on error: */
Matt Sarett9ea75692016-01-08 13:00:42 -05002089 if (result == 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002090 return 0;
2091 }
2092
2093 /* Otherwise this is the case where the input is in a format currently
2094 * supported by the rest of the libpng write code; call it directly.
2095 */
2096 else
2097 {
2098 png_const_bytep row = png_voidcast(png_const_bytep, display->first_row);
2099 ptrdiff_t row_bytes = display->row_bytes;
2100 png_uint_32 y = image->height;
2101
2102 while (y-- > 0)
2103 {
2104 png_write_row(png_ptr, row);
2105 row += row_bytes;
2106 }
2107 }
2108
2109 png_write_end(png_ptr, info_ptr);
2110 return 1;
2111}
2112
2113int PNGAPI
2114png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit,
2115 const void *buffer, png_int_32 row_stride, const void *colormap)
2116{
2117 /* Write the image to the given (FILE*). */
2118 if (image != NULL && image->version == PNG_IMAGE_VERSION)
2119 {
2120 if (file != NULL)
2121 {
Matt Sarett9ea75692016-01-08 13:00:42 -05002122 if (png_image_write_init(image) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002123 {
2124 png_image_write_control display;
2125 int result;
2126
2127 /* This is slightly evil, but png_init_io doesn't do anything other
2128 * than this and we haven't changed the standard IO functions so
2129 * this saves a 'safe' function.
2130 */
2131 image->opaque->png_ptr->io_ptr = file;
2132
2133 memset(&display, 0, (sizeof display));
2134 display.image = image;
2135 display.buffer = buffer;
2136 display.row_stride = row_stride;
2137 display.colormap = colormap;
2138 display.convert_to_8bit = convert_to_8bit;
2139
2140 result = png_safe_execute(image, png_image_write_main, &display);
2141 png_image_free(image);
2142 return result;
2143 }
2144
2145 else
2146 return 0;
2147 }
2148
2149 else
2150 return png_image_error(image,
2151 "png_image_write_to_stdio: invalid argument");
2152 }
2153
2154 else if (image != NULL)
2155 return png_image_error(image,
2156 "png_image_write_to_stdio: incorrect PNG_IMAGE_VERSION");
2157
2158 else
2159 return 0;
2160}
2161
2162int PNGAPI
2163png_image_write_to_file(png_imagep image, const char *file_name,
2164 int convert_to_8bit, const void *buffer, png_int_32 row_stride,
2165 const void *colormap)
2166{
2167 /* Write the image to the named file. */
2168 if (image != NULL && image->version == PNG_IMAGE_VERSION)
2169 {
2170 if (file_name != NULL)
2171 {
2172 FILE *fp = fopen(file_name, "wb");
2173
2174 if (fp != NULL)
2175 {
2176 if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer,
Matt Sarett9ea75692016-01-08 13:00:42 -05002177 row_stride, colormap) != 0)
Chris Craikb50c2172013-07-29 15:28:30 -07002178 {
2179 int error; /* from fflush/fclose */
2180
2181 /* Make sure the file is flushed correctly. */
2182 if (fflush(fp) == 0 && ferror(fp) == 0)
2183 {
2184 if (fclose(fp) == 0)
2185 return 1;
2186
2187 error = errno; /* from fclose */
2188 }
2189
2190 else
2191 {
2192 error = errno; /* from fflush or ferror */
2193 (void)fclose(fp);
2194 }
2195
2196 (void)remove(file_name);
2197 /* The image has already been cleaned up; this is just used to
2198 * set the error (because the original write succeeded).
2199 */
2200 return png_image_error(image, strerror(error));
2201 }
2202
2203 else
2204 {
2205 /* Clean up: just the opened file. */
2206 (void)fclose(fp);
2207 (void)remove(file_name);
2208 return 0;
2209 }
2210 }
2211
2212 else
2213 return png_image_error(image, strerror(errno));
2214 }
2215
2216 else
2217 return png_image_error(image,
2218 "png_image_write_to_file: invalid argument");
2219 }
2220
2221 else if (image != NULL)
2222 return png_image_error(image,
2223 "png_image_write_to_file: incorrect PNG_IMAGE_VERSION");
2224
2225 else
2226 return 0;
2227}
Matt Sarett9ea75692016-01-08 13:00:42 -05002228# endif /* STDIO */
Chris Craikb50c2172013-07-29 15:28:30 -07002229#endif /* SIMPLIFIED_WRITE */
Matt Sarett9ea75692016-01-08 13:00:42 -05002230#endif /* WRITE */