blob: 4b10b0417cb52c4fb4e4a76aecfac674471c83de [file] [log] [blame]
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001
Andreas Dilger47a0c421997-05-16 02:46:07 -05002/* pngwrite.c - general routines to write a PNG file
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrson13831bc2011-12-21 08:28:28 -06004 * Last changed in libpng 1.6.0 [(PENDING RELEASE)]
Glenn Randers-Pehrson1531bd62012-01-01 14:45:04 -06005 * Copyright (c) 1998-2012 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -05008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060012 */
Guy Schalnat0d580581995-07-20 02:43:20 -050013
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050014#include "pngpriv.h"
John Bowler7875d532011-11-07 22:33:49 -060015#if defined PNG_SIMPLIFIED_WRITE_SUPPORTED && defined PNG_STDIO_SUPPORTED
16# include <errno.h>
17#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050018
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060019#ifdef PNG_WRITE_SUPPORTED
20
Andreas Dilger47a0c421997-05-16 02:46:07 -050021/* Writes all the PNG information. This is the suggested way to use the
22 * library. If you have a new chunk to add, make a function to write it,
23 * and put it in the correct location here. If you want the chunk written
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050024 * after the image data, put it in png_write_end(). I strongly encourage
Andreas Dilger47a0c421997-05-16 02:46:07 -050025 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
26 * the chunk, as that will keep the code from breaking if you want to just
27 * write a plain PNG file. If you have long comments, I suggest writing
28 * them in png_write_end(), and compressing them.
29 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050030void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -060031png_write_info_before_PLTE(png_structrp png_ptr, png_inforp info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050032{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050033 png_debug(1, "in png_write_info_before_PLTE");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050034
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -060035 if (png_ptr == NULL || info_ptr == NULL)
36 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050037
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060038 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
39 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -050040 /* Write PNG signature */
41 png_write_sig(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050042
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050043#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -060044 if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) && \
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060045 (png_ptr->mng_features_permitted))
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060046 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050047 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -050048 png_ptr->mng_features_permitted = 0;
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060049 }
50#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050051
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -050052 /* Write IHDR information. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060053 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060054 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
55 info_ptr->filter_type,
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050056#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060057 info_ptr->interlace_type);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060058#else
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060059 0);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060060#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050061 /* The rest of these check to see if the valid field has the appropriate
62 * flag set, and if it does, writes the chunk.
63 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050064#ifdef PNG_WRITE_gAMA_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060065 if (info_ptr->valid & PNG_INFO_gAMA)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050066 png_write_gAMA_fixed(png_ptr, info_ptr->gamma);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050067#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050068#ifdef PNG_WRITE_sRGB_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060069 if (info_ptr->valid & PNG_INFO_sRGB)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060070 png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060071#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050072
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050073#ifdef PNG_WRITE_iCCP_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060074 if (info_ptr->valid & PNG_INFO_iCCP)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -060075 png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
John Bowlercf499192012-03-01 21:54:07 -060076 info_ptr->iccp_profile, info_ptr->iccp_proflen);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060077#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050078#ifdef PNG_WRITE_sBIT_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060079 if (info_ptr->valid & PNG_INFO_sBIT)
80 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050081#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050082#ifdef PNG_WRITE_cHRM_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060083 if (info_ptr->valid & PNG_INFO_cHRM)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050084 png_write_cHRM_fixed(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -060085 info_ptr->x_white, info_ptr->y_white,
86 info_ptr->x_red, info_ptr->y_red,
87 info_ptr->x_green, info_ptr->y_green,
88 info_ptr->x_blue, info_ptr->y_blue);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060089#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050090
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050091#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060092 if (info_ptr->unknown_chunks_num)
93 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050094 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060095
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050096 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060097
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050098 for (up = info_ptr->unknown_chunks;
99 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
100 up++)
101 {
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500102 int keep = png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500103
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500104 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson36fa2a02011-05-11 06:52:37 -0500105 up->location &&
106 !(up->location & PNG_HAVE_PLTE) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600107 !(up->location & PNG_HAVE_IDAT) &&
Glenn Randers-Pehrson36fa2a02011-05-11 06:52:37 -0500108 !(up->location & PNG_AFTER_IDAT) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600109 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
110 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600111 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500112 if (up->size == 0)
113 png_warning(png_ptr, "Writing zero-length unknown chunk");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500114
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600115 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600116 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500117 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600118 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500119#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600120 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
121 }
122}
123
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500124void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600125png_write_info(png_structrp png_ptr, png_inforp info_ptr)
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600126{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600127#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600128 int i;
129#endif
130
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500131 png_debug(1, "in png_write_info");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600132
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600133 if (png_ptr == NULL || info_ptr == NULL)
134 return;
135
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600136 png_write_info_before_PLTE(png_ptr, info_ptr);
137
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600138 if (info_ptr->valid & PNG_INFO_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500139 png_write_PLTE(png_ptr, info_ptr->palette,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600140 (png_uint_32)info_ptr->num_palette);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500141
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600142 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600143 png_error(png_ptr, "Valid palette required for paletted images");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600144
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500145#ifdef PNG_WRITE_tRNS_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600146 if (info_ptr->valid & PNG_INFO_tRNS)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500147 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500148#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500149 /* Invert the alpha channel (in tRNS) */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500150 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600151 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500152 {
153 int j;
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500154 for (j = 0; j<(int)info_ptr->num_trans; j++)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500155 info_ptr->trans_alpha[j] =
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -0500156 (png_byte)(255 - info_ptr->trans_alpha[j]);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500157 }
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600158#endif
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -0500159 png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600160 info_ptr->num_trans, info_ptr->color_type);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500161 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500162#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500163#ifdef PNG_WRITE_bKGD_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600164 if (info_ptr->valid & PNG_INFO_bKGD)
165 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500166#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500167
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500168#ifdef PNG_WRITE_hIST_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600169 if (info_ptr->valid & PNG_INFO_hIST)
170 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500171#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500172
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500173#ifdef PNG_WRITE_oFFs_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600174 if (info_ptr->valid & PNG_INFO_oFFs)
175 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600176 info_ptr->offset_unit_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500177#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500178
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500179#ifdef PNG_WRITE_pCAL_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500180 if (info_ptr->valid & PNG_INFO_pCAL)
181 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600182 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
183 info_ptr->pcal_units, info_ptr->pcal_params);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500184#endif
Glenn Randers-Pehrson59020592009-05-18 10:52:12 -0500185
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500186#ifdef PNG_WRITE_sCAL_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500187 if (info_ptr->valid & PNG_INFO_sCAL)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600188 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600189 info_ptr->scal_s_width, info_ptr->scal_s_height);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500190#endif /* sCAL */
191
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500192#ifdef PNG_WRITE_pHYs_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500193 if (info_ptr->valid & PNG_INFO_pHYs)
194 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600195 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500196#endif /* pHYs */
197
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500198#ifdef PNG_WRITE_tIME_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600199 if (info_ptr->valid & PNG_INFO_tIME)
Guy Schalnate5a37791996-06-05 15:50:50 -0500200 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600201 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600202 png_ptr->mode |= PNG_WROTE_tIME;
Guy Schalnate5a37791996-06-05 15:50:50 -0500203 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500204#endif /* tIME */
205
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500206#ifdef PNG_WRITE_sPLT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600207 if (info_ptr->valid & PNG_INFO_sPLT)
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600208 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
209 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500210#endif /* sPLT */
211
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500212#ifdef PNG_WRITE_TEXT_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500213 /* Check to see if we need to write text chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500214 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500215 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500216 png_debug2(2, "Writing header text chunk %d, type %d", i,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600217 info_ptr->text[i].compression);
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500218 /* An internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600219 if (info_ptr->text[i].compression > 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600220 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500221#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600222 /* Write international chunk */
223 png_write_iTXt(png_ptr,
224 info_ptr->text[i].compression,
225 info_ptr->text[i].key,
226 info_ptr->text[i].lang,
227 info_ptr->text[i].lang_key,
228 info_ptr->text[i].text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600229#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600230 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600231#endif
232 /* Mark this chunk as written */
233 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
234 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500235
Andreas Dilger47a0c421997-05-16 02:46:07 -0500236 /* If we want a compressed text chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600237 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500238 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500239#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500240 /* Write compressed chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500241 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600242 info_ptr->text[i].text, 0,
243 info_ptr->text[i].compression);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500244#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600245 png_warning(png_ptr, "Unable to write compressed text");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500246#endif
247 /* Mark this chunk as written */
248 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
249 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500250
Andreas Dilger47a0c421997-05-16 02:46:07 -0500251 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
252 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500253#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500254 /* Write uncompressed chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500255 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600256 info_ptr->text[i].text,
257 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500258 /* Mark this chunk as written */
259 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500260#else
261 /* Can't get here */
262 png_warning(png_ptr, "Unable to write uncompressed text");
263#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500264 }
265 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500266#endif /* tEXt */
267
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500268#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600269 if (info_ptr->unknown_chunks_num)
270 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500271 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600272
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500273 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600274
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500275 for (up = info_ptr->unknown_chunks;
276 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
277 up++)
278 {
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500279 int keep = png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500280 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson36fa2a02011-05-11 06:52:37 -0500281 up->location &&
282 (up->location & PNG_HAVE_PLTE) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600283 !(up->location & PNG_HAVE_IDAT) &&
Glenn Randers-Pehrson36fa2a02011-05-11 06:52:37 -0500284 !(up->location & PNG_AFTER_IDAT) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600285 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
286 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600287 {
288 png_write_chunk(png_ptr, up->name, up->data, up->size);
289 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500290 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600291 }
292#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500293}
Guy Schalnat0d580581995-07-20 02:43:20 -0500294
Andreas Dilger47a0c421997-05-16 02:46:07 -0500295/* Writes the end of the PNG file. If you don't want to write comments or
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600296 * time information, you can pass NULL for info. If you already wrote these
297 * in png_write_info(), do not write them again here. If you have long
298 * comments, I suggest writing them here, and compressing them.
299 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500300void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600301png_write_end(png_structrp png_ptr, png_inforp info_ptr)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500302{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500303 png_debug(1, "in png_write_end");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500304
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600305 if (png_ptr == NULL)
306 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500307
Andreas Dilger47a0c421997-05-16 02:46:07 -0500308 if (!(png_ptr->mode & PNG_HAVE_IDAT))
309 png_error(png_ptr, "No IDATs written into file");
310
Glenn Randers-Pehrsoneeb1bb62012-03-02 22:10:15 -0600311#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
312 if (png_ptr->num_palette_max > png_ptr->num_palette)
313 png_warning(png_ptr, "Wrote palette index exceeding num_palette");
314#endif
315
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500316 /* See if user wants us to write information chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500317 if (info_ptr != NULL)
318 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500319#ifdef PNG_WRITE_TEXT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500320 int i; /* local index variable */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600321#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500322#ifdef PNG_WRITE_tIME_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500323 /* Check to see if user has supplied a time chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600324 if ((info_ptr->valid & PNG_INFO_tIME) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600325 !(png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500326 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500327
Andreas Dilger47a0c421997-05-16 02:46:07 -0500328#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500329#ifdef PNG_WRITE_TEXT_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500330 /* Loop through comment chunks */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600331 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500332 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500333 png_debug2(2, "Writing trailer text chunk %d, type %d", i,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500334 info_ptr->text[i].compression);
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500335 /* An internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600336 if (info_ptr->text[i].compression > 0)
337 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500338#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500339 /* Write international chunk */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500340 png_write_iTXt(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600341 info_ptr->text[i].compression,
342 info_ptr->text[i].key,
343 info_ptr->text[i].lang,
344 info_ptr->text[i].lang_key,
345 info_ptr->text[i].text);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600346#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500347 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600348#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500349 /* Mark this chunk as written */
350 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600351 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500352
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600353 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
Guy Schalnat0d580581995-07-20 02:43:20 -0500354 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500355#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500356 /* Write compressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600357 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600358 info_ptr->text[i].text, 0,
359 info_ptr->text[i].compression);
Guy Schalnate5a37791996-06-05 15:50:50 -0500360#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600361 png_warning(png_ptr, "Unable to write compressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500362#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500363 /* Mark this chunk as written */
364 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500365 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500366
Andreas Dilger47a0c421997-05-16 02:46:07 -0500367 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500368 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500369#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500370 /* Write uncompressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600371 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600372 info_ptr->text[i].text, 0);
Guy Schalnate5a37791996-06-05 15:50:50 -0500373#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600374 png_warning(png_ptr, "Unable to write uncompressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500375#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500376
Andreas Dilger47a0c421997-05-16 02:46:07 -0500377 /* Mark this chunk as written */
378 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500379 }
380 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600381#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500382#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600383 if (info_ptr->unknown_chunks_num)
384 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500385 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600386
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500387 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600388
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500389 for (up = info_ptr->unknown_chunks;
390 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
391 up++)
392 {
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500393 int keep = png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500394 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson36fa2a02011-05-11 06:52:37 -0500395 up->location &&
396 (up->location & PNG_AFTER_IDAT) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600397 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
398 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600399 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600400 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600401 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500402 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600403 }
404#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500405 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500406
407 png_ptr->mode |= PNG_AFTER_IDAT;
408
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500409 /* Write end of PNG file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500410 png_write_IEND(png_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500411 /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
412 * and restored again in libpng-1.2.30, may cause some applications that
413 * do not set png_ptr->output_flush_fn to crash. If your application
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600414 * experiences a problem, please try building libpng with
415 * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500416 * png-mng-implement at lists.sf.net .
417 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500418#ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600419# ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
Glenn Randers-Pehrson32fc5ce2000-07-24 06:34:14 -0500420 png_flush(png_ptr);
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600421# endif
Glenn Randers-Pehrsondbed41f2008-08-19 18:20:52 -0500422#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500423}
424
Glenn Randers-Pehrson65d235a2009-11-02 11:32:30 -0600425#ifdef PNG_CONVERT_tIME_SUPPORTED
Glenn Randers-Pehrson99106de2009-11-01 16:26:14 -0600426/* "tm" structure is not supported on WindowsCE */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500427void PNGAPI
John Bowlerbaeb6d12011-11-26 18:21:02 -0600428png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm * ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500429{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500430 png_debug(1, "in png_convert_from_struct_tm");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500431
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600432 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
433 ptime->month = (png_byte)(ttime->tm_mon + 1);
434 ptime->day = (png_byte)ttime->tm_mday;
435 ptime->hour = (png_byte)ttime->tm_hour;
436 ptime->minute = (png_byte)ttime->tm_min;
437 ptime->second = (png_byte)ttime->tm_sec;
Guy Schalnat0d580581995-07-20 02:43:20 -0500438}
439
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500440void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600441png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500442{
443 struct tm *tbuf;
444
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500445 png_debug(1, "in png_convert_from_time_t");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500446
Guy Schalnat0d580581995-07-20 02:43:20 -0500447 tbuf = gmtime(&ttime);
448 png_convert_from_struct_tm(ptime, tbuf);
449}
Guy Schalnat6d764711995-12-19 03:22:19 -0600450#endif
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500451
Andreas Dilger47a0c421997-05-16 02:46:07 -0500452/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500453PNG_FUNCTION(png_structp,PNGAPI
454png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
455 png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500456{
John Bowlerd332c672011-12-21 17:36:12 -0600457#ifndef PNG_USER_MEM_SUPPORTED
John Bowler5d567862011-12-24 09:12:00 -0600458 png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
John Bowlerd332c672011-12-21 17:36:12 -0600459 error_fn, warn_fn, NULL, NULL, NULL);
460#else
461 return png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
462 warn_fn, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500463}
464
465/* Alternate initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500466PNG_FUNCTION(png_structp,PNGAPI
467png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600468 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500469 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500470{
John Bowler5d567862011-12-24 09:12:00 -0600471 png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
John Bowlerd332c672011-12-21 17:36:12 -0600472 error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500473#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500474
John Bowler42a2b552012-03-05 20:57:40 -0600475 /* Set the zlib control values to defaults; they can be overridden by the
476 * application after the struct has been created.
477 */
John Bowlerb5d00512012-03-09 09:15:18 -0600478 png_ptr->zbuffer_size = PNG_ZBUF_SIZE;
479
John Bowler42a2b552012-03-05 20:57:40 -0600480 png_ptr->zlib_strategy = Z_FILTERED; /* may be overridden if no filters */
481 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
482 png_ptr->zlib_mem_level = 8;
483 png_ptr->zlib_window_bits = 15;
484 png_ptr->zlib_method = 8;
485
486#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
487 png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY;
488 png_ptr->zlib_text_level = Z_DEFAULT_COMPRESSION;
489 png_ptr->zlib_text_mem_level = 8;
490 png_ptr->zlib_text_window_bits = 15;
491 png_ptr->zlib_text_method = 8;
492#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
493
John Bowlerd332c672011-12-21 17:36:12 -0600494 if (png_ptr != NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500495 {
John Bowlerd332c672011-12-21 17:36:12 -0600496 /* TODO: delay this, it can be done in png_init_io() (if the app doesn't
497 * do it itself) avoiding setting the default function if it is not
498 * required.
499 */
500 png_set_write_fn(png_ptr, NULL, NULL, NULL);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500501 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500502
John Bowlerd332c672011-12-21 17:36:12 -0600503 return png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500504}
505
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500506
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600507/* Write a few rows of image data. If the image is interlaced,
508 * either you will have to write the 7 sub images, or, if you
509 * have called png_set_interlace_handling(), you will have to
510 * "write" the image seven times.
511 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500512void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600513png_write_rows(png_structrp png_ptr, png_bytepp row,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600514 png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500515{
516 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600517 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500518
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500519 png_debug(1, "in png_write_rows");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600520
521 if (png_ptr == NULL)
522 return;
523
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500524 /* Loop through the rows */
Guy Schalnat0d580581995-07-20 02:43:20 -0500525 for (i = 0, rp = row; i < num_rows; i++, rp++)
526 {
527 png_write_row(png_ptr, *rp);
528 }
529}
530
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600531/* Write the image. You only need to call this function once, even
532 * if you are writing an interlaced image.
533 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500534void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600535png_write_image(png_structrp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500536{
537 png_uint_32 i; /* row index */
538 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600539 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500540
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600541 if (png_ptr == NULL)
542 return;
543
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500544 png_debug(1, "in png_write_image");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500545
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500546#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500547 /* Initialize interlace handling. If image is not interlaced,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500548 * this will set pass to 1
549 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500550 num_pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600551#else
552 num_pass = 1;
553#endif
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500554 /* Loop through passes */
Guy Schalnat0d580581995-07-20 02:43:20 -0500555 for (pass = 0; pass < num_pass; pass++)
556 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500557 /* Loop through image */
Guy Schalnat0d580581995-07-20 02:43:20 -0500558 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
559 {
560 png_write_row(png_ptr, *rp);
561 }
562 }
563}
564
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500565/* Called by user to write a row of image data */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500566void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600567png_write_row(png_structrp png_ptr, png_const_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500568{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500569 /* 1.5.6: moved from png_struct to be a local structure: */
570 png_row_info row_info;
571
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600572 if (png_ptr == NULL)
573 return;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500574
Glenn Randers-Pehrson632a84e2010-03-09 22:28:33 -0600575 png_debug2(1, "in png_write_row (row %u, pass %d)",
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600576 png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600577
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500578 /* Initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600579 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500580 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500581 /* Make sure we wrote the header info */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500582 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
583 png_error(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600584 "png_write_info was never called before png_write_row");
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600585
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500586 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500587#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500588 if (png_ptr->transformations & PNG_INVERT_MONO)
589 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500590#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500591
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500592#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500593 if (png_ptr->transformations & PNG_FILLER)
594 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500595#endif
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600596#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
597 defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500598 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600599 png_warning(png_ptr,
600 "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500601#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500602
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500603#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500604 if (png_ptr->transformations & PNG_PACK)
605 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500606#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500607
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500608#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500609 if (png_ptr->transformations & PNG_SHIFT)
610 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500611#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500612
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500613#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500614 if (png_ptr->transformations & PNG_BGR)
615 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500616#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500617
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500618#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500619 if (png_ptr->transformations & PNG_SWAP_BYTES)
620 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500621#endif
622
Guy Schalnat0d580581995-07-20 02:43:20 -0500623 png_write_start_row(png_ptr);
624 }
625
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500626#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500627 /* If interlaced and not interested in row, return */
Guy Schalnat0d580581995-07-20 02:43:20 -0500628 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
629 {
630 switch (png_ptr->pass)
631 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600632 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600633 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500634 {
635 png_write_finish_row(png_ptr);
636 return;
637 }
638 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500639
Guy Schalnat0d580581995-07-20 02:43:20 -0500640 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600641 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500642 {
643 png_write_finish_row(png_ptr);
644 return;
645 }
646 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500647
Guy Schalnat0d580581995-07-20 02:43:20 -0500648 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600649 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500650 {
651 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600652 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500653 }
654 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500655
Guy Schalnat0d580581995-07-20 02:43:20 -0500656 case 3:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600657 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
Guy Schalnat0d580581995-07-20 02:43:20 -0500658 {
659 png_write_finish_row(png_ptr);
660 return;
661 }
662 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500663
Guy Schalnat0d580581995-07-20 02:43:20 -0500664 case 4:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600665 if ((png_ptr->row_number & 0x03) != 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500666 {
667 png_write_finish_row(png_ptr);
668 return;
669 }
670 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500671
Guy Schalnat0d580581995-07-20 02:43:20 -0500672 case 5:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600673 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500674 {
675 png_write_finish_row(png_ptr);
676 return;
677 }
678 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500679
Guy Schalnat0d580581995-07-20 02:43:20 -0500680 case 6:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600681 if (!(png_ptr->row_number & 0x01))
Guy Schalnat0d580581995-07-20 02:43:20 -0500682 {
683 png_write_finish_row(png_ptr);
684 return;
685 }
686 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500687
688 default: /* error: ignore it */
689 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500690 }
691 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600692#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500693
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500694 /* Set up row info for transformations */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500695 row_info.color_type = png_ptr->color_type;
696 row_info.width = png_ptr->usr_width;
697 row_info.channels = png_ptr->usr_channels;
698 row_info.bit_depth = png_ptr->usr_bit_depth;
699 row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels);
700 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600701
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500702 png_debug1(3, "row_info->color_type = %d", row_info.color_type);
703 png_debug1(3, "row_info->width = %u", row_info.width);
704 png_debug1(3, "row_info->channels = %d", row_info.channels);
705 png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth);
706 png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth);
707 png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500708
709 /* Copy user's row into buffer, leaving room for filter byte. */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500710 png_memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500711
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500712#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500713 /* Handle interlacing */
Guy Schalnat0d580581995-07-20 02:43:20 -0500714 if (png_ptr->interlaced && png_ptr->pass < 6 &&
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500715 (png_ptr->transformations & PNG_INTERLACE))
Guy Schalnat0d580581995-07-20 02:43:20 -0500716 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500717 png_do_write_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500718 /* This should always get caught above, but still ... */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500719 if (!(row_info.width))
Guy Schalnat0d580581995-07-20 02:43:20 -0500720 {
721 png_write_finish_row(png_ptr);
722 return;
723 }
724 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600725#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500726
John Bowler4a12f4a2011-04-17 18:34:22 -0500727#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500728 /* Handle other transformations */
Guy Schalnat0d580581995-07-20 02:43:20 -0500729 if (png_ptr->transformations)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500730 png_do_write_transformations(png_ptr, &row_info);
John Bowler4a12f4a2011-04-17 18:34:22 -0500731#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500732
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500733 /* At this point the row_info pixel depth must match the 'transformed' depth,
734 * which is also the output depth.
735 */
736 if (row_info.pixel_depth != png_ptr->pixel_depth ||
737 row_info.pixel_depth != png_ptr->transformed_pixel_depth)
738 png_error(png_ptr, "internal write transform logic error");
739
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500740#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600741 /* Write filter_method 64 (intrapixel differencing) only if
742 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
743 * 2. Libpng did not write a PNG signature (this filter_method is only
744 * used in PNG datastreams that are embedded in MNG datastreams) and
745 * 3. The application called png_permit_mng_features with a mask that
746 * included PNG_FLAG_MNG_FILTER_64 and
747 * 4. The filter_method is 64 and
748 * 5. The color_type is RGB or RGBA
749 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500750 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500751 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600752 {
753 /* Intrapixel differencing */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500754 png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600755 }
756#endif
757
Glenn Randers-Pehrson363ae652012-03-01 21:39:29 -0600758/* Added at libpng-1.5.10 */
759#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
John Bowlerdee75772012-03-01 18:55:54 -0600760 /* Check for out-of-range palette index */
Glenn Randers-Pehrsoneeb1bb62012-03-02 22:10:15 -0600761 if(row_info.color_type == PNG_COLOR_TYPE_PALETTE)
762 png_do_check_palette_indexes(png_ptr, &row_info);
John Bowlerdee75772012-03-01 18:55:54 -0600763#endif
764
Andreas Dilger47a0c421997-05-16 02:46:07 -0500765 /* Find a filter if necessary, filter the row and write it out. */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500766 png_write_find_filter(png_ptr, &row_info);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600767
768 if (png_ptr->write_row_fn != NULL)
769 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500770}
771
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500772#ifdef PNG_WRITE_FLUSH_SUPPORTED
Guy Schalnat0f716451995-11-28 11:22:13 -0600773/* Set the automatic flush interval or 0 to turn flushing off */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500774void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600775png_set_flush(png_structrp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600776{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500777 png_debug(1, "in png_set_flush");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500778
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600779 if (png_ptr == NULL)
780 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500781
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600782 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600783}
784
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500785/* Flush the current output buffers now */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500786void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600787png_write_flush(png_structrp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600788{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500789 png_debug(1, "in png_write_flush");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500790
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600791 if (png_ptr == NULL)
792 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500793
Guy Schalnate5a37791996-06-05 15:50:50 -0500794 /* We have already written out all of the data */
795 if (png_ptr->row_number >= png_ptr->num_rows)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500796 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600797
John Bowlerb5d00512012-03-09 09:15:18 -0600798 png_compress_IDAT(png_ptr, NULL, 0, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600799 png_ptr->flush_rows = 0;
800 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600801}
802#endif /* PNG_WRITE_FLUSH_SUPPORTED */
803
John Bowlerd332c672011-12-21 17:36:12 -0600804#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
John Bowler5d567862011-12-24 09:12:00 -0600805static void png_reset_filter_heuristics(png_structrp png_ptr); /* forward decl */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500806#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600807
John Bowlerd332c672011-12-21 17:36:12 -0600808/* Free any memory used in png_ptr struct without freeing the struct itself. */
809static void
John Bowler5d567862011-12-24 09:12:00 -0600810png_write_destroy(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500811{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500812 png_debug(1, "in png_write_destroy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500813
814 /* Free any memory zlib uses */
John Bowler42a2b552012-03-05 20:57:40 -0600815 if (png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED)
John Bowlerc5bef942011-05-05 17:35:39 -0500816 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -0500817
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500818 /* Free our memory. png_free checks NULL for us. */
John Bowlerb5d00512012-03-09 09:15:18 -0600819 png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600820 png_free(png_ptr, png_ptr->row_buf);
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500821#ifdef PNG_WRITE_FILTER_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600822 png_free(png_ptr, png_ptr->prev_row);
823 png_free(png_ptr, png_ptr->sub_row);
824 png_free(png_ptr, png_ptr->up_row);
825 png_free(png_ptr, png_ptr->avg_row);
826 png_free(png_ptr, png_ptr->paeth_row);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500827#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600828
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500829#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500830 /* Use this to save a little code space, it doesn't free the filter_costs */
831 png_reset_filter_heuristics(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500832 png_free(png_ptr, png_ptr->filter_costs);
833 png_free(png_ptr, png_ptr->inv_filter_costs);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600834#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500835
John Bowlerd332c672011-12-21 17:36:12 -0600836#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
837 png_free(png_ptr, png_ptr->chunk_list);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600838#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500839
John Bowlerd332c672011-12-21 17:36:12 -0600840 /* The error handling and memory handling information is left intact at this
841 * point: the jmp_buf may still have to be freed. See png_destroy_png_struct
842 * for how this happens.
843 */
844}
Guy Schalnate5a37791996-06-05 15:50:50 -0500845
John Bowlerd332c672011-12-21 17:36:12 -0600846/* Free all memory used by the write.
847 * In libpng 1.6.0 this API changed quietly to no longer accept a NULL value for
848 * *png_ptr_ptr. Prior to 1.6.0 it would accept such a value and it would free
849 * the passed in info_structs but it would quietly fail to free any of the data
850 * inside them. In 1.6.0 it quietly does nothing (it has to be quiet because it
851 * has no png_ptr.)
852 */
853void PNGAPI
854png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
855{
856 png_debug(1, "in png_destroy_write_struct");
Guy Schalnate5a37791996-06-05 15:50:50 -0500857
John Bowlerd332c672011-12-21 17:36:12 -0600858 if (png_ptr_ptr != NULL)
859 {
John Bowler5d567862011-12-24 09:12:00 -0600860 png_structrp png_ptr = *png_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500861
John Bowlerd332c672011-12-21 17:36:12 -0600862 if (png_ptr != NULL) /* added in libpng 1.6.0 */
863 {
864 png_destroy_info_struct(png_ptr, info_ptr_ptr);
865
866 *png_ptr_ptr = NULL;
867 png_write_destroy(png_ptr);
868 png_destroy_png_struct(png_ptr);
869 }
870 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500871}
Guy Schalnate5a37791996-06-05 15:50:50 -0500872
Andreas Dilger47a0c421997-05-16 02:46:07 -0500873/* Allow the application to select one or more row filters to use. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500874void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600875png_set_filter(png_structrp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500876{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500877 png_debug(1, "in png_set_filter");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500878
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600879 if (png_ptr == NULL)
880 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500881
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500882#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500883 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600884 (method == PNG_INTRAPIXEL_DIFFERENCING))
885 method = PNG_FILTER_TYPE_BASE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500886
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600887#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500888 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500889 {
890 switch (filters & (PNG_ALL_FILTERS | 0x07))
891 {
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500892#ifdef PNG_WRITE_FILTER_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500893 case 5:
894 case 6:
Andreas Dilger47a0c421997-05-16 02:46:07 -0500895 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500896#endif /* PNG_WRITE_FILTER_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500897 case PNG_FILTER_VALUE_NONE:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600898 png_ptr->do_filter = PNG_FILTER_NONE; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500899
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500900#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500901 case PNG_FILTER_VALUE_SUB:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600902 png_ptr->do_filter = PNG_FILTER_SUB; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500903
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500904 case PNG_FILTER_VALUE_UP:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600905 png_ptr->do_filter = PNG_FILTER_UP; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500906
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500907 case PNG_FILTER_VALUE_AVG:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600908 png_ptr->do_filter = PNG_FILTER_AVG; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500909
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500910 case PNG_FILTER_VALUE_PAETH:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600911 png_ptr->do_filter = PNG_FILTER_PAETH; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500912
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600913 default:
914 png_ptr->do_filter = (png_byte)filters; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500915#else
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600916 default:
917 png_warning(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500918#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500919 }
920
Andreas Dilger47a0c421997-05-16 02:46:07 -0500921 /* If we have allocated the row_buf, this means we have already started
922 * with the image and we should have allocated all of the filter buffers
923 * that have been selected. If prev_row isn't already allocated, then
924 * it is too late to start using the filters that need it, since we
925 * will be missing the data in the previous row. If an application
926 * wants to start and stop using particular filters during compression,
927 * it should start out with all of the filters, and then add and
928 * remove them after the start of compression.
Guy Schalnate5a37791996-06-05 15:50:50 -0500929 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500930 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500931 {
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500932#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600933 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500934 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500935 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600936 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500937 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -0500938 }
939
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600940 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500941 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500942 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500943 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500944 png_warning(png_ptr, "Can't add Up filter after starting");
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500945 png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
946 ~PNG_FILTER_UP);
Guy Schalnate5a37791996-06-05 15:50:50 -0500947 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500948
Guy Schalnate5a37791996-06-05 15:50:50 -0500949 else
950 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500951 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600952 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500953 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -0500954 }
955 }
956
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600957 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500958 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500959 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500960 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500961 png_warning(png_ptr, "Can't add Average filter after starting");
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500962 png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
963 ~PNG_FILTER_AVG);
Guy Schalnate5a37791996-06-05 15:50:50 -0500964 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500965
Guy Schalnate5a37791996-06-05 15:50:50 -0500966 else
967 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500968 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600969 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500970 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -0500971 }
972 }
973
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600974 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
Andreas Dilger47a0c421997-05-16 02:46:07 -0500975 png_ptr->paeth_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500976 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500977 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500978 {
979 png_warning(png_ptr, "Can't add Paeth filter after starting");
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500980 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
Guy Schalnate5a37791996-06-05 15:50:50 -0500981 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500982
Guy Schalnate5a37791996-06-05 15:50:50 -0500983 else
984 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600985 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600986 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500987 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -0500988 }
989 }
990
991 if (png_ptr->do_filter == PNG_NO_FILTERS)
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500992#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500993 png_ptr->do_filter = PNG_FILTER_NONE;
994 }
995 }
996 else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500997 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500998}
999
Andreas Dilger47a0c421997-05-16 02:46:07 -05001000/* This allows us to influence the way in which libpng chooses the "best"
1001 * filter for the current scanline. While the "minimum-sum-of-absolute-
1002 * differences metric is relatively fast and effective, there is some
1003 * question as to whether it can be improved upon by trying to keep the
1004 * filtered data going to zlib more consistent, hopefully resulting in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001005 * better compression.
1006 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001007#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */
Glenn Randers-Pehrson47dc5f72011-03-30 09:59:02 -05001008/* Convenience reset API. */
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001009static void
John Bowler5d567862011-12-24 09:12:00 -06001010png_reset_filter_heuristics(png_structrp png_ptr)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001011{
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001012 /* Clear out any old values in the 'weights' - this must be done because if
1013 * the app calls set_filter_heuristics multiple times with different
1014 * 'num_weights' values we would otherwise potentially have wrong sized
1015 * arrays.
1016 */
1017 png_ptr->num_prev_filters = 0;
1018 png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1019 if (png_ptr->prev_filters != NULL)
1020 {
1021 png_bytep old = png_ptr->prev_filters;
1022 png_ptr->prev_filters = NULL;
1023 png_free(png_ptr, old);
1024 }
1025 if (png_ptr->filter_weights != NULL)
1026 {
1027 png_uint_16p old = png_ptr->filter_weights;
1028 png_ptr->filter_weights = NULL;
1029 png_free(png_ptr, old);
1030 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001031
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001032 if (png_ptr->inv_filter_weights != NULL)
1033 {
1034 png_uint_16p old = png_ptr->inv_filter_weights;
1035 png_ptr->inv_filter_weights = NULL;
1036 png_free(png_ptr, old);
1037 }
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001038
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001039 /* Leave the filter_costs - this array is fixed size. */
1040}
1041
1042static int
John Bowler5d567862011-12-24 09:12:00 -06001043png_init_filter_heuristics(png_structrp png_ptr, int heuristic_method,
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001044 int num_weights)
1045{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001046 if (png_ptr == NULL)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001047 return 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001048
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001049 /* Clear out the arrays */
1050 png_reset_filter_heuristics(png_ptr);
1051
1052 /* Check arguments; the 'reset' function makes the correct settings for the
1053 * unweighted case, but we must handle the weight case by initializing the
1054 * arrays for the caller.
1055 */
1056 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001057 {
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001058 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001059
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001060 if (num_weights > 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001061 {
1062 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001063 (png_uint_32)(png_sizeof(png_byte) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001064
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001065 /* To make sure that the weighting starts out fairly */
1066 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001067 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001068 png_ptr->prev_filters[i] = 255;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001069 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001070
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001071 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
1072 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001073
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001074 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
1075 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001076
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001077 for (i = 0; i < num_weights; i++)
1078 {
1079 png_ptr->inv_filter_weights[i] =
1080 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1081 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001082
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001083 /* Safe to set this now */
1084 png_ptr->num_prev_filters = (png_byte)num_weights;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001085 }
1086
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001087 /* If, in the future, there are other filter methods, this would
1088 * need to be based on png_ptr->filter.
1089 */
1090 if (png_ptr->filter_costs == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001091 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001092 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
1093 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001094
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001095 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
1096 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001097 }
1098
Andreas Dilger47a0c421997-05-16 02:46:07 -05001099 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1100 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001101 png_ptr->inv_filter_costs[i] =
1102 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001103 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001104
1105 /* All the arrays are inited, safe to set this: */
1106 png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_WEIGHTED;
1107
1108 /* Return the 'ok' code. */
1109 return 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001110 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001111 else if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT ||
1112 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001113 {
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001114 return 1;
1115 }
1116 else
1117 {
1118 png_warning(png_ptr, "Unknown filter heuristic method");
1119 return 0;
1120 }
1121}
1122
1123/* Provide floating and fixed point APIs */
1124#ifdef PNG_FLOATING_POINT_SUPPORTED
1125void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001126png_set_filter_heuristics(png_structrp png_ptr, int heuristic_method,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001127 int num_weights, png_const_doublep filter_weights,
1128 png_const_doublep filter_costs)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001129{
1130 png_debug(1, "in png_set_filter_heuristics");
1131
1132 /* The internal API allocates all the arrays and ensures that the elements of
1133 * those arrays are set to the default value.
1134 */
1135 if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
1136 return;
1137
1138 /* If using the weighted method copy in the weights. */
1139 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1140 {
1141 int i;
1142 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001143 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001144 if (filter_weights[i] <= 0.0)
1145 {
1146 png_ptr->inv_filter_weights[i] =
1147 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1148 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001149
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001150 else
1151 {
1152 png_ptr->inv_filter_weights[i] =
1153 (png_uint_16)(PNG_WEIGHT_FACTOR*filter_weights[i]+.5);
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001154
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001155 png_ptr->filter_weights[i] =
1156 (png_uint_16)(PNG_WEIGHT_FACTOR/filter_weights[i]+.5);
1157 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001158 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001159
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001160 /* Here is where we set the relative costs of the different filters. We
1161 * should take the desired compression level into account when setting
1162 * the costs, so that Paeth, for instance, has a high relative cost at low
1163 * compression levels, while it has a lower relative cost at higher
1164 * compression settings. The filter types are in order of increasing
1165 * relative cost, so it would be possible to do this with an algorithm.
1166 */
1167 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) if (filter_costs[i] >= 1.0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001168 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001169 png_ptr->inv_filter_costs[i] =
1170 (png_uint_16)(PNG_COST_FACTOR / filter_costs[i] + .5);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001171
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001172 png_ptr->filter_costs[i] =
1173 (png_uint_16)(PNG_COST_FACTOR * filter_costs[i] + .5);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001174 }
1175 }
1176}
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001177#endif /* FLOATING_POINT */
1178
1179#ifdef PNG_FIXED_POINT_SUPPORTED
1180void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001181png_set_filter_heuristics_fixed(png_structrp png_ptr, int heuristic_method,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001182 int num_weights, png_const_fixed_point_p filter_weights,
1183 png_const_fixed_point_p filter_costs)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001184{
1185 png_debug(1, "in png_set_filter_heuristics_fixed");
1186
1187 /* The internal API allocates all the arrays and ensures that the elements of
1188 * those arrays are set to the default value.
1189 */
1190 if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
1191 return;
1192
1193 /* If using the weighted method copy in the weights. */
1194 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1195 {
1196 int i;
1197 for (i = 0; i < num_weights; i++)
1198 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001199 if (filter_weights[i] <= 0)
1200 {
1201 png_ptr->inv_filter_weights[i] =
1202 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1203 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001204
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001205 else
1206 {
1207 png_ptr->inv_filter_weights[i] = (png_uint_16)
1208 ((PNG_WEIGHT_FACTOR*filter_weights[i]+PNG_FP_HALF)/PNG_FP_1);
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001209
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001210 png_ptr->filter_weights[i] = (png_uint_16)((PNG_WEIGHT_FACTOR*
1211 PNG_FP_1+(filter_weights[i]/2))/filter_weights[i]);
1212 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001213 }
1214
1215 /* Here is where we set the relative costs of the different filters. We
1216 * should take the desired compression level into account when setting
1217 * the costs, so that Paeth, for instance, has a high relative cost at low
1218 * compression levels, while it has a lower relative cost at higher
1219 * compression settings. The filter types are in order of increasing
1220 * relative cost, so it would be possible to do this with an algorithm.
1221 */
1222 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1223 if (filter_costs[i] >= PNG_FP_1)
1224 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001225 png_uint_32 tmp;
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001226
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001227 /* Use a 32 bit unsigned temporary here because otherwise the
1228 * intermediate value will be a 32 bit *signed* integer (ANSI rules)
1229 * and this will get the wrong answer on division.
1230 */
1231 tmp = PNG_COST_FACTOR*PNG_FP_1 + (filter_costs[i]/2);
1232 tmp /= filter_costs[i];
1233
1234 png_ptr->inv_filter_costs[i] = (png_uint_16)tmp;
1235
1236 tmp = PNG_COST_FACTOR * filter_costs[i] + PNG_FP_HALF;
1237 tmp /= PNG_FP_1;
1238
1239 png_ptr->filter_costs[i] = (png_uint_16)tmp;
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001240 }
1241 }
1242}
1243#endif /* FIXED_POINT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001244#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1245
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001246void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001247png_set_compression_level(png_structrp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001248{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001249 png_debug(1, "in png_set_compression_level");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001250
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001251 if (png_ptr == NULL)
1252 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001253
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001254 png_ptr->zlib_level = level;
1255}
1256
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001257void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001258png_set_compression_mem_level(png_structrp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001259{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001260 png_debug(1, "in png_set_compression_mem_level");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001261
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001262 if (png_ptr == NULL)
1263 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001264
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001265 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001266}
1267
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001268void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001269png_set_compression_strategy(png_structrp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001270{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001271 png_debug(1, "in png_set_compression_strategy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001272
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001273 if (png_ptr == NULL)
1274 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001275
John Bowler42a2b552012-03-05 20:57:40 -06001276 /* The flag setting here prevents the libpng dynamic selection of strategy.
1277 */
Guy Schalnate5a37791996-06-05 15:50:50 -05001278 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001279 png_ptr->zlib_strategy = strategy;
1280}
1281
Glenn Randers-Pehrsonaf855e42011-05-07 10:52:49 -05001282/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
1283 * smaller value of window_bits if it can do so safely.
1284 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001285void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001286png_set_compression_window_bits(png_structrp png_ptr, int window_bits)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001287{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001288 if (png_ptr == NULL)
1289 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001290
John Bowler42a2b552012-03-05 20:57:40 -06001291 /* Prior to 1.6.0 this would warn but then set the window_bits value, this
1292 * meant that negative window bits values could be selected which would cause
1293 * libpng to write a non-standard PNG file with raw deflate or gzip
1294 * compressed IDAT or ancilliary chunks. Such files can be read and there is
1295 * no warning on read, so this seems like a very bad idea.
1296 */
Guy Schalnate5a37791996-06-05 15:50:50 -05001297 if (window_bits > 15)
John Bowler42a2b552012-03-05 20:57:40 -06001298 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001299 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
John Bowler42a2b552012-03-05 20:57:40 -06001300 window_bits = 15;
1301 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001302
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001303 else if (window_bits < 8)
John Bowler42a2b552012-03-05 20:57:40 -06001304 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001305 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
John Bowler42a2b552012-03-05 20:57:40 -06001306 window_bits = 8;
1307 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001308
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001309 png_ptr->zlib_window_bits = window_bits;
1310}
1311
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001312void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001313png_set_compression_method(png_structrp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001314{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001315 png_debug(1, "in png_set_compression_method");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001316
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001317 if (png_ptr == NULL)
1318 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001319
John Bowler42a2b552012-03-05 20:57:40 -06001320 /* This would produce an invalid PNG file if it worked, but it doesn't and
1321 * deflate will fault it, so it is harmless to just warn here.
1322 */
Guy Schalnate5a37791996-06-05 15:50:50 -05001323 if (method != 8)
1324 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001325
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001326 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -05001327}
1328
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001329/* The following were added to libpng-1.5.4 */
John Bowlerb2bee332011-06-10 23:24:58 -05001330#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001331void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001332png_set_text_compression_level(png_structrp png_ptr, int level)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001333{
1334 png_debug(1, "in png_set_text_compression_level");
1335
1336 if (png_ptr == NULL)
1337 return;
1338
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001339 png_ptr->zlib_text_level = level;
1340}
1341
1342void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001343png_set_text_compression_mem_level(png_structrp png_ptr, int mem_level)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001344{
1345 png_debug(1, "in png_set_text_compression_mem_level");
1346
1347 if (png_ptr == NULL)
1348 return;
1349
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001350 png_ptr->zlib_text_mem_level = mem_level;
1351}
1352
1353void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001354png_set_text_compression_strategy(png_structrp png_ptr, int strategy)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001355{
1356 png_debug(1, "in png_set_text_compression_strategy");
1357
1358 if (png_ptr == NULL)
1359 return;
1360
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001361 png_ptr->zlib_text_strategy = strategy;
1362}
1363
Glenn Randers-Pehrsonaf855e42011-05-07 10:52:49 -05001364/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
1365 * smaller value of window_bits if it can do so safely.
1366 */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001367void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001368png_set_text_compression_window_bits(png_structrp png_ptr, int window_bits)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001369{
1370 if (png_ptr == NULL)
1371 return;
1372
1373 if (window_bits > 15)
John Bowler42a2b552012-03-05 20:57:40 -06001374 {
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001375 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
John Bowler42a2b552012-03-05 20:57:40 -06001376 window_bits = 15;
1377 }
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001378
1379 else if (window_bits < 8)
John Bowler42a2b552012-03-05 20:57:40 -06001380 {
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001381 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
John Bowler42a2b552012-03-05 20:57:40 -06001382 window_bits = 8;
1383 }
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001384
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001385 png_ptr->zlib_text_window_bits = window_bits;
1386}
1387
1388void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001389png_set_text_compression_method(png_structrp png_ptr, int method)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001390{
1391 png_debug(1, "in png_set_text_compression_method");
1392
1393 if (png_ptr == NULL)
1394 return;
1395
1396 if (method != 8)
1397 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1398
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001399 png_ptr->zlib_text_method = method;
1400}
John Bowlerb2bee332011-06-10 23:24:58 -05001401#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001402/* end of API added to libpng-1.5.4 */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001403
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001404void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001405png_set_write_status_fn(png_structrp png_ptr, png_write_status_ptr write_row_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001406{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001407 if (png_ptr == NULL)
1408 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001409
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001410 png_ptr->write_row_fn = write_row_fn;
1411}
1412
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001413#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001414void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001415png_set_write_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001416 write_user_transform_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001417{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001418 png_debug(1, "in png_set_write_user_transform_fn");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001419
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001420 if (png_ptr == NULL)
1421 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001422
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001423 png_ptr->transformations |= PNG_USER_TRANSFORM;
1424 png_ptr->write_user_transform_fn = write_user_transform_fn;
1425}
1426#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001427
1428
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001429#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001430void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001431png_write_png(png_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001432 int transforms, voidp params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001433{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001434 if (png_ptr == NULL || info_ptr == NULL)
1435 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001436
1437 /* Write the file header information. */
1438 png_write_info(png_ptr, info_ptr);
1439
1440 /* ------ these transformations don't touch the info structure ------- */
1441
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001442#ifdef PNG_WRITE_INVERT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001443 /* Invert monochrome pixels */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001444 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001445 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001446#endif
1447
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001448#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001449 /* Shift the pixels up to a legal bit depth and fill in
1450 * as appropriate to correctly scale the image.
1451 */
1452 if ((transforms & PNG_TRANSFORM_SHIFT)
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001453 && (info_ptr->valid & PNG_INFO_sBIT))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001454 png_set_shift(png_ptr, &info_ptr->sig_bit);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001455#endif
1456
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001457#ifdef PNG_WRITE_PACK_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001458 /* Pack pixels into bytes */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001459 if (transforms & PNG_TRANSFORM_PACKING)
1460 png_set_packing(png_ptr);
1461#endif
1462
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001463#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001464 /* Swap location of alpha bytes from ARGB to RGBA */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001465 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001466 png_set_swap_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001467#endif
1468
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001469#ifdef PNG_WRITE_FILLER_SUPPORTED
Glenn Randers-Pehrson47539062011-05-05 07:32:30 -05001470 /* Pack XRGB/RGBX/ARGB/RGBA into RGB (4 channels -> 3 channels) */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001471 if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001472 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001473
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001474 else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001475 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001476#endif
1477
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001478#ifdef PNG_WRITE_BGR_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001479 /* Flip BGR pixels to RGB */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001480 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001481 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001482#endif
1483
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001484#ifdef PNG_WRITE_SWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001485 /* Swap bytes of 16-bit files to most significant byte first */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001486 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001487 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001488#endif
1489
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001490#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001491 /* Swap bits of 1, 2, 4 bit packed pixel formats */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001492 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001493 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001494#endif
1495
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001496#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001497 /* Invert the alpha channel from opacity to transparency */
1498 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1499 png_set_invert_alpha(png_ptr);
1500#endif
1501
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001502 /* ----------------------- end of transformations ------------------- */
1503
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001504 /* Write the bits */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001505 if (info_ptr->valid & PNG_INFO_IDAT)
1506 png_write_image(png_ptr, info_ptr->row_pointers);
1507
1508 /* It is REQUIRED to call this to finish writing the rest of the file */
1509 png_write_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001510
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001511 PNG_UNUSED(transforms) /* Quiet compiler warnings */
1512 PNG_UNUSED(params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001513}
1514#endif
John Bowler7875d532011-11-07 22:33:49 -06001515
1516
1517#ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
1518#ifdef PNG_STDIO_SUPPORTED /* currently required for png_image_write_* */
1519/* Initialize the write structure - general purpose utility. */
1520static int
1521png_image_write_init(png_imagep image)
1522{
1523 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, image,
1524 png_safe_error, png_safe_warning);
1525
1526 if (png_ptr != NULL)
1527 {
1528 png_infop info_ptr = png_create_info_struct(png_ptr);
1529
1530 if (info_ptr != NULL)
1531 {
John Bowler4fa96a42011-11-16 16:39:16 -06001532 png_controlp control = png_voidcast(png_controlp,
1533 png_malloc_warn(png_ptr, sizeof *control));
John Bowler7875d532011-11-07 22:33:49 -06001534
1535 if (control != NULL)
1536 {
John Bowlerbaeb6d12011-11-26 18:21:02 -06001537 png_memset(control, 0, sizeof *control);
John Bowler7875d532011-11-07 22:33:49 -06001538
1539 control->png_ptr = png_ptr;
1540 control->info_ptr = info_ptr;
1541 control->for_write = 1;
1542
1543 image->opaque = control;
1544 return 1;
1545 }
1546
1547 /* Error clean up */
1548 png_destroy_info_struct(png_ptr, &info_ptr);
1549 }
1550
1551 png_destroy_write_struct(&png_ptr, NULL);
1552 }
1553
1554 return png_image_error(image, "png_image_read: out of memory");
1555}
1556
1557/* Arguments to png_image_write_main: */
1558typedef struct
1559{
1560 /* Arguments: */
1561 png_imagep image;
1562 png_const_voidp buffer;
1563 png_int_32 row_stride;
John Bowler5bc90382012-01-23 22:43:22 -06001564 png_const_voidp colormap;
John Bowler7875d532011-11-07 22:33:49 -06001565 int convert_to_8bit;
1566 /* Local variables: */
1567 png_const_voidp first_row;
1568 ptrdiff_t row_bytes;
1569 png_voidp local_row;
1570} png_image_write_control;
1571
Glenn Randers-Pehrsonefc4b692011-11-07 23:31:34 -06001572/* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to
1573 * do any necessary byte swapping. The component order is defined by the
John Bowler7875d532011-11-07 22:33:49 -06001574 * png_image format value.
1575 */
1576static int
1577png_write_image_16bit(png_voidp argument)
1578{
John Bowler4fa96a42011-11-16 16:39:16 -06001579 png_image_write_control *display = png_voidcast(png_image_write_control*,
1580 argument);
John Bowler7875d532011-11-07 22:33:49 -06001581 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06001582 png_structrp png_ptr = image->opaque->png_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001583
John Bowler4fa96a42011-11-16 16:39:16 -06001584 png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
1585 display->first_row);
1586 png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row);
John Bowler7875d532011-11-07 22:33:49 -06001587 png_uint_16p row_end;
John Bowler1c25b9b2012-02-29 10:49:28 -06001588 const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
John Bowler7875d532011-11-07 22:33:49 -06001589 int aindex = 0;
1590 png_uint_32 y = image->height;
1591
1592 if (image->format & PNG_FORMAT_FLAG_ALPHA)
1593 {
1594 if (image->format & PNG_FORMAT_FLAG_AFIRST)
1595 {
1596 aindex = -1;
1597 ++input_row; /* To point to the first component */
1598 ++output_row;
1599 }
1600
1601 else
1602 aindex = channels;
1603 }
1604
1605 else
1606 png_error(png_ptr, "png_write_image: internal call error");
1607
1608 /* Work out the output row end and count over this, note that the increment
1609 * above to 'row' means that row_end can actually be beyond the end of the
Glenn Randers-Pehrsonefc4b692011-11-07 23:31:34 -06001610 * row; this is correct.
John Bowler7875d532011-11-07 22:33:49 -06001611 */
1612 row_end = output_row + image->width * (channels+1);
1613
1614 while (y-- > 0)
1615 {
1616 png_const_uint_16p in_ptr = input_row;
1617 png_uint_16p out_ptr = output_row;
1618
1619 while (out_ptr < row_end)
1620 {
John Bowler1c25b9b2012-02-29 10:49:28 -06001621 const png_uint_16 alpha = in_ptr[aindex];
John Bowler7875d532011-11-07 22:33:49 -06001622 png_uint_32 reciprocal = 0;
1623 int c;
1624
1625 out_ptr[aindex] = alpha;
1626
1627 /* Calculate a reciprocal. The correct calculation is simply
Glenn Randers-Pehrsonefc4b692011-11-07 23:31:34 -06001628 * component/alpha*65535 << 15. (I.e. 15 bits of precision); this
John Bowler7875d532011-11-07 22:33:49 -06001629 * allows correct rounding by adding .5 before the shift. 'reciprocal'
1630 * is only initialized when required.
1631 */
1632 if (alpha > 0 && alpha < 65535)
1633 reciprocal = ((0xffff<<15)+(alpha>>1))/alpha;
1634
1635 c = channels;
1636 do /* always at least one channel */
1637 {
1638 png_uint_16 component = *in_ptr++;
1639
1640 /* The following gives 65535 for an alpha of 0, which is fine,
1641 * otherwise if 0/0 is represented as some other value there is more
1642 * likely to be a discontinuity which will probably damage
1643 * compression when moving from a fully transparent area to a
1644 * nearly transparent one. (The assumption here is that opaque
1645 * areas tend not to be 0 intensity.)
1646 */
1647 if (component >= alpha)
1648 component = 65535;
1649
1650 /* component<alpha, so component/alpha is less than one and
1651 * component*reciprocal is less than 2^31.
1652 */
1653 else if (component > 0 && alpha < 65535)
1654 {
1655 png_uint_32 calc = component * reciprocal;
1656 calc += 16384; /* round to nearest */
1657 component = (png_uint_16)(calc >> 15);
1658 }
1659
1660 *out_ptr++ = component;
1661 }
1662 while (--c > 0);
1663
1664 /* Skip to next component (skip the intervening alpha channel) */
1665 ++in_ptr;
1666 ++out_ptr;
1667 }
1668
John Bowler4fa96a42011-11-16 16:39:16 -06001669 png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row));
John Bowler7875d532011-11-07 22:33:49 -06001670 input_row += display->row_bytes/(sizeof (png_uint_16));
1671 }
1672
1673 return 1;
1674}
1675
1676/* Given 16-bit input (1 to 4 channels) write 8-bit output. If an alpha channel
1677 * is present it must be removed from the components, the components are then
1678 * written in sRGB encoding. No components are added or removed.
John Bowler5bc90382012-01-23 22:43:22 -06001679 *
1680 * Calculate an alpha reciprocal to reverse pre-multiplication. As above the
1681 * calculation can be done to 15 bits of accuracy; however, the output needs to
1682 * be scaled in the range 0..255*65535, so include that scaling here.
John Bowler7875d532011-11-07 22:33:49 -06001683 */
John Bowler5bc90382012-01-23 22:43:22 -06001684#define UNP_RECIPROCAL(alpha) ((((0xffff*0xff)<<7)+(alpha>>1))/alpha)
1685
1686static png_byte
1687png_unpremultiply(png_uint_32 component, png_uint_32 alpha,
1688 png_uint_32 reciprocal/*from the above macro*/)
1689{
1690 /* The following gives 1.0 for an alpha of 0, which is fine, otherwise if 0/0
1691 * is represented as some other value there is more likely to be a
1692 * discontinuity which will probably damage compression when moving from a
1693 * fully transparent area to a nearly transparent one. (The assumption here
1694 * is that opaque areas tend not to be 0 intensity.)
1695 *
1696 * There is a rounding problem here; if alpha is less than 128 it will end up
1697 * as 0 when scaled to 8 bits. To avoid introducing spurious colors into the
1698 * output change for this too.
1699 */
1700 if (component >= alpha || alpha < 128)
1701 return 255;
1702
1703 /* component<alpha, so component/alpha is less than one and
1704 * component*reciprocal is less than 2^31.
1705 */
1706 else if (component > 0)
1707 {
1708 /* The test is that alpha/257 (rounded) is less than 255, the first value
1709 * that becomes 255 is 65407.
1710 * NOTE: this must agree with the PNG_DIV257 macro (which must, therefore,
1711 * be exact!) [Could also test reciprocal != 0]
1712 */
1713 if (alpha < 65407)
1714 {
1715 component *= reciprocal;
1716 component += 64; /* round to nearest */
1717 component >>= 7;
1718 }
1719
1720 else
1721 component *= 255;
1722
1723 /* Convert the component to sRGB. */
1724 return (png_byte)PNG_sRGB_FROM_LINEAR(component);
1725 }
1726
1727 else
1728 return 0;
1729}
1730
John Bowler7875d532011-11-07 22:33:49 -06001731static int
1732png_write_image_8bit(png_voidp argument)
1733{
John Bowler4fa96a42011-11-16 16:39:16 -06001734 png_image_write_control *display = png_voidcast(png_image_write_control*,
1735 argument);
John Bowler7875d532011-11-07 22:33:49 -06001736 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06001737 png_structrp png_ptr = image->opaque->png_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001738
John Bowler4fa96a42011-11-16 16:39:16 -06001739 png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
1740 display->first_row);
1741 png_bytep output_row = png_voidcast(png_bytep, display->local_row);
John Bowler7875d532011-11-07 22:33:49 -06001742 png_uint_32 y = image->height;
John Bowler1c25b9b2012-02-29 10:49:28 -06001743 const int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
John Bowler7875d532011-11-07 22:33:49 -06001744
1745 if (image->format & PNG_FORMAT_FLAG_ALPHA)
1746 {
1747 png_bytep row_end;
1748 int aindex;
1749
1750 if (image->format & PNG_FORMAT_FLAG_AFIRST)
1751 {
1752 aindex = -1;
1753 ++input_row; /* To point to the first component */
1754 ++output_row;
1755 }
1756
1757 else
1758 aindex = channels;
1759
1760 /* Use row_end in place of a loop counter: */
1761 row_end = output_row + image->width * (channels+1);
1762
1763 while (y-- > 0)
1764 {
1765 png_const_uint_16p in_ptr = input_row;
1766 png_bytep out_ptr = output_row;
1767
John Bowler1c25b9b2012-02-29 10:49:28 -06001768 while (out_ptr < row_end)
John Bowler7875d532011-11-07 22:33:49 -06001769 {
1770 png_uint_16 alpha = in_ptr[aindex];
John Bowler5bc90382012-01-23 22:43:22 -06001771 png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
John Bowler7875d532011-11-07 22:33:49 -06001772 png_uint_32 reciprocal = 0;
1773 int c;
1774
John Bowler5bc90382012-01-23 22:43:22 -06001775 /* Scale and write the alpha channel. */
1776 out_ptr[aindex] = alphabyte;
John Bowler7875d532011-11-07 22:33:49 -06001777
John Bowler5bc90382012-01-23 22:43:22 -06001778 if (alphabyte > 0 && alphabyte < 255)
1779 reciprocal = UNP_RECIPROCAL(alpha);
John Bowler7875d532011-11-07 22:33:49 -06001780
1781 c = channels;
1782 do /* always at least one channel */
John Bowler5bc90382012-01-23 22:43:22 -06001783 *out_ptr++ = png_unpremultiply(*in_ptr++, alpha, reciprocal);
John Bowler7875d532011-11-07 22:33:49 -06001784 while (--c > 0);
1785
1786 /* Skip to next component (skip the intervening alpha channel) */
1787 ++in_ptr;
1788 ++out_ptr;
1789 } /* while out_ptr < row_end */
John Bowler3615d032011-11-08 10:38:09 -06001790
John Bowler4fa96a42011-11-16 16:39:16 -06001791 png_write_row(png_ptr, png_voidcast(png_const_bytep,
1792 display->local_row));
John Bowler3615d032011-11-08 10:38:09 -06001793 input_row += display->row_bytes/(sizeof (png_uint_16));
John Bowler7875d532011-11-07 22:33:49 -06001794 } /* while y */
1795 }
1796
1797 else
1798 {
1799 /* No alpha channel, so the row_end really is the end of the row and it
1800 * is sufficient to loop over the components one by one.
1801 */
1802 png_bytep row_end = output_row + image->width * channels;
1803
1804 while (y-- > 0)
1805 {
1806 png_const_uint_16p in_ptr = input_row;
1807 png_bytep out_ptr = output_row;
1808
1809 while (out_ptr < row_end)
1810 {
1811 png_uint_32 component = *in_ptr++;
1812
1813 component *= 255;
1814 *out_ptr++ = (png_byte)PNG_sRGB_FROM_LINEAR(component);
1815 }
1816
1817 png_write_row(png_ptr, output_row);
1818 input_row += display->row_bytes/(sizeof (png_uint_16));
1819 }
1820 }
1821
1822 return 1;
1823}
1824
John Bowler5bc90382012-01-23 22:43:22 -06001825static void
1826png_image_set_PLTE(png_image_write_control *display)
1827{
1828 const png_imagep image = display->image;
1829 const void *cmap = display->colormap;
1830 const int entries = image->colormap_entries > 256 ? 256 :
1831 (int)image->colormap_entries;
1832
1833 /* NOTE: the caller must check for cmap != NULL and entries != 0 */
1834 const png_uint_32 format = image->format;
1835 const int channels = PNG_IMAGE_SAMPLE_CHANNELS(format);
1836
1837# ifdef PNG_FORMAT_BGR_SUPPORTED
1838 const int afirst = (format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
1839 (format & PNG_FORMAT_FLAG_ALPHA) != 0;
1840# else
1841# define afirst 0
1842# endif
1843
1844# ifdef PNG_FORMAT_BGR_SUPPORTED
1845 const int bgr = (format & PNG_FORMAT_FLAG_BGR) ? 2 : 0;
1846# else
1847# define bgr 0
1848# endif
1849
1850 int i, num_trans;
1851 png_color palette[256];
1852 png_byte tRNS[256];
1853
1854 memset(tRNS, 255, sizeof tRNS);
1855 memset(palette, 0, sizeof palette);
1856
1857 for (i=num_trans=0; i<entries; ++i)
1858 {
1859 /* This gets automatically converted to sRGB with reversal of the
1860 * pre-multiplication if the color-map has an alpha channel.
1861 */
1862 if (format & PNG_FORMAT_FLAG_LINEAR)
1863 {
1864 png_const_uint_16p entry = png_voidcast(png_const_uint_16p, cmap);
John Bowler6f237b62012-03-02 13:13:15 -06001865
John Bowler5bc90382012-01-23 22:43:22 -06001866 entry += i * channels;
1867
1868 if (channels & 1) /* no alpha */
1869 {
1870 if (channels >= 3) /* RGB */
1871 {
1872 palette[i].blue = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
1873 entry[(2 ^ bgr)]);
1874 palette[i].green = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
1875 entry[1]);
1876 palette[i].red = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
1877 entry[bgr]);
1878 }
1879
1880 else /* Gray */
1881 palette[i].blue = palette[i].red = palette[i].green =
1882 (png_byte)PNG_sRGB_FROM_LINEAR(255 * *entry);
1883 }
1884
1885 else /* alpha */
1886 {
1887 png_uint_16 alpha = entry[afirst ? 0 : channels-1];
1888 png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
1889 png_uint_32 reciprocal = 0;
1890
1891 /* Calculate a reciprocal, as in the png_write_image_8bit code above
1892 * this is designed to produce a value scaled to 255*65535 when
1893 * divided by 128 (i.e. asr 7).
1894 */
1895 if (alphabyte > 0 && alphabyte < 255)
1896 reciprocal = (((0xffff*0xff)<<7)+(alpha>>1))/alpha;
1897
1898 tRNS[i] = alphabyte;
1899 if (alphabyte < 255)
1900 num_trans = i+1;
1901
1902 if (channels >= 3) /* RGB */
1903 {
1904 palette[i].blue = png_unpremultiply(entry[afirst + (2 ^ bgr)],
1905 alpha, reciprocal);
1906 palette[i].green = png_unpremultiply(entry[afirst + 1], alpha,
1907 reciprocal);
1908 palette[i].red = png_unpremultiply(entry[afirst + bgr], alpha,
1909 reciprocal);
1910 }
1911
1912 else /* gray */
1913 palette[i].blue = palette[i].red = palette[i].green =
1914 png_unpremultiply(entry[afirst], alpha, reciprocal);
1915 }
1916 }
1917
1918 else /* Color-map has sRGB values */
1919 {
1920 png_const_bytep entry = png_voidcast(png_const_bytep, cmap);
John Bowler6f237b62012-03-02 13:13:15 -06001921
John Bowler5bc90382012-01-23 22:43:22 -06001922 entry += i * channels;
1923
1924 switch (channels)
1925 {
1926 case 4:
1927 tRNS[i] = entry[afirst ? 0 : 3];
1928 if (tRNS[i] < 255)
1929 num_trans = i+1;
1930 /* FALL THROUGH */
1931 case 3:
1932 palette[i].blue = entry[afirst + (2 ^ bgr)];
1933 palette[i].green = entry[afirst + 1];
1934 palette[i].red = entry[afirst + bgr];
1935 break;
1936
1937 case 2:
1938 tRNS[i] = entry[1 ^ afirst];
1939 if (tRNS[i] < 255)
1940 num_trans = i+1;
1941 /* FALL THROUGH */
1942 case 1:
1943 palette[i].blue = palette[i].red = palette[i].green =
1944 entry[afirst];
1945 break;
1946
1947 default:
1948 break;
1949 }
1950 }
1951 }
1952
1953# ifdef afirst
1954# undef afirst
1955# endif
1956# ifdef bgr
1957# undef bgr
1958# endif
1959
1960 png_set_PLTE(image->opaque->png_ptr, image->opaque->info_ptr, palette,
1961 entries);
1962
1963 if (num_trans > 0)
1964 png_set_tRNS(image->opaque->png_ptr, image->opaque->info_ptr, tRNS,
1965 num_trans, NULL);
1966
1967 image->colormap_entries = entries;
1968}
1969
John Bowler7875d532011-11-07 22:33:49 -06001970static int
1971png_image_write_main(png_voidp argument)
1972{
John Bowler4fa96a42011-11-16 16:39:16 -06001973 png_image_write_control *display = png_voidcast(png_image_write_control*,
1974 argument);
John Bowler7875d532011-11-07 22:33:49 -06001975 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06001976 png_structrp png_ptr = image->opaque->png_ptr;
1977 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001978 png_uint_32 format = image->format;
1979
John Bowler5bc90382012-01-23 22:43:22 -06001980 int colormap = (format & PNG_FORMAT_FLAG_COLORMAP) != 0;
1981 int linear = !colormap && (format & PNG_FORMAT_FLAG_LINEAR) != 0; /* input */
1982 int alpha = !colormap && (format & PNG_FORMAT_FLAG_ALPHA) != 0;
1983 int write_16bit = linear && !colormap && !display->convert_to_8bit;
John Bowler7875d532011-11-07 22:33:49 -06001984
John Bowler3706d732011-11-21 10:28:06 -06001985 /* Default the 'row_stride' parameter if required. */
1986 if (display->row_stride == 0)
1987 display->row_stride = PNG_IMAGE_ROW_STRIDE(*image);
1988
John Bowler7875d532011-11-07 22:33:49 -06001989 /* Set the required transforms then write the rows in the correct order. */
John Bowler04336ba2012-01-16 07:48:36 -06001990 if (format & PNG_FORMAT_FLAG_COLORMAP)
John Bowler5bc90382012-01-23 22:43:22 -06001991 {
1992 if (display->colormap != NULL && image->colormap_entries > 0)
1993 {
1994 png_uint_32 entries = image->colormap_entries;
1995
1996 png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
1997 entries > 16 ? 8 : (entries > 4 ? 4 : (entries > 2 ? 2 : 1)),
1998 PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
1999 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
2000
2001 png_image_set_PLTE(display);
2002 }
2003
2004 else
2005 png_error(image->opaque->png_ptr,
2006 "no color-map for color-mapped image");
2007 }
John Bowler04336ba2012-01-16 07:48:36 -06002008
2009 else
2010 png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
2011 write_16bit ? 16 : 8,
2012 ((format & PNG_FORMAT_FLAG_COLOR) ? PNG_COLOR_MASK_COLOR : 0) +
2013 ((format & PNG_FORMAT_FLAG_ALPHA) ? PNG_COLOR_MASK_ALPHA : 0),
2014 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
John Bowler7875d532011-11-07 22:33:49 -06002015
2016 /* Counter-intuitively the data transformations must be called *after*
2017 * png_write_info, not before as in the read code, but the 'set' functions
2018 * must still be called before. Just set the color space information, never
2019 * write an interlaced image.
2020 */
John Bowler5bc90382012-01-23 22:43:22 -06002021
John Bowler7875d532011-11-07 22:33:49 -06002022 if (write_16bit)
2023 {
2024 /* The gamma here is 1.0 (linear) and the cHRM chunk matches sRGB. */
2025 png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_LINEAR);
John Bowlerdd819152011-11-08 14:29:45 -06002026
2027 if (!(image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB))
2028 png_set_cHRM_fixed(png_ptr, info_ptr,
2029 /* color x y */
2030 /* white */ 31270, 32900,
2031 /* red */ 64000, 33000,
2032 /* green */ 30000, 60000,
2033 /* blue */ 15000, 6000
2034 );
John Bowler7875d532011-11-07 22:33:49 -06002035 }
2036
John Bowlerdd819152011-11-08 14:29:45 -06002037 else if (!(image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB))
John Bowler7875d532011-11-07 22:33:49 -06002038 png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL);
2039
John Bowlerdd819152011-11-08 14:29:45 -06002040 /* Else writing an 8-bit file and the *colors* aren't sRGB, but the 8-bit
2041 * space must still be gamma encoded.
2042 */
2043 else
2044 png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE);
2045
John Bowler7875d532011-11-07 22:33:49 -06002046 /* Write the file header. */
2047 png_write_info(png_ptr, info_ptr);
2048
2049 /* Now set up the data transformations (*after* the header is written),
2050 * remove the handled transformations from the 'format' flags for checking.
John Bowlere6fb6912011-11-08 21:35:16 -06002051 *
2052 * First check for a little endian system if writing 16 bit files.
John Bowler7875d532011-11-07 22:33:49 -06002053 */
John Bowler7875d532011-11-07 22:33:49 -06002054 if (write_16bit)
2055 {
2056 PNG_CONST png_uint_16 le = 0x0001;
2057
2058 if (*(png_const_bytep)&le)
2059 png_set_swap(png_ptr);
2060 }
2061
2062# ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
2063 if (format & PNG_FORMAT_FLAG_BGR)
2064 {
John Bowler5bc90382012-01-23 22:43:22 -06002065 if (!colormap && (format & PNG_FORMAT_FLAG_COLOR) != 0)
John Bowlere6fb6912011-11-08 21:35:16 -06002066 png_set_bgr(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06002067 format &= ~PNG_FORMAT_FLAG_BGR;
2068 }
2069# endif
2070
2071# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
2072 if (format & PNG_FORMAT_FLAG_AFIRST)
2073 {
John Bowler5bc90382012-01-23 22:43:22 -06002074 if (!colormap && (format & PNG_FORMAT_FLAG_ALPHA) != 0)
John Bowlere6fb6912011-11-08 21:35:16 -06002075 png_set_swap_alpha(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06002076 format &= ~PNG_FORMAT_FLAG_AFIRST;
2077 }
2078# endif
2079
John Bowler5bc90382012-01-23 22:43:22 -06002080 /* If there are 16 or fewer color-map entries we wrote a lower bit depth
2081 * above, but the application data is still byte packed.
2082 */
2083 if (colormap && image->colormap_entries <= 16)
2084 png_set_packing(png_ptr);
2085
John Bowlere6fb6912011-11-08 21:35:16 -06002086 /* That should have handled all (both) the transforms. */
John Bowler89c2f842011-11-16 12:04:39 -06002087 if ((format & ~(png_uint_32)(PNG_FORMAT_FLAG_COLOR | PNG_FORMAT_FLAG_LINEAR |
John Bowler5bc90382012-01-23 22:43:22 -06002088 PNG_FORMAT_FLAG_ALPHA | PNG_FORMAT_FLAG_COLORMAP)) != 0)
John Bowler7875d532011-11-07 22:33:49 -06002089 png_error(png_ptr, "png_write_image: unsupported transformation");
2090
2091 {
John Bowler4fa96a42011-11-16 16:39:16 -06002092 png_const_bytep row = png_voidcast(png_const_bytep, display->buffer);
John Bowler7875d532011-11-07 22:33:49 -06002093 ptrdiff_t row_bytes = display->row_stride;
2094
2095 if (linear)
2096 row_bytes *= sizeof (png_uint_16);
2097
2098 if (row_bytes < 0)
2099 row += (image->height-1) * (-row_bytes);
2100
2101 display->first_row = row;
2102 display->row_bytes = row_bytes;
2103 }
2104
John Bowlerdee75772012-03-01 18:55:54 -06002105 /* Apply 'fast' options if the flag is set. */
2106 if ((image->flags & PNG_IMAGE_FLAG_FAST) != 0)
2107 {
2108 png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_NO_FILTERS);
2109 /* NOTE: determined by experiment using pngstest, this reflects some
2110 * balance between the time to write the image once and the time to read
2111 * it about 50 times. The speed-up in pngstest was about 10-20% of the
2112 * total (user) time on a heavily loaded system.
2113 */
2114 png_set_compression_level(png_ptr, 3);
2115 }
2116
John Bowler7875d532011-11-07 22:33:49 -06002117 /* Check for the cases that currently require a pre-transform on the row
2118 * before it is written. This only applies when the input is 16-bit and
2119 * either there is an alpha channel or it is converted to 8-bit.
2120 */
John Bowler5bc90382012-01-23 22:43:22 -06002121 if ((linear && alpha) || (!colormap && display->convert_to_8bit))
John Bowler7875d532011-11-07 22:33:49 -06002122 {
John Bowler4fa96a42011-11-16 16:39:16 -06002123 png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
2124 png_get_rowbytes(png_ptr, info_ptr)));
John Bowler7875d532011-11-07 22:33:49 -06002125 int result;
2126
2127 display->local_row = row;
2128 if (write_16bit)
2129 result = png_safe_execute(image, png_write_image_16bit, display);
2130 else
2131 result = png_safe_execute(image, png_write_image_8bit, display);
2132 display->local_row = NULL;
2133
2134 png_free(png_ptr, row);
2135
2136 /* Skip the 'write_end' on error: */
2137 if (!result)
2138 return 0;
2139 }
2140
2141 /* Otherwise this is the case where the input is in a format currently
2142 * supported by the rest of the libpng write code; call it directly.
2143 */
2144 else
2145 {
John Bowler4fa96a42011-11-16 16:39:16 -06002146 png_const_bytep row = png_voidcast(png_const_bytep, display->first_row);
John Bowler7875d532011-11-07 22:33:49 -06002147 ptrdiff_t row_bytes = display->row_bytes;
2148 png_uint_32 y = image->height;
2149
2150 while (y-- > 0)
2151 {
2152 png_write_row(png_ptr, row);
2153 row += row_bytes;
2154 }
2155 }
2156
2157 png_write_end(png_ptr, info_ptr);
2158 return 1;
2159}
2160
2161int PNGAPI
John Bowlerdd819152011-11-08 14:29:45 -06002162png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit,
John Bowler5bc90382012-01-23 22:43:22 -06002163 const void *buffer, png_int_32 row_stride, const void *colormap)
John Bowler7875d532011-11-07 22:33:49 -06002164{
2165 /* Write the image to the given (FILE*). */
John Bowler5bc90382012-01-23 22:43:22 -06002166 if (image != NULL || image->version != PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06002167 {
2168 if (file != NULL)
2169 {
2170 if (png_image_write_init(image))
2171 {
2172 png_image_write_control display;
2173 int result;
2174
2175 /* This is slightly evil, but png_init_io doesn't do anything other
2176 * than this and we haven't changed the standard IO functions so
2177 * this saves a 'safe' function.
2178 */
2179 image->opaque->png_ptr->io_ptr = file;
2180
John Bowlerbaeb6d12011-11-26 18:21:02 -06002181 png_memset(&display, 0, sizeof display);
John Bowler7875d532011-11-07 22:33:49 -06002182 display.image = image;
2183 display.buffer = buffer;
2184 display.row_stride = row_stride;
John Bowler5bc90382012-01-23 22:43:22 -06002185 display.colormap = colormap;
John Bowler7875d532011-11-07 22:33:49 -06002186 display.convert_to_8bit = convert_to_8bit;
2187
2188 result = png_safe_execute(image, png_image_write_main, &display);
2189 png_image_free(image);
2190 return result;
2191 }
2192
2193 else
2194 return 0;
2195 }
2196
2197 else
2198 return png_image_error(image,
2199 "png_image_write_to_stdio: invalid argument");
2200 }
2201
2202 else
2203 return 0;
2204}
2205
2206int PNGAPI
John Bowlerdd819152011-11-08 14:29:45 -06002207png_image_write_to_file(png_imagep image, const char *file_name,
John Bowler5bc90382012-01-23 22:43:22 -06002208 int convert_to_8bit, const void *buffer, png_int_32 row_stride,
2209 const void *colormap)
John Bowler7875d532011-11-07 22:33:49 -06002210{
2211 /* Write the image to the named file. */
John Bowler5bc90382012-01-23 22:43:22 -06002212 if (image != NULL || image->version != PNG_IMAGE_VERSION)
John Bowler7875d532011-11-07 22:33:49 -06002213 {
2214 if (file_name != NULL)
2215 {
2216 FILE *fp = fopen(file_name, "wb");
2217
2218 if (fp != NULL)
2219 {
John Bowlerdd819152011-11-08 14:29:45 -06002220 if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer,
John Bowler5bc90382012-01-23 22:43:22 -06002221 row_stride, colormap))
John Bowler7875d532011-11-07 22:33:49 -06002222 {
John Bowlerdd819152011-11-08 14:29:45 -06002223 int error; /* from fflush/fclose */
John Bowler7875d532011-11-07 22:33:49 -06002224
John Bowlerdd819152011-11-08 14:29:45 -06002225 /* Make sure the file is flushed correctly. */
2226 if (fflush(fp) == 0 && ferror(fp) == 0)
John Bowler7875d532011-11-07 22:33:49 -06002227 {
John Bowlerdd819152011-11-08 14:29:45 -06002228 if (fclose(fp) == 0)
2229 return 1;
John Bowler7875d532011-11-07 22:33:49 -06002230
John Bowlerdd819152011-11-08 14:29:45 -06002231 error = errno; /* from fclose */
John Bowler7875d532011-11-07 22:33:49 -06002232 }
2233
John Bowlerdd819152011-11-08 14:29:45 -06002234 else
2235 {
2236 error = errno; /* from fflush or ferror */
2237 (void)fclose(fp);
2238 }
2239
2240 (void)remove(file_name);
2241 /* The image has already been cleaned up; this is just used to
2242 * set the error (because the original write succeeded).
2243 */
2244 return png_image_error(image, strerror(error));
John Bowler7875d532011-11-07 22:33:49 -06002245 }
2246
2247 else
2248 {
2249 /* Clean up: just the opened file. */
2250 (void)fclose(fp);
John Bowlerdd819152011-11-08 14:29:45 -06002251 (void)remove(file_name);
John Bowler7875d532011-11-07 22:33:49 -06002252 return 0;
2253 }
2254 }
2255
2256 else
2257 return png_image_error(image, strerror(errno));
2258 }
2259
2260 else
2261 return png_image_error(image,
2262 "png_image_write_to_file: invalid argument");
2263 }
2264
2265 else
2266 return 0;
2267}
2268#endif /* PNG_STDIO_SUPPORTED */
2269#endif /* SIMPLIFIED_WRITE */
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -06002270#endif /* PNG_WRITE_SUPPORTED */