blob: 9fa5ae0ba8ffff581f4d1ad7c8f706af28d8a729 [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,
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050076 (png_charp)info_ptr->iccp_profile, (int)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-Pehrson3dfe9372009-08-22 08:44:23 -0500311 /* See if user wants us to write information chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500312 if (info_ptr != NULL)
313 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500314#ifdef PNG_WRITE_TEXT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500315 int i; /* local index variable */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600316#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500317#ifdef PNG_WRITE_tIME_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500318 /* Check to see if user has supplied a time chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600319 if ((info_ptr->valid & PNG_INFO_tIME) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600320 !(png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500321 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500322
Andreas Dilger47a0c421997-05-16 02:46:07 -0500323#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500324#ifdef PNG_WRITE_TEXT_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500325 /* Loop through comment chunks */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600326 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500327 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500328 png_debug2(2, "Writing trailer text chunk %d, type %d", i,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500329 info_ptr->text[i].compression);
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500330 /* An internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600331 if (info_ptr->text[i].compression > 0)
332 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500333#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500334 /* Write international chunk */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500335 png_write_iTXt(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600336 info_ptr->text[i].compression,
337 info_ptr->text[i].key,
338 info_ptr->text[i].lang,
339 info_ptr->text[i].lang_key,
340 info_ptr->text[i].text);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600341#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500342 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600343#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500344 /* Mark this chunk as written */
345 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600346 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500347
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600348 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
Guy Schalnat0d580581995-07-20 02:43:20 -0500349 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500350#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500351 /* Write compressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600352 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600353 info_ptr->text[i].text, 0,
354 info_ptr->text[i].compression);
Guy Schalnate5a37791996-06-05 15:50:50 -0500355#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600356 png_warning(png_ptr, "Unable to write compressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500357#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500358 /* Mark this chunk as written */
359 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500360 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500361
Andreas Dilger47a0c421997-05-16 02:46:07 -0500362 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500363 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500364#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500365 /* Write uncompressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600366 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600367 info_ptr->text[i].text, 0);
Guy Schalnate5a37791996-06-05 15:50:50 -0500368#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600369 png_warning(png_ptr, "Unable to write uncompressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500370#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500371
Andreas Dilger47a0c421997-05-16 02:46:07 -0500372 /* Mark this chunk as written */
373 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500374 }
375 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600376#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500377#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600378 if (info_ptr->unknown_chunks_num)
379 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500380 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600381
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500382 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600383
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500384 for (up = info_ptr->unknown_chunks;
385 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
386 up++)
387 {
Glenn Randers-Pehrson614b91d2009-10-17 19:00:18 -0500388 int keep = png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500389 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson36fa2a02011-05-11 06:52:37 -0500390 up->location &&
391 (up->location & PNG_AFTER_IDAT) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600392 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
393 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600394 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600395 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600396 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500397 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600398 }
399#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500400 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500401
402 png_ptr->mode |= PNG_AFTER_IDAT;
403
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500404 /* Write end of PNG file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500405 png_write_IEND(png_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500406 /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
407 * and restored again in libpng-1.2.30, may cause some applications that
408 * do not set png_ptr->output_flush_fn to crash. If your application
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600409 * experiences a problem, please try building libpng with
410 * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500411 * png-mng-implement at lists.sf.net .
412 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500413#ifdef PNG_WRITE_FLUSH_SUPPORTED
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600414# ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
Glenn Randers-Pehrson32fc5ce2000-07-24 06:34:14 -0500415 png_flush(png_ptr);
Glenn Randers-Pehrsonedcd6e12009-11-20 20:28:29 -0600416# endif
Glenn Randers-Pehrsondbed41f2008-08-19 18:20:52 -0500417#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500418}
419
Glenn Randers-Pehrson65d235a2009-11-02 11:32:30 -0600420#ifdef PNG_CONVERT_tIME_SUPPORTED
Glenn Randers-Pehrson99106de2009-11-01 16:26:14 -0600421/* "tm" structure is not supported on WindowsCE */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500422void PNGAPI
John Bowlerbaeb6d12011-11-26 18:21:02 -0600423png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm * ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500424{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500425 png_debug(1, "in png_convert_from_struct_tm");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500426
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600427 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
428 ptime->month = (png_byte)(ttime->tm_mon + 1);
429 ptime->day = (png_byte)ttime->tm_mday;
430 ptime->hour = (png_byte)ttime->tm_hour;
431 ptime->minute = (png_byte)ttime->tm_min;
432 ptime->second = (png_byte)ttime->tm_sec;
Guy Schalnat0d580581995-07-20 02:43:20 -0500433}
434
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500435void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600436png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500437{
438 struct tm *tbuf;
439
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500440 png_debug(1, "in png_convert_from_time_t");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500441
Guy Schalnat0d580581995-07-20 02:43:20 -0500442 tbuf = gmtime(&ttime);
443 png_convert_from_struct_tm(ptime, tbuf);
444}
Guy Schalnat6d764711995-12-19 03:22:19 -0600445#endif
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500446
Andreas Dilger47a0c421997-05-16 02:46:07 -0500447/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500448PNG_FUNCTION(png_structp,PNGAPI
449png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
450 png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500451{
John Bowlerd332c672011-12-21 17:36:12 -0600452#ifndef PNG_USER_MEM_SUPPORTED
John Bowler5d567862011-12-24 09:12:00 -0600453 png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
John Bowlerd332c672011-12-21 17:36:12 -0600454 error_fn, warn_fn, NULL, NULL, NULL);
455#else
456 return png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
457 warn_fn, NULL, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500458}
459
460/* Alternate initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500461PNG_FUNCTION(png_structp,PNGAPI
462png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600463 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500464 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500465{
John Bowler5d567862011-12-24 09:12:00 -0600466 png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
John Bowlerd332c672011-12-21 17:36:12 -0600467 error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500468#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500469
John Bowlerd332c672011-12-21 17:36:12 -0600470 if (png_ptr != NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500471 {
John Bowlerd332c672011-12-21 17:36:12 -0600472 /* TODO: delay this, it can be done in png_init_io() (if the app doesn't
473 * do it itself) avoiding setting the default function if it is not
474 * required.
475 */
476 png_set_write_fn(png_ptr, NULL, NULL, NULL);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500477 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500478
John Bowlerd332c672011-12-21 17:36:12 -0600479 return png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500480}
481
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500482
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600483/* Write a few rows of image data. If the image is interlaced,
484 * either you will have to write the 7 sub images, or, if you
485 * have called png_set_interlace_handling(), you will have to
486 * "write" the image seven times.
487 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500488void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600489png_write_rows(png_structrp png_ptr, png_bytepp row,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600490 png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500491{
492 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600493 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500494
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500495 png_debug(1, "in png_write_rows");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600496
497 if (png_ptr == NULL)
498 return;
499
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500500 /* Loop through the rows */
Guy Schalnat0d580581995-07-20 02:43:20 -0500501 for (i = 0, rp = row; i < num_rows; i++, rp++)
502 {
503 png_write_row(png_ptr, *rp);
504 }
505}
506
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600507/* Write the image. You only need to call this function once, even
508 * if you are writing an interlaced image.
509 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500510void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600511png_write_image(png_structrp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500512{
513 png_uint_32 i; /* row index */
514 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600515 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500516
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600517 if (png_ptr == NULL)
518 return;
519
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500520 png_debug(1, "in png_write_image");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500521
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500522#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500523 /* Initialize interlace handling. If image is not interlaced,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500524 * this will set pass to 1
525 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500526 num_pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600527#else
528 num_pass = 1;
529#endif
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500530 /* Loop through passes */
Guy Schalnat0d580581995-07-20 02:43:20 -0500531 for (pass = 0; pass < num_pass; pass++)
532 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500533 /* Loop through image */
Guy Schalnat0d580581995-07-20 02:43:20 -0500534 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
535 {
536 png_write_row(png_ptr, *rp);
537 }
538 }
539}
540
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500541/* Called by user to write a row of image data */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500542void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600543png_write_row(png_structrp png_ptr, png_const_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500544{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500545 /* 1.5.6: moved from png_struct to be a local structure: */
546 png_row_info row_info;
547
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600548 if (png_ptr == NULL)
549 return;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500550
Glenn Randers-Pehrson632a84e2010-03-09 22:28:33 -0600551 png_debug2(1, "in png_write_row (row %u, pass %d)",
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600552 png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600553
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500554 /* Initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600555 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500556 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500557 /* Make sure we wrote the header info */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500558 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
559 png_error(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600560 "png_write_info was never called before png_write_row");
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600561
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500562 /* Check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500563#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500564 if (png_ptr->transformations & PNG_INVERT_MONO)
565 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500566#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500567
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500568#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500569 if (png_ptr->transformations & PNG_FILLER)
570 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500571#endif
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600572#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
573 defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500574 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600575 png_warning(png_ptr,
576 "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500577#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500578
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500579#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500580 if (png_ptr->transformations & PNG_PACK)
581 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500582#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500583
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500584#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500585 if (png_ptr->transformations & PNG_SHIFT)
586 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500587#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500588
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500589#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500590 if (png_ptr->transformations & PNG_BGR)
591 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500592#endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500593
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500594#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500595 if (png_ptr->transformations & PNG_SWAP_BYTES)
596 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500597#endif
598
Guy Schalnat0d580581995-07-20 02:43:20 -0500599 png_write_start_row(png_ptr);
600 }
601
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500602#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500603 /* If interlaced and not interested in row, return */
Guy Schalnat0d580581995-07-20 02:43:20 -0500604 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
605 {
606 switch (png_ptr->pass)
607 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600608 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600609 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500610 {
611 png_write_finish_row(png_ptr);
612 return;
613 }
614 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500615
Guy Schalnat0d580581995-07-20 02:43:20 -0500616 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600617 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500618 {
619 png_write_finish_row(png_ptr);
620 return;
621 }
622 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500623
Guy Schalnat0d580581995-07-20 02:43:20 -0500624 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600625 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500626 {
627 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600628 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500629 }
630 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500631
Guy Schalnat0d580581995-07-20 02:43:20 -0500632 case 3:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600633 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
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 4:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600641 if ((png_ptr->row_number & 0x03) != 2)
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 5:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600649 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500650 {
651 png_write_finish_row(png_ptr);
652 return;
653 }
654 break;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500655
Guy Schalnat0d580581995-07-20 02:43:20 -0500656 case 6:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600657 if (!(png_ptr->row_number & 0x01))
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
664 default: /* error: ignore it */
665 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500666 }
667 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600668#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500669
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500670 /* Set up row info for transformations */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500671 row_info.color_type = png_ptr->color_type;
672 row_info.width = png_ptr->usr_width;
673 row_info.channels = png_ptr->usr_channels;
674 row_info.bit_depth = png_ptr->usr_bit_depth;
675 row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels);
676 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600677
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500678 png_debug1(3, "row_info->color_type = %d", row_info.color_type);
679 png_debug1(3, "row_info->width = %u", row_info.width);
680 png_debug1(3, "row_info->channels = %d", row_info.channels);
681 png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth);
682 png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth);
683 png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500684
685 /* Copy user's row into buffer, leaving room for filter byte. */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500686 png_memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500687
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500688#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500689 /* Handle interlacing */
Guy Schalnat0d580581995-07-20 02:43:20 -0500690 if (png_ptr->interlaced && png_ptr->pass < 6 &&
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500691 (png_ptr->transformations & PNG_INTERLACE))
Guy Schalnat0d580581995-07-20 02:43:20 -0500692 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500693 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 -0500694 /* This should always get caught above, but still ... */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500695 if (!(row_info.width))
Guy Schalnat0d580581995-07-20 02:43:20 -0500696 {
697 png_write_finish_row(png_ptr);
698 return;
699 }
700 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600701#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500702
John Bowler4a12f4a2011-04-17 18:34:22 -0500703#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500704 /* Handle other transformations */
Guy Schalnat0d580581995-07-20 02:43:20 -0500705 if (png_ptr->transformations)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500706 png_do_write_transformations(png_ptr, &row_info);
John Bowler4a12f4a2011-04-17 18:34:22 -0500707#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500708
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500709 /* At this point the row_info pixel depth must match the 'transformed' depth,
710 * which is also the output depth.
711 */
712 if (row_info.pixel_depth != png_ptr->pixel_depth ||
713 row_info.pixel_depth != png_ptr->transformed_pixel_depth)
714 png_error(png_ptr, "internal write transform logic error");
715
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500716#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600717 /* Write filter_method 64 (intrapixel differencing) only if
718 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
719 * 2. Libpng did not write a PNG signature (this filter_method is only
720 * used in PNG datastreams that are embedded in MNG datastreams) and
721 * 3. The application called png_permit_mng_features with a mask that
722 * included PNG_FLAG_MNG_FILTER_64 and
723 * 4. The filter_method is 64 and
724 * 5. The color_type is RGB or RGBA
725 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500726 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500727 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600728 {
729 /* Intrapixel differencing */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500730 png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600731 }
732#endif
733
Andreas Dilger47a0c421997-05-16 02:46:07 -0500734 /* Find a filter if necessary, filter the row and write it out. */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500735 png_write_find_filter(png_ptr, &row_info);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600736
737 if (png_ptr->write_row_fn != NULL)
738 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500739}
740
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500741#ifdef PNG_WRITE_FLUSH_SUPPORTED
Guy Schalnat0f716451995-11-28 11:22:13 -0600742/* Set the automatic flush interval or 0 to turn flushing off */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500743void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600744png_set_flush(png_structrp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600745{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500746 png_debug(1, "in png_set_flush");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500747
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600748 if (png_ptr == NULL)
749 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500750
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600751 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600752}
753
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500754/* Flush the current output buffers now */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500755void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600756png_write_flush(png_structrp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600757{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600758 int wrote_IDAT;
Guy Schalnat0f716451995-11-28 11:22:13 -0600759
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500760 png_debug(1, "in png_write_flush");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500761
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600762 if (png_ptr == NULL)
763 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500764
Guy Schalnate5a37791996-06-05 15:50:50 -0500765 /* We have already written out all of the data */
766 if (png_ptr->row_number >= png_ptr->num_rows)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500767 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600768
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600769 do
770 {
771 int ret;
Guy Schalnat0f716451995-11-28 11:22:13 -0600772
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500773 /* Compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600774 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600775 wrote_IDAT = 0;
Guy Schalnat0f716451995-11-28 11:22:13 -0600776
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500777 /* Check for compression errors */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600778 if (ret != Z_OK)
779 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500780 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600781 png_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500782
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600783 else
784 png_error(png_ptr, "zlib error");
785 }
Guy Schalnat0f716451995-11-28 11:22:13 -0600786
Andreas Dilger47a0c421997-05-16 02:46:07 -0500787 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600788 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500789 /* Write the IDAT and reset the zlib output buffer */
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600790 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600791 wrote_IDAT = 1;
792 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500793 } while (wrote_IDAT == 1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600794
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600795 /* If there is any data left to be output, write it into a new IDAT */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600796 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600797 {
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -0500798 /* Write the IDAT and reset the zlib output buffer */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600799 png_write_IDAT(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600800 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600801 }
802 png_ptr->flush_rows = 0;
803 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600804}
805#endif /* PNG_WRITE_FLUSH_SUPPORTED */
806
John Bowlerd332c672011-12-21 17:36:12 -0600807#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
John Bowler5d567862011-12-24 09:12:00 -0600808static void png_reset_filter_heuristics(png_structrp png_ptr); /* forward decl */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500809#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600810
John Bowlerd332c672011-12-21 17:36:12 -0600811/* Free any memory used in png_ptr struct without freeing the struct itself. */
812static void
John Bowler5d567862011-12-24 09:12:00 -0600813png_write_destroy(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500814{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500815 png_debug(1, "in png_write_destroy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500816
817 /* Free any memory zlib uses */
John Bowlerc5bef942011-05-05 17:35:39 -0500818 if (png_ptr->zlib_state != PNG_ZLIB_UNINITIALIZED)
819 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -0500820
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500821 /* Free our memory. png_free checks NULL for us. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600822 png_free(png_ptr, png_ptr->zbuf);
823 png_free(png_ptr, png_ptr->row_buf);
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500824#ifdef PNG_WRITE_FILTER_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600825 png_free(png_ptr, png_ptr->prev_row);
826 png_free(png_ptr, png_ptr->sub_row);
827 png_free(png_ptr, png_ptr->up_row);
828 png_free(png_ptr, png_ptr->avg_row);
829 png_free(png_ptr, png_ptr->paeth_row);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500830#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600831
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500832#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -0500833 /* Use this to save a little code space, it doesn't free the filter_costs */
834 png_reset_filter_heuristics(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500835 png_free(png_ptr, png_ptr->filter_costs);
836 png_free(png_ptr, png_ptr->inv_filter_costs);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600837#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500838
John Bowlerd332c672011-12-21 17:36:12 -0600839#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
840 png_free(png_ptr, png_ptr->chunk_list);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600841#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500842
John Bowlerd332c672011-12-21 17:36:12 -0600843 /* The error handling and memory handling information is left intact at this
844 * point: the jmp_buf may still have to be freed. See png_destroy_png_struct
845 * for how this happens.
846 */
847}
Guy Schalnate5a37791996-06-05 15:50:50 -0500848
John Bowlerd332c672011-12-21 17:36:12 -0600849/* Free all memory used by the write.
850 * In libpng 1.6.0 this API changed quietly to no longer accept a NULL value for
851 * *png_ptr_ptr. Prior to 1.6.0 it would accept such a value and it would free
852 * the passed in info_structs but it would quietly fail to free any of the data
853 * inside them. In 1.6.0 it quietly does nothing (it has to be quiet because it
854 * has no png_ptr.)
855 */
856void PNGAPI
857png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
858{
859 png_debug(1, "in png_destroy_write_struct");
Guy Schalnate5a37791996-06-05 15:50:50 -0500860
John Bowlerd332c672011-12-21 17:36:12 -0600861 if (png_ptr_ptr != NULL)
862 {
John Bowler5d567862011-12-24 09:12:00 -0600863 png_structrp png_ptr = *png_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500864
John Bowlerd332c672011-12-21 17:36:12 -0600865 if (png_ptr != NULL) /* added in libpng 1.6.0 */
866 {
867 png_destroy_info_struct(png_ptr, info_ptr_ptr);
868
869 *png_ptr_ptr = NULL;
870 png_write_destroy(png_ptr);
871 png_destroy_png_struct(png_ptr);
872 }
873 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500874}
Guy Schalnate5a37791996-06-05 15:50:50 -0500875
Andreas Dilger47a0c421997-05-16 02:46:07 -0500876/* Allow the application to select one or more row filters to use. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500877void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600878png_set_filter(png_structrp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500879{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500880 png_debug(1, "in png_set_filter");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500881
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600882 if (png_ptr == NULL)
883 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500884
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500885#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500886 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600887 (method == PNG_INTRAPIXEL_DIFFERENCING))
888 method = PNG_FILTER_TYPE_BASE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500889
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600890#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500891 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500892 {
893 switch (filters & (PNG_ALL_FILTERS | 0x07))
894 {
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500895#ifdef PNG_WRITE_FILTER_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500896 case 5:
897 case 6:
Andreas Dilger47a0c421997-05-16 02:46:07 -0500898 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500899#endif /* PNG_WRITE_FILTER_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500900 case PNG_FILTER_VALUE_NONE:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600901 png_ptr->do_filter = PNG_FILTER_NONE; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500902
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500903#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500904 case PNG_FILTER_VALUE_SUB:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600905 png_ptr->do_filter = PNG_FILTER_SUB; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500906
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500907 case PNG_FILTER_VALUE_UP:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600908 png_ptr->do_filter = PNG_FILTER_UP; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500909
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500910 case PNG_FILTER_VALUE_AVG:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600911 png_ptr->do_filter = PNG_FILTER_AVG; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500912
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500913 case PNG_FILTER_VALUE_PAETH:
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600914 png_ptr->do_filter = PNG_FILTER_PAETH; break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500915
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600916 default:
917 png_ptr->do_filter = (png_byte)filters; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500918#else
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600919 default:
920 png_warning(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500921#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500922 }
923
Andreas Dilger47a0c421997-05-16 02:46:07 -0500924 /* If we have allocated the row_buf, this means we have already started
925 * with the image and we should have allocated all of the filter buffers
926 * that have been selected. If prev_row isn't already allocated, then
927 * it is too late to start using the filters that need it, since we
928 * will be missing the data in the previous row. If an application
929 * wants to start and stop using particular filters during compression,
930 * it should start out with all of the filters, and then add and
931 * remove them after the start of compression.
Guy Schalnate5a37791996-06-05 15:50:50 -0500932 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500933 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500934 {
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500935#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600936 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500937 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500938 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600939 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500940 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -0500941 }
942
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600943 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500944 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500945 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500946 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500947 png_warning(png_ptr, "Can't add Up filter after starting");
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500948 png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
949 ~PNG_FILTER_UP);
Guy Schalnate5a37791996-06-05 15:50:50 -0500950 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500951
Guy Schalnate5a37791996-06-05 15:50:50 -0500952 else
953 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500954 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600955 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500956 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -0500957 }
958 }
959
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600960 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500961 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500962 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500963 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500964 png_warning(png_ptr, "Can't add Average filter after starting");
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500965 png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
966 ~PNG_FILTER_AVG);
Guy Schalnate5a37791996-06-05 15:50:50 -0500967 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500968
Guy Schalnate5a37791996-06-05 15:50:50 -0500969 else
970 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500971 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600972 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500973 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -0500974 }
975 }
976
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600977 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
Andreas Dilger47a0c421997-05-16 02:46:07 -0500978 png_ptr->paeth_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500979 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500980 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500981 {
982 png_warning(png_ptr, "Can't add Paeth filter after starting");
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500983 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
Guy Schalnate5a37791996-06-05 15:50:50 -0500984 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500985
Guy Schalnate5a37791996-06-05 15:50:50 -0500986 else
987 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600988 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -0600989 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500990 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -0500991 }
992 }
993
994 if (png_ptr->do_filter == PNG_NO_FILTERS)
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500995#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500996 png_ptr->do_filter = PNG_FILTER_NONE;
997 }
998 }
999 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05001000 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001001}
1002
Andreas Dilger47a0c421997-05-16 02:46:07 -05001003/* This allows us to influence the way in which libpng chooses the "best"
1004 * filter for the current scanline. While the "minimum-sum-of-absolute-
1005 * differences metric is relatively fast and effective, there is some
1006 * question as to whether it can be improved upon by trying to keep the
1007 * filtered data going to zlib more consistent, hopefully resulting in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001008 * better compression.
1009 */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001010#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */
Glenn Randers-Pehrson47dc5f72011-03-30 09:59:02 -05001011/* Convenience reset API. */
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001012static void
John Bowler5d567862011-12-24 09:12:00 -06001013png_reset_filter_heuristics(png_structrp png_ptr)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001014{
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001015 /* Clear out any old values in the 'weights' - this must be done because if
1016 * the app calls set_filter_heuristics multiple times with different
1017 * 'num_weights' values we would otherwise potentially have wrong sized
1018 * arrays.
1019 */
1020 png_ptr->num_prev_filters = 0;
1021 png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1022 if (png_ptr->prev_filters != NULL)
1023 {
1024 png_bytep old = png_ptr->prev_filters;
1025 png_ptr->prev_filters = NULL;
1026 png_free(png_ptr, old);
1027 }
1028 if (png_ptr->filter_weights != NULL)
1029 {
1030 png_uint_16p old = png_ptr->filter_weights;
1031 png_ptr->filter_weights = NULL;
1032 png_free(png_ptr, old);
1033 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001034
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001035 if (png_ptr->inv_filter_weights != NULL)
1036 {
1037 png_uint_16p old = png_ptr->inv_filter_weights;
1038 png_ptr->inv_filter_weights = NULL;
1039 png_free(png_ptr, old);
1040 }
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001041
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001042 /* Leave the filter_costs - this array is fixed size. */
1043}
1044
1045static int
John Bowler5d567862011-12-24 09:12:00 -06001046png_init_filter_heuristics(png_structrp png_ptr, int heuristic_method,
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001047 int num_weights)
1048{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001049 if (png_ptr == NULL)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001050 return 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001051
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001052 /* Clear out the arrays */
1053 png_reset_filter_heuristics(png_ptr);
1054
1055 /* Check arguments; the 'reset' function makes the correct settings for the
1056 * unweighted case, but we must handle the weight case by initializing the
1057 * arrays for the caller.
1058 */
1059 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001060 {
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001061 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001062
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001063 if (num_weights > 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001064 {
1065 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001066 (png_uint_32)(png_sizeof(png_byte) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001067
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001068 /* To make sure that the weighting starts out fairly */
1069 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001070 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001071 png_ptr->prev_filters[i] = 255;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001072 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001073
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001074 png_ptr->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 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
1078 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001079
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001080 for (i = 0; i < num_weights; i++)
1081 {
1082 png_ptr->inv_filter_weights[i] =
1083 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1084 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001085
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001086 /* Safe to set this now */
1087 png_ptr->num_prev_filters = (png_byte)num_weights;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001088 }
1089
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001090 /* If, in the future, there are other filter methods, this would
1091 * need to be based on png_ptr->filter.
1092 */
1093 if (png_ptr->filter_costs == NULL)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001094 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001095 png_ptr->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
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001098 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
1099 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001100 }
1101
Andreas Dilger47a0c421997-05-16 02:46:07 -05001102 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1103 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001104 png_ptr->inv_filter_costs[i] =
1105 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001106 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001107
1108 /* All the arrays are inited, safe to set this: */
1109 png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_WEIGHTED;
1110
1111 /* Return the 'ok' code. */
1112 return 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001113 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001114 else if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT ||
1115 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001116 {
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001117 return 1;
1118 }
1119 else
1120 {
1121 png_warning(png_ptr, "Unknown filter heuristic method");
1122 return 0;
1123 }
1124}
1125
1126/* Provide floating and fixed point APIs */
1127#ifdef PNG_FLOATING_POINT_SUPPORTED
1128void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001129png_set_filter_heuristics(png_structrp png_ptr, int heuristic_method,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001130 int num_weights, png_const_doublep filter_weights,
1131 png_const_doublep filter_costs)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001132{
1133 png_debug(1, "in png_set_filter_heuristics");
1134
1135 /* The internal API allocates all the arrays and ensures that the elements of
1136 * those arrays are set to the default value.
1137 */
1138 if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
1139 return;
1140
1141 /* If using the weighted method copy in the weights. */
1142 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1143 {
1144 int i;
1145 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001146 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001147 if (filter_weights[i] <= 0.0)
1148 {
1149 png_ptr->inv_filter_weights[i] =
1150 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1151 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001152
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001153 else
1154 {
1155 png_ptr->inv_filter_weights[i] =
1156 (png_uint_16)(PNG_WEIGHT_FACTOR*filter_weights[i]+.5);
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001157
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001158 png_ptr->filter_weights[i] =
1159 (png_uint_16)(PNG_WEIGHT_FACTOR/filter_weights[i]+.5);
1160 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001161 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001162
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001163 /* Here is where we set the relative costs of the different filters. We
1164 * should take the desired compression level into account when setting
1165 * the costs, so that Paeth, for instance, has a high relative cost at low
1166 * compression levels, while it has a lower relative cost at higher
1167 * compression settings. The filter types are in order of increasing
1168 * relative cost, so it would be possible to do this with an algorithm.
1169 */
1170 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) if (filter_costs[i] >= 1.0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001171 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001172 png_ptr->inv_filter_costs[i] =
1173 (png_uint_16)(PNG_COST_FACTOR / filter_costs[i] + .5);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001174
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001175 png_ptr->filter_costs[i] =
1176 (png_uint_16)(PNG_COST_FACTOR * filter_costs[i] + .5);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001177 }
1178 }
1179}
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001180#endif /* FLOATING_POINT */
1181
1182#ifdef PNG_FIXED_POINT_SUPPORTED
1183void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001184png_set_filter_heuristics_fixed(png_structrp png_ptr, int heuristic_method,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001185 int num_weights, png_const_fixed_point_p filter_weights,
1186 png_const_fixed_point_p filter_costs)
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001187{
1188 png_debug(1, "in png_set_filter_heuristics_fixed");
1189
1190 /* The internal API allocates all the arrays and ensures that the elements of
1191 * those arrays are set to the default value.
1192 */
1193 if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
1194 return;
1195
1196 /* If using the weighted method copy in the weights. */
1197 if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1198 {
1199 int i;
1200 for (i = 0; i < num_weights; i++)
1201 {
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001202 if (filter_weights[i] <= 0)
1203 {
1204 png_ptr->inv_filter_weights[i] =
1205 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1206 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001207
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001208 else
1209 {
1210 png_ptr->inv_filter_weights[i] = (png_uint_16)
1211 ((PNG_WEIGHT_FACTOR*filter_weights[i]+PNG_FP_HALF)/PNG_FP_1);
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001212
Glenn Randers-Pehrson3df324d2010-07-31 13:45:04 -05001213 png_ptr->filter_weights[i] = (png_uint_16)((PNG_WEIGHT_FACTOR*
1214 PNG_FP_1+(filter_weights[i]/2))/filter_weights[i]);
1215 }
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001216 }
1217
1218 /* Here is where we set the relative costs of the different filters. We
1219 * should take the desired compression level into account when setting
1220 * the costs, so that Paeth, for instance, has a high relative cost at low
1221 * compression levels, while it has a lower relative cost at higher
1222 * compression settings. The filter types are in order of increasing
1223 * relative cost, so it would be possible to do this with an algorithm.
1224 */
1225 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1226 if (filter_costs[i] >= PNG_FP_1)
1227 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001228 png_uint_32 tmp;
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001229
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001230 /* Use a 32 bit unsigned temporary here because otherwise the
1231 * intermediate value will be a 32 bit *signed* integer (ANSI rules)
1232 * and this will get the wrong answer on division.
1233 */
1234 tmp = PNG_COST_FACTOR*PNG_FP_1 + (filter_costs[i]/2);
1235 tmp /= filter_costs[i];
1236
1237 png_ptr->inv_filter_costs[i] = (png_uint_16)tmp;
1238
1239 tmp = PNG_COST_FACTOR * filter_costs[i] + PNG_FP_HALF;
1240 tmp /= PNG_FP_1;
1241
1242 png_ptr->filter_costs[i] = (png_uint_16)tmp;
Glenn Randers-Pehrson4009a762010-07-31 06:34:36 -05001243 }
1244 }
1245}
1246#endif /* FIXED_POINT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001247#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1248
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001249void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001250png_set_compression_level(png_structrp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001251{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001252 png_debug(1, "in png_set_compression_level");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001253
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001254 if (png_ptr == NULL)
1255 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001256
Guy Schalnate5a37791996-06-05 15:50:50 -05001257 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001258 png_ptr->zlib_level = level;
1259}
1260
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001261void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001262png_set_compression_mem_level(png_structrp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001263{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001264 png_debug(1, "in png_set_compression_mem_level");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001265
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001266 if (png_ptr == NULL)
1267 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001268
Guy Schalnate5a37791996-06-05 15:50:50 -05001269 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001270 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001271}
1272
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001273void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001274png_set_compression_strategy(png_structrp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001275{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001276 png_debug(1, "in png_set_compression_strategy");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001277
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001278 if (png_ptr == NULL)
1279 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001280
Guy Schalnate5a37791996-06-05 15:50:50 -05001281 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001282 png_ptr->zlib_strategy = strategy;
1283}
1284
Glenn Randers-Pehrsonaf855e42011-05-07 10:52:49 -05001285/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
1286 * smaller value of window_bits if it can do so safely.
1287 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001288void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001289png_set_compression_window_bits(png_structrp png_ptr, int window_bits)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001290{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001291 if (png_ptr == NULL)
1292 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001293
Guy Schalnate5a37791996-06-05 15:50:50 -05001294 if (window_bits > 15)
1295 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001296
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001297 else if (window_bits < 8)
1298 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001299
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001300#ifndef WBITS_8_OK
Glenn Randers-Pehrson3dfe9372009-08-22 08:44:23 -05001301 /* Avoid libpng bug with 256-byte windows */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001302 if (window_bits == 8)
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001303 {
1304 png_warning(png_ptr, "Compression window is being reset to 512");
1305 window_bits = 9;
1306 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001307
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001308#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001309 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001310 png_ptr->zlib_window_bits = window_bits;
1311}
1312
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001313void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001314png_set_compression_method(png_structrp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001315{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001316 png_debug(1, "in png_set_compression_method");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001317
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001318 if (png_ptr == NULL)
1319 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001320
Guy Schalnate5a37791996-06-05 15:50:50 -05001321 if (method != 8)
1322 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001323
Guy Schalnate5a37791996-06-05 15:50:50 -05001324 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001325 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -05001326}
1327
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001328/* The following were added to libpng-1.5.4 */
John Bowlerb2bee332011-06-10 23:24:58 -05001329#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001330void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001331png_set_text_compression_level(png_structrp png_ptr, int level)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001332{
1333 png_debug(1, "in png_set_text_compression_level");
1334
1335 if (png_ptr == NULL)
1336 return;
1337
1338 png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_LEVEL;
1339 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
1350 png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_MEM_LEVEL;
1351 png_ptr->zlib_text_mem_level = mem_level;
1352}
1353
1354void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001355png_set_text_compression_strategy(png_structrp png_ptr, int strategy)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001356{
1357 png_debug(1, "in png_set_text_compression_strategy");
1358
1359 if (png_ptr == NULL)
1360 return;
1361
1362 png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_STRATEGY;
1363 png_ptr->zlib_text_strategy = strategy;
1364}
1365
Glenn Randers-Pehrsonaf855e42011-05-07 10:52:49 -05001366/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
1367 * smaller value of window_bits if it can do so safely.
1368 */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001369void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001370png_set_text_compression_window_bits(png_structrp png_ptr, int window_bits)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001371{
1372 if (png_ptr == NULL)
1373 return;
1374
1375 if (window_bits > 15)
1376 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
1377
1378 else if (window_bits < 8)
1379 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
1380
1381#ifndef WBITS_8_OK
1382 /* Avoid libpng bug with 256-byte windows */
1383 if (window_bits == 8)
1384 {
1385 png_warning(png_ptr, "Text compression window is being reset to 512");
1386 window_bits = 9;
1387 }
1388
1389#endif
1390 png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_WINDOW_BITS;
1391 png_ptr->zlib_text_window_bits = window_bits;
1392}
1393
1394void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001395png_set_text_compression_method(png_structrp png_ptr, int method)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001396{
1397 png_debug(1, "in png_set_text_compression_method");
1398
1399 if (png_ptr == NULL)
1400 return;
1401
1402 if (method != 8)
1403 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1404
1405 png_ptr->flags |= PNG_FLAG_ZTXT_CUSTOM_METHOD;
1406 png_ptr->zlib_text_method = method;
1407}
John Bowlerb2bee332011-06-10 23:24:58 -05001408#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001409/* end of API added to libpng-1.5.4 */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -05001410
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001411void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001412png_set_write_status_fn(png_structrp png_ptr, png_write_status_ptr write_row_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001413{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001414 if (png_ptr == NULL)
1415 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001416
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001417 png_ptr->write_row_fn = write_row_fn;
1418}
1419
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001420#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001421void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001422png_set_write_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001423 write_user_transform_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001424{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001425 png_debug(1, "in png_set_write_user_transform_fn");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001426
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001427 if (png_ptr == NULL)
1428 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001429
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001430 png_ptr->transformations |= PNG_USER_TRANSFORM;
1431 png_ptr->write_user_transform_fn = write_user_transform_fn;
1432}
1433#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001434
1435
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001436#ifdef PNG_INFO_IMAGE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001437void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001438png_write_png(png_structrp png_ptr, png_inforp info_ptr,
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001439 int transforms, voidp params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001440{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001441 if (png_ptr == NULL || info_ptr == NULL)
1442 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001443
1444 /* Write the file header information. */
1445 png_write_info(png_ptr, info_ptr);
1446
1447 /* ------ these transformations don't touch the info structure ------- */
1448
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001449#ifdef PNG_WRITE_INVERT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001450 /* Invert monochrome pixels */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001451 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001452 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001453#endif
1454
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001455#ifdef PNG_WRITE_SHIFT_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001456 /* Shift the pixels up to a legal bit depth and fill in
1457 * as appropriate to correctly scale the image.
1458 */
1459 if ((transforms & PNG_TRANSFORM_SHIFT)
Glenn Randers-Pehrson45624d62010-03-03 11:40:43 -06001460 && (info_ptr->valid & PNG_INFO_sBIT))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001461 png_set_shift(png_ptr, &info_ptr->sig_bit);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001462#endif
1463
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001464#ifdef PNG_WRITE_PACK_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001465 /* Pack pixels into bytes */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001466 if (transforms & PNG_TRANSFORM_PACKING)
1467 png_set_packing(png_ptr);
1468#endif
1469
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001470#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001471 /* Swap location of alpha bytes from ARGB to RGBA */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001472 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001473 png_set_swap_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001474#endif
1475
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001476#ifdef PNG_WRITE_FILLER_SUPPORTED
Glenn Randers-Pehrson47539062011-05-05 07:32:30 -05001477 /* Pack XRGB/RGBX/ARGB/RGBA into RGB (4 channels -> 3 channels) */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001478 if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001479 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001480
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001481 else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001482 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001483#endif
1484
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001485#ifdef PNG_WRITE_BGR_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001486 /* Flip BGR pixels to RGB */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001487 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001488 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001489#endif
1490
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001491#ifdef PNG_WRITE_SWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001492 /* Swap bytes of 16-bit files to most significant byte first */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001493 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001494 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001495#endif
1496
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001497#ifdef PNG_WRITE_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001498 /* Swap bits of 1, 2, 4 bit packed pixel formats */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001499 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001500 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001501#endif
1502
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001503#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson6878eb62009-06-29 16:45:53 -05001504 /* Invert the alpha channel from opacity to transparency */
1505 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1506 png_set_invert_alpha(png_ptr);
1507#endif
1508
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001509 /* ----------------------- end of transformations ------------------- */
1510
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001511 /* Write the bits */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001512 if (info_ptr->valid & PNG_INFO_IDAT)
1513 png_write_image(png_ptr, info_ptr->row_pointers);
1514
1515 /* It is REQUIRED to call this to finish writing the rest of the file */
1516 png_write_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001517
Glenn Randers-Pehrsond546f432010-12-04 20:41:36 -06001518 PNG_UNUSED(transforms) /* Quiet compiler warnings */
1519 PNG_UNUSED(params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001520}
1521#endif
John Bowler7875d532011-11-07 22:33:49 -06001522
1523
1524#ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
1525#ifdef PNG_STDIO_SUPPORTED /* currently required for png_image_write_* */
1526/* Initialize the write structure - general purpose utility. */
1527static int
1528png_image_write_init(png_imagep image)
1529{
1530 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, image,
1531 png_safe_error, png_safe_warning);
1532
1533 if (png_ptr != NULL)
1534 {
1535 png_infop info_ptr = png_create_info_struct(png_ptr);
1536
1537 if (info_ptr != NULL)
1538 {
John Bowler4fa96a42011-11-16 16:39:16 -06001539 png_controlp control = png_voidcast(png_controlp,
1540 png_malloc_warn(png_ptr, sizeof *control));
John Bowler7875d532011-11-07 22:33:49 -06001541
1542 if (control != NULL)
1543 {
John Bowlerbaeb6d12011-11-26 18:21:02 -06001544 png_memset(control, 0, sizeof *control);
John Bowler7875d532011-11-07 22:33:49 -06001545
1546 control->png_ptr = png_ptr;
1547 control->info_ptr = info_ptr;
1548 control->for_write = 1;
1549
1550 image->opaque = control;
1551 return 1;
1552 }
1553
1554 /* Error clean up */
1555 png_destroy_info_struct(png_ptr, &info_ptr);
1556 }
1557
1558 png_destroy_write_struct(&png_ptr, NULL);
1559 }
1560
1561 return png_image_error(image, "png_image_read: out of memory");
1562}
1563
1564/* Arguments to png_image_write_main: */
1565typedef struct
1566{
1567 /* Arguments: */
1568 png_imagep image;
1569 png_const_voidp buffer;
1570 png_int_32 row_stride;
1571 int convert_to_8bit;
1572 /* Local variables: */
1573 png_const_voidp first_row;
1574 ptrdiff_t row_bytes;
1575 png_voidp local_row;
1576} png_image_write_control;
1577
Glenn Randers-Pehrsonefc4b692011-11-07 23:31:34 -06001578/* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to
1579 * do any necessary byte swapping. The component order is defined by the
John Bowler7875d532011-11-07 22:33:49 -06001580 * png_image format value.
1581 */
1582static int
1583png_write_image_16bit(png_voidp argument)
1584{
John Bowler4fa96a42011-11-16 16:39:16 -06001585 png_image_write_control *display = png_voidcast(png_image_write_control*,
1586 argument);
John Bowler7875d532011-11-07 22:33:49 -06001587 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06001588 png_structrp png_ptr = image->opaque->png_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001589
John Bowler4fa96a42011-11-16 16:39:16 -06001590 png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
1591 display->first_row);
1592 png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row);
John Bowler7875d532011-11-07 22:33:49 -06001593 png_uint_16p row_end;
1594 int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
1595 int aindex = 0;
1596 png_uint_32 y = image->height;
1597
1598 if (image->format & PNG_FORMAT_FLAG_ALPHA)
1599 {
1600 if (image->format & PNG_FORMAT_FLAG_AFIRST)
1601 {
1602 aindex = -1;
1603 ++input_row; /* To point to the first component */
1604 ++output_row;
1605 }
1606
1607 else
1608 aindex = channels;
1609 }
1610
1611 else
1612 png_error(png_ptr, "png_write_image: internal call error");
1613
1614 /* Work out the output row end and count over this, note that the increment
1615 * above to 'row' means that row_end can actually be beyond the end of the
Glenn Randers-Pehrsonefc4b692011-11-07 23:31:34 -06001616 * row; this is correct.
John Bowler7875d532011-11-07 22:33:49 -06001617 */
1618 row_end = output_row + image->width * (channels+1);
1619
1620 while (y-- > 0)
1621 {
1622 png_const_uint_16p in_ptr = input_row;
1623 png_uint_16p out_ptr = output_row;
1624
1625 while (out_ptr < row_end)
1626 {
1627 png_uint_16 alpha = in_ptr[aindex];
1628 png_uint_32 reciprocal = 0;
1629 int c;
1630
1631 out_ptr[aindex] = alpha;
1632
1633 /* Calculate a reciprocal. The correct calculation is simply
Glenn Randers-Pehrsonefc4b692011-11-07 23:31:34 -06001634 * component/alpha*65535 << 15. (I.e. 15 bits of precision); this
John Bowler7875d532011-11-07 22:33:49 -06001635 * allows correct rounding by adding .5 before the shift. 'reciprocal'
1636 * is only initialized when required.
1637 */
1638 if (alpha > 0 && alpha < 65535)
1639 reciprocal = ((0xffff<<15)+(alpha>>1))/alpha;
1640
1641 c = channels;
1642 do /* always at least one channel */
1643 {
1644 png_uint_16 component = *in_ptr++;
1645
1646 /* The following gives 65535 for an alpha of 0, which is fine,
1647 * otherwise if 0/0 is represented as some other value there is more
1648 * likely to be a discontinuity which will probably damage
1649 * compression when moving from a fully transparent area to a
1650 * nearly transparent one. (The assumption here is that opaque
1651 * areas tend not to be 0 intensity.)
1652 */
1653 if (component >= alpha)
1654 component = 65535;
1655
1656 /* component<alpha, so component/alpha is less than one and
1657 * component*reciprocal is less than 2^31.
1658 */
1659 else if (component > 0 && alpha < 65535)
1660 {
1661 png_uint_32 calc = component * reciprocal;
1662 calc += 16384; /* round to nearest */
1663 component = (png_uint_16)(calc >> 15);
1664 }
1665
1666 *out_ptr++ = component;
1667 }
1668 while (--c > 0);
1669
1670 /* Skip to next component (skip the intervening alpha channel) */
1671 ++in_ptr;
1672 ++out_ptr;
1673 }
1674
John Bowler4fa96a42011-11-16 16:39:16 -06001675 png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row));
John Bowler7875d532011-11-07 22:33:49 -06001676 input_row += display->row_bytes/(sizeof (png_uint_16));
1677 }
1678
1679 return 1;
1680}
1681
1682/* Given 16-bit input (1 to 4 channels) write 8-bit output. If an alpha channel
1683 * is present it must be removed from the components, the components are then
1684 * written in sRGB encoding. No components are added or removed.
1685 */
1686static int
1687png_write_image_8bit(png_voidp argument)
1688{
John Bowler4fa96a42011-11-16 16:39:16 -06001689 png_image_write_control *display = png_voidcast(png_image_write_control*,
1690 argument);
John Bowler7875d532011-11-07 22:33:49 -06001691 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06001692 png_structrp png_ptr = image->opaque->png_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001693
John Bowler4fa96a42011-11-16 16:39:16 -06001694 png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
1695 display->first_row);
1696 png_bytep output_row = png_voidcast(png_bytep, display->local_row);
John Bowler7875d532011-11-07 22:33:49 -06001697 png_uint_32 y = image->height;
1698 int channels = (image->format & PNG_FORMAT_FLAG_COLOR) ? 3 : 1;
1699
1700 if (image->format & PNG_FORMAT_FLAG_ALPHA)
1701 {
1702 png_bytep row_end;
1703 int aindex;
1704
1705 if (image->format & PNG_FORMAT_FLAG_AFIRST)
1706 {
1707 aindex = -1;
1708 ++input_row; /* To point to the first component */
1709 ++output_row;
1710 }
1711
1712 else
1713 aindex = channels;
1714
1715 /* Use row_end in place of a loop counter: */
1716 row_end = output_row + image->width * (channels+1);
1717
1718 while (y-- > 0)
1719 {
1720 png_const_uint_16p in_ptr = input_row;
1721 png_bytep out_ptr = output_row;
1722
1723 if (aindex != 0) while (out_ptr < row_end) /* Alpha channel case */
1724 {
1725 png_uint_16 alpha = in_ptr[aindex];
1726 png_uint_32 reciprocal = 0;
1727 int c;
1728
1729 /* Scale and write the alpha channel. See pngrtran.c
1730 * png_do_scale_16_to_8 for a discussion of this calculation. The
1731 * code here has machine native values, so use:
1732 *
1733 * (V * 255 + 32895) >> 16
1734 */
1735 out_ptr[aindex] = (png_byte)((alpha * 255 + 32895) >> 16);
1736
1737 /* Calculate a reciprocal. As above the calculation can be done to
1738 * 15 bits of accuracy, however the output needs to be scaled in the
1739 * range 0..255*65535, so include that scaling here.
1740 */
1741 if (alpha > 0 && alpha < 65535)
1742 reciprocal = (((0xffff*0xff)<<7)+(alpha>>1))/alpha;
1743
1744 c = channels;
1745 do /* always at least one channel */
1746 {
1747 /* Need 32 bit accuracy in the sRGB tables */
1748 png_uint_32 component = *in_ptr++;
1749
John Bowler18c5cfa2011-11-16 14:26:34 -06001750 /* The following gives 1.0 for an alpha of 0, which is fine,
John Bowler7875d532011-11-07 22:33:49 -06001751 * otherwise if 0/0 is represented as some other value there is
1752 * more likely to be a discontinuity which will probably damage
1753 * compression when moving from a fully transparent area to a
1754 * nearly transparent one. (The assumption here is that opaque
1755 * areas tend not to be 0 intensity.)
1756 */
1757 if (component >= alpha)
1758 *out_ptr++ = 255;
1759
1760 /* component<alpha, so component/alpha is less than one and
1761 * component*reciprocal is less than 2^31.
1762 */
John Bowler18c5cfa2011-11-16 14:26:34 -06001763 else if (component > 0)
John Bowler7875d532011-11-07 22:33:49 -06001764 {
John Bowler18c5cfa2011-11-16 14:26:34 -06001765 if (alpha < 65535)
1766 {
1767 component *= reciprocal;
1768 component += 64; /* round to nearest */
1769 component >>= 7;
1770 }
1771
1772 else
1773 component *= 255;
John Bowler7875d532011-11-07 22:33:49 -06001774
1775 /* Convert the component to sRGB. */
1776 *out_ptr++ = (png_byte)PNG_sRGB_FROM_LINEAR(component);
1777 }
1778
1779 else
1780 *out_ptr++ = 0;
1781 }
1782 while (--c > 0);
1783
1784 /* Skip to next component (skip the intervening alpha channel) */
1785 ++in_ptr;
1786 ++out_ptr;
1787 } /* while out_ptr < row_end */
John Bowler3615d032011-11-08 10:38:09 -06001788
John Bowler4fa96a42011-11-16 16:39:16 -06001789 png_write_row(png_ptr, png_voidcast(png_const_bytep,
1790 display->local_row));
John Bowler3615d032011-11-08 10:38:09 -06001791 input_row += display->row_bytes/(sizeof (png_uint_16));
John Bowler7875d532011-11-07 22:33:49 -06001792 } /* while y */
1793 }
1794
1795 else
1796 {
1797 /* No alpha channel, so the row_end really is the end of the row and it
1798 * is sufficient to loop over the components one by one.
1799 */
1800 png_bytep row_end = output_row + image->width * channels;
1801
1802 while (y-- > 0)
1803 {
1804 png_const_uint_16p in_ptr = input_row;
1805 png_bytep out_ptr = output_row;
1806
1807 while (out_ptr < row_end)
1808 {
1809 png_uint_32 component = *in_ptr++;
1810
1811 component *= 255;
1812 *out_ptr++ = (png_byte)PNG_sRGB_FROM_LINEAR(component);
1813 }
1814
1815 png_write_row(png_ptr, output_row);
1816 input_row += display->row_bytes/(sizeof (png_uint_16));
1817 }
1818 }
1819
1820 return 1;
1821}
1822
1823static int
1824png_image_write_main(png_voidp argument)
1825{
John Bowler4fa96a42011-11-16 16:39:16 -06001826 png_image_write_control *display = png_voidcast(png_image_write_control*,
1827 argument);
John Bowler7875d532011-11-07 22:33:49 -06001828 png_imagep image = display->image;
John Bowler5d567862011-12-24 09:12:00 -06001829 png_structrp png_ptr = image->opaque->png_ptr;
1830 png_inforp info_ptr = image->opaque->info_ptr;
John Bowler7875d532011-11-07 22:33:49 -06001831 png_uint_32 format = image->format;
1832
1833 int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0; /* input */
1834 int alpha = (format & PNG_FORMAT_FLAG_ALPHA) != 0;
1835 int write_16bit = linear && !display->convert_to_8bit;
1836
John Bowler3706d732011-11-21 10:28:06 -06001837 /* Default the 'row_stride' parameter if required. */
1838 if (display->row_stride == 0)
1839 display->row_stride = PNG_IMAGE_ROW_STRIDE(*image);
1840
John Bowler7875d532011-11-07 22:33:49 -06001841 /* Set the required transforms then write the rows in the correct order. */
1842 png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
1843 write_16bit ? 16 : 8,
1844 ((format & PNG_FORMAT_FLAG_COLOR) ? PNG_COLOR_MASK_COLOR : 0) +
1845 ((format & PNG_FORMAT_FLAG_ALPHA) ? PNG_COLOR_MASK_ALPHA : 0),
1846 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
1847
1848 /* Counter-intuitively the data transformations must be called *after*
1849 * png_write_info, not before as in the read code, but the 'set' functions
1850 * must still be called before. Just set the color space information, never
1851 * write an interlaced image.
1852 */
1853 if (write_16bit)
1854 {
1855 /* The gamma here is 1.0 (linear) and the cHRM chunk matches sRGB. */
1856 png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_LINEAR);
John Bowlerdd819152011-11-08 14:29:45 -06001857
1858 if (!(image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB))
1859 png_set_cHRM_fixed(png_ptr, info_ptr,
1860 /* color x y */
1861 /* white */ 31270, 32900,
1862 /* red */ 64000, 33000,
1863 /* green */ 30000, 60000,
1864 /* blue */ 15000, 6000
1865 );
John Bowler7875d532011-11-07 22:33:49 -06001866 }
1867
John Bowlerdd819152011-11-08 14:29:45 -06001868 else if (!(image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB))
John Bowler7875d532011-11-07 22:33:49 -06001869 png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL);
1870
John Bowlerdd819152011-11-08 14:29:45 -06001871 /* Else writing an 8-bit file and the *colors* aren't sRGB, but the 8-bit
1872 * space must still be gamma encoded.
1873 */
1874 else
1875 png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE);
1876
John Bowler7875d532011-11-07 22:33:49 -06001877 /* Write the file header. */
1878 png_write_info(png_ptr, info_ptr);
1879
1880 /* Now set up the data transformations (*after* the header is written),
1881 * remove the handled transformations from the 'format' flags for checking.
John Bowlere6fb6912011-11-08 21:35:16 -06001882 *
1883 * First check for a little endian system if writing 16 bit files.
John Bowler7875d532011-11-07 22:33:49 -06001884 */
John Bowler7875d532011-11-07 22:33:49 -06001885 if (write_16bit)
1886 {
1887 PNG_CONST png_uint_16 le = 0x0001;
1888
1889 if (*(png_const_bytep)&le)
1890 png_set_swap(png_ptr);
1891 }
1892
1893# ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
1894 if (format & PNG_FORMAT_FLAG_BGR)
1895 {
John Bowlere6fb6912011-11-08 21:35:16 -06001896 if (format & PNG_FORMAT_FLAG_COLOR)
1897 png_set_bgr(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001898 format &= ~PNG_FORMAT_FLAG_BGR;
1899 }
1900# endif
1901
1902# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
1903 if (format & PNG_FORMAT_FLAG_AFIRST)
1904 {
John Bowlere6fb6912011-11-08 21:35:16 -06001905 if (format & PNG_FORMAT_FLAG_ALPHA)
1906 png_set_swap_alpha(png_ptr);
John Bowler7875d532011-11-07 22:33:49 -06001907 format &= ~PNG_FORMAT_FLAG_AFIRST;
1908 }
1909# endif
1910
John Bowlere6fb6912011-11-08 21:35:16 -06001911 /* That should have handled all (both) the transforms. */
John Bowler89c2f842011-11-16 12:04:39 -06001912 if ((format & ~(png_uint_32)(PNG_FORMAT_FLAG_COLOR | PNG_FORMAT_FLAG_LINEAR |
John Bowlere6fb6912011-11-08 21:35:16 -06001913 PNG_FORMAT_FLAG_ALPHA)) != 0)
John Bowler7875d532011-11-07 22:33:49 -06001914 png_error(png_ptr, "png_write_image: unsupported transformation");
1915
1916 {
John Bowler4fa96a42011-11-16 16:39:16 -06001917 png_const_bytep row = png_voidcast(png_const_bytep, display->buffer);
John Bowler7875d532011-11-07 22:33:49 -06001918 ptrdiff_t row_bytes = display->row_stride;
1919
1920 if (linear)
1921 row_bytes *= sizeof (png_uint_16);
1922
1923 if (row_bytes < 0)
1924 row += (image->height-1) * (-row_bytes);
1925
1926 display->first_row = row;
1927 display->row_bytes = row_bytes;
1928 }
1929
1930 /* Check for the cases that currently require a pre-transform on the row
1931 * before it is written. This only applies when the input is 16-bit and
1932 * either there is an alpha channel or it is converted to 8-bit.
1933 */
1934 if ((linear && alpha) || display->convert_to_8bit)
1935 {
John Bowler4fa96a42011-11-16 16:39:16 -06001936 png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
1937 png_get_rowbytes(png_ptr, info_ptr)));
John Bowler7875d532011-11-07 22:33:49 -06001938 int result;
1939
1940 display->local_row = row;
1941 if (write_16bit)
1942 result = png_safe_execute(image, png_write_image_16bit, display);
1943 else
1944 result = png_safe_execute(image, png_write_image_8bit, display);
1945 display->local_row = NULL;
1946
1947 png_free(png_ptr, row);
1948
1949 /* Skip the 'write_end' on error: */
1950 if (!result)
1951 return 0;
1952 }
1953
1954 /* Otherwise this is the case where the input is in a format currently
1955 * supported by the rest of the libpng write code; call it directly.
1956 */
1957 else
1958 {
John Bowler4fa96a42011-11-16 16:39:16 -06001959 png_const_bytep row = png_voidcast(png_const_bytep, display->first_row);
John Bowler7875d532011-11-07 22:33:49 -06001960 ptrdiff_t row_bytes = display->row_bytes;
1961 png_uint_32 y = image->height;
1962
1963 while (y-- > 0)
1964 {
1965 png_write_row(png_ptr, row);
1966 row += row_bytes;
1967 }
1968 }
1969
1970 png_write_end(png_ptr, info_ptr);
1971 return 1;
1972}
1973
1974int PNGAPI
John Bowlerdd819152011-11-08 14:29:45 -06001975png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit,
John Bowler7875d532011-11-07 22:33:49 -06001976 const void *buffer, png_int_32 row_stride)
1977{
1978 /* Write the image to the given (FILE*). */
1979 if (image != NULL)
1980 {
1981 if (file != NULL)
1982 {
1983 if (png_image_write_init(image))
1984 {
1985 png_image_write_control display;
1986 int result;
1987
1988 /* This is slightly evil, but png_init_io doesn't do anything other
1989 * than this and we haven't changed the standard IO functions so
1990 * this saves a 'safe' function.
1991 */
1992 image->opaque->png_ptr->io_ptr = file;
1993
John Bowlerbaeb6d12011-11-26 18:21:02 -06001994 png_memset(&display, 0, sizeof display);
John Bowler7875d532011-11-07 22:33:49 -06001995 display.image = image;
1996 display.buffer = buffer;
1997 display.row_stride = row_stride;
1998 display.convert_to_8bit = convert_to_8bit;
1999
2000 result = png_safe_execute(image, png_image_write_main, &display);
2001 png_image_free(image);
2002 return result;
2003 }
2004
2005 else
2006 return 0;
2007 }
2008
2009 else
2010 return png_image_error(image,
2011 "png_image_write_to_stdio: invalid argument");
2012 }
2013
2014 else
2015 return 0;
2016}
2017
2018int PNGAPI
John Bowlerdd819152011-11-08 14:29:45 -06002019png_image_write_to_file(png_imagep image, const char *file_name,
John Bowler7875d532011-11-07 22:33:49 -06002020 int convert_to_8bit, const void *buffer, png_int_32 row_stride)
2021{
2022 /* Write the image to the named file. */
2023 if (image != NULL)
2024 {
2025 if (file_name != NULL)
2026 {
2027 FILE *fp = fopen(file_name, "wb");
2028
2029 if (fp != NULL)
2030 {
John Bowlerdd819152011-11-08 14:29:45 -06002031 if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer,
2032 row_stride))
John Bowler7875d532011-11-07 22:33:49 -06002033 {
John Bowlerdd819152011-11-08 14:29:45 -06002034 int error; /* from fflush/fclose */
John Bowler7875d532011-11-07 22:33:49 -06002035
John Bowlerdd819152011-11-08 14:29:45 -06002036 /* Make sure the file is flushed correctly. */
2037 if (fflush(fp) == 0 && ferror(fp) == 0)
John Bowler7875d532011-11-07 22:33:49 -06002038 {
John Bowlerdd819152011-11-08 14:29:45 -06002039 if (fclose(fp) == 0)
2040 return 1;
John Bowler7875d532011-11-07 22:33:49 -06002041
John Bowlerdd819152011-11-08 14:29:45 -06002042 error = errno; /* from fclose */
John Bowler7875d532011-11-07 22:33:49 -06002043 }
2044
John Bowlerdd819152011-11-08 14:29:45 -06002045 else
2046 {
2047 error = errno; /* from fflush or ferror */
2048 (void)fclose(fp);
2049 }
2050
2051 (void)remove(file_name);
2052 /* The image has already been cleaned up; this is just used to
2053 * set the error (because the original write succeeded).
2054 */
2055 return png_image_error(image, strerror(error));
John Bowler7875d532011-11-07 22:33:49 -06002056 }
2057
2058 else
2059 {
2060 /* Clean up: just the opened file. */
2061 (void)fclose(fp);
John Bowlerdd819152011-11-08 14:29:45 -06002062 (void)remove(file_name);
John Bowler7875d532011-11-07 22:33:49 -06002063 return 0;
2064 }
2065 }
2066
2067 else
2068 return png_image_error(image, strerror(errno));
2069 }
2070
2071 else
2072 return png_image_error(image,
2073 "png_image_write_to_file: invalid argument");
2074 }
2075
2076 else
2077 return 0;
2078}
2079#endif /* PNG_STDIO_SUPPORTED */
2080#endif /* SIMPLIFIED_WRITE */
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -06002081#endif /* PNG_WRITE_SUPPORTED */