blob: ffc994650889bec75e010e50e49c389795de5302 [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-Pehrson79134c62009-02-14 10:32:18 -06004 * Last changed in libpng 1.4.0 [February 14, 2009]
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06006 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05007 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 */
Guy Schalnat0d580581995-07-20 02:43:20 -050010
11/* get internal access to png.h */
Guy Schalnat0d580581995-07-20 02:43:20 -050012#include "png.h"
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -060013#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050014#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050015
Andreas Dilger47a0c421997-05-16 02:46:07 -050016/* Writes all the PNG information. This is the suggested way to use the
17 * library. If you have a new chunk to add, make a function to write it,
18 * and put it in the correct location here. If you want the chunk written
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050019 * after the image data, put it in png_write_end(). I strongly encourage
Andreas Dilger47a0c421997-05-16 02:46:07 -050020 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
21 * the chunk, as that will keep the code from breaking if you want to just
22 * write a plain PNG file. If you have long comments, I suggest writing
23 * them in png_write_end(), and compressing them.
24 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050025void PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060026png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050027{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050028 png_debug(1, "in png_write_info_before_PLTE");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -060029 if (png_ptr == NULL || info_ptr == NULL)
30 return;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060031 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
32 {
Guy Schalnat0d580581995-07-20 02:43:20 -050033 png_write_sig(png_ptr); /* write PNG signature */
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060034#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050035 if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&(png_ptr->mng_features_permitted))
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060036 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050037 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060038 png_ptr->mng_features_permitted=0;
39 }
40#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050041 /* write IHDR information. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060042 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
43 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060044 info_ptr->filter_type,
45#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
46 info_ptr->interlace_type);
47#else
48 0);
49#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050050 /* the rest of these check to see if the valid field has the appropriate
51 flag set, and if it does, writes the chunk. */
Guy Schalnat51f0eb41995-09-26 05:22:39 -050052#if defined(PNG_WRITE_gAMA_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060053 if (info_ptr->valid & PNG_INFO_gAMA)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060054 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060055# ifdef PNG_FLOATING_POINT_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060056 png_write_gAMA(png_ptr, info_ptr->gamma);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060057#else
58#ifdef PNG_FIXED_POINT_SUPPORTED
59 png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060060# endif
61#endif
62 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -050063#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060064#if defined(PNG_WRITE_sRGB_SUPPORTED)
65 if (info_ptr->valid & PNG_INFO_sRGB)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060066 png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060067#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060068#if defined(PNG_WRITE_iCCP_SUPPORTED)
69 if (info_ptr->valid & PNG_INFO_iCCP)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -060070 png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060071 info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
72#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050073#if defined(PNG_WRITE_sBIT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060074 if (info_ptr->valid & PNG_INFO_sBIT)
75 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050076#endif
77#if defined(PNG_WRITE_cHRM_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060078 if (info_ptr->valid & PNG_INFO_cHRM)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060079 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060080#ifdef PNG_FLOATING_POINT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050081 png_write_cHRM(png_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060082 info_ptr->x_white, info_ptr->y_white,
83 info_ptr->x_red, info_ptr->y_red,
84 info_ptr->x_green, info_ptr->y_green,
85 info_ptr->x_blue, info_ptr->y_blue);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060086#else
87# ifdef PNG_FIXED_POINT_SUPPORTED
88 png_write_cHRM_fixed(png_ptr,
89 info_ptr->int_x_white, info_ptr->int_y_white,
90 info_ptr->int_x_red, info_ptr->int_y_red,
91 info_ptr->int_x_green, info_ptr->int_y_green,
92 info_ptr->int_x_blue, info_ptr->int_y_blue);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060093# endif
94#endif
95 }
96#endif
97#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
98 if (info_ptr->unknown_chunks_num)
99 {
100 png_unknown_chunk *up;
101
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500102 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600103
104 for (up = info_ptr->unknown_chunks;
105 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
106 up++)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600107 {
108 int keep=png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500109 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500110 up->location && !(up->location & PNG_HAVE_PLTE) &&
111 !(up->location & PNG_HAVE_IDAT) &&
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500112 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600113 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
114 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500115 if (up->size == 0)
116 png_warning(png_ptr, "Writing zero-length unknown chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600117 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600118 }
119 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600120 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500121#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600122 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
123 }
124}
125
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500126void PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600127png_write_info(png_structp png_ptr, png_infop info_ptr)
128{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600129#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600130 int i;
131#endif
132
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500133 png_debug(1, "in png_write_info");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600134
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600135 if (png_ptr == NULL || info_ptr == NULL)
136 return;
137
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600138 png_write_info_before_PLTE(png_ptr, info_ptr);
139
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600140 if (info_ptr->valid & PNG_INFO_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500141 png_write_PLTE(png_ptr, info_ptr->palette,
142 (png_uint_32)info_ptr->num_palette);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600143 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600144 png_error(png_ptr, "Valid palette required for paletted images");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600145
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500146#if defined(PNG_WRITE_tRNS_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600147 if (info_ptr->valid & PNG_INFO_tRNS)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600148 {
149#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
150 /* invert the alpha channel (in tRNS) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600151 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600152 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
153 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600154 int j;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600155 for (j=0; j<(int)info_ptr->num_trans; j++)
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500156 info_ptr->trans[j] = (png_byte)(255 - info_ptr->trans[j]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600157 }
158#endif
Glenn Randers-Pehrson56f63962008-10-06 10:16:17 -0500159 png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_color),
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600160 info_ptr->num_trans, info_ptr->color_type);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600161 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500162#endif
163#if defined(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
167#if defined(PNG_WRITE_hIST_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600168 if (info_ptr->valid & PNG_INFO_hIST)
169 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500170#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500171#if defined(PNG_WRITE_oFFs_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600172 if (info_ptr->valid & PNG_INFO_oFFs)
173 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
174 info_ptr->offset_unit_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500175#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500176#if defined(PNG_WRITE_pCAL_SUPPORTED)
177 if (info_ptr->valid & PNG_INFO_pCAL)
178 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
179 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
180 info_ptr->pcal_units, info_ptr->pcal_params);
181#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600182#if defined(PNG_WRITE_sCAL_SUPPORTED)
183 if (info_ptr->valid & PNG_INFO_sCAL)
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600184#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600185 png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,
186 info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);
187#else
188#ifdef PNG_FIXED_POINT_SUPPORTED
189 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600190 info_ptr->scal_s_width, info_ptr->scal_s_height);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600191#else
192 png_warning(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500193 "png_write_sCAL not supported; sCAL chunk not written");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600194#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600195#endif
196#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500197#if defined(PNG_WRITE_pHYs_SUPPORTED)
198 if (info_ptr->valid & PNG_INFO_pHYs)
199 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
200 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
201#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500202#if defined(PNG_WRITE_tIME_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600203 if (info_ptr->valid & PNG_INFO_tIME)
Guy Schalnate5a37791996-06-05 15:50:50 -0500204 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600205 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600206 png_ptr->mode |= PNG_WROTE_tIME;
Guy Schalnate5a37791996-06-05 15:50:50 -0500207 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500208#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600209#if defined(PNG_WRITE_sPLT_SUPPORTED)
210 if (info_ptr->valid & PNG_INFO_sPLT)
211 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
212 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
213#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600214#if defined(PNG_WRITE_TEXT_SUPPORTED)
Guy Schalnate5a37791996-06-05 15:50:50 -0500215 /* Check to see if we need to write text chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500216 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500217 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500218 png_debug2(2, "Writing header text chunk %d, type %d", i,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500219 info_ptr->text[i].compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600220 /* an internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600221 if (info_ptr->text[i].compression > 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600222 {
223#if defined(PNG_WRITE_iTXt_SUPPORTED)
224 /* write international chunk */
225 png_write_iTXt(png_ptr,
226 info_ptr->text[i].compression,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600227 info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600228 info_ptr->text[i].lang,
229 info_ptr->text[i].lang_key,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600230 info_ptr->text[i].text);
231#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600232 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600233#endif
234 /* Mark this chunk as written */
235 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
236 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500237 /* If we want a compressed text chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600238 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500239 {
240#if defined(PNG_WRITE_zTXt_SUPPORTED)
241 /* write compressed chunk */
242 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600243 info_ptr->text[i].text, 0,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500244 info_ptr->text[i].compression);
245#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600246 png_warning(png_ptr, "Unable to write compressed text");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500247#endif
248 /* Mark this chunk as written */
249 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
250 }
251 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
252 {
253#if defined(PNG_WRITE_tEXt_SUPPORTED)
254 /* write uncompressed chunk */
255 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600256 info_ptr->text[i].text,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600257 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500258#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600259 png_warning(png_ptr, "Unable to write uncompressed text");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500260#endif
261 /* Mark this chunk as written */
262 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
263 }
264 }
265#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600266#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
267 if (info_ptr->unknown_chunks_num)
268 {
269 png_unknown_chunk *up;
270
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500271 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600272
273 for (up = info_ptr->unknown_chunks;
274 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
275 up++)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600276 {
277 int keep=png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500278 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600279 up->location && (up->location & PNG_HAVE_PLTE) &&
280 !(up->location & PNG_HAVE_IDAT) &&
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500281 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600282 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
283 {
284 png_write_chunk(png_ptr, up->name, up->data, up->size);
285 }
286 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600287 }
288#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500289}
Guy Schalnat0d580581995-07-20 02:43:20 -0500290
Andreas Dilger47a0c421997-05-16 02:46:07 -0500291/* Writes the end of the PNG file. If you don't want to write comments or
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600292 * time information, you can pass NULL for info. If you already wrote these
293 * in png_write_info(), do not write them again here. If you have long
294 * comments, I suggest writing them here, and compressing them.
295 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500296void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500297png_write_end(png_structp png_ptr, png_infop info_ptr)
298{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500299 png_debug(1, "in png_write_end");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600300 if (png_ptr == NULL)
301 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500302 if (!(png_ptr->mode & PNG_HAVE_IDAT))
303 png_error(png_ptr, "No IDATs written into file");
304
305 /* see if user wants us to write information chunks */
306 if (info_ptr != NULL)
307 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600308#if defined(PNG_WRITE_TEXT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500309 int i; /* local index variable */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600310#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500311#if defined(PNG_WRITE_tIME_SUPPORTED)
312 /* check to see if user has supplied a time chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600313 if ((info_ptr->valid & PNG_INFO_tIME) &&
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600314 !(png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500315 png_write_tIME(png_ptr, &(info_ptr->mod_time));
316#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600317#if defined(PNG_WRITE_TEXT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500318 /* loop through comment chunks */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600319 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500320 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500321 png_debug2(2, "Writing trailer text chunk %d, type %d", i,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500322 info_ptr->text[i].compression);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600323 /* an internationalized chunk? */
324 if (info_ptr->text[i].compression > 0)
325 {
326#if defined(PNG_WRITE_iTXt_SUPPORTED)
327 /* write international chunk */
328 png_write_iTXt(png_ptr,
329 info_ptr->text[i].compression,
330 info_ptr->text[i].key,
331 info_ptr->text[i].lang,
332 info_ptr->text[i].lang_key,
333 info_ptr->text[i].text);
334#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600335 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600336#endif
337 /* Mark this chunk as written */
338 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
339 }
340 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
Guy Schalnat0d580581995-07-20 02:43:20 -0500341 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500342#if defined(PNG_WRITE_zTXt_SUPPORTED)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600343 /* write compressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600344 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600345 info_ptr->text[i].text, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600346 info_ptr->text[i].compression);
Guy Schalnate5a37791996-06-05 15:50:50 -0500347#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600348 png_warning(png_ptr, "Unable to write compressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500349#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500350 /* Mark this chunk as written */
351 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500352 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500353 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500354 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500355#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500356 /* write uncompressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600357 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600358 info_ptr->text[i].text, 0);
Guy Schalnate5a37791996-06-05 15:50:50 -0500359#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600360 png_warning(png_ptr, "Unable to write uncompressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500361#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500362
Andreas Dilger47a0c421997-05-16 02:46:07 -0500363 /* Mark this chunk as written */
364 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500365 }
366 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600367#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600368#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
369 if (info_ptr->unknown_chunks_num)
370 {
371 png_unknown_chunk *up;
372
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500373 png_debug(5, "writing extra chunks");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600374
375 for (up = info_ptr->unknown_chunks;
376 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
377 up++)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600378 {
379 int keep=png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500380 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600381 up->location && (up->location & PNG_AFTER_IDAT) &&
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500382 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600383 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
384 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600385 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600386 }
387 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600388 }
389#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500390 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500391
392 png_ptr->mode |= PNG_AFTER_IDAT;
393
Andreas Dilger47a0c421997-05-16 02:46:07 -0500394 /* write end of PNG file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500395 png_write_IEND(png_ptr);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500396 /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
397 * and restored again in libpng-1.2.30, may cause some applications that
398 * do not set png_ptr->output_flush_fn to crash. If your application
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600399 * experiences a problem, please report the event to
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500400 * png-mng-implement at lists.sf.net .
401 */
Glenn Randers-Pehrsondbed41f2008-08-19 18:20:52 -0500402#if defined(PNG_WRITE_FLUSH_SUPPORTED)
Glenn Randers-Pehrson32fc5ce2000-07-24 06:34:14 -0500403 png_flush(png_ptr);
Glenn Randers-Pehrsondbed41f2008-08-19 18:20:52 -0500404#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500405}
406
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500407#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500408/* "time.h" functions are not supported on WindowsCE */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500409void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600410png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500411{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500412 png_debug(1, "in png_convert_from_struct_tm");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600413 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
414 ptime->month = (png_byte)(ttime->tm_mon + 1);
415 ptime->day = (png_byte)ttime->tm_mday;
416 ptime->hour = (png_byte)ttime->tm_hour;
417 ptime->minute = (png_byte)ttime->tm_min;
418 ptime->second = (png_byte)ttime->tm_sec;
Guy Schalnat0d580581995-07-20 02:43:20 -0500419}
420
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500421void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600422png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500423{
424 struct tm *tbuf;
425
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500426 png_debug(1, "in png_convert_from_time_t");
Guy Schalnat0d580581995-07-20 02:43:20 -0500427 tbuf = gmtime(&ttime);
428 png_convert_from_struct_tm(ptime, tbuf);
429}
Guy Schalnat6d764711995-12-19 03:22:19 -0600430#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500431
Andreas Dilger47a0c421997-05-16 02:46:07 -0500432/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500433png_structp PNGAPI
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500434png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600435 png_error_ptr error_fn, png_error_ptr warn_fn)
Guy Schalnat0d580581995-07-20 02:43:20 -0500436{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500437#ifdef PNG_USER_MEM_SUPPORTED
438 return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500439 warn_fn, NULL, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500440}
441
442/* Alternate initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500443png_structp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500444png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
445 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
446 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
447{
448#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600449#ifdef PNG_SETJMP_SUPPORTED
450 volatile
451#endif
452 png_structp png_ptr;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600453#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600454#ifdef USE_FAR_KEYWORD
455 jmp_buf jmpbuf;
456#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600457#endif
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500458 int i;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500459 png_debug(1, "in png_create_write_struct");
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500460#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600461 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500462 (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500463#else
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600464 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500465#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600466 if (png_ptr == NULL)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500467 return (NULL);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600468
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500469 /* added at libpng-1.2.6 */
470#ifdef PNG_SET_USER_LIMITS_SUPPORTED
471 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
472 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
473#endif
474
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600475#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600476#ifdef USE_FAR_KEYWORD
477 if (setjmp(jmpbuf))
478#else
Guy Schalnate5a37791996-06-05 15:50:50 -0500479 if (setjmp(png_ptr->jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600480#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500481 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600482 png_free(png_ptr, png_ptr->zbuf);
Guy Schalnate5a37791996-06-05 15:50:50 -0500483 png_destroy_struct(png_ptr);
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500484 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500485 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600486#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500487 png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600488#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600489#endif
490
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500491#ifdef PNG_USER_MEM_SUPPORTED
492 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
493#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600494 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnate5a37791996-06-05 15:50:50 -0500495
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500496 if (user_png_ver)
Guy Schalnate5a37791996-06-05 15:50:50 -0500497 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500498 i=0;
499 do
500 {
501 if (user_png_ver[i] != png_libpng_ver[i])
502 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
503 } while (png_libpng_ver[i++]);
504 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500505
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500506 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500507 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500508 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
509 * we must recompile any applications that use any older library version.
510 * For versions after libpng 1.0, we will be compatible, so we need
511 * only check the first digit.
512 */
513 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500514 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500515 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
516 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500517#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500518 char msg[80];
519 if (user_png_ver)
520 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500521 png_snprintf(msg, 80,
522 "Application was compiled with png.h from libpng-%.20s",
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500523 user_png_ver);
524 png_warning(png_ptr, msg);
525 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500526 png_snprintf(msg, 80,
527 "Application is running with png.c from libpng-%.20s",
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500528 png_libpng_ver);
529 png_warning(png_ptr, msg);
530#endif
531#ifdef PNG_ERROR_NUMBERS_SUPPORTED
532 png_ptr->flags=0;
533#endif
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500534 png_error(png_ptr,
535 "Incompatible libpng version in application and library");
536 }
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500537 }
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500538
Guy Schalnat0d580581995-07-20 02:43:20 -0500539 /* initialize zbuf - compression buffer */
540 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500541 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
542 (png_uint_32)png_ptr->zbuf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500543
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500544 png_set_write_fn(png_ptr, NULL, NULL, NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500545
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600546#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
547 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500548 1, NULL, NULL);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600549#endif
550
Glenn Randers-Pehrson03008a02002-04-27 10:11:25 -0500551#ifdef PNG_SETJMP_SUPPORTED
552/* Applications that neglect to set up their own setjmp() and then encounter
553 a png_error() will longjmp here. Since the jmpbuf is then meaningless we
554 abort instead of returning. */
555#ifdef USE_FAR_KEYWORD
556 if (setjmp(jmpbuf))
557 PNG_ABORT();
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500558 png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrson03008a02002-04-27 10:11:25 -0500559#else
560 if (setjmp(png_ptr->jmpbuf))
561 PNG_ABORT();
562#endif
563#endif
564 return (png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500565}
566
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500567/* Initialize png_ptr structure, and allocate any memory needed */
568
569
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500570void PNGAPI
571png_write_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
572 png_size_t png_struct_size)
573{
574 png_structp png_ptr=*ptr_ptr;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600575#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500576 jmp_buf tmp_jmp; /* to save current jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600577#endif
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600578
Glenn Randers-Pehrson4deeb792006-03-22 16:21:59 -0600579 int i = 0;
580
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600581 if (png_ptr == NULL)
582 return;
583
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500584 do
585 {
586 if (user_png_ver[i] != png_libpng_ver[i])
587 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500588#ifdef PNG_LEGACY_SUPPORTED
589 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
590#else
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500591 png_ptr->warning_fn=NULL;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500592 png_warning(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500593 "Application uses deprecated png_write_init() and should be recompiled.");
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500594 break;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500595#endif
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500596 }
597 } while (png_libpng_ver[i++]);
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500598
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500599 png_debug(1, "in png_write_init_3");
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500600
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600601#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500602 /* save jump buffer and error functions */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500603 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600604#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500605
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500606 if (png_sizeof(png_struct) > png_struct_size)
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500607 {
608 png_destroy_struct(png_ptr);
609 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
610 *ptr_ptr = png_ptr;
611 }
612
Guy Schalnate5a37791996-06-05 15:50:50 -0500613 /* reset all variables to 0 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500614 png_memset(png_ptr, 0, png_sizeof(png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -0500615
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500616 /* added at libpng-1.2.6 */
617#ifdef PNG_SET_USER_LIMITS_SUPPORTED
618 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
619 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
620#endif
621
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600622#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500623 /* restore jump buffer */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500624 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600625#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500626
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500627 png_set_write_fn(png_ptr, NULL, NULL, NULL);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500628
Guy Schalnate5a37791996-06-05 15:50:50 -0500629 /* initialize zbuf - compression buffer */
630 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500631 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
632 (png_uint_32)png_ptr->zbuf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500633
634#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
635 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500636 1, NULL, NULL);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500637#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500638}
639
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600640/* Write a few rows of image data. If the image is interlaced,
641 * either you will have to write the 7 sub images, or, if you
642 * have called png_set_interlace_handling(), you will have to
643 * "write" the image seven times.
644 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500645void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600646png_write_rows(png_structp png_ptr, png_bytepp row,
Guy Schalnat0d580581995-07-20 02:43:20 -0500647 png_uint_32 num_rows)
648{
649 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600650 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500651
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500652 png_debug(1, "in png_write_rows");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600653
654 if (png_ptr == NULL)
655 return;
656
Guy Schalnat0d580581995-07-20 02:43:20 -0500657 /* loop through the rows */
658 for (i = 0, rp = row; i < num_rows; i++, rp++)
659 {
660 png_write_row(png_ptr, *rp);
661 }
662}
663
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600664/* Write the image. You only need to call this function once, even
665 * if you are writing an interlaced image.
666 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500667void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600668png_write_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500669{
670 png_uint_32 i; /* row index */
671 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600672 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500673
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600674 if (png_ptr == NULL)
675 return;
676
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500677 png_debug(1, "in png_write_image");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600678#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500679 /* intialize interlace handling. If image is not interlaced,
680 this will set pass to 1 */
681 num_pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600682#else
683 num_pass = 1;
684#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500685 /* loop through passes */
686 for (pass = 0; pass < num_pass; pass++)
687 {
688 /* loop through image */
689 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
690 {
691 png_write_row(png_ptr, *rp);
692 }
693 }
694}
695
Guy Schalnate5a37791996-06-05 15:50:50 -0500696/* called by user to write a row of image data */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500697void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600698png_write_row(png_structp png_ptr, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500699{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600700 if (png_ptr == NULL)
701 return;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500702 png_debug2(1, "in png_write_row (row %ld, pass %d)",
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600703 png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600704
Guy Schalnat0d580581995-07-20 02:43:20 -0500705 /* initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600706 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500707 {
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600708 /* make sure we wrote the header info */
709 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
710 png_error(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500711 "png_write_info was never called before png_write_row");
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600712
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500713 /* check for transforms that have been set but were defined out */
714#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
715 if (png_ptr->transformations & PNG_INVERT_MONO)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500716 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500717#endif
718#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
719 if (png_ptr->transformations & PNG_FILLER)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500720 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500721#endif
722#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
723 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500724 png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500725#endif
726#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
727 if (png_ptr->transformations & PNG_PACK)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500728 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500729#endif
730#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
731 if (png_ptr->transformations & PNG_SHIFT)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500732 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500733#endif
734#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
735 if (png_ptr->transformations & PNG_BGR)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500736 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500737#endif
738#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
739 if (png_ptr->transformations & PNG_SWAP_BYTES)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500740 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500741#endif
742
Guy Schalnat0d580581995-07-20 02:43:20 -0500743 png_write_start_row(png_ptr);
744 }
745
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500746#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500747 /* if interlaced and not interested in row, return */
748 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
749 {
750 switch (png_ptr->pass)
751 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600752 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600753 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500754 {
755 png_write_finish_row(png_ptr);
756 return;
757 }
758 break;
759 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600760 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500761 {
762 png_write_finish_row(png_ptr);
763 return;
764 }
765 break;
766 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600767 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500768 {
769 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600770 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500771 }
772 break;
773 case 3:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600774 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
Guy Schalnat0d580581995-07-20 02:43:20 -0500775 {
776 png_write_finish_row(png_ptr);
777 return;
778 }
779 break;
780 case 4:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600781 if ((png_ptr->row_number & 0x03) != 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500782 {
783 png_write_finish_row(png_ptr);
784 return;
785 }
786 break;
787 case 5:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600788 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500789 {
790 png_write_finish_row(png_ptr);
791 return;
792 }
793 break;
794 case 6:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600795 if (!(png_ptr->row_number & 0x01))
Guy Schalnat0d580581995-07-20 02:43:20 -0500796 {
797 png_write_finish_row(png_ptr);
798 return;
799 }
800 break;
801 }
802 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600803#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500804
805 /* set up row info for transformations */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600806 png_ptr->row_info.color_type = png_ptr->color_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500807 png_ptr->row_info.width = png_ptr->usr_width;
808 png_ptr->row_info.channels = png_ptr->usr_channels;
809 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600810 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
811 png_ptr->row_info.channels);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600812
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500813 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
814 png_ptr->row_info.width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500815
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500816 png_debug1(3, "row_info->color_type = %d", png_ptr->row_info.color_type);
817 png_debug1(3, "row_info->width = %lu", png_ptr->row_info.width);
818 png_debug1(3, "row_info->channels = %d", png_ptr->row_info.channels);
819 png_debug1(3, "row_info->bit_depth = %d", png_ptr->row_info.bit_depth);
820 png_debug1(3, "row_info->pixel_depth = %d", png_ptr->row_info.pixel_depth);
821 png_debug1(3, "row_info->rowbytes = %lu", png_ptr->row_info.rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500822
823 /* Copy user's row into buffer, leaving room for filter byte. */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500824 png_memcpy(png_ptr->row_buf + 1, row, png_ptr->row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500825
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500826#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500827 /* handle interlacing */
828 if (png_ptr->interlaced && png_ptr->pass < 6 &&
829 (png_ptr->transformations & PNG_INTERLACE))
830 {
831 png_do_write_interlace(&(png_ptr->row_info),
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600832 png_ptr->row_buf + 1, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500833 /* this should always get caught above, but still ... */
834 if (!(png_ptr->row_info.width))
835 {
836 png_write_finish_row(png_ptr);
837 return;
838 }
839 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600840#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500841
842 /* handle other transformations */
843 if (png_ptr->transformations)
844 png_do_write_transformations(png_ptr);
845
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600846#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600847 /* Write filter_method 64 (intrapixel differencing) only if
848 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
849 * 2. Libpng did not write a PNG signature (this filter_method is only
850 * used in PNG datastreams that are embedded in MNG datastreams) and
851 * 3. The application called png_permit_mng_features with a mask that
852 * included PNG_FLAG_MNG_FILTER_64 and
853 * 4. The filter_method is 64 and
854 * 5. The color_type is RGB or RGBA
855 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500856 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600857 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
858 {
859 /* Intrapixel differencing */
860 png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
861 }
862#endif
863
Andreas Dilger47a0c421997-05-16 02:46:07 -0500864 /* Find a filter if necessary, filter the row and write it out. */
Guy Schalnate5a37791996-06-05 15:50:50 -0500865 png_write_find_filter(png_ptr, &(png_ptr->row_info));
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600866
867 if (png_ptr->write_row_fn != NULL)
868 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500869}
870
Guy Schalnat0f716451995-11-28 11:22:13 -0600871#if defined(PNG_WRITE_FLUSH_SUPPORTED)
872/* Set the automatic flush interval or 0 to turn flushing off */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500873void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600874png_set_flush(png_structp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600875{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500876 png_debug(1, "in png_set_flush");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600877 if (png_ptr == NULL)
878 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600879 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600880}
881
882/* flush the current output buffers now */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500883void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600884png_write_flush(png_structp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600885{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600886 int wrote_IDAT;
Guy Schalnat0f716451995-11-28 11:22:13 -0600887
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500888 png_debug(1, "in png_write_flush");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600889 if (png_ptr == NULL)
890 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500891 /* We have already written out all of the data */
892 if (png_ptr->row_number >= png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600893 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600894
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600895 do
896 {
897 int ret;
Guy Schalnat0f716451995-11-28 11:22:13 -0600898
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600899 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600900 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600901 wrote_IDAT = 0;
Guy Schalnat0f716451995-11-28 11:22:13 -0600902
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600903 /* check for compression errors */
904 if (ret != Z_OK)
905 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500906 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600907 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600908 else
909 png_error(png_ptr, "zlib error");
910 }
Guy Schalnat0f716451995-11-28 11:22:13 -0600911
Andreas Dilger47a0c421997-05-16 02:46:07 -0500912 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600913 {
914 /* write the IDAT and reset the zlib output buffer */
915 png_write_IDAT(png_ptr, png_ptr->zbuf,
916 png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600917 png_ptr->zstream.next_out = png_ptr->zbuf;
918 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600919 wrote_IDAT = 1;
920 }
921 } while(wrote_IDAT == 1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600922
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600923 /* If there is any data left to be output, write it into a new IDAT */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600924 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600925 {
926 /* write the IDAT and reset the zlib output buffer */
927 png_write_IDAT(png_ptr, png_ptr->zbuf,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600928 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
929 png_ptr->zstream.next_out = png_ptr->zbuf;
930 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600931 }
932 png_ptr->flush_rows = 0;
933 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600934}
935#endif /* PNG_WRITE_FLUSH_SUPPORTED */
936
Guy Schalnate5a37791996-06-05 15:50:50 -0500937/* free all memory used by the write */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500938void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600939png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500940{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600941 png_structp png_ptr = NULL;
942 png_infop info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500943#ifdef PNG_USER_MEM_SUPPORTED
944 png_free_ptr free_fn = NULL;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500945 png_voidp mem_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500946#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600947
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500948 png_debug(1, "in png_destroy_write_struct");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500949 if (png_ptr_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500950 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600951 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500952#ifdef PNG_USER_MEM_SUPPORTED
953 free_fn = png_ptr->free_fn;
Glenn Randers-Pehrsonfcbd7872002-04-07 16:35:38 -0500954 mem_ptr = png_ptr->mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500955#endif
956 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600957
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500958#ifdef PNG_USER_MEM_SUPPORTED
959 if (png_ptr != NULL)
960 {
961 free_fn = png_ptr->free_fn;
962 mem_ptr = png_ptr->mem_ptr;
963 }
964#endif
965
Andreas Dilger47a0c421997-05-16 02:46:07 -0500966 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600967 info_ptr = *info_ptr_ptr;
968
Andreas Dilger47a0c421997-05-16 02:46:07 -0500969 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500970 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500971 if (png_ptr != NULL)
972 {
973 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600974
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500975#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500976 if (png_ptr->num_chunk_list)
977 {
978 png_free(png_ptr, png_ptr->chunk_list);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500979 png_ptr->num_chunk_list = 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500980 }
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500981#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500982 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600983
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500984#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500985 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
986 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500987#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600988 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500989#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500990 *info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500991 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600992
Andreas Dilger47a0c421997-05-16 02:46:07 -0500993 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500994 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600995 png_write_destroy(png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500996#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500997 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
998 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500999#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001000 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001001#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001002 *png_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001003 }
1004}
1005
1006
Andreas Dilger47a0c421997-05-16 02:46:07 -05001007/* Free any memory used in png_ptr struct (old method) */
Glenn Randers-Pehrsonf64a06f2001-04-11 07:38:00 -05001008void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001009png_write_destroy(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001010{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001011#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001012 jmp_buf tmp_jmp; /* save jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001013#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001014 png_error_ptr error_fn;
1015 png_error_ptr warning_fn;
1016 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001017#ifdef PNG_USER_MEM_SUPPORTED
1018 png_free_ptr free_fn;
1019#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001020
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001021 png_debug(1, "in png_write_destroy");
Guy Schalnat0d580581995-07-20 02:43:20 -05001022 /* free any memory zlib uses */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001023 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -05001024
Guy Schalnat0d580581995-07-20 02:43:20 -05001025 /* free our memory. png_free checks NULL for us. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001026 png_free(png_ptr, png_ptr->zbuf);
1027 png_free(png_ptr, png_ptr->row_buf);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001028#ifndef PNG_NO_WRITE_FILTER
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001029 png_free(png_ptr, png_ptr->prev_row);
1030 png_free(png_ptr, png_ptr->sub_row);
1031 png_free(png_ptr, png_ptr->up_row);
1032 png_free(png_ptr, png_ptr->avg_row);
1033 png_free(png_ptr, png_ptr->paeth_row);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001034#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001035
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001036#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001037 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001038#endif
1039
Andreas Dilger47a0c421997-05-16 02:46:07 -05001040#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1041 png_free(png_ptr, png_ptr->prev_filters);
1042 png_free(png_ptr, png_ptr->filter_weights);
1043 png_free(png_ptr, png_ptr->inv_filter_weights);
1044 png_free(png_ptr, png_ptr->filter_costs);
1045 png_free(png_ptr, png_ptr->inv_filter_costs);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001046#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001047
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001048#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001049 /* reset structure */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001050 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001051#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001052
1053 error_fn = png_ptr->error_fn;
1054 warning_fn = png_ptr->warning_fn;
1055 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001056#ifdef PNG_USER_MEM_SUPPORTED
1057 free_fn = png_ptr->free_fn;
1058#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001059
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001060 png_memset(png_ptr, 0, png_sizeof(png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -05001061
1062 png_ptr->error_fn = error_fn;
1063 png_ptr->warning_fn = warning_fn;
1064 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001065#ifdef PNG_USER_MEM_SUPPORTED
1066 png_ptr->free_fn = free_fn;
1067#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001068
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001069#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001070 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001071#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001072}
Guy Schalnate5a37791996-06-05 15:50:50 -05001073
Andreas Dilger47a0c421997-05-16 02:46:07 -05001074/* Allow the application to select one or more row filters to use. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001075void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -05001076png_set_filter(png_structp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001077{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001078 png_debug(1, "in png_set_filter");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001079 if (png_ptr == NULL)
1080 return;
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -06001081#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001082 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -06001083 (method == PNG_INTRAPIXEL_DIFFERENCING))
1084 method = PNG_FILTER_TYPE_BASE;
1085#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001086 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001087 {
1088 switch (filters & (PNG_ALL_FILTERS | 0x07))
1089 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001090#ifndef PNG_NO_WRITE_FILTER
Guy Schalnate5a37791996-06-05 15:50:50 -05001091 case 5:
1092 case 6:
Andreas Dilger47a0c421997-05-16 02:46:07 -05001093 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001094#endif /* PNG_NO_WRITE_FILTER */
1095 case PNG_FILTER_VALUE_NONE:
1096 png_ptr->do_filter=PNG_FILTER_NONE; break;
1097#ifndef PNG_NO_WRITE_FILTER
1098 case PNG_FILTER_VALUE_SUB:
1099 png_ptr->do_filter=PNG_FILTER_SUB; break;
1100 case PNG_FILTER_VALUE_UP:
1101 png_ptr->do_filter=PNG_FILTER_UP; break;
1102 case PNG_FILTER_VALUE_AVG:
1103 png_ptr->do_filter=PNG_FILTER_AVG; break;
1104 case PNG_FILTER_VALUE_PAETH:
1105 png_ptr->do_filter=PNG_FILTER_PAETH; break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001106 default: png_ptr->do_filter = (png_byte)filters; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001107#else
1108 default: png_warning(png_ptr, "Unknown row filter for method 0");
1109#endif /* PNG_NO_WRITE_FILTER */
Guy Schalnate5a37791996-06-05 15:50:50 -05001110 }
1111
Andreas Dilger47a0c421997-05-16 02:46:07 -05001112 /* If we have allocated the row_buf, this means we have already started
1113 * with the image and we should have allocated all of the filter buffers
1114 * that have been selected. If prev_row isn't already allocated, then
1115 * it is too late to start using the filters that need it, since we
1116 * will be missing the data in the previous row. If an application
1117 * wants to start and stop using particular filters during compression,
1118 * it should start out with all of the filters, and then add and
1119 * remove them after the start of compression.
Guy Schalnate5a37791996-06-05 15:50:50 -05001120 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001121 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001122 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001123#ifndef PNG_NO_WRITE_FILTER
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001124 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001125 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001126 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001127 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001128 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001129 }
1130
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001131 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001132 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001133 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001134 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001135 png_warning(png_ptr, "Can't add Up filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -05001136 png_ptr->do_filter &= ~PNG_FILTER_UP;
1137 }
1138 else
1139 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001140 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001141 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001142 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001143 }
1144 }
1145
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001146 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001147 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001148 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001149 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001150 png_warning(png_ptr, "Can't add Average filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -05001151 png_ptr->do_filter &= ~PNG_FILTER_AVG;
1152 }
1153 else
1154 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001155 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001156 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001157 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001158 }
1159 }
1160
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001161 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
Andreas Dilger47a0c421997-05-16 02:46:07 -05001162 png_ptr->paeth_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001163 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001164 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001165 {
1166 png_warning(png_ptr, "Can't add Paeth filter after starting");
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001167 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
Guy Schalnate5a37791996-06-05 15:50:50 -05001168 }
1169 else
1170 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001171 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001172 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001173 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001174 }
1175 }
1176
1177 if (png_ptr->do_filter == PNG_NO_FILTERS)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001178#endif /* PNG_NO_WRITE_FILTER */
Guy Schalnate5a37791996-06-05 15:50:50 -05001179 png_ptr->do_filter = PNG_FILTER_NONE;
1180 }
1181 }
1182 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05001183 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001184}
1185
Andreas Dilger47a0c421997-05-16 02:46:07 -05001186/* This allows us to influence the way in which libpng chooses the "best"
1187 * filter for the current scanline. While the "minimum-sum-of-absolute-
1188 * differences metric is relatively fast and effective, there is some
1189 * question as to whether it can be improved upon by trying to keep the
1190 * filtered data going to zlib more consistent, hopefully resulting in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001191 * better compression.
1192 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001193#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001194void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -05001195png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
1196 int num_weights, png_doublep filter_weights,
1197 png_doublep filter_costs)
1198{
1199 int i;
1200
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001201 png_debug(1, "in png_set_filter_heuristics");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001202 if (png_ptr == NULL)
1203 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001204 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
1205 {
1206 png_warning(png_ptr, "Unknown filter heuristic method");
1207 return;
1208 }
1209
1210 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
1211 {
1212 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1213 }
1214
1215 if (num_weights < 0 || filter_weights == NULL ||
1216 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
1217 {
1218 num_weights = 0;
1219 }
1220
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001221 png_ptr->num_prev_filters = (png_byte)num_weights;
1222 png_ptr->heuristic_method = (png_byte)heuristic_method;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001223
1224 if (num_weights > 0)
1225 {
1226 if (png_ptr->prev_filters == NULL)
1227 {
1228 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001229 (png_uint_32)(png_sizeof(png_byte) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001230
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001231 /* To make sure that the weighting starts out fairly */
1232 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001233 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001234 png_ptr->prev_filters[i] = 255;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001235 }
1236 }
1237
1238 if (png_ptr->filter_weights == NULL)
1239 {
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001240 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001241 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001242
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001243 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001244 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001245 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001246 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001247 png_ptr->inv_filter_weights[i] =
1248 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001249 }
1250 }
1251
1252 for (i = 0; i < num_weights; i++)
1253 {
1254 if (filter_weights[i] < 0.0)
1255 {
1256 png_ptr->inv_filter_weights[i] =
1257 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1258 }
1259 else
1260 {
1261 png_ptr->inv_filter_weights[i] =
1262 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
1263 png_ptr->filter_weights[i] =
1264 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
1265 }
1266 }
1267 }
1268
1269 /* If, in the future, there are other filter methods, this would
1270 * need to be based on png_ptr->filter.
1271 */
1272 if (png_ptr->filter_costs == NULL)
1273 {
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001274 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001275 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001276
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001277 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001278 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001279
1280 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1281 {
1282 png_ptr->inv_filter_costs[i] =
1283 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1284 }
1285 }
1286
1287 /* Here is where we set the relative costs of the different filters. We
1288 * should take the desired compression level into account when setting
1289 * the costs, so that Paeth, for instance, has a high relative cost at low
1290 * compression levels, while it has a lower relative cost at higher
1291 * compression settings. The filter types are in order of increasing
1292 * relative cost, so it would be possible to do this with an algorithm.
1293 */
1294 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1295 {
1296 if (filter_costs == NULL || filter_costs[i] < 0.0)
1297 {
1298 png_ptr->inv_filter_costs[i] =
1299 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1300 }
1301 else if (filter_costs[i] >= 1.0)
1302 {
1303 png_ptr->inv_filter_costs[i] =
1304 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
1305 png_ptr->filter_costs[i] =
1306 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
1307 }
1308 }
1309}
1310#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1311
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001312void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001313png_set_compression_level(png_structp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001314{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001315 png_debug(1, "in png_set_compression_level");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001316 if (png_ptr == NULL)
1317 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001318 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001319 png_ptr->zlib_level = level;
1320}
1321
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001322void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001323png_set_compression_mem_level(png_structp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001324{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001325 png_debug(1, "in png_set_compression_mem_level");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001326 if (png_ptr == NULL)
1327 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001328 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001329 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001330}
1331
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001332void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001333png_set_compression_strategy(png_structp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001334{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001335 png_debug(1, "in png_set_compression_strategy");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001336 if (png_ptr == NULL)
1337 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001338 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001339 png_ptr->zlib_strategy = strategy;
1340}
1341
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001342void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001343png_set_compression_window_bits(png_structp png_ptr, int window_bits)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001344{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001345 if (png_ptr == NULL)
1346 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001347 if (window_bits > 15)
1348 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001349 else if (window_bits < 8)
1350 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001351#ifndef WBITS_8_OK
1352 /* avoid libpng bug with 256-byte windows */
1353 if (window_bits == 8)
1354 {
1355 png_warning(png_ptr, "Compression window is being reset to 512");
1356 window_bits=9;
1357 }
1358#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001359 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001360 png_ptr->zlib_window_bits = window_bits;
1361}
1362
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001363void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001364png_set_compression_method(png_structp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001365{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001366 png_debug(1, "in png_set_compression_method");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001367 if (png_ptr == NULL)
1368 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001369 if (method != 8)
1370 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1371 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001372 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -05001373}
1374
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001375void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001376png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1377{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001378 if (png_ptr == NULL)
1379 return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001380 png_ptr->write_row_fn = write_row_fn;
1381}
1382
1383#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001384void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001385png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
1386 write_user_transform_fn)
1387{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001388 png_debug(1, "in png_set_write_user_transform_fn");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001389 if (png_ptr == NULL)
1390 return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001391 png_ptr->transformations |= PNG_USER_TRANSFORM;
1392 png_ptr->write_user_transform_fn = write_user_transform_fn;
1393}
1394#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001395
1396
1397#if defined(PNG_INFO_IMAGE_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001398void PNGAPI
1399png_write_png(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001400 int transforms, voidp params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001401{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001402 if (png_ptr == NULL || info_ptr == NULL)
1403 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001404#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
1405 /* invert the alpha channel from opacity to transparency */
1406 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1407 png_set_invert_alpha(png_ptr);
1408#endif
1409
1410 /* Write the file header information. */
1411 png_write_info(png_ptr, info_ptr);
1412
1413 /* ------ these transformations don't touch the info structure ------- */
1414
1415#if defined(PNG_WRITE_INVERT_SUPPORTED)
1416 /* invert monochrome pixels */
1417 if (transforms & PNG_TRANSFORM_INVERT_MONO)
1418 png_set_invert_mono(png_ptr);
1419#endif
1420
1421#if defined(PNG_WRITE_SHIFT_SUPPORTED)
1422 /* Shift the pixels up to a legal bit depth and fill in
1423 * as appropriate to correctly scale the image.
1424 */
1425 if ((transforms & PNG_TRANSFORM_SHIFT)
1426 && (info_ptr->valid & PNG_INFO_sBIT))
1427 png_set_shift(png_ptr, &info_ptr->sig_bit);
1428#endif
1429
1430#if defined(PNG_WRITE_PACK_SUPPORTED)
1431 /* pack pixels into bytes */
1432 if (transforms & PNG_TRANSFORM_PACKING)
1433 png_set_packing(png_ptr);
1434#endif
1435
1436#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
1437 /* swap location of alpha bytes from ARGB to RGBA */
1438 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1439 png_set_swap_alpha(png_ptr);
1440#endif
1441
1442#if defined(PNG_WRITE_FILLER_SUPPORTED)
Glenn Randers-Pehrson4a82d692008-12-15 16:25:05 -06001443 /* Pack XRGB/RGBX/ARGB/RGBA into * RGB (4 channels -> 3 channels) */
Glenn Randers-Pehrson1eb14e92008-12-10 07:14:45 -06001444 if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)
1445 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
1446 else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
1447 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001448#endif
1449
1450#if defined(PNG_WRITE_BGR_SUPPORTED)
1451 /* flip BGR pixels to RGB */
1452 if (transforms & PNG_TRANSFORM_BGR)
1453 png_set_bgr(png_ptr);
1454#endif
1455
1456#if defined(PNG_WRITE_SWAP_SUPPORTED)
1457 /* swap bytes of 16-bit files to most significant byte first */
1458 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1459 png_set_swap(png_ptr);
1460#endif
1461
1462#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
1463 /* swap bits of 1, 2, 4 bit packed pixel formats */
1464 if (transforms & PNG_TRANSFORM_PACKSWAP)
1465 png_set_packswap(png_ptr);
1466#endif
1467
1468 /* ----------------------- end of transformations ------------------- */
1469
1470 /* write the bits */
1471 if (info_ptr->valid & PNG_INFO_IDAT)
1472 png_write_image(png_ptr, info_ptr->row_pointers);
1473
1474 /* It is REQUIRED to call this to finish writing the rest of the file */
1475 png_write_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001476
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001477 transforms = transforms; /* quiet compiler warnings */
1478 params = params;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001479}
1480#endif
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -06001481#endif /* PNG_WRITE_SUPPORTED */