blob: 87b0f06e27082d9be2528f20c3aeb3d39c46ab6e [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-Pehrson668af4e2009-06-24 06:35:59 -05004 * Last changed in libpng 1.4.0 [June 24, 2009]
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06005 * Copyright (c) 1998-2009 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 *
9 * This code is released under the zlib-libpng license.
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050010 * For conditions of distribution and use, see copyright notice, disclaimer,
11 * and license in png.h
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060012 */
Guy Schalnat0d580581995-07-20 02:43:20 -050013
14/* get internal access to png.h */
Guy Schalnat0d580581995-07-20 02:43:20 -050015#include "png.h"
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -060016#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050017#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050018
Andreas Dilger47a0c421997-05-16 02:46:07 -050019/* Writes all the PNG information. This is the suggested way to use the
20 * library. If you have a new chunk to add, make a function to write it,
21 * and put it in the correct location here. If you want the chunk written
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050022 * after the image data, put it in png_write_end(). I strongly encourage
Andreas Dilger47a0c421997-05-16 02:46:07 -050023 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
24 * the chunk, as that will keep the code from breaking if you want to just
25 * write a plain PNG file. If you have long comments, I suggest writing
26 * them in png_write_end(), and compressing them.
27 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050028void PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060029png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050030{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050031 png_debug(1, "in png_write_info_before_PLTE");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -060032 if (png_ptr == NULL || info_ptr == NULL)
33 return;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060034 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
35 {
Guy Schalnat0d580581995-07-20 02:43:20 -050036 png_write_sig(png_ptr); /* write PNG signature */
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060037#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050038 if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&(png_ptr->mng_features_permitted))
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060039 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050040 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060041 png_ptr->mng_features_permitted=0;
42 }
43#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050044 /* write IHDR information. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060045 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
46 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060047 info_ptr->filter_type,
48#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
49 info_ptr->interlace_type);
50#else
51 0);
52#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050053 /* The rest of these check to see if the valid field has the appropriate
54 * flag set, and if it does, writes the chunk.
55 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -050056#if defined(PNG_WRITE_gAMA_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060057 if (info_ptr->valid & PNG_INFO_gAMA)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060058 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060059# ifdef PNG_FLOATING_POINT_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060060 png_write_gAMA(png_ptr, info_ptr->gamma);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060061#else
62#ifdef PNG_FIXED_POINT_SUPPORTED
63 png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060064# endif
65#endif
66 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -050067#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060068#if defined(PNG_WRITE_sRGB_SUPPORTED)
69 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-Pehrson166c5a31999-12-10 09:43:02 -060072#if defined(PNG_WRITE_iCCP_SUPPORTED)
73 if (info_ptr->valid & PNG_INFO_iCCP)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -060074 png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060075 info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
76#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050077#if defined(PNG_WRITE_sBIT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060078 if (info_ptr->valid & PNG_INFO_sBIT)
79 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050080#endif
81#if defined(PNG_WRITE_cHRM_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060082 if (info_ptr->valid & PNG_INFO_cHRM)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060083 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060084#ifdef PNG_FLOATING_POINT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050085 png_write_cHRM(png_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060086 info_ptr->x_white, info_ptr->y_white,
87 info_ptr->x_red, info_ptr->y_red,
88 info_ptr->x_green, info_ptr->y_green,
89 info_ptr->x_blue, info_ptr->y_blue);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060090#else
91# ifdef PNG_FIXED_POINT_SUPPORTED
92 png_write_cHRM_fixed(png_ptr,
93 info_ptr->int_x_white, info_ptr->int_y_white,
94 info_ptr->int_x_red, info_ptr->int_y_red,
95 info_ptr->int_x_green, info_ptr->int_y_green,
96 info_ptr->int_x_blue, info_ptr->int_y_blue);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060097# endif
98#endif
99 }
100#endif
101#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
102 if (info_ptr->unknown_chunks_num)
103 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500104 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600105
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500106 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600107
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500108 for (up = info_ptr->unknown_chunks;
109 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
110 up++)
111 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600112 int keep=png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500113 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500114 up->location && !(up->location & PNG_HAVE_PLTE) &&
115 !(up->location & PNG_HAVE_IDAT) &&
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500116 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600117 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
118 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500119 if (up->size == 0)
120 png_warning(png_ptr, "Writing zero-length unknown chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600121 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600122 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500123 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600124 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500125#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600126 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
127 }
128}
129
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500130void PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600131png_write_info(png_structp png_ptr, png_infop info_ptr)
132{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600133#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600134 int i;
135#endif
136
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500137 png_debug(1, "in png_write_info");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600138
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600139 if (png_ptr == NULL || info_ptr == NULL)
140 return;
141
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600142 png_write_info_before_PLTE(png_ptr, info_ptr);
143
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600144 if (info_ptr->valid & PNG_INFO_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500145 png_write_PLTE(png_ptr, info_ptr->palette,
146 (png_uint_32)info_ptr->num_palette);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600147 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600148 png_error(png_ptr, "Valid palette required for paletted images");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600149
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500150#if defined(PNG_WRITE_tRNS_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600151 if (info_ptr->valid & PNG_INFO_tRNS)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500152 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600153#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500154 /* invert the alpha channel (in tRNS) */
155 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
156 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
157 {
158 int j;
159 for (j=0; j<(int)info_ptr->num_trans; j++)
160 info_ptr->trans[j] = (png_byte)(255 - info_ptr->trans[j]);
161 }
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600162#endif
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500163 png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_color),
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600164 info_ptr->num_trans, info_ptr->color_type);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500165 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500166#endif
167#if defined(PNG_WRITE_bKGD_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600168 if (info_ptr->valid & PNG_INFO_bKGD)
169 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500170#endif
171#if defined(PNG_WRITE_hIST_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600172 if (info_ptr->valid & PNG_INFO_hIST)
173 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500174#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500175#if defined(PNG_WRITE_oFFs_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600176 if (info_ptr->valid & PNG_INFO_oFFs)
177 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
178 info_ptr->offset_unit_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500179#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500180#if defined(PNG_WRITE_pCAL_SUPPORTED)
181 if (info_ptr->valid & PNG_INFO_pCAL)
182 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
183 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
184 info_ptr->pcal_units, info_ptr->pcal_params);
185#endif
Glenn Randers-Pehrson59020592009-05-18 10:52:12 -0500186
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500187#if defined(PNG_sCAL_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600188 if (info_ptr->valid & PNG_INFO_sCAL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500189#if defined(PNG_WRITE_sCAL_SUPPORTED)
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600190#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600191 png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,
192 info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);
Glenn Randers-Pehrson59020592009-05-18 10:52:12 -0500193#else /* !FLOATING_POINT */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600194#ifdef PNG_FIXED_POINT_SUPPORTED
195 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600196 info_ptr->scal_s_width, info_ptr->scal_s_height);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500197#endif /* FIXED_POINT */
198#endif /* FLOATING_POINT */
Glenn Randers-Pehrson59020592009-05-18 10:52:12 -0500199#else /* !WRITE_sCAL */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600200 png_warning(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500201 "png_write_sCAL not supported; sCAL chunk not written");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500202#endif /* WRITE_sCAL */
203#endif /* sCAL */
204
Andreas Dilger47a0c421997-05-16 02:46:07 -0500205#if defined(PNG_WRITE_pHYs_SUPPORTED)
206 if (info_ptr->valid & PNG_INFO_pHYs)
207 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
208 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500209#endif /* pHYs */
210
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500211#if defined(PNG_WRITE_tIME_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600212 if (info_ptr->valid & PNG_INFO_tIME)
Guy Schalnate5a37791996-06-05 15:50:50 -0500213 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600214 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600215 png_ptr->mode |= PNG_WROTE_tIME;
Guy Schalnate5a37791996-06-05 15:50:50 -0500216 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500217#endif /* tIME */
218
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600219#if defined(PNG_WRITE_sPLT_SUPPORTED)
220 if (info_ptr->valid & PNG_INFO_sPLT)
221 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
222 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500223#endif /* sPLT */
224
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600225#if defined(PNG_WRITE_TEXT_SUPPORTED)
Guy Schalnate5a37791996-06-05 15:50:50 -0500226 /* Check to see if we need to write text chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500227 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500228 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500229 png_debug2(2, "Writing header text chunk %d, type %d", i,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500230 info_ptr->text[i].compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600231 /* an internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600232 if (info_ptr->text[i].compression > 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600233 {
234#if defined(PNG_WRITE_iTXt_SUPPORTED)
235 /* write international chunk */
236 png_write_iTXt(png_ptr,
237 info_ptr->text[i].compression,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600238 info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600239 info_ptr->text[i].lang,
240 info_ptr->text[i].lang_key,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600241 info_ptr->text[i].text);
242#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600243 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600244#endif
245 /* Mark this chunk as written */
246 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
247 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500248 /* If we want a compressed text chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600249 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500250 {
251#if defined(PNG_WRITE_zTXt_SUPPORTED)
252 /* write compressed chunk */
253 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600254 info_ptr->text[i].text, 0,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500255 info_ptr->text[i].compression);
256#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600257 png_warning(png_ptr, "Unable to write compressed text");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500258#endif
259 /* Mark this chunk as written */
260 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
261 }
262 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
263 {
264#if defined(PNG_WRITE_tEXt_SUPPORTED)
265 /* write uncompressed chunk */
266 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600267 info_ptr->text[i].text,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600268 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500269 /* Mark this chunk as written */
270 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500271#else
272 /* Can't get here */
273 png_warning(png_ptr, "Unable to write uncompressed text");
274#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500275 }
276 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500277#endif /* tEXt */
278
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600279#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
280 if (info_ptr->unknown_chunks_num)
281 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500282 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600283
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500284 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600285
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500286 for (up = info_ptr->unknown_chunks;
287 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
288 up++)
289 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600290 int keep=png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500291 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600292 up->location && (up->location & PNG_HAVE_PLTE) &&
293 !(up->location & PNG_HAVE_IDAT) &&
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500294 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600295 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
296 {
297 png_write_chunk(png_ptr, up->name, up->data, up->size);
298 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500299 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600300 }
301#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500302}
Guy Schalnat0d580581995-07-20 02:43:20 -0500303
Andreas Dilger47a0c421997-05-16 02:46:07 -0500304/* Writes the end of the PNG file. If you don't want to write comments or
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600305 * time information, you can pass NULL for info. If you already wrote these
306 * in png_write_info(), do not write them again here. If you have long
307 * comments, I suggest writing them here, and compressing them.
308 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500309void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500310png_write_end(png_structp png_ptr, png_infop info_ptr)
311{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500312 png_debug(1, "in png_write_end");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600313 if (png_ptr == NULL)
314 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500315 if (!(png_ptr->mode & PNG_HAVE_IDAT))
316 png_error(png_ptr, "No IDATs written into file");
317
318 /* see if user wants us to write information chunks */
319 if (info_ptr != NULL)
320 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600321#if defined(PNG_WRITE_TEXT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500322 int i; /* local index variable */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600323#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500324#if defined(PNG_WRITE_tIME_SUPPORTED)
325 /* check to see if user has supplied a time chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600326 if ((info_ptr->valid & PNG_INFO_tIME) &&
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600327 !(png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500328 png_write_tIME(png_ptr, &(info_ptr->mod_time));
329#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600330#if defined(PNG_WRITE_TEXT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500331 /* loop through comment chunks */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600332 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500333 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500334 png_debug2(2, "Writing trailer text chunk %d, type %d", i,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500335 info_ptr->text[i].compression);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600336 /* an internationalized chunk? */
337 if (info_ptr->text[i].compression > 0)
338 {
339#if defined(PNG_WRITE_iTXt_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500340 /* write international chunk */
341 png_write_iTXt(png_ptr,
342 info_ptr->text[i].compression,
343 info_ptr->text[i].key,
344 info_ptr->text[i].lang,
345 info_ptr->text[i].lang_key,
346 info_ptr->text[i].text);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600347#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500348 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600349#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500350 /* Mark this chunk as written */
351 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600352 }
353 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
Guy Schalnat0d580581995-07-20 02:43:20 -0500354 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500355#if defined(PNG_WRITE_zTXt_SUPPORTED)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600356 /* write compressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600357 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600358 info_ptr->text[i].text, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600359 info_ptr->text[i].compression);
Guy Schalnate5a37791996-06-05 15:50:50 -0500360#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600361 png_warning(png_ptr, "Unable to write compressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500362#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500363 /* Mark this chunk as written */
364 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500365 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500366 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500367 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500368#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500369 /* write uncompressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600370 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600371 info_ptr->text[i].text, 0);
Guy Schalnate5a37791996-06-05 15:50:50 -0500372#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600373 png_warning(png_ptr, "Unable to write uncompressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500374#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500375
Andreas Dilger47a0c421997-05-16 02:46:07 -0500376 /* Mark this chunk as written */
377 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500378 }
379 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600380#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600381#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
382 if (info_ptr->unknown_chunks_num)
383 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500384 png_unknown_chunk *up;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600385
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500386 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600387
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500388 for (up = info_ptr->unknown_chunks;
389 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
390 up++)
391 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600392 int keep=png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500393 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600394 up->location && (up->location & PNG_AFTER_IDAT) &&
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500395 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600396 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
397 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600398 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600399 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500400 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600401 }
402#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500403 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500404
405 png_ptr->mode |= PNG_AFTER_IDAT;
406
Andreas Dilger47a0c421997-05-16 02:46:07 -0500407 /* write end of PNG file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500408 png_write_IEND(png_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500409 /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
410 * and restored again in libpng-1.2.30, may cause some applications that
411 * do not set png_ptr->output_flush_fn to crash. If your application
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600412 * experiences a problem, please report the event to
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500413 * png-mng-implement at lists.sf.net .
414 */
Glenn Randers-Pehrsondbed41f2008-08-19 18:20:52 -0500415#if defined(PNG_WRITE_FLUSH_SUPPORTED)
Glenn Randers-Pehrson32fc5ce2000-07-24 06:34:14 -0500416 png_flush(png_ptr);
Glenn Randers-Pehrsondbed41f2008-08-19 18:20:52 -0500417#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500418}
419
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500420#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500421/* "time.h" functions are not supported on WindowsCE */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500422void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600423png_convert_from_struct_tm(png_timep ptime, struct tm FAR * 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");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600426 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
427 ptime->month = (png_byte)(ttime->tm_mon + 1);
428 ptime->day = (png_byte)ttime->tm_mday;
429 ptime->hour = (png_byte)ttime->tm_hour;
430 ptime->minute = (png_byte)ttime->tm_min;
431 ptime->second = (png_byte)ttime->tm_sec;
Guy Schalnat0d580581995-07-20 02:43:20 -0500432}
433
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500434void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600435png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500436{
437 struct tm *tbuf;
438
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500439 png_debug(1, "in png_convert_from_time_t");
Guy Schalnat0d580581995-07-20 02:43:20 -0500440 tbuf = gmtime(&ttime);
441 png_convert_from_struct_tm(ptime, tbuf);
442}
Guy Schalnat6d764711995-12-19 03:22:19 -0600443#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500444
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500445
Andreas Dilger47a0c421997-05-16 02:46:07 -0500446/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500447png_structp PNGAPI
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500448png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600449 png_error_ptr error_fn, png_error_ptr warn_fn)
Guy Schalnat0d580581995-07-20 02:43:20 -0500450{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500451#ifdef PNG_USER_MEM_SUPPORTED
452 return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500453 warn_fn, NULL, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500454}
455
456/* Alternate initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500457png_structp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500458png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
459 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
460 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
461{
462#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500463 int png_cleanup_needed = 0;
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600464#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500465 volatile
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600466#endif
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500467 png_structp png_ptr;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600468#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600469#ifdef USE_FAR_KEYWORD
470 jmp_buf jmpbuf;
471#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600472#endif
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500473 int i;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500474 png_debug(1, "in png_create_write_struct");
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500475#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600476 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500477 (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500478#else
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600479 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500480#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600481 if (png_ptr == NULL)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500482 return (NULL);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600483
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500484 /* added at libpng-1.2.6 */
485#ifdef PNG_SET_USER_LIMITS_SUPPORTED
486 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
487 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
488#endif
489
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500490#ifdef PNG_SETJMP_SUPPORTED
491/* Applications that neglect to set up their own setjmp() and then
492 encounter a png_error() will longjmp here. Since the jmpbuf is
493 then meaningless we abort instead of returning. */
494#ifdef USE_FAR_KEYWORD
495 if (setjmp(jmpbuf))
496#else
497 if (setjmp(png_ptr->jmpbuf))
498#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500499 PNG_ABORT();
Glenn Randers-Pehrsonf0a8fe02009-04-14 08:28:15 -0500500#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600501
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500502#ifdef PNG_USER_MEM_SUPPORTED
503 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
504#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600505 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnate5a37791996-06-05 15:50:50 -0500506
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500507 if (user_png_ver)
Guy Schalnate5a37791996-06-05 15:50:50 -0500508 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500509 i=0;
510 do
511 {
512 if (user_png_ver[i] != png_libpng_ver[i])
513 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
514 } while (png_libpng_ver[i++]);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500515 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500516
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500517 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500518 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500519 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
520 * we must recompile any applications that use any older library version.
521 * For versions after libpng 1.0, we will be compatible, so we need
522 * only check the first digit.
523 */
524 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500525 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500526 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
527 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500528#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500529 char msg[80];
530 if (user_png_ver)
531 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500532 png_snprintf(msg, 80,
533 "Application was compiled with png.h from libpng-%.20s",
534 user_png_ver);
535 png_warning(png_ptr, msg);
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500536 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500537 png_snprintf(msg, 80,
538 "Application is running with png.c from libpng-%.20s",
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500539 png_libpng_ver);
540 png_warning(png_ptr, msg);
541#endif
542#ifdef PNG_ERROR_NUMBERS_SUPPORTED
543 png_ptr->flags=0;
544#endif
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500545 png_warning(png_ptr,
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500546 "Incompatible libpng version in application and library");
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500547 png_cleanup_needed = 1;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500548 }
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500549 }
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500550
Guy Schalnat0d580581995-07-20 02:43:20 -0500551 /* initialize zbuf - compression buffer */
552 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500553 if (!png_cleanup_needed)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500554 {
555 png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,
556 png_ptr->zbuf_size);
557 if (png_ptr->zbuf == NULL)
558 png_cleanup_needed = 1;
559 }
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500560 if (png_cleanup_needed)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500561 {
562 /* Clean up PNG structure and deallocate any memory. */
563 png_free(png_ptr, png_ptr->zbuf);
564 png_ptr->zbuf = NULL;
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500565#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500566 png_destroy_struct_2((png_voidp)png_ptr,
567 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500568#else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500569 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsona93c9422009-04-13 11:41:33 -0500570#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500571 return (NULL);
572 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500573
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500574 png_set_write_fn(png_ptr, NULL, NULL, NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500575
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600576#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
577 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500578 1, NULL, NULL);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600579#endif
580
Glenn Randers-Pehrson03008a02002-04-27 10:11:25 -0500581 return (png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500582}
583
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500584
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600585/* Write a few rows of image data. If the image is interlaced,
586 * either you will have to write the 7 sub images, or, if you
587 * have called png_set_interlace_handling(), you will have to
588 * "write" the image seven times.
589 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500590void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600591png_write_rows(png_structp png_ptr, png_bytepp row,
Guy Schalnat0d580581995-07-20 02:43:20 -0500592 png_uint_32 num_rows)
593{
594 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600595 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500596
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500597 png_debug(1, "in png_write_rows");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600598
599 if (png_ptr == NULL)
600 return;
601
Guy Schalnat0d580581995-07-20 02:43:20 -0500602 /* loop through the rows */
603 for (i = 0, rp = row; i < num_rows; i++, rp++)
604 {
605 png_write_row(png_ptr, *rp);
606 }
607}
608
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600609/* Write the image. You only need to call this function once, even
610 * if you are writing an interlaced image.
611 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500612void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600613png_write_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500614{
615 png_uint_32 i; /* row index */
616 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600617 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500618
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600619 if (png_ptr == NULL)
620 return;
621
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500622 png_debug(1, "in png_write_image");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600623#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500624 /* Initialize interlace handling. If image is not interlaced,
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500625 * this will set pass to 1
626 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500627 num_pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600628#else
629 num_pass = 1;
630#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500631 /* loop through passes */
632 for (pass = 0; pass < num_pass; pass++)
633 {
634 /* loop through image */
635 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
636 {
637 png_write_row(png_ptr, *rp);
638 }
639 }
640}
641
Guy Schalnate5a37791996-06-05 15:50:50 -0500642/* called by user to write a row of image data */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500643void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600644png_write_row(png_structp png_ptr, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500645{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600646 if (png_ptr == NULL)
647 return;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500648 png_debug2(1, "in png_write_row (row %ld, pass %d)",
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600649 png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600650
Guy Schalnat0d580581995-07-20 02:43:20 -0500651 /* initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600652 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500653 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500654 /* make sure we wrote the header info */
655 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
656 png_error(png_ptr,
657 "png_write_info was never called before png_write_row");
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600658
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500659 /* check for transforms that have been set but were defined out */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500660#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500661 if (png_ptr->transformations & PNG_INVERT_MONO)
662 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500663#endif
664#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500665 if (png_ptr->transformations & PNG_FILLER)
666 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500667#endif
668#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500669 if (png_ptr->transformations & PNG_PACKSWAP)
670 png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500671#endif
672#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500673 if (png_ptr->transformations & PNG_PACK)
674 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500675#endif
676#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500677 if (png_ptr->transformations & PNG_SHIFT)
678 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500679#endif
680#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500681 if (png_ptr->transformations & PNG_BGR)
682 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500683#endif
684#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500685 if (png_ptr->transformations & PNG_SWAP_BYTES)
686 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500687#endif
688
Guy Schalnat0d580581995-07-20 02:43:20 -0500689 png_write_start_row(png_ptr);
690 }
691
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500692#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500693 /* if interlaced and not interested in row, return */
694 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
695 {
696 switch (png_ptr->pass)
697 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600698 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600699 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500700 {
701 png_write_finish_row(png_ptr);
702 return;
703 }
704 break;
705 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600706 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500707 {
708 png_write_finish_row(png_ptr);
709 return;
710 }
711 break;
712 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600713 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500714 {
715 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600716 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500717 }
718 break;
719 case 3:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600720 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
Guy Schalnat0d580581995-07-20 02:43:20 -0500721 {
722 png_write_finish_row(png_ptr);
723 return;
724 }
725 break;
726 case 4:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600727 if ((png_ptr->row_number & 0x03) != 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500728 {
729 png_write_finish_row(png_ptr);
730 return;
731 }
732 break;
733 case 5:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600734 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500735 {
736 png_write_finish_row(png_ptr);
737 return;
738 }
739 break;
740 case 6:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600741 if (!(png_ptr->row_number & 0x01))
Guy Schalnat0d580581995-07-20 02:43:20 -0500742 {
743 png_write_finish_row(png_ptr);
744 return;
745 }
746 break;
747 }
748 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600749#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500750
751 /* set up row info for transformations */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600752 png_ptr->row_info.color_type = png_ptr->color_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500753 png_ptr->row_info.width = png_ptr->usr_width;
754 png_ptr->row_info.channels = png_ptr->usr_channels;
755 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600756 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
757 png_ptr->row_info.channels);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600758
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500759 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
760 png_ptr->row_info.width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500761
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500762 png_debug1(3, "row_info->color_type = %d", png_ptr->row_info.color_type);
763 png_debug1(3, "row_info->width = %lu", png_ptr->row_info.width);
764 png_debug1(3, "row_info->channels = %d", png_ptr->row_info.channels);
765 png_debug1(3, "row_info->bit_depth = %d", png_ptr->row_info.bit_depth);
766 png_debug1(3, "row_info->pixel_depth = %d", png_ptr->row_info.pixel_depth);
767 png_debug1(3, "row_info->rowbytes = %lu", png_ptr->row_info.rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500768
769 /* Copy user's row into buffer, leaving room for filter byte. */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500770 png_memcpy(png_ptr->row_buf + 1, row, png_ptr->row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500771
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500772#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500773 /* Handle interlacing */
Guy Schalnat0d580581995-07-20 02:43:20 -0500774 if (png_ptr->interlaced && png_ptr->pass < 6 &&
775 (png_ptr->transformations & PNG_INTERLACE))
776 {
777 png_do_write_interlace(&(png_ptr->row_info),
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600778 png_ptr->row_buf + 1, png_ptr->pass);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500779 /* This should always get caught above, but still ... */
Guy Schalnat0d580581995-07-20 02:43:20 -0500780 if (!(png_ptr->row_info.width))
781 {
782 png_write_finish_row(png_ptr);
783 return;
784 }
785 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600786#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500787
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500788 /* Handle other transformations */
Guy Schalnat0d580581995-07-20 02:43:20 -0500789 if (png_ptr->transformations)
790 png_do_write_transformations(png_ptr);
791
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600792#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600793 /* Write filter_method 64 (intrapixel differencing) only if
794 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
795 * 2. Libpng did not write a PNG signature (this filter_method is only
796 * used in PNG datastreams that are embedded in MNG datastreams) and
797 * 3. The application called png_permit_mng_features with a mask that
798 * included PNG_FLAG_MNG_FILTER_64 and
799 * 4. The filter_method is 64 and
800 * 5. The color_type is RGB or RGBA
801 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500802 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600803 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
804 {
805 /* Intrapixel differencing */
806 png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
807 }
808#endif
809
Andreas Dilger47a0c421997-05-16 02:46:07 -0500810 /* Find a filter if necessary, filter the row and write it out. */
Guy Schalnate5a37791996-06-05 15:50:50 -0500811 png_write_find_filter(png_ptr, &(png_ptr->row_info));
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600812
813 if (png_ptr->write_row_fn != NULL)
814 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500815}
816
Guy Schalnat0f716451995-11-28 11:22:13 -0600817#if defined(PNG_WRITE_FLUSH_SUPPORTED)
818/* Set the automatic flush interval or 0 to turn flushing off */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500819void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600820png_set_flush(png_structp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600821{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500822 png_debug(1, "in png_set_flush");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600823 if (png_ptr == NULL)
824 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600825 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600826}
827
828/* flush the current output buffers now */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500829void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600830png_write_flush(png_structp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600831{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600832 int wrote_IDAT;
Guy Schalnat0f716451995-11-28 11:22:13 -0600833
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500834 png_debug(1, "in png_write_flush");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600835 if (png_ptr == NULL)
836 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500837 /* We have already written out all of the data */
838 if (png_ptr->row_number >= png_ptr->num_rows)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500839 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600840
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600841 do
842 {
843 int ret;
Guy Schalnat0f716451995-11-28 11:22:13 -0600844
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600845 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600846 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600847 wrote_IDAT = 0;
Guy Schalnat0f716451995-11-28 11:22:13 -0600848
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600849 /* check for compression errors */
850 if (ret != Z_OK)
851 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500852 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600853 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600854 else
855 png_error(png_ptr, "zlib error");
856 }
Guy Schalnat0f716451995-11-28 11:22:13 -0600857
Andreas Dilger47a0c421997-05-16 02:46:07 -0500858 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600859 {
860 /* write the IDAT and reset the zlib output buffer */
861 png_write_IDAT(png_ptr, png_ptr->zbuf,
862 png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600863 png_ptr->zstream.next_out = png_ptr->zbuf;
864 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600865 wrote_IDAT = 1;
866 }
867 } while(wrote_IDAT == 1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600868
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600869 /* If there is any data left to be output, write it into a new IDAT */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600870 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600871 {
872 /* write the IDAT and reset the zlib output buffer */
873 png_write_IDAT(png_ptr, png_ptr->zbuf,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600874 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
875 png_ptr->zstream.next_out = png_ptr->zbuf;
876 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600877 }
878 png_ptr->flush_rows = 0;
879 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600880}
881#endif /* PNG_WRITE_FLUSH_SUPPORTED */
882
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500883/* Free all memory used by the write */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500884void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600885png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500886{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600887 png_structp png_ptr = NULL;
888 png_infop info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500889#ifdef PNG_USER_MEM_SUPPORTED
890 png_free_ptr free_fn = NULL;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500891 png_voidp mem_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500892#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600893
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500894 png_debug(1, "in png_destroy_write_struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500895 if (png_ptr_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500896 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600897 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500898#ifdef PNG_USER_MEM_SUPPORTED
899 free_fn = png_ptr->free_fn;
Glenn Randers-Pehrsonfcbd7872002-04-07 16:35:38 -0500900 mem_ptr = png_ptr->mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500901#endif
902 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600903
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500904#ifdef PNG_USER_MEM_SUPPORTED
905 if (png_ptr != NULL)
906 {
907 free_fn = png_ptr->free_fn;
908 mem_ptr = png_ptr->mem_ptr;
909 }
910#endif
911
Andreas Dilger47a0c421997-05-16 02:46:07 -0500912 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600913 info_ptr = *info_ptr_ptr;
914
Andreas Dilger47a0c421997-05-16 02:46:07 -0500915 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500916 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500917 if (png_ptr != NULL)
918 {
919 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600920
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500921#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500922 if (png_ptr->num_chunk_list)
923 {
924 png_free(png_ptr, png_ptr->chunk_list);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500925 png_ptr->num_chunk_list = 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500926 }
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500927#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500928 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600929
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500930#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500931 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
932 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500933#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600934 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500935#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500936 *info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500937 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600938
Andreas Dilger47a0c421997-05-16 02:46:07 -0500939 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500940 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600941 png_write_destroy(png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500942#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500943 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
944 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500945#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600946 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500947#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500948 *png_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500949 }
950}
951
952
Andreas Dilger47a0c421997-05-16 02:46:07 -0500953/* Free any memory used in png_ptr struct (old method) */
Glenn Randers-Pehrsonf64a06f2001-04-11 07:38:00 -0500954void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600955png_write_destroy(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500956{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600957#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600958 jmp_buf tmp_jmp; /* save jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600959#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500960 png_error_ptr error_fn;
961 png_error_ptr warning_fn;
962 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500963#ifdef PNG_USER_MEM_SUPPORTED
964 png_free_ptr free_fn;
965#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500966
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500967 png_debug(1, "in png_write_destroy");
Guy Schalnat0d580581995-07-20 02:43:20 -0500968 /* free any memory zlib uses */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600969 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -0500970
Guy Schalnat0d580581995-07-20 02:43:20 -0500971 /* free our memory. png_free checks NULL for us. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600972 png_free(png_ptr, png_ptr->zbuf);
973 png_free(png_ptr, png_ptr->row_buf);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500974#ifndef PNG_NO_WRITE_FILTER
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600975 png_free(png_ptr, png_ptr->prev_row);
976 png_free(png_ptr, png_ptr->sub_row);
977 png_free(png_ptr, png_ptr->up_row);
978 png_free(png_ptr, png_ptr->avg_row);
979 png_free(png_ptr, png_ptr->paeth_row);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500980#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600981
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600982#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600983 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600984#endif
985
Andreas Dilger47a0c421997-05-16 02:46:07 -0500986#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
987 png_free(png_ptr, png_ptr->prev_filters);
988 png_free(png_ptr, png_ptr->filter_weights);
989 png_free(png_ptr, png_ptr->inv_filter_weights);
990 png_free(png_ptr, png_ptr->filter_costs);
991 png_free(png_ptr, png_ptr->inv_filter_costs);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600992#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500993
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600994#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500995 /* Reset structure */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500996 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600997#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500998
999 error_fn = png_ptr->error_fn;
1000 warning_fn = png_ptr->warning_fn;
1001 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001002#ifdef PNG_USER_MEM_SUPPORTED
1003 free_fn = png_ptr->free_fn;
1004#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001005
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001006 png_memset(png_ptr, 0, png_sizeof(png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -05001007
1008 png_ptr->error_fn = error_fn;
1009 png_ptr->warning_fn = warning_fn;
1010 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001011#ifdef PNG_USER_MEM_SUPPORTED
1012 png_ptr->free_fn = free_fn;
1013#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001014
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001015#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001016 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001017#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001018}
Guy Schalnate5a37791996-06-05 15:50:50 -05001019
Andreas Dilger47a0c421997-05-16 02:46:07 -05001020/* Allow the application to select one or more row filters to use. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001021void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -05001022png_set_filter(png_structp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001023{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001024 png_debug(1, "in png_set_filter");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001025 if (png_ptr == NULL)
1026 return;
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -06001027#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001028 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -06001029 (method == PNG_INTRAPIXEL_DIFFERENCING))
1030 method = PNG_FILTER_TYPE_BASE;
1031#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001032 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001033 {
1034 switch (filters & (PNG_ALL_FILTERS | 0x07))
1035 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001036#ifndef PNG_NO_WRITE_FILTER
Guy Schalnate5a37791996-06-05 15:50:50 -05001037 case 5:
1038 case 6:
Andreas Dilger47a0c421997-05-16 02:46:07 -05001039 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001040#endif /* PNG_NO_WRITE_FILTER */
1041 case PNG_FILTER_VALUE_NONE:
1042 png_ptr->do_filter=PNG_FILTER_NONE; break;
1043#ifndef PNG_NO_WRITE_FILTER
1044 case PNG_FILTER_VALUE_SUB:
1045 png_ptr->do_filter=PNG_FILTER_SUB; break;
1046 case PNG_FILTER_VALUE_UP:
1047 png_ptr->do_filter=PNG_FILTER_UP; break;
1048 case PNG_FILTER_VALUE_AVG:
1049 png_ptr->do_filter=PNG_FILTER_AVG; break;
1050 case PNG_FILTER_VALUE_PAETH:
1051 png_ptr->do_filter=PNG_FILTER_PAETH; break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001052 default: png_ptr->do_filter = (png_byte)filters; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001053#else
1054 default: png_warning(png_ptr, "Unknown row filter for method 0");
1055#endif /* PNG_NO_WRITE_FILTER */
Guy Schalnate5a37791996-06-05 15:50:50 -05001056 }
1057
Andreas Dilger47a0c421997-05-16 02:46:07 -05001058 /* If we have allocated the row_buf, this means we have already started
1059 * with the image and we should have allocated all of the filter buffers
1060 * that have been selected. If prev_row isn't already allocated, then
1061 * it is too late to start using the filters that need it, since we
1062 * will be missing the data in the previous row. If an application
1063 * wants to start and stop using particular filters during compression,
1064 * it should start out with all of the filters, and then add and
1065 * remove them after the start of compression.
Guy Schalnate5a37791996-06-05 15:50:50 -05001066 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001067 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001068 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001069#ifndef PNG_NO_WRITE_FILTER
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001070 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001071 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001072 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001073 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001074 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001075 }
1076
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001077 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001078 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001079 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001080 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001081 png_warning(png_ptr, "Can't add Up filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -05001082 png_ptr->do_filter &= ~PNG_FILTER_UP;
1083 }
1084 else
1085 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001086 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001087 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001088 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001089 }
1090 }
1091
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001092 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001093 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001094 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001095 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001096 png_warning(png_ptr, "Can't add Average filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -05001097 png_ptr->do_filter &= ~PNG_FILTER_AVG;
1098 }
1099 else
1100 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001101 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001102 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001103 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001104 }
1105 }
1106
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001107 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
Andreas Dilger47a0c421997-05-16 02:46:07 -05001108 png_ptr->paeth_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001109 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001110 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001111 {
1112 png_warning(png_ptr, "Can't add Paeth filter after starting");
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001113 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
Guy Schalnate5a37791996-06-05 15:50:50 -05001114 }
1115 else
1116 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001117 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001118 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001119 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001120 }
1121 }
1122
1123 if (png_ptr->do_filter == PNG_NO_FILTERS)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001124#endif /* PNG_NO_WRITE_FILTER */
Guy Schalnate5a37791996-06-05 15:50:50 -05001125 png_ptr->do_filter = PNG_FILTER_NONE;
1126 }
1127 }
1128 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05001129 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001130}
1131
Andreas Dilger47a0c421997-05-16 02:46:07 -05001132/* This allows us to influence the way in which libpng chooses the "best"
1133 * filter for the current scanline. While the "minimum-sum-of-absolute-
1134 * differences metric is relatively fast and effective, there is some
1135 * question as to whether it can be improved upon by trying to keep the
1136 * filtered data going to zlib more consistent, hopefully resulting in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001137 * better compression.
1138 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001139#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001140void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -05001141png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
1142 int num_weights, png_doublep filter_weights,
1143 png_doublep filter_costs)
1144{
1145 int i;
1146
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001147 png_debug(1, "in png_set_filter_heuristics");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001148 if (png_ptr == NULL)
1149 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001150 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
1151 {
1152 png_warning(png_ptr, "Unknown filter heuristic method");
1153 return;
1154 }
1155
1156 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
1157 {
1158 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1159 }
1160
1161 if (num_weights < 0 || filter_weights == NULL ||
1162 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
1163 {
1164 num_weights = 0;
1165 }
1166
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001167 png_ptr->num_prev_filters = (png_byte)num_weights;
1168 png_ptr->heuristic_method = (png_byte)heuristic_method;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001169
1170 if (num_weights > 0)
1171 {
1172 if (png_ptr->prev_filters == NULL)
1173 {
1174 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001175 (png_uint_32)(png_sizeof(png_byte) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001176
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001177 /* To make sure that the weighting starts out fairly */
1178 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001179 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001180 png_ptr->prev_filters[i] = 255;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001181 }
1182 }
1183
1184 if (png_ptr->filter_weights == NULL)
1185 {
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001186 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001187 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001188
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001189 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001190 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001191 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001192 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001193 png_ptr->inv_filter_weights[i] =
1194 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001195 }
1196 }
1197
1198 for (i = 0; i < num_weights; i++)
1199 {
1200 if (filter_weights[i] < 0.0)
1201 {
1202 png_ptr->inv_filter_weights[i] =
1203 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1204 }
1205 else
1206 {
1207 png_ptr->inv_filter_weights[i] =
1208 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
1209 png_ptr->filter_weights[i] =
1210 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
1211 }
1212 }
1213 }
1214
1215 /* If, in the future, there are other filter methods, this would
1216 * need to be based on png_ptr->filter.
1217 */
1218 if (png_ptr->filter_costs == NULL)
1219 {
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001220 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001221 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001222
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001223 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001224 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001225
1226 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1227 {
1228 png_ptr->inv_filter_costs[i] =
1229 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1230 }
1231 }
1232
1233 /* Here is where we set the relative costs of the different filters. We
1234 * should take the desired compression level into account when setting
1235 * the costs, so that Paeth, for instance, has a high relative cost at low
1236 * compression levels, while it has a lower relative cost at higher
1237 * compression settings. The filter types are in order of increasing
1238 * relative cost, so it would be possible to do this with an algorithm.
1239 */
1240 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1241 {
1242 if (filter_costs == NULL || filter_costs[i] < 0.0)
1243 {
1244 png_ptr->inv_filter_costs[i] =
1245 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1246 }
1247 else if (filter_costs[i] >= 1.0)
1248 {
1249 png_ptr->inv_filter_costs[i] =
1250 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
1251 png_ptr->filter_costs[i] =
1252 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
1253 }
1254 }
1255}
1256#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1257
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001258void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001259png_set_compression_level(png_structp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001260{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001261 png_debug(1, "in png_set_compression_level");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001262 if (png_ptr == NULL)
1263 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001264 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001265 png_ptr->zlib_level = level;
1266}
1267
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001268void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001269png_set_compression_mem_level(png_structp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001270{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001271 png_debug(1, "in png_set_compression_mem_level");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001272 if (png_ptr == NULL)
1273 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001274 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001275 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001276}
1277
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001278void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001279png_set_compression_strategy(png_structp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001280{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001281 png_debug(1, "in png_set_compression_strategy");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001282 if (png_ptr == NULL)
1283 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001284 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001285 png_ptr->zlib_strategy = strategy;
1286}
1287
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001288void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001289png_set_compression_window_bits(png_structp 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;
Guy Schalnate5a37791996-06-05 15:50:50 -05001293 if (window_bits > 15)
1294 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001295 else if (window_bits < 8)
1296 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001297#ifndef WBITS_8_OK
1298 /* avoid libpng bug with 256-byte windows */
1299 if (window_bits == 8)
1300 {
1301 png_warning(png_ptr, "Compression window is being reset to 512");
1302 window_bits=9;
1303 }
1304#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001305 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001306 png_ptr->zlib_window_bits = window_bits;
1307}
1308
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001309void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001310png_set_compression_method(png_structp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001311{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001312 png_debug(1, "in png_set_compression_method");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001313 if (png_ptr == NULL)
1314 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001315 if (method != 8)
1316 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1317 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001318 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -05001319}
1320
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001321void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001322png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1323{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001324 if (png_ptr == NULL)
1325 return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001326 png_ptr->write_row_fn = write_row_fn;
1327}
1328
1329#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001330void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001331png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
1332 write_user_transform_fn)
1333{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001334 png_debug(1, "in png_set_write_user_transform_fn");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001335 if (png_ptr == NULL)
1336 return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001337 png_ptr->transformations |= PNG_USER_TRANSFORM;
1338 png_ptr->write_user_transform_fn = write_user_transform_fn;
1339}
1340#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001341
1342
1343#if defined(PNG_INFO_IMAGE_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001344void PNGAPI
1345png_write_png(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001346 int transforms, voidp params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001347{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001348 if (png_ptr == NULL || info_ptr == NULL)
1349 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001350#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001351 /* Invert the alpha channel from opacity to transparency */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001352 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001353 png_set_invert_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001354#endif
1355
1356 /* Write the file header information. */
1357 png_write_info(png_ptr, info_ptr);
1358
1359 /* ------ these transformations don't touch the info structure ------- */
1360
1361#if defined(PNG_WRITE_INVERT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001362 /* Invert monochrome pixels */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001363 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001364 png_set_invert_mono(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001365#endif
1366
1367#if defined(PNG_WRITE_SHIFT_SUPPORTED)
1368 /* Shift the pixels up to a legal bit depth and fill in
1369 * as appropriate to correctly scale the image.
1370 */
1371 if ((transforms & PNG_TRANSFORM_SHIFT)
1372 && (info_ptr->valid & PNG_INFO_sBIT))
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001373 png_set_shift(png_ptr, &info_ptr->sig_bit);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001374#endif
1375
1376#if defined(PNG_WRITE_PACK_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001377 /* Pack pixels into bytes */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001378 if (transforms & PNG_TRANSFORM_PACKING)
1379 png_set_packing(png_ptr);
1380#endif
1381
1382#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001383 /* Swap location of alpha bytes from ARGB to RGBA */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001384 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001385 png_set_swap_alpha(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001386#endif
1387
1388#if defined(PNG_WRITE_FILLER_SUPPORTED)
Glenn Randers-Pehrson4a82d692008-12-15 16:25:05 -06001389 /* Pack XRGB/RGBX/ARGB/RGBA into * RGB (4 channels -> 3 channels) */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001390 if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001391 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001392 else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001393 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001394#endif
1395
1396#if defined(PNG_WRITE_BGR_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001397 /* Flip BGR pixels to RGB */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001398 if (transforms & PNG_TRANSFORM_BGR)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001399 png_set_bgr(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001400#endif
1401
1402#if defined(PNG_WRITE_SWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001403 /* Swap bytes of 16-bit files to most significant byte first */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001404 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001405 png_set_swap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001406#endif
1407
1408#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001409 /* Swap bits of 1, 2, 4 bit packed pixel formats */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001410 if (transforms & PNG_TRANSFORM_PACKSWAP)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001411 png_set_packswap(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001412#endif
1413
1414 /* ----------------------- end of transformations ------------------- */
1415
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001416 /* Write the bits */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001417 if (info_ptr->valid & PNG_INFO_IDAT)
1418 png_write_image(png_ptr, info_ptr->row_pointers);
1419
1420 /* It is REQUIRED to call this to finish writing the rest of the file */
1421 png_write_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001422
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001423 transforms = transforms; /* quiet compiler warnings */
1424 params = params;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001425}
1426#endif
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -06001427#endif /* PNG_WRITE_SUPPORTED */