blob: 17d285b15d3b484fde798c7c59bf7a32dd7876ca [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-Pehrson170b70c2006-03-10 10:19:04 -06004 * Last changed in libpng 1.2.9 March 10, 2006
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06006 * Copyright (c) 1998-2006 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 */
12#define PNG_INTERNAL
13#include "png.h"
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -060014#ifdef PNG_WRITE_SUPPORTED
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-Pehrson5379b241999-11-27 10:22:33 -060028 png_debug(1, "in png_write_info_before_PLTE\n");
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)
35 if((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&(png_ptr->mng_features_permitted))
36 {
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -060037 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
102 png_debug(5, "writing extra chunks\n");
103
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-Pehrson166c5a31999-12-10 09:43:02 -0600115 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600116 }
117 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600118 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500119#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600120 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
121 }
122}
123
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500124void PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600125png_write_info(png_structp png_ptr, png_infop info_ptr)
126{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600127#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600128 int i;
129#endif
130
131 png_debug(1, "in png_write_info\n");
132
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600133 if (png_ptr == NULL || info_ptr == NULL)
134 return;
135
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600136 png_write_info_before_PLTE(png_ptr, info_ptr);
137
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600138 if (info_ptr->valid & PNG_INFO_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500139 png_write_PLTE(png_ptr, info_ptr->palette,
140 (png_uint_32)info_ptr->num_palette);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600141 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600142 png_error(png_ptr, "Valid palette required for paletted images");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600143
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500144#if defined(PNG_WRITE_tRNS_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600145 if (info_ptr->valid & PNG_INFO_tRNS)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600146 {
147#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
148 /* invert the alpha channel (in tRNS) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600149 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600150 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
151 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600152 int j;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600153 for (j=0; j<(int)info_ptr->num_trans; j++)
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500154 info_ptr->trans[j] = (png_byte)(255 - info_ptr->trans[j]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600155 }
156#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600157 png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values),
158 info_ptr->num_trans, info_ptr->color_type);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600159 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500160#endif
161#if defined(PNG_WRITE_bKGD_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600162 if (info_ptr->valid & PNG_INFO_bKGD)
163 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500164#endif
165#if defined(PNG_WRITE_hIST_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600166 if (info_ptr->valid & PNG_INFO_hIST)
167 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500168#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500169#if defined(PNG_WRITE_oFFs_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600170 if (info_ptr->valid & PNG_INFO_oFFs)
171 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
172 info_ptr->offset_unit_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500173#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500174#if defined(PNG_WRITE_pCAL_SUPPORTED)
175 if (info_ptr->valid & PNG_INFO_pCAL)
176 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
177 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
178 info_ptr->pcal_units, info_ptr->pcal_params);
179#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600180#if defined(PNG_WRITE_sCAL_SUPPORTED)
181 if (info_ptr->valid & PNG_INFO_sCAL)
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600182#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600183 png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,
184 info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);
185#else
186#ifdef PNG_FIXED_POINT_SUPPORTED
187 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600188 info_ptr->scal_s_width, info_ptr->scal_s_height);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600189#else
190 png_warning(png_ptr,
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600191 "png_write_sCAL not supported; sCAL chunk not written.");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600192#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600193#endif
194#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500195#if defined(PNG_WRITE_pHYs_SUPPORTED)
196 if (info_ptr->valid & PNG_INFO_pHYs)
197 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
198 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
199#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500200#if defined(PNG_WRITE_tIME_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600201 if (info_ptr->valid & PNG_INFO_tIME)
Guy Schalnate5a37791996-06-05 15:50:50 -0500202 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600203 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600204 png_ptr->mode |= PNG_WROTE_tIME;
Guy Schalnate5a37791996-06-05 15:50:50 -0500205 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500206#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600207#if defined(PNG_WRITE_sPLT_SUPPORTED)
208 if (info_ptr->valid & PNG_INFO_sPLT)
209 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
210 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
211#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600212#if defined(PNG_WRITE_TEXT_SUPPORTED)
Guy Schalnate5a37791996-06-05 15:50:50 -0500213 /* Check to see if we need to write text chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500214 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500215 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500216 png_debug2(2, "Writing header text chunk %d, type %d\n", i,
217 info_ptr->text[i].compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600218 /* an internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600219 if (info_ptr->text[i].compression > 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600220 {
221#if defined(PNG_WRITE_iTXt_SUPPORTED)
222 /* write international chunk */
223 png_write_iTXt(png_ptr,
224 info_ptr->text[i].compression,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600225 info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600226 info_ptr->text[i].lang,
227 info_ptr->text[i].lang_key,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600228 info_ptr->text[i].text);
229#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600230 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600231#endif
232 /* Mark this chunk as written */
233 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
234 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500235 /* If we want a compressed text chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600236 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500237 {
238#if defined(PNG_WRITE_zTXt_SUPPORTED)
239 /* write compressed chunk */
240 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600241 info_ptr->text[i].text, 0,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500242 info_ptr->text[i].compression);
243#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600244 png_warning(png_ptr, "Unable to write compressed text");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500245#endif
246 /* Mark this chunk as written */
247 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
248 }
249 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
250 {
251#if defined(PNG_WRITE_tEXt_SUPPORTED)
252 /* write uncompressed chunk */
253 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600254 info_ptr->text[i].text,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600255 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500256#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600257 png_warning(png_ptr, "Unable to write uncompressed 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_NONE_WR;
261 }
262 }
263#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600264#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
265 if (info_ptr->unknown_chunks_num)
266 {
267 png_unknown_chunk *up;
268
269 png_debug(5, "writing extra chunks\n");
270
271 for (up = info_ptr->unknown_chunks;
272 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
273 up++)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600274 {
275 int keep=png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500276 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600277 up->location && (up->location & PNG_HAVE_PLTE) &&
278 !(up->location & PNG_HAVE_IDAT) &&
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500279 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600280 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
281 {
282 png_write_chunk(png_ptr, up->name, up->data, up->size);
283 }
284 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600285 }
286#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500287}
Guy Schalnat0d580581995-07-20 02:43:20 -0500288
Andreas Dilger47a0c421997-05-16 02:46:07 -0500289/* Writes the end of the PNG file. If you don't want to write comments or
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600290 * time information, you can pass NULL for info. If you already wrote these
291 * in png_write_info(), do not write them again here. If you have long
292 * comments, I suggest writing them here, and compressing them.
293 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500294void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500295png_write_end(png_structp png_ptr, png_infop info_ptr)
296{
297 png_debug(1, "in png_write_end\n");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600298 if (png_ptr == NULL)
299 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500300 if (!(png_ptr->mode & PNG_HAVE_IDAT))
301 png_error(png_ptr, "No IDATs written into file");
302
303 /* see if user wants us to write information chunks */
304 if (info_ptr != NULL)
305 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600306#if defined(PNG_WRITE_TEXT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500307 int i; /* local index variable */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600308#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500309#if defined(PNG_WRITE_tIME_SUPPORTED)
310 /* check to see if user has supplied a time chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600311 if ((info_ptr->valid & PNG_INFO_tIME) &&
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600312 !(png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500313 png_write_tIME(png_ptr, &(info_ptr->mod_time));
314#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600315#if defined(PNG_WRITE_TEXT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500316 /* loop through comment chunks */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600317 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500318 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500319 png_debug2(2, "Writing trailer text chunk %d, type %d\n", i,
320 info_ptr->text[i].compression);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600321 /* an internationalized chunk? */
322 if (info_ptr->text[i].compression > 0)
323 {
324#if defined(PNG_WRITE_iTXt_SUPPORTED)
325 /* write international chunk */
326 png_write_iTXt(png_ptr,
327 info_ptr->text[i].compression,
328 info_ptr->text[i].key,
329 info_ptr->text[i].lang,
330 info_ptr->text[i].lang_key,
331 info_ptr->text[i].text);
332#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600333 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600334#endif
335 /* Mark this chunk as written */
336 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
337 }
338 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
Guy Schalnat0d580581995-07-20 02:43:20 -0500339 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500340#if defined(PNG_WRITE_zTXt_SUPPORTED)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600341 /* write compressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600342 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600343 info_ptr->text[i].text, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600344 info_ptr->text[i].compression);
Guy Schalnate5a37791996-06-05 15:50:50 -0500345#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600346 png_warning(png_ptr, "Unable to write compressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500347#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500348 /* Mark this chunk as written */
349 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500350 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500351 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500352 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500353#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500354 /* write uncompressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600355 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600356 info_ptr->text[i].text, 0);
Guy Schalnate5a37791996-06-05 15:50:50 -0500357#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600358 png_warning(png_ptr, "Unable to write uncompressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500359#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500360
Andreas Dilger47a0c421997-05-16 02:46:07 -0500361 /* Mark this chunk as written */
362 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500363 }
364 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600365#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600366#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
367 if (info_ptr->unknown_chunks_num)
368 {
369 png_unknown_chunk *up;
370
371 png_debug(5, "writing extra chunks\n");
372
373 for (up = info_ptr->unknown_chunks;
374 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
375 up++)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600376 {
377 int keep=png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500378 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600379 up->location && (up->location & PNG_AFTER_IDAT) &&
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500380 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600381 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
382 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600383 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600384 }
385 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600386 }
387#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500388 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500389
390 png_ptr->mode |= PNG_AFTER_IDAT;
391
Andreas Dilger47a0c421997-05-16 02:46:07 -0500392 /* write end of PNG file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500393 png_write_IEND(png_ptr);
Glenn Randers-Pehrsond56aca72000-11-23 11:51:42 -0600394#if 0
395/* This flush, added in libpng-1.0.8, causes some applications to crash
396 because they do not set png_ptr->output_flush_fn */
Glenn Randers-Pehrson32fc5ce2000-07-24 06:34:14 -0500397 png_flush(png_ptr);
Glenn Randers-Pehrsond56aca72000-11-23 11:51:42 -0600398#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500399}
400
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500401#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500402#if !defined(_WIN32_WCE)
403/* "time.h" functions are not supported on WindowsCE */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500404void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600405png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500406{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500407 png_debug(1, "in png_convert_from_struct_tm\n");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600408 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
409 ptime->month = (png_byte)(ttime->tm_mon + 1);
410 ptime->day = (png_byte)ttime->tm_mday;
411 ptime->hour = (png_byte)ttime->tm_hour;
412 ptime->minute = (png_byte)ttime->tm_min;
413 ptime->second = (png_byte)ttime->tm_sec;
Guy Schalnat0d580581995-07-20 02:43:20 -0500414}
415
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500416void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600417png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500418{
419 struct tm *tbuf;
420
Andreas Dilger47a0c421997-05-16 02:46:07 -0500421 png_debug(1, "in png_convert_from_time_t\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500422 tbuf = gmtime(&ttime);
423 png_convert_from_struct_tm(ptime, tbuf);
424}
Guy Schalnat6d764711995-12-19 03:22:19 -0600425#endif
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500426#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500427
Andreas Dilger47a0c421997-05-16 02:46:07 -0500428/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500429png_structp PNGAPI
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500430png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600431 png_error_ptr error_fn, png_error_ptr warn_fn)
Guy Schalnat0d580581995-07-20 02:43:20 -0500432{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500433#ifdef PNG_USER_MEM_SUPPORTED
434 return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500435 warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500436}
437
438/* Alternate initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500439png_structp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500440png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
441 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
442 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
443{
444#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500445 png_structp png_ptr;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600446#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600447#ifdef USE_FAR_KEYWORD
448 jmp_buf jmpbuf;
449#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600450#endif
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500451 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500452 png_debug(1, "in png_create_write_struct\n");
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500453#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600454 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
455 (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500456#else
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600457 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500458#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600459 if (png_ptr == NULL)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500460 return (NULL);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600461
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600462#if !defined(PNG_1_0_X)
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500463#ifdef PNG_ASSEMBLER_CODE_SUPPORTED
464 png_init_mmx_flags(png_ptr); /* 1.2.0 addition */
465#endif
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600466#endif /* PNG_1_0_X */
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500467
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500468 /* added at libpng-1.2.6 */
469#ifdef PNG_SET_USER_LIMITS_SUPPORTED
470 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
471 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
472#endif
473
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600474#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600475#ifdef USE_FAR_KEYWORD
476 if (setjmp(jmpbuf))
477#else
Guy Schalnate5a37791996-06-05 15:50:50 -0500478 if (setjmp(png_ptr->jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600479#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500480 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600481 png_free(png_ptr, png_ptr->zbuf);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500482 png_ptr->zbuf=NULL;
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-Pehrson5fea36f2004-07-28 08:20:44 -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-Pehrson75294572000-05-06 14:09:57 -0500496 i=0;
497 do
Guy Schalnate5a37791996-06-05 15:50:50 -0500498 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500499 if(user_png_ver[i] != png_libpng_ver[i])
500 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
501 } while (png_libpng_ver[i++]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500502
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500503 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500504 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500505 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
506 * we must recompile any applications that use any older library version.
507 * For versions after libpng 1.0, we will be compatible, so we need
508 * only check the first digit.
509 */
510 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500511 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500512 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
513 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500514#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
515 char msg[80];
516 if (user_png_ver)
517 {
518 sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
519 user_png_ver);
520 png_warning(png_ptr, msg);
521 }
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600522 sprintf(msg, "Application is running with png.c from libpng-%.20s",
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500523 png_libpng_ver);
524 png_warning(png_ptr, msg);
525#endif
526#ifdef PNG_ERROR_NUMBERS_SUPPORTED
527 png_ptr->flags=0;
528#endif
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500529 png_error(png_ptr,
530 "Incompatible libpng version in application and library");
531 }
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500532 }
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500533
Guy Schalnat0d580581995-07-20 02:43:20 -0500534 /* initialize zbuf - compression buffer */
535 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600536 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
537 (png_uint_32)png_ptr->zbuf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500538
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500539 png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
540 png_flush_ptr_NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500541
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600542#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
543 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500544 1, png_doublep_NULL, png_doublep_NULL);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600545#endif
546
Glenn Randers-Pehrson03008a02002-04-27 10:11:25 -0500547#ifdef PNG_SETJMP_SUPPORTED
548/* Applications that neglect to set up their own setjmp() and then encounter
549 a png_error() will longjmp here. Since the jmpbuf is then meaningless we
550 abort instead of returning. */
551#ifdef USE_FAR_KEYWORD
552 if (setjmp(jmpbuf))
553 PNG_ABORT();
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500554 png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
Glenn Randers-Pehrson03008a02002-04-27 10:11:25 -0500555#else
556 if (setjmp(png_ptr->jmpbuf))
557 PNG_ABORT();
558#endif
559#endif
560 return (png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500561}
562
Andreas Dilger47a0c421997-05-16 02:46:07 -0500563/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson1ce08362006-03-08 23:35:59 -0600564#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600565/* Deprecated. */
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500566#undef png_write_init
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500567void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500568png_write_init(png_structp png_ptr)
569{
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500570 /* We only come here via pre-1.0.7-compiled applications */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500571 png_write_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500572}
573
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500574void PNGAPI
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500575png_write_init_2(png_structp png_ptr, png_const_charp user_png_ver,
576 png_size_t png_struct_size, png_size_t png_info_size)
577{
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500578 /* We only come here via pre-1.0.12-compiled applications */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500579#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500580 if(png_sizeof(png_struct) > png_struct_size ||
581 png_sizeof(png_info) > png_info_size)
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500582 {
583 char msg[80];
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500584 png_ptr->warning_fn=NULL;
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500585 if (user_png_ver)
586 {
587 sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
588 user_png_ver);
589 png_warning(png_ptr, msg);
590 }
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600591 sprintf(msg, "Application is running with png.c from libpng-%.20s",
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500592 png_libpng_ver);
593 png_warning(png_ptr, msg);
594 }
595#endif
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500596 if(png_sizeof(png_struct) > png_struct_size)
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500597 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500598 png_ptr->error_fn=NULL;
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500599#ifdef PNG_ERROR_NUMBERS_SUPPORTED
600 png_ptr->flags=0;
601#endif
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500602 png_error(png_ptr,
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500603 "The png struct allocated by the application for writing is too small.");
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500604 }
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500605 if(png_sizeof(png_info) > png_info_size)
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500606 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500607 png_ptr->error_fn=NULL;
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500608#ifdef PNG_ERROR_NUMBERS_SUPPORTED
609 png_ptr->flags=0;
610#endif
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500611 png_error(png_ptr,
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500612 "The info struct allocated by the application for writing is too small.");
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500613 }
614 png_write_init_3(&png_ptr, user_png_ver, png_struct_size);
615}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600616#endif /* PNG_1_0_X || PNG_1_2_X */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500617
618
619void PNGAPI
620png_write_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
621 png_size_t png_struct_size)
622{
623 png_structp png_ptr=*ptr_ptr;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600624#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500625 jmp_buf tmp_jmp; /* to save current jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600626#endif
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600627
628 if (png_ptr == NULL)
629 return;
630
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500631 int i = 0;
632 do
633 {
634 if (user_png_ver[i] != png_libpng_ver[i])
635 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500636#ifdef PNG_LEGACY_SUPPORTED
637 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
638#else
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500639 png_ptr->warning_fn=NULL;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500640 png_warning(png_ptr,
641 "Application uses deprecated png_write_init() and should be recompiled.");
642 break;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500643#endif
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500644 }
645 } while (png_libpng_ver[i++]);
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500646
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500647 png_debug(1, "in png_write_init_3\n");
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500648
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600649#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500650 /* save jump buffer and error functions */
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500651 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600652#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500653
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500654 if (png_sizeof(png_struct) > png_struct_size)
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500655 {
656 png_destroy_struct(png_ptr);
657 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
658 *ptr_ptr = png_ptr;
659 }
660
Guy Schalnate5a37791996-06-05 15:50:50 -0500661 /* reset all variables to 0 */
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500662 png_memset(png_ptr, 0, png_sizeof (png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -0500663
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500664 /* added at libpng-1.2.6 */
665#ifdef PNG_SET_USER_LIMITS_SUPPORTED
666 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
667 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
668#endif
669
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600670#if !defined(PNG_1_0_X)
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500671#ifdef PNG_ASSEMBLER_CODE_SUPPORTED
672 png_init_mmx_flags(png_ptr); /* 1.2.0 addition */
673#endif
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600674#endif /* PNG_1_0_X */
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500675
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600676#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500677 /* restore jump buffer */
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500678 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600679#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500680
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500681 png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
682 png_flush_ptr_NULL);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500683
Guy Schalnate5a37791996-06-05 15:50:50 -0500684 /* initialize zbuf - compression buffer */
685 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600686 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
687 (png_uint_32)png_ptr->zbuf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500688
689#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
690 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500691 1, png_doublep_NULL, png_doublep_NULL);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500692#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500693}
694
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600695/* Write a few rows of image data. If the image is interlaced,
696 * either you will have to write the 7 sub images, or, if you
697 * have called png_set_interlace_handling(), you will have to
698 * "write" the image seven times.
699 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500700void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600701png_write_rows(png_structp png_ptr, png_bytepp row,
Guy Schalnat0d580581995-07-20 02:43:20 -0500702 png_uint_32 num_rows)
703{
704 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600705 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500706
Andreas Dilger47a0c421997-05-16 02:46:07 -0500707 png_debug(1, "in png_write_rows\n");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600708
709 if (png_ptr == NULL)
710 return;
711
Guy Schalnat0d580581995-07-20 02:43:20 -0500712 /* loop through the rows */
713 for (i = 0, rp = row; i < num_rows; i++, rp++)
714 {
715 png_write_row(png_ptr, *rp);
716 }
717}
718
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600719/* Write the image. You only need to call this function once, even
720 * if you are writing an interlaced image.
721 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500722void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600723png_write_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500724{
725 png_uint_32 i; /* row index */
726 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600727 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500728
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600729 if (png_ptr == NULL)
730 return;
731
Andreas Dilger47a0c421997-05-16 02:46:07 -0500732 png_debug(1, "in png_write_image\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600733#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500734 /* intialize interlace handling. If image is not interlaced,
735 this will set pass to 1 */
736 num_pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600737#else
738 num_pass = 1;
739#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500740 /* loop through passes */
741 for (pass = 0; pass < num_pass; pass++)
742 {
743 /* loop through image */
744 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
745 {
746 png_write_row(png_ptr, *rp);
747 }
748 }
749}
750
Guy Schalnate5a37791996-06-05 15:50:50 -0500751/* called by user to write a row of image data */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500752void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600753png_write_row(png_structp png_ptr, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500754{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600755 if (png_ptr == NULL)
756 return;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600757 png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
758 png_ptr->row_number, png_ptr->pass);
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600759
Guy Schalnat0d580581995-07-20 02:43:20 -0500760 /* initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600761 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500762 {
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600763 /* make sure we wrote the header info */
764 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
765 png_error(png_ptr,
766 "png_write_info was never called before png_write_row.");
767
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500768 /* check for transforms that have been set but were defined out */
769#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
770 if (png_ptr->transformations & PNG_INVERT_MONO)
771 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
772#endif
773#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
774 if (png_ptr->transformations & PNG_FILLER)
775 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
776#endif
777#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
778 if (png_ptr->transformations & PNG_PACKSWAP)
779 png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
780#endif
781#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
782 if (png_ptr->transformations & PNG_PACK)
783 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined.");
784#endif
785#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
786 if (png_ptr->transformations & PNG_SHIFT)
787 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
788#endif
789#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
790 if (png_ptr->transformations & PNG_BGR)
791 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined.");
792#endif
793#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
794 if (png_ptr->transformations & PNG_SWAP_BYTES)
795 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
796#endif
797
Guy Schalnat0d580581995-07-20 02:43:20 -0500798 png_write_start_row(png_ptr);
799 }
800
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500801#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500802 /* if interlaced and not interested in row, return */
803 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
804 {
805 switch (png_ptr->pass)
806 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600807 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600808 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500809 {
810 png_write_finish_row(png_ptr);
811 return;
812 }
813 break;
814 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600815 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500816 {
817 png_write_finish_row(png_ptr);
818 return;
819 }
820 break;
821 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600822 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500823 {
824 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600825 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500826 }
827 break;
828 case 3:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600829 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
Guy Schalnat0d580581995-07-20 02:43:20 -0500830 {
831 png_write_finish_row(png_ptr);
832 return;
833 }
834 break;
835 case 4:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600836 if ((png_ptr->row_number & 0x03) != 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500837 {
838 png_write_finish_row(png_ptr);
839 return;
840 }
841 break;
842 case 5:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600843 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500844 {
845 png_write_finish_row(png_ptr);
846 return;
847 }
848 break;
849 case 6:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600850 if (!(png_ptr->row_number & 0x01))
Guy Schalnat0d580581995-07-20 02:43:20 -0500851 {
852 png_write_finish_row(png_ptr);
853 return;
854 }
855 break;
856 }
857 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600858#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500859
860 /* set up row info for transformations */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600861 png_ptr->row_info.color_type = png_ptr->color_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500862 png_ptr->row_info.width = png_ptr->usr_width;
863 png_ptr->row_info.channels = png_ptr->usr_channels;
864 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600865 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
866 png_ptr->row_info.channels);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600867
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500868 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
869 png_ptr->row_info.width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500870
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600871 png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500872 png_debug1(3, "row_info->width = %lu\n", png_ptr->row_info.width);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600873 png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels);
874 png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth);
875 png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500876 png_debug1(3, "row_info->rowbytes = %lu\n", png_ptr->row_info.rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500877
878 /* Copy user's row into buffer, leaving room for filter byte. */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600879 png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600880 png_ptr->row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500881
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500882#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500883 /* handle interlacing */
884 if (png_ptr->interlaced && png_ptr->pass < 6 &&
885 (png_ptr->transformations & PNG_INTERLACE))
886 {
887 png_do_write_interlace(&(png_ptr->row_info),
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600888 png_ptr->row_buf + 1, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500889 /* this should always get caught above, but still ... */
890 if (!(png_ptr->row_info.width))
891 {
892 png_write_finish_row(png_ptr);
893 return;
894 }
895 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600896#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500897
898 /* handle other transformations */
899 if (png_ptr->transformations)
900 png_do_write_transformations(png_ptr);
901
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600902#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600903 /* Write filter_method 64 (intrapixel differencing) only if
904 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
905 * 2. Libpng did not write a PNG signature (this filter_method is only
906 * used in PNG datastreams that are embedded in MNG datastreams) and
907 * 3. The application called png_permit_mng_features with a mask that
908 * included PNG_FLAG_MNG_FILTER_64 and
909 * 4. The filter_method is 64 and
910 * 5. The color_type is RGB or RGBA
911 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600912 if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
913 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
914 {
915 /* Intrapixel differencing */
916 png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
917 }
918#endif
919
Andreas Dilger47a0c421997-05-16 02:46:07 -0500920 /* Find a filter if necessary, filter the row and write it out. */
Guy Schalnate5a37791996-06-05 15:50:50 -0500921 png_write_find_filter(png_ptr, &(png_ptr->row_info));
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600922
923 if (png_ptr->write_row_fn != NULL)
924 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500925}
926
Guy Schalnat0f716451995-11-28 11:22:13 -0600927#if defined(PNG_WRITE_FLUSH_SUPPORTED)
928/* Set the automatic flush interval or 0 to turn flushing off */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500929void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600930png_set_flush(png_structp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600931{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500932 png_debug(1, "in png_set_flush\n");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600933 if (png_ptr == NULL)
934 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600935 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600936}
937
938/* flush the current output buffers now */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500939void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600940png_write_flush(png_structp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600941{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600942 int wrote_IDAT;
Guy Schalnat0f716451995-11-28 11:22:13 -0600943
Andreas Dilger47a0c421997-05-16 02:46:07 -0500944 png_debug(1, "in png_write_flush\n");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -0600945 if (png_ptr == NULL)
946 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500947 /* We have already written out all of the data */
948 if (png_ptr->row_number >= png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600949 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600950
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600951 do
952 {
953 int ret;
Guy Schalnat0f716451995-11-28 11:22:13 -0600954
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600955 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600956 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600957 wrote_IDAT = 0;
Guy Schalnat0f716451995-11-28 11:22:13 -0600958
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600959 /* check for compression errors */
960 if (ret != Z_OK)
961 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500962 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600963 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600964 else
965 png_error(png_ptr, "zlib error");
966 }
Guy Schalnat0f716451995-11-28 11:22:13 -0600967
Andreas Dilger47a0c421997-05-16 02:46:07 -0500968 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600969 {
970 /* write the IDAT and reset the zlib output buffer */
971 png_write_IDAT(png_ptr, png_ptr->zbuf,
972 png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600973 png_ptr->zstream.next_out = png_ptr->zbuf;
974 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600975 wrote_IDAT = 1;
976 }
977 } while(wrote_IDAT == 1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600978
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600979 /* If there is any data left to be output, write it into a new IDAT */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600980 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600981 {
982 /* write the IDAT and reset the zlib output buffer */
983 png_write_IDAT(png_ptr, png_ptr->zbuf,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600984 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
985 png_ptr->zstream.next_out = png_ptr->zbuf;
986 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600987 }
988 png_ptr->flush_rows = 0;
989 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600990}
991#endif /* PNG_WRITE_FLUSH_SUPPORTED */
992
Guy Schalnate5a37791996-06-05 15:50:50 -0500993/* free all memory used by the write */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500994void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600995png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500996{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600997 png_structp png_ptr = NULL;
998 png_infop info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500999#ifdef PNG_USER_MEM_SUPPORTED
1000 png_free_ptr free_fn = NULL;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001001 png_voidp mem_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001002#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001003
Andreas Dilger47a0c421997-05-16 02:46:07 -05001004 png_debug(1, "in png_destroy_write_struct\n");
1005 if (png_ptr_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001006 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001007 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001008#ifdef PNG_USER_MEM_SUPPORTED
1009 free_fn = png_ptr->free_fn;
Glenn Randers-Pehrsonfcbd7872002-04-07 16:35:38 -05001010 mem_ptr = png_ptr->mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001011#endif
1012 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001013
Andreas Dilger47a0c421997-05-16 02:46:07 -05001014 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001015 info_ptr = *info_ptr_ptr;
1016
Andreas Dilger47a0c421997-05-16 02:46:07 -05001017 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001018 {
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001019 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
1020
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001021#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001022 if (png_ptr->num_chunk_list)
1023 {
1024 png_free(png_ptr, png_ptr->chunk_list);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001025 png_ptr->chunk_list=NULL;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001026 png_ptr->num_chunk_list=0;
1027 }
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001028#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001029
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001030#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001031 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
1032 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001033#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001034 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001035#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001036 *info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001037 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001038
Andreas Dilger47a0c421997-05-16 02:46:07 -05001039 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001040 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001041 png_write_destroy(png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001042#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001043 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
1044 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001045#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001046 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001047#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001048 *png_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001049 }
1050}
1051
1052
Andreas Dilger47a0c421997-05-16 02:46:07 -05001053/* Free any memory used in png_ptr struct (old method) */
Glenn Randers-Pehrsonf64a06f2001-04-11 07:38:00 -05001054void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001055png_write_destroy(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001056{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001057#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001058 jmp_buf tmp_jmp; /* save jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001059#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001060 png_error_ptr error_fn;
1061 png_error_ptr warning_fn;
1062 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001063#ifdef PNG_USER_MEM_SUPPORTED
1064 png_free_ptr free_fn;
1065#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001066
Andreas Dilger47a0c421997-05-16 02:46:07 -05001067 png_debug(1, "in png_write_destroy\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001068 /* free any memory zlib uses */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001069 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -05001070
Guy Schalnat0d580581995-07-20 02:43:20 -05001071 /* free our memory. png_free checks NULL for us. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001072 png_free(png_ptr, png_ptr->zbuf);
1073 png_free(png_ptr, png_ptr->row_buf);
1074 png_free(png_ptr, png_ptr->prev_row);
1075 png_free(png_ptr, png_ptr->sub_row);
1076 png_free(png_ptr, png_ptr->up_row);
1077 png_free(png_ptr, png_ptr->avg_row);
1078 png_free(png_ptr, png_ptr->paeth_row);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001079
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001080#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001081 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001082#endif
1083
Andreas Dilger47a0c421997-05-16 02:46:07 -05001084#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1085 png_free(png_ptr, png_ptr->prev_filters);
1086 png_free(png_ptr, png_ptr->filter_weights);
1087 png_free(png_ptr, png_ptr->inv_filter_weights);
1088 png_free(png_ptr, png_ptr->filter_costs);
1089 png_free(png_ptr, png_ptr->inv_filter_costs);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001090#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001091
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001092#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001093 /* reset structure */
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001094 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001095#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001096
1097 error_fn = png_ptr->error_fn;
1098 warning_fn = png_ptr->warning_fn;
1099 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001100#ifdef PNG_USER_MEM_SUPPORTED
1101 free_fn = png_ptr->free_fn;
1102#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001103
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001104 png_memset(png_ptr, 0, png_sizeof (png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -05001105
1106 png_ptr->error_fn = error_fn;
1107 png_ptr->warning_fn = warning_fn;
1108 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001109#ifdef PNG_USER_MEM_SUPPORTED
1110 png_ptr->free_fn = free_fn;
1111#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001112
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001113#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001114 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001115#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001116}
Guy Schalnate5a37791996-06-05 15:50:50 -05001117
Andreas Dilger47a0c421997-05-16 02:46:07 -05001118/* Allow the application to select one or more row filters to use. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001119void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -05001120png_set_filter(png_structp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001121{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001122 png_debug(1, "in png_set_filter\n");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001123 if (png_ptr == NULL)
1124 return;
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -06001125#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrsonf05f8032000-12-23 14:27:39 -06001126 if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -06001127 (method == PNG_INTRAPIXEL_DIFFERENCING))
1128 method = PNG_FILTER_TYPE_BASE;
1129#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001130 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001131 {
1132 switch (filters & (PNG_ALL_FILTERS | 0x07))
1133 {
1134 case 5:
1135 case 6:
Andreas Dilger47a0c421997-05-16 02:46:07 -05001136 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
1137 case PNG_FILTER_VALUE_NONE: png_ptr->do_filter=PNG_FILTER_NONE; break;
1138 case PNG_FILTER_VALUE_SUB: png_ptr->do_filter=PNG_FILTER_SUB; break;
1139 case PNG_FILTER_VALUE_UP: png_ptr->do_filter=PNG_FILTER_UP; break;
1140 case PNG_FILTER_VALUE_AVG: png_ptr->do_filter=PNG_FILTER_AVG; break;
1141 case PNG_FILTER_VALUE_PAETH: png_ptr->do_filter=PNG_FILTER_PAETH;break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001142 default: png_ptr->do_filter = (png_byte)filters; break;
1143 }
1144
Andreas Dilger47a0c421997-05-16 02:46:07 -05001145 /* If we have allocated the row_buf, this means we have already started
1146 * with the image and we should have allocated all of the filter buffers
1147 * that have been selected. If prev_row isn't already allocated, then
1148 * it is too late to start using the filters that need it, since we
1149 * will be missing the data in the previous row. If an application
1150 * wants to start and stop using particular filters during compression,
1151 * it should start out with all of the filters, and then add and
1152 * remove them after the start of compression.
Guy Schalnate5a37791996-06-05 15:50:50 -05001153 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001154 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001155 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001156 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001157 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001158 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001159 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001160 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001161 }
1162
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001163 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001164 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001165 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001166 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001167 png_warning(png_ptr, "Can't add Up filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -05001168 png_ptr->do_filter &= ~PNG_FILTER_UP;
1169 }
1170 else
1171 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001172 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001173 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001174 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001175 }
1176 }
1177
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001178 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001179 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001180 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001181 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001182 png_warning(png_ptr, "Can't add Average filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -05001183 png_ptr->do_filter &= ~PNG_FILTER_AVG;
1184 }
1185 else
1186 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001187 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001188 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001189 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001190 }
1191 }
1192
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001193 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
Andreas Dilger47a0c421997-05-16 02:46:07 -05001194 png_ptr->paeth_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001195 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001196 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001197 {
1198 png_warning(png_ptr, "Can't add Paeth filter after starting");
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001199 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
Guy Schalnate5a37791996-06-05 15:50:50 -05001200 }
1201 else
1202 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001203 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001204 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001205 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001206 }
1207 }
1208
1209 if (png_ptr->do_filter == PNG_NO_FILTERS)
1210 png_ptr->do_filter = PNG_FILTER_NONE;
1211 }
1212 }
1213 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05001214 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001215}
1216
Andreas Dilger47a0c421997-05-16 02:46:07 -05001217/* This allows us to influence the way in which libpng chooses the "best"
1218 * filter for the current scanline. While the "minimum-sum-of-absolute-
1219 * differences metric is relatively fast and effective, there is some
1220 * question as to whether it can be improved upon by trying to keep the
1221 * filtered data going to zlib more consistent, hopefully resulting in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001222 * better compression.
1223 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001224#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001225void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -05001226png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
1227 int num_weights, png_doublep filter_weights,
1228 png_doublep filter_costs)
1229{
1230 int i;
1231
1232 png_debug(1, "in png_set_filter_heuristics\n");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001233 if (png_ptr == NULL)
1234 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001235 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
1236 {
1237 png_warning(png_ptr, "Unknown filter heuristic method");
1238 return;
1239 }
1240
1241 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
1242 {
1243 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1244 }
1245
1246 if (num_weights < 0 || filter_weights == NULL ||
1247 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
1248 {
1249 num_weights = 0;
1250 }
1251
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001252 png_ptr->num_prev_filters = (png_byte)num_weights;
1253 png_ptr->heuristic_method = (png_byte)heuristic_method;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001254
1255 if (num_weights > 0)
1256 {
1257 if (png_ptr->prev_filters == NULL)
1258 {
1259 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001260 (png_uint_32)(png_sizeof(png_byte) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001261
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001262 /* To make sure that the weighting starts out fairly */
1263 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001264 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001265 png_ptr->prev_filters[i] = 255;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001266 }
1267 }
1268
1269 if (png_ptr->filter_weights == NULL)
1270 {
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001271 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001272 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001273
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001274 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001275 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001276 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001277 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001278 png_ptr->inv_filter_weights[i] =
1279 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001280 }
1281 }
1282
1283 for (i = 0; i < num_weights; i++)
1284 {
1285 if (filter_weights[i] < 0.0)
1286 {
1287 png_ptr->inv_filter_weights[i] =
1288 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1289 }
1290 else
1291 {
1292 png_ptr->inv_filter_weights[i] =
1293 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
1294 png_ptr->filter_weights[i] =
1295 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
1296 }
1297 }
1298 }
1299
1300 /* If, in the future, there are other filter methods, this would
1301 * need to be based on png_ptr->filter.
1302 */
1303 if (png_ptr->filter_costs == NULL)
1304 {
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001305 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001306 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001307
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001308 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001309 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001310
1311 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1312 {
1313 png_ptr->inv_filter_costs[i] =
1314 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1315 }
1316 }
1317
1318 /* Here is where we set the relative costs of the different filters. We
1319 * should take the desired compression level into account when setting
1320 * the costs, so that Paeth, for instance, has a high relative cost at low
1321 * compression levels, while it has a lower relative cost at higher
1322 * compression settings. The filter types are in order of increasing
1323 * relative cost, so it would be possible to do this with an algorithm.
1324 */
1325 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1326 {
1327 if (filter_costs == NULL || filter_costs[i] < 0.0)
1328 {
1329 png_ptr->inv_filter_costs[i] =
1330 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1331 }
1332 else if (filter_costs[i] >= 1.0)
1333 {
1334 png_ptr->inv_filter_costs[i] =
1335 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
1336 png_ptr->filter_costs[i] =
1337 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
1338 }
1339 }
1340}
1341#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1342
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001343void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001344png_set_compression_level(png_structp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001345{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001346 png_debug(1, "in png_set_compression_level\n");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001347 if (png_ptr == NULL)
1348 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001349 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001350 png_ptr->zlib_level = level;
1351}
1352
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001353void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001354png_set_compression_mem_level(png_structp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001355{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001356 png_debug(1, "in png_set_compression_mem_level\n");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001357 if (png_ptr == NULL)
1358 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001359 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001360 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001361}
1362
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001363void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001364png_set_compression_strategy(png_structp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001365{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001366 png_debug(1, "in png_set_compression_strategy\n");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001367 if (png_ptr == NULL)
1368 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001369 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001370 png_ptr->zlib_strategy = strategy;
1371}
1372
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001373void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001374png_set_compression_window_bits(png_structp png_ptr, int window_bits)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001375{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001376 if (png_ptr == NULL)
1377 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001378 if (window_bits > 15)
1379 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001380 else if (window_bits < 8)
1381 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001382#ifndef WBITS_8_OK
1383 /* avoid libpng bug with 256-byte windows */
1384 if (window_bits == 8)
1385 {
1386 png_warning(png_ptr, "Compression window is being reset to 512");
1387 window_bits=9;
1388 }
1389#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001390 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001391 png_ptr->zlib_window_bits = window_bits;
1392}
1393
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001394void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001395png_set_compression_method(png_structp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001396{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001397 png_debug(1, "in png_set_compression_method\n");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001398 if (png_ptr == NULL)
1399 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001400 if (method != 8)
1401 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1402 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001403 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -05001404}
1405
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001406void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001407png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1408{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001409 if (png_ptr == NULL)
1410 return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001411 png_ptr->write_row_fn = write_row_fn;
1412}
1413
1414#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001415void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001416png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
1417 write_user_transform_fn)
1418{
1419 png_debug(1, "in png_set_write_user_transform_fn\n");
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001420 if (png_ptr == NULL)
1421 return;
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001422 png_ptr->transformations |= PNG_USER_TRANSFORM;
1423 png_ptr->write_user_transform_fn = write_user_transform_fn;
1424}
1425#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001426
1427
1428#if defined(PNG_INFO_IMAGE_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001429void PNGAPI
1430png_write_png(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001431 int transforms, voidp params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001432{
Glenn Randers-Pehrson170b70c2006-03-10 10:19:04 -06001433 if (png_ptr == NULL || info_ptr == NULL)
1434 return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001435#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
1436 /* invert the alpha channel from opacity to transparency */
1437 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1438 png_set_invert_alpha(png_ptr);
1439#endif
1440
1441 /* Write the file header information. */
1442 png_write_info(png_ptr, info_ptr);
1443
1444 /* ------ these transformations don't touch the info structure ------- */
1445
1446#if defined(PNG_WRITE_INVERT_SUPPORTED)
1447 /* invert monochrome pixels */
1448 if (transforms & PNG_TRANSFORM_INVERT_MONO)
1449 png_set_invert_mono(png_ptr);
1450#endif
1451
1452#if defined(PNG_WRITE_SHIFT_SUPPORTED)
1453 /* Shift the pixels up to a legal bit depth and fill in
1454 * as appropriate to correctly scale the image.
1455 */
1456 if ((transforms & PNG_TRANSFORM_SHIFT)
1457 && (info_ptr->valid & PNG_INFO_sBIT))
1458 png_set_shift(png_ptr, &info_ptr->sig_bit);
1459#endif
1460
1461#if defined(PNG_WRITE_PACK_SUPPORTED)
1462 /* pack pixels into bytes */
1463 if (transforms & PNG_TRANSFORM_PACKING)
1464 png_set_packing(png_ptr);
1465#endif
1466
1467#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
1468 /* swap location of alpha bytes from ARGB to RGBA */
1469 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1470 png_set_swap_alpha(png_ptr);
1471#endif
1472
1473#if defined(PNG_WRITE_FILLER_SUPPORTED)
1474 /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
1475 * RGB (4 channels -> 3 channels). The second parameter is not used.
1476 */
1477 if (transforms & PNG_TRANSFORM_STRIP_FILLER)
1478 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
1479#endif
1480
1481#if defined(PNG_WRITE_BGR_SUPPORTED)
1482 /* flip BGR pixels to RGB */
1483 if (transforms & PNG_TRANSFORM_BGR)
1484 png_set_bgr(png_ptr);
1485#endif
1486
1487#if defined(PNG_WRITE_SWAP_SUPPORTED)
1488 /* swap bytes of 16-bit files to most significant byte first */
1489 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1490 png_set_swap(png_ptr);
1491#endif
1492
1493#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
1494 /* swap bits of 1, 2, 4 bit packed pixel formats */
1495 if (transforms & PNG_TRANSFORM_PACKSWAP)
1496 png_set_packswap(png_ptr);
1497#endif
1498
1499 /* ----------------------- end of transformations ------------------- */
1500
1501 /* write the bits */
1502 if (info_ptr->valid & PNG_INFO_IDAT)
1503 png_write_image(png_ptr, info_ptr->row_pointers);
1504
1505 /* It is REQUIRED to call this to finish writing the rest of the file */
1506 png_write_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001507
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001508 if(transforms == 0 || params == NULL)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001509 /* quiet compiler warnings */ return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001510}
1511#endif
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -06001512#endif /* PNG_WRITE_SUPPORTED */