blob: c17c8020a27d9bfb16f06db07b7e4e6038e323fd [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-Pehrsonb6ce43d1998-01-01 07:13:13 -06004 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06005 * Copyright (c) 1998-2006 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 */
Guy Schalnat0d580581995-07-20 02:43:20 -05009
10/* get internal access to png.h */
11#define PNG_INTERNAL
12#include "png.h"
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -060013#ifdef PNG_WRITE_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050014
Andreas Dilger47a0c421997-05-16 02:46:07 -050015/* Writes all the PNG information. This is the suggested way to use the
16 * library. If you have a new chunk to add, make a function to write it,
17 * and put it in the correct location here. If you want the chunk written
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050018 * after the image data, put it in png_write_end(). I strongly encourage
Andreas Dilger47a0c421997-05-16 02:46:07 -050019 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
20 * the chunk, as that will keep the code from breaking if you want to just
21 * write a plain PNG file. If you have long comments, I suggest writing
22 * them in png_write_end(), and compressing them.
23 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050024void PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060025png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050026{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -060027 png_debug(1, "in png_write_info_before_PLTE\n");
28 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
29 {
Guy Schalnat0d580581995-07-20 02:43:20 -050030 png_write_sig(png_ptr); /* write PNG signature */
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060031#if defined(PNG_MNG_FEATURES_SUPPORTED)
32 if((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&(png_ptr->mng_features_permitted))
33 {
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -060034 png_warning(png_ptr,"MNG features are not allowed in a PNG datastream");
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060035 png_ptr->mng_features_permitted=0;
36 }
37#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050038 /* write IHDR information. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060039 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
40 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060041 info_ptr->filter_type,
42#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
43 info_ptr->interlace_type);
44#else
45 0);
46#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050047 /* the rest of these check to see if the valid field has the appropriate
48 flag set, and if it does, writes the chunk. */
Guy Schalnat51f0eb41995-09-26 05:22:39 -050049#if defined(PNG_WRITE_gAMA_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060050 if (info_ptr->valid & PNG_INFO_gAMA)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060051 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060052# ifdef PNG_FLOATING_POINT_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060053 png_write_gAMA(png_ptr, info_ptr->gamma);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060054#else
55#ifdef PNG_FIXED_POINT_SUPPORTED
56 png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060057# endif
58#endif
59 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -050060#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060061#if defined(PNG_WRITE_sRGB_SUPPORTED)
62 if (info_ptr->valid & PNG_INFO_sRGB)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060063 png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060064#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060065#if defined(PNG_WRITE_iCCP_SUPPORTED)
66 if (info_ptr->valid & PNG_INFO_iCCP)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -060067 png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060068 info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
69#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050070#if defined(PNG_WRITE_sBIT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060071 if (info_ptr->valid & PNG_INFO_sBIT)
72 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050073#endif
74#if defined(PNG_WRITE_cHRM_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060075 if (info_ptr->valid & PNG_INFO_cHRM)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060076 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060077#ifdef PNG_FLOATING_POINT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050078 png_write_cHRM(png_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060079 info_ptr->x_white, info_ptr->y_white,
80 info_ptr->x_red, info_ptr->y_red,
81 info_ptr->x_green, info_ptr->y_green,
82 info_ptr->x_blue, info_ptr->y_blue);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060083#else
84# ifdef PNG_FIXED_POINT_SUPPORTED
85 png_write_cHRM_fixed(png_ptr,
86 info_ptr->int_x_white, info_ptr->int_y_white,
87 info_ptr->int_x_red, info_ptr->int_y_red,
88 info_ptr->int_x_green, info_ptr->int_y_green,
89 info_ptr->int_x_blue, info_ptr->int_y_blue);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060090# endif
91#endif
92 }
93#endif
94#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
95 if (info_ptr->unknown_chunks_num)
96 {
97 png_unknown_chunk *up;
98
99 png_debug(5, "writing extra chunks\n");
100
101 for (up = info_ptr->unknown_chunks;
102 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
103 up++)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600104 {
105 int keep=png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500106 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500107 up->location && !(up->location & PNG_HAVE_PLTE) &&
108 !(up->location & PNG_HAVE_IDAT) &&
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500109 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600110 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
111 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600112 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600113 }
114 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600115 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500116#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600117 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
118 }
119}
120
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500121void PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600122png_write_info(png_structp png_ptr, png_infop info_ptr)
123{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600124#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600125 int i;
126#endif
127
128 png_debug(1, "in png_write_info\n");
129
130 png_write_info_before_PLTE(png_ptr, info_ptr);
131
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600132 if (info_ptr->valid & PNG_INFO_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500133 png_write_PLTE(png_ptr, info_ptr->palette,
134 (png_uint_32)info_ptr->num_palette);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600135 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600136 png_error(png_ptr, "Valid palette required for paletted images");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600137
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500138#if defined(PNG_WRITE_tRNS_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600139 if (info_ptr->valid & PNG_INFO_tRNS)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600140 {
141#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
142 /* invert the alpha channel (in tRNS) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600143 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600144 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
145 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600146 int j;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600147 for (j=0; j<(int)info_ptr->num_trans; j++)
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500148 info_ptr->trans[j] = (png_byte)(255 - info_ptr->trans[j]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600149 }
150#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600151 png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values),
152 info_ptr->num_trans, info_ptr->color_type);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600153 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500154#endif
155#if defined(PNG_WRITE_bKGD_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600156 if (info_ptr->valid & PNG_INFO_bKGD)
157 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500158#endif
159#if defined(PNG_WRITE_hIST_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600160 if (info_ptr->valid & PNG_INFO_hIST)
161 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500162#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500163#if defined(PNG_WRITE_oFFs_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600164 if (info_ptr->valid & PNG_INFO_oFFs)
165 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
166 info_ptr->offset_unit_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500167#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500168#if defined(PNG_WRITE_pCAL_SUPPORTED)
169 if (info_ptr->valid & PNG_INFO_pCAL)
170 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
171 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
172 info_ptr->pcal_units, info_ptr->pcal_params);
173#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600174#if defined(PNG_WRITE_sCAL_SUPPORTED)
175 if (info_ptr->valid & PNG_INFO_sCAL)
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600176#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600177 png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,
178 info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);
179#else
180#ifdef PNG_FIXED_POINT_SUPPORTED
181 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600182 info_ptr->scal_s_width, info_ptr->scal_s_height);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600183#else
184 png_warning(png_ptr,
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600185 "png_write_sCAL not supported; sCAL chunk not written.");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600186#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600187#endif
188#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500189#if defined(PNG_WRITE_pHYs_SUPPORTED)
190 if (info_ptr->valid & PNG_INFO_pHYs)
191 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
192 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
193#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500194#if defined(PNG_WRITE_tIME_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600195 if (info_ptr->valid & PNG_INFO_tIME)
Guy Schalnate5a37791996-06-05 15:50:50 -0500196 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600197 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600198 png_ptr->mode |= PNG_WROTE_tIME;
Guy Schalnate5a37791996-06-05 15:50:50 -0500199 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500200#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600201#if defined(PNG_WRITE_sPLT_SUPPORTED)
202 if (info_ptr->valid & PNG_INFO_sPLT)
203 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
204 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
205#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600206#if defined(PNG_WRITE_TEXT_SUPPORTED)
Guy Schalnate5a37791996-06-05 15:50:50 -0500207 /* Check to see if we need to write text chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500208 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500209 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500210 png_debug2(2, "Writing header text chunk %d, type %d\n", i,
211 info_ptr->text[i].compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600212 /* an internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600213 if (info_ptr->text[i].compression > 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600214 {
215#if defined(PNG_WRITE_iTXt_SUPPORTED)
216 /* write international chunk */
217 png_write_iTXt(png_ptr,
218 info_ptr->text[i].compression,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600219 info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600220 info_ptr->text[i].lang,
221 info_ptr->text[i].lang_key,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600222 info_ptr->text[i].text);
223#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600224 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600225#endif
226 /* Mark this chunk as written */
227 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
228 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500229 /* If we want a compressed text chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600230 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500231 {
232#if defined(PNG_WRITE_zTXt_SUPPORTED)
233 /* write compressed chunk */
234 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600235 info_ptr->text[i].text, 0,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500236 info_ptr->text[i].compression);
237#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600238 png_warning(png_ptr, "Unable to write compressed text");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500239#endif
240 /* Mark this chunk as written */
241 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
242 }
243 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
244 {
245#if defined(PNG_WRITE_tEXt_SUPPORTED)
246 /* write uncompressed chunk */
247 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600248 info_ptr->text[i].text,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600249 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500250#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600251 png_warning(png_ptr, "Unable to write uncompressed text");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500252#endif
253 /* Mark this chunk as written */
254 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
255 }
256 }
257#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600258#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
259 if (info_ptr->unknown_chunks_num)
260 {
261 png_unknown_chunk *up;
262
263 png_debug(5, "writing extra chunks\n");
264
265 for (up = info_ptr->unknown_chunks;
266 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
267 up++)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600268 {
269 int keep=png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500270 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600271 up->location && (up->location & PNG_HAVE_PLTE) &&
272 !(up->location & PNG_HAVE_IDAT) &&
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500273 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600274 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
275 {
276 png_write_chunk(png_ptr, up->name, up->data, up->size);
277 }
278 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600279 }
280#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500281}
Guy Schalnat0d580581995-07-20 02:43:20 -0500282
Andreas Dilger47a0c421997-05-16 02:46:07 -0500283/* Writes the end of the PNG file. If you don't want to write comments or
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600284 * time information, you can pass NULL for info. If you already wrote these
285 * in png_write_info(), do not write them again here. If you have long
286 * comments, I suggest writing them here, and compressing them.
287 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500288void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500289png_write_end(png_structp png_ptr, png_infop info_ptr)
290{
291 png_debug(1, "in png_write_end\n");
292 if (!(png_ptr->mode & PNG_HAVE_IDAT))
293 png_error(png_ptr, "No IDATs written into file");
294
295 /* see if user wants us to write information chunks */
296 if (info_ptr != NULL)
297 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600298#if defined(PNG_WRITE_TEXT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500299 int i; /* local index variable */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600300#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500301#if defined(PNG_WRITE_tIME_SUPPORTED)
302 /* check to see if user has supplied a time chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600303 if ((info_ptr->valid & PNG_INFO_tIME) &&
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600304 !(png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500305 png_write_tIME(png_ptr, &(info_ptr->mod_time));
306#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600307#if defined(PNG_WRITE_TEXT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500308 /* loop through comment chunks */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600309 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500310 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500311 png_debug2(2, "Writing trailer text chunk %d, type %d\n", i,
312 info_ptr->text[i].compression);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600313 /* an internationalized chunk? */
314 if (info_ptr->text[i].compression > 0)
315 {
316#if defined(PNG_WRITE_iTXt_SUPPORTED)
317 /* write international chunk */
318 png_write_iTXt(png_ptr,
319 info_ptr->text[i].compression,
320 info_ptr->text[i].key,
321 info_ptr->text[i].lang,
322 info_ptr->text[i].lang_key,
323 info_ptr->text[i].text);
324#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600325 png_warning(png_ptr, "Unable to write international text");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600326#endif
327 /* Mark this chunk as written */
328 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
329 }
330 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
Guy Schalnat0d580581995-07-20 02:43:20 -0500331 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500332#if defined(PNG_WRITE_zTXt_SUPPORTED)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600333 /* write compressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600334 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600335 info_ptr->text[i].text, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600336 info_ptr->text[i].compression);
Guy Schalnate5a37791996-06-05 15:50:50 -0500337#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600338 png_warning(png_ptr, "Unable to write compressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500339#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500340 /* Mark this chunk as written */
341 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500342 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500343 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500344 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500345#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500346 /* write uncompressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600347 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600348 info_ptr->text[i].text, 0);
Guy Schalnate5a37791996-06-05 15:50:50 -0500349#else
Glenn Randers-Pehrsonc3d51c12006-03-02 07:23:18 -0600350 png_warning(png_ptr, "Unable to write uncompressed text");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500351#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500352
Andreas Dilger47a0c421997-05-16 02:46:07 -0500353 /* Mark this chunk as written */
354 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500355 }
356 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600357#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600358#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
359 if (info_ptr->unknown_chunks_num)
360 {
361 png_unknown_chunk *up;
362
363 png_debug(5, "writing extra chunks\n");
364
365 for (up = info_ptr->unknown_chunks;
366 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
367 up++)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600368 {
369 int keep=png_handle_as_unknown(png_ptr, up->name);
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500370 if (keep != PNG_HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600371 up->location && (up->location & PNG_AFTER_IDAT) &&
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -0500372 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600373 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
374 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600375 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600376 }
377 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600378 }
379#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500380 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500381
382 png_ptr->mode |= PNG_AFTER_IDAT;
383
Andreas Dilger47a0c421997-05-16 02:46:07 -0500384 /* write end of PNG file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500385 png_write_IEND(png_ptr);
Glenn Randers-Pehrsond56aca72000-11-23 11:51:42 -0600386#if 0
387/* This flush, added in libpng-1.0.8, causes some applications to crash
388 because they do not set png_ptr->output_flush_fn */
Glenn Randers-Pehrson32fc5ce2000-07-24 06:34:14 -0500389 png_flush(png_ptr);
Glenn Randers-Pehrsond56aca72000-11-23 11:51:42 -0600390#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500391}
392
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500393#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500394#if !defined(_WIN32_WCE)
395/* "time.h" functions are not supported on WindowsCE */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500396void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600397png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500398{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500399 png_debug(1, "in png_convert_from_struct_tm\n");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600400 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
401 ptime->month = (png_byte)(ttime->tm_mon + 1);
402 ptime->day = (png_byte)ttime->tm_mday;
403 ptime->hour = (png_byte)ttime->tm_hour;
404 ptime->minute = (png_byte)ttime->tm_min;
405 ptime->second = (png_byte)ttime->tm_sec;
Guy Schalnat0d580581995-07-20 02:43:20 -0500406}
407
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500408void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600409png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500410{
411 struct tm *tbuf;
412
Andreas Dilger47a0c421997-05-16 02:46:07 -0500413 png_debug(1, "in png_convert_from_time_t\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500414 tbuf = gmtime(&ttime);
415 png_convert_from_struct_tm(ptime, tbuf);
416}
Guy Schalnat6d764711995-12-19 03:22:19 -0600417#endif
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500418#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500419
Andreas Dilger47a0c421997-05-16 02:46:07 -0500420/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500421png_structp PNGAPI
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500422png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600423 png_error_ptr error_fn, png_error_ptr warn_fn)
Guy Schalnat0d580581995-07-20 02:43:20 -0500424{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500425#ifdef PNG_USER_MEM_SUPPORTED
426 return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500427 warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500428}
429
430/* Alternate initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500431png_structp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500432png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
433 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
434 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
435{
436#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500437 png_structp png_ptr;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600438#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600439#ifdef USE_FAR_KEYWORD
440 jmp_buf jmpbuf;
441#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600442#endif
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500443 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500444 png_debug(1, "in png_create_write_struct\n");
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500445#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600446 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
447 (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500448#else
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600449 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500450#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600451 if (png_ptr == NULL)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500452 return (NULL);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600453
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600454#if !defined(PNG_1_0_X)
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500455#ifdef PNG_ASSEMBLER_CODE_SUPPORTED
456 png_init_mmx_flags(png_ptr); /* 1.2.0 addition */
457#endif
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600458#endif /* PNG_1_0_X */
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500459
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500460 /* added at libpng-1.2.6 */
461#ifdef PNG_SET_USER_LIMITS_SUPPORTED
462 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
463 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
464#endif
465
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600466#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600467#ifdef USE_FAR_KEYWORD
468 if (setjmp(jmpbuf))
469#else
Guy Schalnate5a37791996-06-05 15:50:50 -0500470 if (setjmp(png_ptr->jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600471#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500472 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600473 png_free(png_ptr, png_ptr->zbuf);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500474 png_ptr->zbuf=NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500475 png_destroy_struct(png_ptr);
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500476 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500477 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600478#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500479 png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600480#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600481#endif
482
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500483#ifdef PNG_USER_MEM_SUPPORTED
484 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
485#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600486 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnate5a37791996-06-05 15:50:50 -0500487
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500488 i=0;
489 do
Guy Schalnate5a37791996-06-05 15:50:50 -0500490 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500491 if(user_png_ver[i] != png_libpng_ver[i])
492 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
493 } while (png_libpng_ver[i++]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500494
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500495 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500496 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500497 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
498 * we must recompile any applications that use any older library version.
499 * For versions after libpng 1.0, we will be compatible, so we need
500 * only check the first digit.
501 */
502 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500503 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500504 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
505 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500506#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
507 char msg[80];
508 if (user_png_ver)
509 {
510 sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
511 user_png_ver);
512 png_warning(png_ptr, msg);
513 }
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600514 sprintf(msg, "Application is running with png.c from libpng-%.20s",
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500515 png_libpng_ver);
516 png_warning(png_ptr, msg);
517#endif
518#ifdef PNG_ERROR_NUMBERS_SUPPORTED
519 png_ptr->flags=0;
520#endif
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500521 png_error(png_ptr,
522 "Incompatible libpng version in application and library");
523 }
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500524 }
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500525
Guy Schalnat0d580581995-07-20 02:43:20 -0500526 /* initialize zbuf - compression buffer */
527 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600528 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
529 (png_uint_32)png_ptr->zbuf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500530
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500531 png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
532 png_flush_ptr_NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500533
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600534#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
535 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500536 1, png_doublep_NULL, png_doublep_NULL);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600537#endif
538
Glenn Randers-Pehrson03008a02002-04-27 10:11:25 -0500539#ifdef PNG_SETJMP_SUPPORTED
540/* Applications that neglect to set up their own setjmp() and then encounter
541 a png_error() will longjmp here. Since the jmpbuf is then meaningless we
542 abort instead of returning. */
543#ifdef USE_FAR_KEYWORD
544 if (setjmp(jmpbuf))
545 PNG_ABORT();
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500546 png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
Glenn Randers-Pehrson03008a02002-04-27 10:11:25 -0500547#else
548 if (setjmp(png_ptr->jmpbuf))
549 PNG_ABORT();
550#endif
551#endif
552 return (png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500553}
554
Andreas Dilger47a0c421997-05-16 02:46:07 -0500555/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600556#if defined(PNG_1_0_X) || defined (PNG_1_2_X)
557/* Deprecated. */
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500558#undef png_write_init
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500559void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500560png_write_init(png_structp png_ptr)
561{
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500562 /* We only come here via pre-1.0.7-compiled applications */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500563 png_write_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500564}
565
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500566void PNGAPI
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500567png_write_init_2(png_structp png_ptr, png_const_charp user_png_ver,
568 png_size_t png_struct_size, png_size_t png_info_size)
569{
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500570 /* We only come here via pre-1.0.12-compiled applications */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500571#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500572 if(png_sizeof(png_struct) > png_struct_size ||
573 png_sizeof(png_info) > png_info_size)
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500574 {
575 char msg[80];
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500576 png_ptr->warning_fn=NULL;
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500577 if (user_png_ver)
578 {
579 sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
580 user_png_ver);
581 png_warning(png_ptr, msg);
582 }
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600583 sprintf(msg, "Application is running with png.c from libpng-%.20s",
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500584 png_libpng_ver);
585 png_warning(png_ptr, msg);
586 }
587#endif
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500588 if(png_sizeof(png_struct) > png_struct_size)
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500589 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500590 png_ptr->error_fn=NULL;
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500591#ifdef PNG_ERROR_NUMBERS_SUPPORTED
592 png_ptr->flags=0;
593#endif
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500594 png_error(png_ptr,
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500595 "The png struct allocated by the application for writing is too small.");
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500596 }
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500597 if(png_sizeof(png_info) > png_info_size)
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500598 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500599 png_ptr->error_fn=NULL;
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500600#ifdef PNG_ERROR_NUMBERS_SUPPORTED
601 png_ptr->flags=0;
602#endif
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500603 png_error(png_ptr,
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500604 "The info struct allocated by the application for writing is too small.");
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500605 }
606 png_write_init_3(&png_ptr, user_png_ver, png_struct_size);
607}
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600608#endif /* PNG_1_0_X || PNG_1_2_X */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500609
610
611void PNGAPI
612png_write_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
613 png_size_t png_struct_size)
614{
615 png_structp png_ptr=*ptr_ptr;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600616#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500617 jmp_buf tmp_jmp; /* to save current jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600618#endif
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500619 int i = 0;
620 do
621 {
622 if (user_png_ver[i] != png_libpng_ver[i])
623 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500624#ifdef PNG_LEGACY_SUPPORTED
625 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
626#else
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500627 png_ptr->warning_fn=NULL;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500628 png_warning(png_ptr,
629 "Application uses deprecated png_write_init() and should be recompiled.");
630 break;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500631#endif
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500632 }
633 } while (png_libpng_ver[i++]);
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500634
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500635 png_debug(1, "in png_write_init_3\n");
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500636
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600637#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500638 /* save jump buffer and error functions */
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500639 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600640#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500641
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500642 if (png_sizeof(png_struct) > png_struct_size)
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500643 {
644 png_destroy_struct(png_ptr);
645 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
646 *ptr_ptr = png_ptr;
647 }
648
Guy Schalnate5a37791996-06-05 15:50:50 -0500649 /* reset all variables to 0 */
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500650 png_memset(png_ptr, 0, png_sizeof (png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -0500651
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500652 /* added at libpng-1.2.6 */
653#ifdef PNG_SET_USER_LIMITS_SUPPORTED
654 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
655 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
656#endif
657
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600658#if !defined(PNG_1_0_X)
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500659#ifdef PNG_ASSEMBLER_CODE_SUPPORTED
660 png_init_mmx_flags(png_ptr); /* 1.2.0 addition */
661#endif
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600662#endif /* PNG_1_0_X */
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500663
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600664#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500665 /* restore jump buffer */
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500666 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600667#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500668
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500669 png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
670 png_flush_ptr_NULL);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500671
Guy Schalnate5a37791996-06-05 15:50:50 -0500672 /* initialize zbuf - compression buffer */
673 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600674 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
675 (png_uint_32)png_ptr->zbuf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500676
677#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
678 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500679 1, png_doublep_NULL, png_doublep_NULL);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500680#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500681}
682
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600683/* Write a few rows of image data. If the image is interlaced,
684 * either you will have to write the 7 sub images, or, if you
685 * have called png_set_interlace_handling(), you will have to
686 * "write" the image seven times.
687 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500688void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600689png_write_rows(png_structp png_ptr, png_bytepp row,
Guy Schalnat0d580581995-07-20 02:43:20 -0500690 png_uint_32 num_rows)
691{
692 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600693 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500694
Andreas Dilger47a0c421997-05-16 02:46:07 -0500695 png_debug(1, "in png_write_rows\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500696 /* loop through the rows */
697 for (i = 0, rp = row; i < num_rows; i++, rp++)
698 {
699 png_write_row(png_ptr, *rp);
700 }
701}
702
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600703/* Write the image. You only need to call this function once, even
704 * if you are writing an interlaced image.
705 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500706void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600707png_write_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500708{
709 png_uint_32 i; /* row index */
710 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600711 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500712
Andreas Dilger47a0c421997-05-16 02:46:07 -0500713 png_debug(1, "in png_write_image\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600714#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500715 /* intialize interlace handling. If image is not interlaced,
716 this will set pass to 1 */
717 num_pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600718#else
719 num_pass = 1;
720#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500721 /* loop through passes */
722 for (pass = 0; pass < num_pass; pass++)
723 {
724 /* loop through image */
725 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
726 {
727 png_write_row(png_ptr, *rp);
728 }
729 }
730}
731
Guy Schalnate5a37791996-06-05 15:50:50 -0500732/* called by user to write a row of image data */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500733void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600734png_write_row(png_structp png_ptr, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500735{
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600736 png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
737 png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500738 /* initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600739 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500740 {
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600741 /* make sure we wrote the header info */
742 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
743 png_error(png_ptr,
744 "png_write_info was never called before png_write_row.");
745
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500746 /* check for transforms that have been set but were defined out */
747#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
748 if (png_ptr->transformations & PNG_INVERT_MONO)
749 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
750#endif
751#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
752 if (png_ptr->transformations & PNG_FILLER)
753 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
754#endif
755#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
756 if (png_ptr->transformations & PNG_PACKSWAP)
757 png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
758#endif
759#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
760 if (png_ptr->transformations & PNG_PACK)
761 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined.");
762#endif
763#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
764 if (png_ptr->transformations & PNG_SHIFT)
765 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
766#endif
767#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
768 if (png_ptr->transformations & PNG_BGR)
769 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined.");
770#endif
771#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
772 if (png_ptr->transformations & PNG_SWAP_BYTES)
773 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
774#endif
775
Guy Schalnat0d580581995-07-20 02:43:20 -0500776 png_write_start_row(png_ptr);
777 }
778
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500779#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500780 /* if interlaced and not interested in row, return */
781 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
782 {
783 switch (png_ptr->pass)
784 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600785 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600786 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500787 {
788 png_write_finish_row(png_ptr);
789 return;
790 }
791 break;
792 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600793 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500794 {
795 png_write_finish_row(png_ptr);
796 return;
797 }
798 break;
799 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600800 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500801 {
802 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600803 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500804 }
805 break;
806 case 3:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600807 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
Guy Schalnat0d580581995-07-20 02:43:20 -0500808 {
809 png_write_finish_row(png_ptr);
810 return;
811 }
812 break;
813 case 4:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600814 if ((png_ptr->row_number & 0x03) != 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500815 {
816 png_write_finish_row(png_ptr);
817 return;
818 }
819 break;
820 case 5:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600821 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500822 {
823 png_write_finish_row(png_ptr);
824 return;
825 }
826 break;
827 case 6:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600828 if (!(png_ptr->row_number & 0x01))
Guy Schalnat0d580581995-07-20 02:43:20 -0500829 {
830 png_write_finish_row(png_ptr);
831 return;
832 }
833 break;
834 }
835 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600836#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500837
838 /* set up row info for transformations */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600839 png_ptr->row_info.color_type = png_ptr->color_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500840 png_ptr->row_info.width = png_ptr->usr_width;
841 png_ptr->row_info.channels = png_ptr->usr_channels;
842 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600843 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
844 png_ptr->row_info.channels);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600845
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500846 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
847 png_ptr->row_info.width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500848
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600849 png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500850 png_debug1(3, "row_info->width = %lu\n", png_ptr->row_info.width);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600851 png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels);
852 png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth);
853 png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500854 png_debug1(3, "row_info->rowbytes = %lu\n", png_ptr->row_info.rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500855
856 /* Copy user's row into buffer, leaving room for filter byte. */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600857 png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600858 png_ptr->row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500859
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500860#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500861 /* handle interlacing */
862 if (png_ptr->interlaced && png_ptr->pass < 6 &&
863 (png_ptr->transformations & PNG_INTERLACE))
864 {
865 png_do_write_interlace(&(png_ptr->row_info),
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600866 png_ptr->row_buf + 1, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500867 /* this should always get caught above, but still ... */
868 if (!(png_ptr->row_info.width))
869 {
870 png_write_finish_row(png_ptr);
871 return;
872 }
873 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600874#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500875
876 /* handle other transformations */
877 if (png_ptr->transformations)
878 png_do_write_transformations(png_ptr);
879
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600880#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600881 /* Write filter_method 64 (intrapixel differencing) only if
882 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
883 * 2. Libpng did not write a PNG signature (this filter_method is only
884 * used in PNG datastreams that are embedded in MNG datastreams) and
885 * 3. The application called png_permit_mng_features with a mask that
886 * included PNG_FLAG_MNG_FILTER_64 and
887 * 4. The filter_method is 64 and
888 * 5. The color_type is RGB or RGBA
889 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600890 if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
891 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
892 {
893 /* Intrapixel differencing */
894 png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
895 }
896#endif
897
Andreas Dilger47a0c421997-05-16 02:46:07 -0500898 /* Find a filter if necessary, filter the row and write it out. */
Guy Schalnate5a37791996-06-05 15:50:50 -0500899 png_write_find_filter(png_ptr, &(png_ptr->row_info));
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600900
901 if (png_ptr->write_row_fn != NULL)
902 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500903}
904
Guy Schalnat0f716451995-11-28 11:22:13 -0600905#if defined(PNG_WRITE_FLUSH_SUPPORTED)
906/* Set the automatic flush interval or 0 to turn flushing off */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500907void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600908png_set_flush(png_structp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600909{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500910 png_debug(1, "in png_set_flush\n");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600911 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600912}
913
914/* flush the current output buffers now */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500915void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600916png_write_flush(png_structp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600917{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600918 int wrote_IDAT;
Guy Schalnat0f716451995-11-28 11:22:13 -0600919
Andreas Dilger47a0c421997-05-16 02:46:07 -0500920 png_debug(1, "in png_write_flush\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500921 /* We have already written out all of the data */
922 if (png_ptr->row_number >= png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600923 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600924
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600925 do
926 {
927 int ret;
Guy Schalnat0f716451995-11-28 11:22:13 -0600928
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600929 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600930 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600931 wrote_IDAT = 0;
Guy Schalnat0f716451995-11-28 11:22:13 -0600932
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600933 /* check for compression errors */
934 if (ret != Z_OK)
935 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500936 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600937 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600938 else
939 png_error(png_ptr, "zlib error");
940 }
Guy Schalnat0f716451995-11-28 11:22:13 -0600941
Andreas Dilger47a0c421997-05-16 02:46:07 -0500942 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600943 {
944 /* write the IDAT and reset the zlib output buffer */
945 png_write_IDAT(png_ptr, png_ptr->zbuf,
946 png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600947 png_ptr->zstream.next_out = png_ptr->zbuf;
948 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600949 wrote_IDAT = 1;
950 }
951 } while(wrote_IDAT == 1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600952
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600953 /* If there is any data left to be output, write it into a new IDAT */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600954 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600955 {
956 /* write the IDAT and reset the zlib output buffer */
957 png_write_IDAT(png_ptr, png_ptr->zbuf,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600958 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
959 png_ptr->zstream.next_out = png_ptr->zbuf;
960 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600961 }
962 png_ptr->flush_rows = 0;
963 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600964}
965#endif /* PNG_WRITE_FLUSH_SUPPORTED */
966
Guy Schalnate5a37791996-06-05 15:50:50 -0500967/* free all memory used by the write */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500968void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600969png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500970{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600971 png_structp png_ptr = NULL;
972 png_infop info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500973#ifdef PNG_USER_MEM_SUPPORTED
974 png_free_ptr free_fn = NULL;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500975 png_voidp mem_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500976#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600977
Andreas Dilger47a0c421997-05-16 02:46:07 -0500978 png_debug(1, "in png_destroy_write_struct\n");
979 if (png_ptr_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500980 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600981 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500982#ifdef PNG_USER_MEM_SUPPORTED
983 free_fn = png_ptr->free_fn;
Glenn Randers-Pehrsonfcbd7872002-04-07 16:35:38 -0500984 mem_ptr = png_ptr->mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500985#endif
986 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600987
Andreas Dilger47a0c421997-05-16 02:46:07 -0500988 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600989 info_ptr = *info_ptr_ptr;
990
Andreas Dilger47a0c421997-05-16 02:46:07 -0500991 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500992 {
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600993 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
994
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500995#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600996 if (png_ptr->num_chunk_list)
997 {
998 png_free(png_ptr, png_ptr->chunk_list);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500999 png_ptr->chunk_list=NULL;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001000 png_ptr->num_chunk_list=0;
1001 }
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001002#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001003
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001004#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001005 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
1006 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001007#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001008 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001009#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001010 *info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001011 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001012
Andreas Dilger47a0c421997-05-16 02:46:07 -05001013 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001014 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001015 png_write_destroy(png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001016#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001017 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
1018 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001019#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001020 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001021#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001022 *png_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001023 }
1024}
1025
1026
Andreas Dilger47a0c421997-05-16 02:46:07 -05001027/* Free any memory used in png_ptr struct (old method) */
Glenn Randers-Pehrsonf64a06f2001-04-11 07:38:00 -05001028void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001029png_write_destroy(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001030{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001031#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001032 jmp_buf tmp_jmp; /* save jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001033#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001034 png_error_ptr error_fn;
1035 png_error_ptr warning_fn;
1036 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001037#ifdef PNG_USER_MEM_SUPPORTED
1038 png_free_ptr free_fn;
1039#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001040
Andreas Dilger47a0c421997-05-16 02:46:07 -05001041 png_debug(1, "in png_write_destroy\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001042 /* free any memory zlib uses */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001043 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -05001044
Guy Schalnat0d580581995-07-20 02:43:20 -05001045 /* free our memory. png_free checks NULL for us. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001046 png_free(png_ptr, png_ptr->zbuf);
1047 png_free(png_ptr, png_ptr->row_buf);
1048 png_free(png_ptr, png_ptr->prev_row);
1049 png_free(png_ptr, png_ptr->sub_row);
1050 png_free(png_ptr, png_ptr->up_row);
1051 png_free(png_ptr, png_ptr->avg_row);
1052 png_free(png_ptr, png_ptr->paeth_row);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001053
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001054#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001055 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001056#endif
1057
Andreas Dilger47a0c421997-05-16 02:46:07 -05001058#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1059 png_free(png_ptr, png_ptr->prev_filters);
1060 png_free(png_ptr, png_ptr->filter_weights);
1061 png_free(png_ptr, png_ptr->inv_filter_weights);
1062 png_free(png_ptr, png_ptr->filter_costs);
1063 png_free(png_ptr, png_ptr->inv_filter_costs);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001064#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001065
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001066#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001067 /* reset structure */
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001068 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001069#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001070
1071 error_fn = png_ptr->error_fn;
1072 warning_fn = png_ptr->warning_fn;
1073 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001074#ifdef PNG_USER_MEM_SUPPORTED
1075 free_fn = png_ptr->free_fn;
1076#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001077
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001078 png_memset(png_ptr, 0, png_sizeof (png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -05001079
1080 png_ptr->error_fn = error_fn;
1081 png_ptr->warning_fn = warning_fn;
1082 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001083#ifdef PNG_USER_MEM_SUPPORTED
1084 png_ptr->free_fn = free_fn;
1085#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001086
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001087#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001088 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001089#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001090}
Guy Schalnate5a37791996-06-05 15:50:50 -05001091
Andreas Dilger47a0c421997-05-16 02:46:07 -05001092/* Allow the application to select one or more row filters to use. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001093void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -05001094png_set_filter(png_structp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001095{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001096 png_debug(1, "in png_set_filter\n");
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -06001097#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrsonf05f8032000-12-23 14:27:39 -06001098 if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -06001099 (method == PNG_INTRAPIXEL_DIFFERENCING))
1100 method = PNG_FILTER_TYPE_BASE;
1101#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001102 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001103 {
1104 switch (filters & (PNG_ALL_FILTERS | 0x07))
1105 {
1106 case 5:
1107 case 6:
Andreas Dilger47a0c421997-05-16 02:46:07 -05001108 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
1109 case PNG_FILTER_VALUE_NONE: png_ptr->do_filter=PNG_FILTER_NONE; break;
1110 case PNG_FILTER_VALUE_SUB: png_ptr->do_filter=PNG_FILTER_SUB; break;
1111 case PNG_FILTER_VALUE_UP: png_ptr->do_filter=PNG_FILTER_UP; break;
1112 case PNG_FILTER_VALUE_AVG: png_ptr->do_filter=PNG_FILTER_AVG; break;
1113 case PNG_FILTER_VALUE_PAETH: png_ptr->do_filter=PNG_FILTER_PAETH;break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001114 default: png_ptr->do_filter = (png_byte)filters; break;
1115 }
1116
Andreas Dilger47a0c421997-05-16 02:46:07 -05001117 /* If we have allocated the row_buf, this means we have already started
1118 * with the image and we should have allocated all of the filter buffers
1119 * that have been selected. If prev_row isn't already allocated, then
1120 * it is too late to start using the filters that need it, since we
1121 * will be missing the data in the previous row. If an application
1122 * wants to start and stop using particular filters during compression,
1123 * it should start out with all of the filters, and then add and
1124 * remove them after the start of compression.
Guy Schalnate5a37791996-06-05 15:50:50 -05001125 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001126 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001127 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001128 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001129 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001130 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001131 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001132 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001133 }
1134
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001135 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001136 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001137 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001138 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001139 png_warning(png_ptr, "Can't add Up filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -05001140 png_ptr->do_filter &= ~PNG_FILTER_UP;
1141 }
1142 else
1143 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001144 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001145 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001146 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001147 }
1148 }
1149
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001150 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001151 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001152 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001153 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001154 png_warning(png_ptr, "Can't add Average filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -05001155 png_ptr->do_filter &= ~PNG_FILTER_AVG;
1156 }
1157 else
1158 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001159 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001160 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001161 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001162 }
1163 }
1164
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001165 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
Andreas Dilger47a0c421997-05-16 02:46:07 -05001166 png_ptr->paeth_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001167 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001168 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001169 {
1170 png_warning(png_ptr, "Can't add Paeth filter after starting");
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001171 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
Guy Schalnate5a37791996-06-05 15:50:50 -05001172 }
1173 else
1174 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001175 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001176 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001177 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001178 }
1179 }
1180
1181 if (png_ptr->do_filter == PNG_NO_FILTERS)
1182 png_ptr->do_filter = PNG_FILTER_NONE;
1183 }
1184 }
1185 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05001186 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001187}
1188
Andreas Dilger47a0c421997-05-16 02:46:07 -05001189/* This allows us to influence the way in which libpng chooses the "best"
1190 * filter for the current scanline. While the "minimum-sum-of-absolute-
1191 * differences metric is relatively fast and effective, there is some
1192 * question as to whether it can be improved upon by trying to keep the
1193 * filtered data going to zlib more consistent, hopefully resulting in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001194 * better compression.
1195 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001196#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001197void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -05001198png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
1199 int num_weights, png_doublep filter_weights,
1200 png_doublep filter_costs)
1201{
1202 int i;
1203
1204 png_debug(1, "in png_set_filter_heuristics\n");
1205 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
1206 {
1207 png_warning(png_ptr, "Unknown filter heuristic method");
1208 return;
1209 }
1210
1211 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
1212 {
1213 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1214 }
1215
1216 if (num_weights < 0 || filter_weights == NULL ||
1217 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
1218 {
1219 num_weights = 0;
1220 }
1221
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001222 png_ptr->num_prev_filters = (png_byte)num_weights;
1223 png_ptr->heuristic_method = (png_byte)heuristic_method;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001224
1225 if (num_weights > 0)
1226 {
1227 if (png_ptr->prev_filters == NULL)
1228 {
1229 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001230 (png_uint_32)(png_sizeof(png_byte) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001231
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001232 /* To make sure that the weighting starts out fairly */
1233 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001234 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001235 png_ptr->prev_filters[i] = 255;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001236 }
1237 }
1238
1239 if (png_ptr->filter_weights == NULL)
1240 {
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001241 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001242 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001243
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001244 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001245 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001246 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001247 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001248 png_ptr->inv_filter_weights[i] =
1249 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001250 }
1251 }
1252
1253 for (i = 0; i < num_weights; i++)
1254 {
1255 if (filter_weights[i] < 0.0)
1256 {
1257 png_ptr->inv_filter_weights[i] =
1258 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1259 }
1260 else
1261 {
1262 png_ptr->inv_filter_weights[i] =
1263 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
1264 png_ptr->filter_weights[i] =
1265 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
1266 }
1267 }
1268 }
1269
1270 /* If, in the future, there are other filter methods, this would
1271 * need to be based on png_ptr->filter.
1272 */
1273 if (png_ptr->filter_costs == NULL)
1274 {
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001275 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001276 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001277
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001278 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001279 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001280
1281 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1282 {
1283 png_ptr->inv_filter_costs[i] =
1284 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1285 }
1286 }
1287
1288 /* Here is where we set the relative costs of the different filters. We
1289 * should take the desired compression level into account when setting
1290 * the costs, so that Paeth, for instance, has a high relative cost at low
1291 * compression levels, while it has a lower relative cost at higher
1292 * compression settings. The filter types are in order of increasing
1293 * relative cost, so it would be possible to do this with an algorithm.
1294 */
1295 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1296 {
1297 if (filter_costs == NULL || filter_costs[i] < 0.0)
1298 {
1299 png_ptr->inv_filter_costs[i] =
1300 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1301 }
1302 else if (filter_costs[i] >= 1.0)
1303 {
1304 png_ptr->inv_filter_costs[i] =
1305 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
1306 png_ptr->filter_costs[i] =
1307 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
1308 }
1309 }
1310}
1311#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1312
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001313void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001314png_set_compression_level(png_structp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001315{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001316 png_debug(1, "in png_set_compression_level\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001317 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001318 png_ptr->zlib_level = level;
1319}
1320
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001321void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001322png_set_compression_mem_level(png_structp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001323{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001324 png_debug(1, "in png_set_compression_mem_level\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001325 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001326 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001327}
1328
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001329void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001330png_set_compression_strategy(png_structp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001331{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001332 png_debug(1, "in png_set_compression_strategy\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001333 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001334 png_ptr->zlib_strategy = strategy;
1335}
1336
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001337void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001338png_set_compression_window_bits(png_structp png_ptr, int window_bits)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001339{
Guy Schalnate5a37791996-06-05 15:50:50 -05001340 if (window_bits > 15)
1341 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001342 else if (window_bits < 8)
1343 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001344#ifndef WBITS_8_OK
1345 /* avoid libpng bug with 256-byte windows */
1346 if (window_bits == 8)
1347 {
1348 png_warning(png_ptr, "Compression window is being reset to 512");
1349 window_bits=9;
1350 }
1351#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001352 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001353 png_ptr->zlib_window_bits = window_bits;
1354}
1355
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001356void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001357png_set_compression_method(png_structp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001358{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001359 png_debug(1, "in png_set_compression_method\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001360 if (method != 8)
1361 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1362 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001363 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -05001364}
1365
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001366void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001367png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1368{
1369 png_ptr->write_row_fn = write_row_fn;
1370}
1371
1372#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001373void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001374png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
1375 write_user_transform_fn)
1376{
1377 png_debug(1, "in png_set_write_user_transform_fn\n");
1378 png_ptr->transformations |= PNG_USER_TRANSFORM;
1379 png_ptr->write_user_transform_fn = write_user_transform_fn;
1380}
1381#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001382
1383
1384#if defined(PNG_INFO_IMAGE_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001385void PNGAPI
1386png_write_png(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001387 int transforms, voidp params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001388{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001389#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
1390 /* invert the alpha channel from opacity to transparency */
1391 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1392 png_set_invert_alpha(png_ptr);
1393#endif
1394
1395 /* Write the file header information. */
1396 png_write_info(png_ptr, info_ptr);
1397
1398 /* ------ these transformations don't touch the info structure ------- */
1399
1400#if defined(PNG_WRITE_INVERT_SUPPORTED)
1401 /* invert monochrome pixels */
1402 if (transforms & PNG_TRANSFORM_INVERT_MONO)
1403 png_set_invert_mono(png_ptr);
1404#endif
1405
1406#if defined(PNG_WRITE_SHIFT_SUPPORTED)
1407 /* Shift the pixels up to a legal bit depth and fill in
1408 * as appropriate to correctly scale the image.
1409 */
1410 if ((transforms & PNG_TRANSFORM_SHIFT)
1411 && (info_ptr->valid & PNG_INFO_sBIT))
1412 png_set_shift(png_ptr, &info_ptr->sig_bit);
1413#endif
1414
1415#if defined(PNG_WRITE_PACK_SUPPORTED)
1416 /* pack pixels into bytes */
1417 if (transforms & PNG_TRANSFORM_PACKING)
1418 png_set_packing(png_ptr);
1419#endif
1420
1421#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
1422 /* swap location of alpha bytes from ARGB to RGBA */
1423 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1424 png_set_swap_alpha(png_ptr);
1425#endif
1426
1427#if defined(PNG_WRITE_FILLER_SUPPORTED)
1428 /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
1429 * RGB (4 channels -> 3 channels). The second parameter is not used.
1430 */
1431 if (transforms & PNG_TRANSFORM_STRIP_FILLER)
1432 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
1433#endif
1434
1435#if defined(PNG_WRITE_BGR_SUPPORTED)
1436 /* flip BGR pixels to RGB */
1437 if (transforms & PNG_TRANSFORM_BGR)
1438 png_set_bgr(png_ptr);
1439#endif
1440
1441#if defined(PNG_WRITE_SWAP_SUPPORTED)
1442 /* swap bytes of 16-bit files to most significant byte first */
1443 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1444 png_set_swap(png_ptr);
1445#endif
1446
1447#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
1448 /* swap bits of 1, 2, 4 bit packed pixel formats */
1449 if (transforms & PNG_TRANSFORM_PACKSWAP)
1450 png_set_packswap(png_ptr);
1451#endif
1452
1453 /* ----------------------- end of transformations ------------------- */
1454
1455 /* write the bits */
1456 if (info_ptr->valid & PNG_INFO_IDAT)
1457 png_write_image(png_ptr, info_ptr->row_pointers);
1458
1459 /* It is REQUIRED to call this to finish writing the rest of the file */
1460 png_write_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001461
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001462 if(transforms == 0 || params == NULL)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001463 /* quiet compiler warnings */ return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001464}
1465#endif
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -06001466#endif /* PNG_WRITE_SUPPORTED */