blob: 9760cc8daab2c11833adb4d1e2a85374e26603cd [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-Pehrson272489d2004-08-04 06:34:52 -05004 * libpng 1.2.6rc1 - August 4, 2004
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05006 * Copyright (c) 1998-2004 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");
29 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
30 {
Guy Schalnat0d580581995-07-20 02:43:20 -050031 png_write_sig(png_ptr); /* write PNG signature */
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060032#if defined(PNG_MNG_FEATURES_SUPPORTED)
33 if((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&(png_ptr->mng_features_permitted))
34 {
35 png_warning(png_ptr,"MNG features are not allowed in a PNG datastream\n");
36 png_ptr->mng_features_permitted=0;
37 }
38#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050039 /* write IHDR information. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060040 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
41 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060042 info_ptr->filter_type,
43#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
44 info_ptr->interlace_type);
45#else
46 0);
47#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050048 /* the rest of these check to see if the valid field has the appropriate
49 flag set, and if it does, writes the chunk. */
Guy Schalnat51f0eb41995-09-26 05:22:39 -050050#if defined(PNG_WRITE_gAMA_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060051 if (info_ptr->valid & PNG_INFO_gAMA)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060052 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060053# ifdef PNG_FLOATING_POINT_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060054 png_write_gAMA(png_ptr, info_ptr->gamma);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060055#else
56#ifdef PNG_FIXED_POINT_SUPPORTED
57 png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060058# endif
59#endif
60 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -050061#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060062#if defined(PNG_WRITE_sRGB_SUPPORTED)
63 if (info_ptr->valid & PNG_INFO_sRGB)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060064 png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060065#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060066#if defined(PNG_WRITE_iCCP_SUPPORTED)
67 if (info_ptr->valid & PNG_INFO_iCCP)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -060068 png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060069 info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
70#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050071#if defined(PNG_WRITE_sBIT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060072 if (info_ptr->valid & PNG_INFO_sBIT)
73 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050074#endif
75#if defined(PNG_WRITE_cHRM_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060076 if (info_ptr->valid & PNG_INFO_cHRM)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060077 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060078#ifdef PNG_FLOATING_POINT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050079 png_write_cHRM(png_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060080 info_ptr->x_white, info_ptr->y_white,
81 info_ptr->x_red, info_ptr->y_red,
82 info_ptr->x_green, info_ptr->y_green,
83 info_ptr->x_blue, info_ptr->y_blue);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060084#else
85# ifdef PNG_FIXED_POINT_SUPPORTED
86 png_write_cHRM_fixed(png_ptr,
87 info_ptr->int_x_white, info_ptr->int_y_white,
88 info_ptr->int_x_red, info_ptr->int_y_red,
89 info_ptr->int_x_green, info_ptr->int_y_green,
90 info_ptr->int_x_blue, info_ptr->int_y_blue);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060091# endif
92#endif
93 }
94#endif
95#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
96 if (info_ptr->unknown_chunks_num)
97 {
98 png_unknown_chunk *up;
99
100 png_debug(5, "writing extra chunks\n");
101
102 for (up = info_ptr->unknown_chunks;
103 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
104 up++)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600105 {
106 int keep=png_handle_as_unknown(png_ptr, up->name);
107 if (keep != HANDLE_CHUNK_NEVER &&
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500108 up->location && !(up->location & PNG_HAVE_PLTE) &&
109 !(up->location & PNG_HAVE_IDAT) &&
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600110 ((up->name[3] & 0x20) || keep == HANDLE_CHUNK_ALWAYS ||
111 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
112 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600113 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600114 }
115 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600116 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500117#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600118 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
119 }
120}
121
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500122void PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600123png_write_info(png_structp png_ptr, png_infop info_ptr)
124{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600125#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600126 int i;
127#endif
128
129 png_debug(1, "in png_write_info\n");
130
131 png_write_info_before_PLTE(png_ptr, info_ptr);
132
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600133 if (info_ptr->valid & PNG_INFO_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500134 png_write_PLTE(png_ptr, info_ptr->palette,
135 (png_uint_32)info_ptr->num_palette);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600136 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500137 png_error(png_ptr, "Valid palette required for paletted images\n");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600138
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500139#if defined(PNG_WRITE_tRNS_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600140 if (info_ptr->valid & PNG_INFO_tRNS)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600141 {
142#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
143 /* invert the alpha channel (in tRNS) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600144 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600145 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
146 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600147 int j;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600148 for (j=0; j<(int)info_ptr->num_trans; j++)
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500149 info_ptr->trans[j] = (png_byte)(255 - info_ptr->trans[j]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600150 }
151#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600152 png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values),
153 info_ptr->num_trans, info_ptr->color_type);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600154 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500155#endif
156#if defined(PNG_WRITE_bKGD_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600157 if (info_ptr->valid & PNG_INFO_bKGD)
158 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500159#endif
160#if defined(PNG_WRITE_hIST_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600161 if (info_ptr->valid & PNG_INFO_hIST)
162 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500163#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500164#if defined(PNG_WRITE_oFFs_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600165 if (info_ptr->valid & PNG_INFO_oFFs)
166 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
167 info_ptr->offset_unit_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500168#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500169#if defined(PNG_WRITE_pCAL_SUPPORTED)
170 if (info_ptr->valid & PNG_INFO_pCAL)
171 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
172 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
173 info_ptr->pcal_units, info_ptr->pcal_params);
174#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600175#if defined(PNG_WRITE_sCAL_SUPPORTED)
176 if (info_ptr->valid & PNG_INFO_sCAL)
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600177#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600178 png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,
179 info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);
180#else
181#ifdef PNG_FIXED_POINT_SUPPORTED
182 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600183 info_ptr->scal_s_width, info_ptr->scal_s_height);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600184#else
185 png_warning(png_ptr,
186 "png_write_sCAL not supported; sCAL chunk not written.\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600187#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600188#endif
189#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500190#if defined(PNG_WRITE_pHYs_SUPPORTED)
191 if (info_ptr->valid & PNG_INFO_pHYs)
192 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
193 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
194#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500195#if defined(PNG_WRITE_tIME_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600196 if (info_ptr->valid & PNG_INFO_tIME)
Guy Schalnate5a37791996-06-05 15:50:50 -0500197 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600198 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600199 png_ptr->mode |= PNG_WROTE_tIME;
Guy Schalnate5a37791996-06-05 15:50:50 -0500200 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500201#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600202#if defined(PNG_WRITE_sPLT_SUPPORTED)
203 if (info_ptr->valid & PNG_INFO_sPLT)
204 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
205 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
206#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600207#if defined(PNG_WRITE_TEXT_SUPPORTED)
Guy Schalnate5a37791996-06-05 15:50:50 -0500208 /* Check to see if we need to write text chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500209 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500210 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500211 png_debug2(2, "Writing header text chunk %d, type %d\n", i,
212 info_ptr->text[i].compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600213 /* an internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600214 if (info_ptr->text[i].compression > 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600215 {
216#if defined(PNG_WRITE_iTXt_SUPPORTED)
217 /* write international chunk */
218 png_write_iTXt(png_ptr,
219 info_ptr->text[i].compression,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600220 info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600221 info_ptr->text[i].lang,
222 info_ptr->text[i].lang_key,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600223 info_ptr->text[i].text);
224#else
225 png_warning(png_ptr, "Unable to write international text\n");
226#endif
227 /* Mark this chunk as written */
228 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
229 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500230 /* If we want a compressed text chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600231 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500232 {
233#if defined(PNG_WRITE_zTXt_SUPPORTED)
234 /* write compressed chunk */
235 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600236 info_ptr->text[i].text, 0,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500237 info_ptr->text[i].compression);
238#else
239 png_warning(png_ptr, "Unable to write compressed text\n");
240#endif
241 /* Mark this chunk as written */
242 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
243 }
244 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
245 {
246#if defined(PNG_WRITE_tEXt_SUPPORTED)
247 /* write uncompressed chunk */
248 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600249 info_ptr->text[i].text,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600250 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500251#else
252 png_warning(png_ptr, "Unable to write uncompressed text\n");
253#endif
254 /* Mark this chunk as written */
255 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
256 }
257 }
258#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600259#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
260 if (info_ptr->unknown_chunks_num)
261 {
262 png_unknown_chunk *up;
263
264 png_debug(5, "writing extra chunks\n");
265
266 for (up = info_ptr->unknown_chunks;
267 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
268 up++)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600269 {
270 int keep=png_handle_as_unknown(png_ptr, up->name);
271 if (keep != HANDLE_CHUNK_NEVER &&
272 up->location && (up->location & PNG_HAVE_PLTE) &&
273 !(up->location & PNG_HAVE_IDAT) &&
274 ((up->name[3] & 0x20) || keep == HANDLE_CHUNK_ALWAYS ||
275 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
276 {
277 png_write_chunk(png_ptr, up->name, up->data, up->size);
278 }
279 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600280 }
281#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500282}
Guy Schalnat0d580581995-07-20 02:43:20 -0500283
Andreas Dilger47a0c421997-05-16 02:46:07 -0500284/* Writes the end of the PNG file. If you don't want to write comments or
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600285 * time information, you can pass NULL for info. If you already wrote these
286 * in png_write_info(), do not write them again here. If you have long
287 * comments, I suggest writing them here, and compressing them.
288 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500289void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500290png_write_end(png_structp png_ptr, png_infop info_ptr)
291{
292 png_debug(1, "in png_write_end\n");
293 if (!(png_ptr->mode & PNG_HAVE_IDAT))
294 png_error(png_ptr, "No IDATs written into file");
295
296 /* see if user wants us to write information chunks */
297 if (info_ptr != NULL)
298 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600299#if defined(PNG_WRITE_TEXT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500300 int i; /* local index variable */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600301#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500302#if defined(PNG_WRITE_tIME_SUPPORTED)
303 /* check to see if user has supplied a time chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600304 if ((info_ptr->valid & PNG_INFO_tIME) &&
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600305 !(png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500306 png_write_tIME(png_ptr, &(info_ptr->mod_time));
307#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600308#if defined(PNG_WRITE_TEXT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500309 /* loop through comment chunks */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600310 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500311 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500312 png_debug2(2, "Writing trailer text chunk %d, type %d\n", i,
313 info_ptr->text[i].compression);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600314 /* an internationalized chunk? */
315 if (info_ptr->text[i].compression > 0)
316 {
317#if defined(PNG_WRITE_iTXt_SUPPORTED)
318 /* write international chunk */
319 png_write_iTXt(png_ptr,
320 info_ptr->text[i].compression,
321 info_ptr->text[i].key,
322 info_ptr->text[i].lang,
323 info_ptr->text[i].lang_key,
324 info_ptr->text[i].text);
325#else
326 png_warning(png_ptr, "Unable to write international text\n");
327#endif
328 /* Mark this chunk as written */
329 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
330 }
331 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
Guy Schalnat0d580581995-07-20 02:43:20 -0500332 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500333#if defined(PNG_WRITE_zTXt_SUPPORTED)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600334 /* write compressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600335 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600336 info_ptr->text[i].text, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600337 info_ptr->text[i].compression);
Guy Schalnate5a37791996-06-05 15:50:50 -0500338#else
339 png_warning(png_ptr, "Unable to write compressed text\n");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500340#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500341 /* Mark this chunk as written */
342 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500343 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500344 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500345 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500346#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500347 /* write uncompressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600348 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600349 info_ptr->text[i].text, 0);
Guy Schalnate5a37791996-06-05 15:50:50 -0500350#else
351 png_warning(png_ptr, "Unable to write uncompressed text\n");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500352#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500353
Andreas Dilger47a0c421997-05-16 02:46:07 -0500354 /* Mark this chunk as written */
355 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500356 }
357 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600358#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600359#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
360 if (info_ptr->unknown_chunks_num)
361 {
362 png_unknown_chunk *up;
363
364 png_debug(5, "writing extra chunks\n");
365
366 for (up = info_ptr->unknown_chunks;
367 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
368 up++)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600369 {
370 int keep=png_handle_as_unknown(png_ptr, up->name);
371 if (keep != HANDLE_CHUNK_NEVER &&
372 up->location && (up->location & PNG_AFTER_IDAT) &&
373 ((up->name[3] & 0x20) || keep == HANDLE_CHUNK_ALWAYS ||
374 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
375 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600376 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600377 }
378 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600379 }
380#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500381 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500382
383 png_ptr->mode |= PNG_AFTER_IDAT;
384
Andreas Dilger47a0c421997-05-16 02:46:07 -0500385 /* write end of PNG file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500386 png_write_IEND(png_ptr);
Glenn Randers-Pehrsond56aca72000-11-23 11:51:42 -0600387#if 0
388/* This flush, added in libpng-1.0.8, causes some applications to crash
389 because they do not set png_ptr->output_flush_fn */
Glenn Randers-Pehrson32fc5ce2000-07-24 06:34:14 -0500390 png_flush(png_ptr);
Glenn Randers-Pehrsond56aca72000-11-23 11:51:42 -0600391#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500392}
393
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500394#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500395#if !defined(_WIN32_WCE)
396/* "time.h" functions are not supported on WindowsCE */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500397void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600398png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500399{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500400 png_debug(1, "in png_convert_from_struct_tm\n");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600401 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
402 ptime->month = (png_byte)(ttime->tm_mon + 1);
403 ptime->day = (png_byte)ttime->tm_mday;
404 ptime->hour = (png_byte)ttime->tm_hour;
405 ptime->minute = (png_byte)ttime->tm_min;
406 ptime->second = (png_byte)ttime->tm_sec;
Guy Schalnat0d580581995-07-20 02:43:20 -0500407}
408
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500409void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600410png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500411{
412 struct tm *tbuf;
413
Andreas Dilger47a0c421997-05-16 02:46:07 -0500414 png_debug(1, "in png_convert_from_time_t\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500415 tbuf = gmtime(&ttime);
416 png_convert_from_struct_tm(ptime, tbuf);
417}
Guy Schalnat6d764711995-12-19 03:22:19 -0600418#endif
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500419#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500420
Andreas Dilger47a0c421997-05-16 02:46:07 -0500421/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500422png_structp PNGAPI
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500423png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600424 png_error_ptr error_fn, png_error_ptr warn_fn)
Guy Schalnat0d580581995-07-20 02:43:20 -0500425{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500426#ifdef PNG_USER_MEM_SUPPORTED
427 return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500428 warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500429}
430
431/* Alternate initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500432png_structp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500433png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
434 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
435 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
436{
437#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500438 png_structp png_ptr;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600439#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600440#ifdef USE_FAR_KEYWORD
441 jmp_buf jmpbuf;
442#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600443#endif
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500444 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500445 png_debug(1, "in png_create_write_struct\n");
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500446#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600447 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
448 (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500449#else
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600450 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500451#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600452 if (png_ptr == NULL)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500453 return (NULL);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600454
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600455#if !defined(PNG_1_0_X)
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500456#ifdef PNG_ASSEMBLER_CODE_SUPPORTED
457 png_init_mmx_flags(png_ptr); /* 1.2.0 addition */
458#endif
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600459#endif /* PNG_1_0_X */
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500460
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500461 /* added at libpng-1.2.6 */
462#ifdef PNG_SET_USER_LIMITS_SUPPORTED
463 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
464 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
465#endif
466
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600467#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600468#ifdef USE_FAR_KEYWORD
469 if (setjmp(jmpbuf))
470#else
Guy Schalnate5a37791996-06-05 15:50:50 -0500471 if (setjmp(png_ptr->jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600472#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500473 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600474 png_free(png_ptr, png_ptr->zbuf);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500475 png_ptr->zbuf=NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500476 png_destroy_struct(png_ptr);
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500477 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500478 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600479#ifdef USE_FAR_KEYWORD
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500480 png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600481#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600482#endif
483
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500484#ifdef PNG_USER_MEM_SUPPORTED
485 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
486#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600487 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnate5a37791996-06-05 15:50:50 -0500488
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500489 i=0;
490 do
Guy Schalnate5a37791996-06-05 15:50:50 -0500491 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500492 if(user_png_ver[i] != png_libpng_ver[i])
493 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
494 } while (png_libpng_ver[i++]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500495
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500496 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500497 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500498 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
499 * we must recompile any applications that use any older library version.
500 * For versions after libpng 1.0, we will be compatible, so we need
501 * only check the first digit.
502 */
503 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500504 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500505 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
506 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500507#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
508 char msg[80];
509 if (user_png_ver)
510 {
511 sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
512 user_png_ver);
513 png_warning(png_ptr, msg);
514 }
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600515 sprintf(msg, "Application is running with png.c from libpng-%.20s",
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500516 png_libpng_ver);
517 png_warning(png_ptr, msg);
518#endif
519#ifdef PNG_ERROR_NUMBERS_SUPPORTED
520 png_ptr->flags=0;
521#endif
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500522 png_error(png_ptr,
523 "Incompatible libpng version in application and library");
524 }
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500525 }
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500526
Guy Schalnat0d580581995-07-20 02:43:20 -0500527 /* initialize zbuf - compression buffer */
528 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600529 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
530 (png_uint_32)png_ptr->zbuf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500531
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500532 png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
533 png_flush_ptr_NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500534
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600535#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
536 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500537 1, png_doublep_NULL, png_doublep_NULL);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600538#endif
539
Glenn Randers-Pehrson03008a02002-04-27 10:11:25 -0500540#ifdef PNG_SETJMP_SUPPORTED
541/* Applications that neglect to set up their own setjmp() and then encounter
542 a png_error() will longjmp here. Since the jmpbuf is then meaningless we
543 abort instead of returning. */
544#ifdef USE_FAR_KEYWORD
545 if (setjmp(jmpbuf))
546 PNG_ABORT();
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500547 png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
Glenn Randers-Pehrson03008a02002-04-27 10:11:25 -0500548#else
549 if (setjmp(png_ptr->jmpbuf))
550 PNG_ABORT();
551#endif
552#endif
553 return (png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500554}
555
Andreas Dilger47a0c421997-05-16 02:46:07 -0500556/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500557#undef png_write_init
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500558void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500559png_write_init(png_structp png_ptr)
560{
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500561 /* We only come here via pre-1.0.7-compiled applications */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500562 png_write_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500563}
564
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500565void PNGAPI
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500566png_write_init_2(png_structp png_ptr, png_const_charp user_png_ver,
567 png_size_t png_struct_size, png_size_t png_info_size)
568{
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500569 /* We only come here via pre-1.0.12-compiled applications */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500570#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500571 if(png_sizeof(png_struct) > png_struct_size ||
572 png_sizeof(png_info) > png_info_size)
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500573 {
574 char msg[80];
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500575 png_ptr->warning_fn=NULL;
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500576 if (user_png_ver)
577 {
578 sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
579 user_png_ver);
580 png_warning(png_ptr, msg);
581 }
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600582 sprintf(msg, "Application is running with png.c from libpng-%.20s",
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500583 png_libpng_ver);
584 png_warning(png_ptr, msg);
585 }
586#endif
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500587 if(png_sizeof(png_struct) > png_struct_size)
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500588 {
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500589 png_ptr->error_fn=NULL;
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500590#ifdef PNG_ERROR_NUMBERS_SUPPORTED
591 png_ptr->flags=0;
592#endif
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500593 png_error(png_ptr,
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500594 "The png struct allocated by the application for writing is too small.");
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500595 }
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500596 if(png_sizeof(png_info) > png_info_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 info struct allocated by the application for writing is too small.");
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500604 }
605 png_write_init_3(&png_ptr, user_png_ver, png_struct_size);
606}
607
608
609void PNGAPI
610png_write_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
611 png_size_t png_struct_size)
612{
613 png_structp png_ptr=*ptr_ptr;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600614#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500615 jmp_buf tmp_jmp; /* to save current jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600616#endif
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500617 int i = 0;
618 do
619 {
620 if (user_png_ver[i] != png_libpng_ver[i])
621 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500622#ifdef PNG_LEGACY_SUPPORTED
623 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
624#else
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500625 png_ptr->warning_fn=NULL;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500626 png_warning(png_ptr,
627 "Application uses deprecated png_write_init() and should be recompiled.");
628 break;
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500629#endif
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500630 }
631 } while (png_libpng_ver[i++]);
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500632
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500633 png_debug(1, "in png_write_init_3\n");
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500634
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600635#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500636 /* save jump buffer and error functions */
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500637 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600638#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500639
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500640 if (png_sizeof(png_struct) > png_struct_size)
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500641 {
642 png_destroy_struct(png_ptr);
643 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
644 *ptr_ptr = png_ptr;
645 }
646
Guy Schalnate5a37791996-06-05 15:50:50 -0500647 /* reset all variables to 0 */
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500648 png_memset(png_ptr, 0, png_sizeof (png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -0500649
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500650 /* added at libpng-1.2.6 */
651#ifdef PNG_SET_USER_LIMITS_SUPPORTED
652 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
653 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
654#endif
655
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600656#if !defined(PNG_1_0_X)
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500657#ifdef PNG_ASSEMBLER_CODE_SUPPORTED
658 png_init_mmx_flags(png_ptr); /* 1.2.0 addition */
659#endif
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600660#endif /* PNG_1_0_X */
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500661
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600662#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500663 /* restore jump buffer */
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500664 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600665#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500666
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500667 png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
668 png_flush_ptr_NULL);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500669
Guy Schalnate5a37791996-06-05 15:50:50 -0500670 /* initialize zbuf - compression buffer */
671 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600672 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
673 (png_uint_32)png_ptr->zbuf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500674
675#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
676 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500677 1, png_doublep_NULL, png_doublep_NULL);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500678#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500679}
680
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600681/* Write a few rows of image data. If the image is interlaced,
682 * either you will have to write the 7 sub images, or, if you
683 * have called png_set_interlace_handling(), you will have to
684 * "write" the image seven times.
685 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500686void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600687png_write_rows(png_structp png_ptr, png_bytepp row,
Guy Schalnat0d580581995-07-20 02:43:20 -0500688 png_uint_32 num_rows)
689{
690 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600691 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500692
Andreas Dilger47a0c421997-05-16 02:46:07 -0500693 png_debug(1, "in png_write_rows\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500694 /* loop through the rows */
695 for (i = 0, rp = row; i < num_rows; i++, rp++)
696 {
697 png_write_row(png_ptr, *rp);
698 }
699}
700
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600701/* Write the image. You only need to call this function once, even
702 * if you are writing an interlaced image.
703 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500704void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600705png_write_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500706{
707 png_uint_32 i; /* row index */
708 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600709 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500710
Andreas Dilger47a0c421997-05-16 02:46:07 -0500711 png_debug(1, "in png_write_image\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600712#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500713 /* intialize interlace handling. If image is not interlaced,
714 this will set pass to 1 */
715 num_pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600716#else
717 num_pass = 1;
718#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500719 /* loop through passes */
720 for (pass = 0; pass < num_pass; pass++)
721 {
722 /* loop through image */
723 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
724 {
725 png_write_row(png_ptr, *rp);
726 }
727 }
728}
729
Guy Schalnate5a37791996-06-05 15:50:50 -0500730/* called by user to write a row of image data */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500731void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600732png_write_row(png_structp png_ptr, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500733{
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600734 png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
735 png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500736 /* initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600737 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500738 {
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -0600739 /* make sure we wrote the header info */
740 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
741 png_error(png_ptr,
742 "png_write_info was never called before png_write_row.");
743
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500744 /* check for transforms that have been set but were defined out */
745#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
746 if (png_ptr->transformations & PNG_INVERT_MONO)
747 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
748#endif
749#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
750 if (png_ptr->transformations & PNG_FILLER)
751 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
752#endif
753#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
754 if (png_ptr->transformations & PNG_PACKSWAP)
755 png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
756#endif
757#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
758 if (png_ptr->transformations & PNG_PACK)
759 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined.");
760#endif
761#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
762 if (png_ptr->transformations & PNG_SHIFT)
763 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
764#endif
765#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
766 if (png_ptr->transformations & PNG_BGR)
767 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined.");
768#endif
769#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
770 if (png_ptr->transformations & PNG_SWAP_BYTES)
771 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
772#endif
773
Guy Schalnat0d580581995-07-20 02:43:20 -0500774 png_write_start_row(png_ptr);
775 }
776
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500777#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500778 /* if interlaced and not interested in row, return */
779 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
780 {
781 switch (png_ptr->pass)
782 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600783 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600784 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500785 {
786 png_write_finish_row(png_ptr);
787 return;
788 }
789 break;
790 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600791 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500792 {
793 png_write_finish_row(png_ptr);
794 return;
795 }
796 break;
797 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600798 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500799 {
800 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600801 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500802 }
803 break;
804 case 3:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600805 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
Guy Schalnat0d580581995-07-20 02:43:20 -0500806 {
807 png_write_finish_row(png_ptr);
808 return;
809 }
810 break;
811 case 4:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600812 if ((png_ptr->row_number & 0x03) != 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500813 {
814 png_write_finish_row(png_ptr);
815 return;
816 }
817 break;
818 case 5:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600819 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500820 {
821 png_write_finish_row(png_ptr);
822 return;
823 }
824 break;
825 case 6:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600826 if (!(png_ptr->row_number & 0x01))
Guy Schalnat0d580581995-07-20 02:43:20 -0500827 {
828 png_write_finish_row(png_ptr);
829 return;
830 }
831 break;
832 }
833 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600834#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500835
836 /* set up row info for transformations */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600837 png_ptr->row_info.color_type = png_ptr->color_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500838 png_ptr->row_info.width = png_ptr->usr_width;
839 png_ptr->row_info.channels = png_ptr->usr_channels;
840 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600841 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
842 png_ptr->row_info.channels);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600843
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500844 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
845 png_ptr->row_info.width);
Guy Schalnat0d580581995-07-20 02:43:20 -0500846
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600847 png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500848 png_debug1(3, "row_info->width = %lu\n", png_ptr->row_info.width);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600849 png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels);
850 png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth);
851 png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500852 png_debug1(3, "row_info->rowbytes = %lu\n", png_ptr->row_info.rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500853
854 /* Copy user's row into buffer, leaving room for filter byte. */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600855 png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600856 png_ptr->row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500857
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500858#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500859 /* handle interlacing */
860 if (png_ptr->interlaced && png_ptr->pass < 6 &&
861 (png_ptr->transformations & PNG_INTERLACE))
862 {
863 png_do_write_interlace(&(png_ptr->row_info),
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600864 png_ptr->row_buf + 1, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500865 /* this should always get caught above, but still ... */
866 if (!(png_ptr->row_info.width))
867 {
868 png_write_finish_row(png_ptr);
869 return;
870 }
871 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600872#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500873
874 /* handle other transformations */
875 if (png_ptr->transformations)
876 png_do_write_transformations(png_ptr);
877
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600878#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600879 /* Write filter_method 64 (intrapixel differencing) only if
880 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
881 * 2. Libpng did not write a PNG signature (this filter_method is only
882 * used in PNG datastreams that are embedded in MNG datastreams) and
883 * 3. The application called png_permit_mng_features with a mask that
884 * included PNG_FLAG_MNG_FILTER_64 and
885 * 4. The filter_method is 64 and
886 * 5. The color_type is RGB or RGBA
887 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600888 if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
889 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
890 {
891 /* Intrapixel differencing */
892 png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
893 }
894#endif
895
Andreas Dilger47a0c421997-05-16 02:46:07 -0500896 /* Find a filter if necessary, filter the row and write it out. */
Guy Schalnate5a37791996-06-05 15:50:50 -0500897 png_write_find_filter(png_ptr, &(png_ptr->row_info));
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600898
899 if (png_ptr->write_row_fn != NULL)
900 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500901}
902
Guy Schalnat0f716451995-11-28 11:22:13 -0600903#if defined(PNG_WRITE_FLUSH_SUPPORTED)
904/* Set the automatic flush interval or 0 to turn flushing off */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500905void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600906png_set_flush(png_structp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600907{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500908 png_debug(1, "in png_set_flush\n");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600909 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600910}
911
912/* flush the current output buffers now */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500913void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600914png_write_flush(png_structp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600915{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600916 int wrote_IDAT;
Guy Schalnat0f716451995-11-28 11:22:13 -0600917
Andreas Dilger47a0c421997-05-16 02:46:07 -0500918 png_debug(1, "in png_write_flush\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500919 /* We have already written out all of the data */
920 if (png_ptr->row_number >= png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600921 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600922
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600923 do
924 {
925 int ret;
Guy Schalnat0f716451995-11-28 11:22:13 -0600926
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600927 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600928 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600929 wrote_IDAT = 0;
Guy Schalnat0f716451995-11-28 11:22:13 -0600930
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600931 /* check for compression errors */
932 if (ret != Z_OK)
933 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500934 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600935 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600936 else
937 png_error(png_ptr, "zlib error");
938 }
Guy Schalnat0f716451995-11-28 11:22:13 -0600939
Andreas Dilger47a0c421997-05-16 02:46:07 -0500940 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600941 {
942 /* write the IDAT and reset the zlib output buffer */
943 png_write_IDAT(png_ptr, png_ptr->zbuf,
944 png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600945 png_ptr->zstream.next_out = png_ptr->zbuf;
946 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600947 wrote_IDAT = 1;
948 }
949 } while(wrote_IDAT == 1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600950
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600951 /* If there is any data left to be output, write it into a new IDAT */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600952 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600953 {
954 /* write the IDAT and reset the zlib output buffer */
955 png_write_IDAT(png_ptr, png_ptr->zbuf,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600956 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
957 png_ptr->zstream.next_out = png_ptr->zbuf;
958 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600959 }
960 png_ptr->flush_rows = 0;
961 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600962}
963#endif /* PNG_WRITE_FLUSH_SUPPORTED */
964
Guy Schalnate5a37791996-06-05 15:50:50 -0500965/* free all memory used by the write */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500966void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600967png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500968{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600969 png_structp png_ptr = NULL;
970 png_infop info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500971#ifdef PNG_USER_MEM_SUPPORTED
972 png_free_ptr free_fn = NULL;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500973 png_voidp mem_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500974#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600975
Andreas Dilger47a0c421997-05-16 02:46:07 -0500976 png_debug(1, "in png_destroy_write_struct\n");
977 if (png_ptr_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500978 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600979 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500980#ifdef PNG_USER_MEM_SUPPORTED
981 free_fn = png_ptr->free_fn;
Glenn Randers-Pehrsonfcbd7872002-04-07 16:35:38 -0500982 mem_ptr = png_ptr->mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500983#endif
984 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600985
Andreas Dilger47a0c421997-05-16 02:46:07 -0500986 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600987 info_ptr = *info_ptr_ptr;
988
Andreas Dilger47a0c421997-05-16 02:46:07 -0500989 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500990 {
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600991 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
992
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500993#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600994 if (png_ptr->num_chunk_list)
995 {
996 png_free(png_ptr, png_ptr->chunk_list);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500997 png_ptr->chunk_list=NULL;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600998 png_ptr->num_chunk_list=0;
999 }
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001000#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001001
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001002#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001003 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
1004 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001005#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001006 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001007#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001008 *info_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001009 }
Guy Schalnat6d764711995-12-19 03:22:19 -06001010
Andreas Dilger47a0c421997-05-16 02:46:07 -05001011 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001012 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001013 png_write_destroy(png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001014#ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001015 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
1016 (png_voidp)mem_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001017#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001018 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001019#endif
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -05001020 *png_ptr_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001021 }
1022}
1023
1024
Andreas Dilger47a0c421997-05-16 02:46:07 -05001025/* Free any memory used in png_ptr struct (old method) */
Glenn Randers-Pehrsonf64a06f2001-04-11 07:38:00 -05001026void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001027png_write_destroy(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001028{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001029#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001030 jmp_buf tmp_jmp; /* save jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001031#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001032 png_error_ptr error_fn;
1033 png_error_ptr warning_fn;
1034 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001035#ifdef PNG_USER_MEM_SUPPORTED
1036 png_free_ptr free_fn;
1037#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001038
Andreas Dilger47a0c421997-05-16 02:46:07 -05001039 png_debug(1, "in png_write_destroy\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001040 /* free any memory zlib uses */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001041 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -05001042
Guy Schalnat0d580581995-07-20 02:43:20 -05001043 /* free our memory. png_free checks NULL for us. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001044 png_free(png_ptr, png_ptr->zbuf);
1045 png_free(png_ptr, png_ptr->row_buf);
1046 png_free(png_ptr, png_ptr->prev_row);
1047 png_free(png_ptr, png_ptr->sub_row);
1048 png_free(png_ptr, png_ptr->up_row);
1049 png_free(png_ptr, png_ptr->avg_row);
1050 png_free(png_ptr, png_ptr->paeth_row);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001051
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001052#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001053 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001054#endif
1055
Andreas Dilger47a0c421997-05-16 02:46:07 -05001056#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1057 png_free(png_ptr, png_ptr->prev_filters);
1058 png_free(png_ptr, png_ptr->filter_weights);
1059 png_free(png_ptr, png_ptr->inv_filter_weights);
1060 png_free(png_ptr, png_ptr->filter_costs);
1061 png_free(png_ptr, png_ptr->inv_filter_costs);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001062#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001063
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001064#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001065 /* reset structure */
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001066 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001067#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001068
1069 error_fn = png_ptr->error_fn;
1070 warning_fn = png_ptr->warning_fn;
1071 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001072#ifdef PNG_USER_MEM_SUPPORTED
1073 free_fn = png_ptr->free_fn;
1074#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001075
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001076 png_memset(png_ptr, 0, png_sizeof (png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -05001077
1078 png_ptr->error_fn = error_fn;
1079 png_ptr->warning_fn = warning_fn;
1080 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001081#ifdef PNG_USER_MEM_SUPPORTED
1082 png_ptr->free_fn = free_fn;
1083#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001084
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001085#ifdef PNG_SETJMP_SUPPORTED
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001086 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001087#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001088}
Guy Schalnate5a37791996-06-05 15:50:50 -05001089
Andreas Dilger47a0c421997-05-16 02:46:07 -05001090/* Allow the application to select one or more row filters to use. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001091void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -05001092png_set_filter(png_structp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001093{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001094 png_debug(1, "in png_set_filter\n");
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -06001095#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrsonf05f8032000-12-23 14:27:39 -06001096 if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -06001097 (method == PNG_INTRAPIXEL_DIFFERENCING))
1098 method = PNG_FILTER_TYPE_BASE;
1099#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001100 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001101 {
1102 switch (filters & (PNG_ALL_FILTERS | 0x07))
1103 {
1104 case 5:
1105 case 6:
Andreas Dilger47a0c421997-05-16 02:46:07 -05001106 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
1107 case PNG_FILTER_VALUE_NONE: png_ptr->do_filter=PNG_FILTER_NONE; break;
1108 case PNG_FILTER_VALUE_SUB: png_ptr->do_filter=PNG_FILTER_SUB; break;
1109 case PNG_FILTER_VALUE_UP: png_ptr->do_filter=PNG_FILTER_UP; break;
1110 case PNG_FILTER_VALUE_AVG: png_ptr->do_filter=PNG_FILTER_AVG; break;
1111 case PNG_FILTER_VALUE_PAETH: png_ptr->do_filter=PNG_FILTER_PAETH;break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001112 default: png_ptr->do_filter = (png_byte)filters; break;
1113 }
1114
Andreas Dilger47a0c421997-05-16 02:46:07 -05001115 /* If we have allocated the row_buf, this means we have already started
1116 * with the image and we should have allocated all of the filter buffers
1117 * that have been selected. If prev_row isn't already allocated, then
1118 * it is too late to start using the filters that need it, since we
1119 * will be missing the data in the previous row. If an application
1120 * wants to start and stop using particular filters during compression,
1121 * it should start out with all of the filters, and then add and
1122 * remove them after the start of compression.
Guy Schalnate5a37791996-06-05 15:50:50 -05001123 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001124 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001125 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001126 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001127 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001128 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001129 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001130 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001131 }
1132
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001133 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001134 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001135 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001136 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001137 png_warning(png_ptr, "Can't add Up filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -05001138 png_ptr->do_filter &= ~PNG_FILTER_UP;
1139 }
1140 else
1141 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001142 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001143 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001144 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001145 }
1146 }
1147
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001148 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001149 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001150 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001151 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001152 png_warning(png_ptr, "Can't add Average filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -05001153 png_ptr->do_filter &= ~PNG_FILTER_AVG;
1154 }
1155 else
1156 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001157 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001158 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001159 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001160 }
1161 }
1162
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001163 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
Andreas Dilger47a0c421997-05-16 02:46:07 -05001164 png_ptr->paeth_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001165 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001166 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001167 {
1168 png_warning(png_ptr, "Can't add Paeth filter after starting");
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001169 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
Guy Schalnate5a37791996-06-05 15:50:50 -05001170 }
1171 else
1172 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001173 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001174 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001175 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001176 }
1177 }
1178
1179 if (png_ptr->do_filter == PNG_NO_FILTERS)
1180 png_ptr->do_filter = PNG_FILTER_NONE;
1181 }
1182 }
1183 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05001184 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001185}
1186
Andreas Dilger47a0c421997-05-16 02:46:07 -05001187/* This allows us to influence the way in which libpng chooses the "best"
1188 * filter for the current scanline. While the "minimum-sum-of-absolute-
1189 * differences metric is relatively fast and effective, there is some
1190 * question as to whether it can be improved upon by trying to keep the
1191 * filtered data going to zlib more consistent, hopefully resulting in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001192 * better compression.
1193 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001194#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001195void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -05001196png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
1197 int num_weights, png_doublep filter_weights,
1198 png_doublep filter_costs)
1199{
1200 int i;
1201
1202 png_debug(1, "in png_set_filter_heuristics\n");
1203 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
1204 {
1205 png_warning(png_ptr, "Unknown filter heuristic method");
1206 return;
1207 }
1208
1209 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
1210 {
1211 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1212 }
1213
1214 if (num_weights < 0 || filter_weights == NULL ||
1215 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
1216 {
1217 num_weights = 0;
1218 }
1219
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001220 png_ptr->num_prev_filters = (png_byte)num_weights;
1221 png_ptr->heuristic_method = (png_byte)heuristic_method;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001222
1223 if (num_weights > 0)
1224 {
1225 if (png_ptr->prev_filters == NULL)
1226 {
1227 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001228 (png_uint_32)(png_sizeof(png_byte) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001229
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001230 /* To make sure that the weighting starts out fairly */
1231 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001232 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001233 png_ptr->prev_filters[i] = 255;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001234 }
1235 }
1236
1237 if (png_ptr->filter_weights == NULL)
1238 {
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001239 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001240 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001241
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001242 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001243 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001244 for (i = 0; i < num_weights; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001245 {
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001246 png_ptr->inv_filter_weights[i] =
1247 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001248 }
1249 }
1250
1251 for (i = 0; i < num_weights; i++)
1252 {
1253 if (filter_weights[i] < 0.0)
1254 {
1255 png_ptr->inv_filter_weights[i] =
1256 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1257 }
1258 else
1259 {
1260 png_ptr->inv_filter_weights[i] =
1261 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
1262 png_ptr->filter_weights[i] =
1263 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
1264 }
1265 }
1266 }
1267
1268 /* If, in the future, there are other filter methods, this would
1269 * need to be based on png_ptr->filter.
1270 */
1271 if (png_ptr->filter_costs == NULL)
1272 {
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001273 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001274 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001275
Glenn Randers-Pehrson87c6bc92001-04-03 22:43:19 -05001276 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001277 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001278
1279 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1280 {
1281 png_ptr->inv_filter_costs[i] =
1282 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1283 }
1284 }
1285
1286 /* Here is where we set the relative costs of the different filters. We
1287 * should take the desired compression level into account when setting
1288 * the costs, so that Paeth, for instance, has a high relative cost at low
1289 * compression levels, while it has a lower relative cost at higher
1290 * compression settings. The filter types are in order of increasing
1291 * relative cost, so it would be possible to do this with an algorithm.
1292 */
1293 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1294 {
1295 if (filter_costs == NULL || filter_costs[i] < 0.0)
1296 {
1297 png_ptr->inv_filter_costs[i] =
1298 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1299 }
1300 else if (filter_costs[i] >= 1.0)
1301 {
1302 png_ptr->inv_filter_costs[i] =
1303 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
1304 png_ptr->filter_costs[i] =
1305 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
1306 }
1307 }
1308}
1309#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1310
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001311void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001312png_set_compression_level(png_structp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001313{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001314 png_debug(1, "in png_set_compression_level\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001315 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001316 png_ptr->zlib_level = level;
1317}
1318
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001319void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001320png_set_compression_mem_level(png_structp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001321{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001322 png_debug(1, "in png_set_compression_mem_level\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001323 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001324 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001325}
1326
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001327void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001328png_set_compression_strategy(png_structp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001329{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001330 png_debug(1, "in png_set_compression_strategy\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001331 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001332 png_ptr->zlib_strategy = strategy;
1333}
1334
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001335void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001336png_set_compression_window_bits(png_structp png_ptr, int window_bits)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001337{
Guy Schalnate5a37791996-06-05 15:50:50 -05001338 if (window_bits > 15)
1339 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001340 else if (window_bits < 8)
1341 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001342#ifndef WBITS_8_OK
1343 /* avoid libpng bug with 256-byte windows */
1344 if (window_bits == 8)
1345 {
1346 png_warning(png_ptr, "Compression window is being reset to 512");
1347 window_bits=9;
1348 }
1349#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001350 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001351 png_ptr->zlib_window_bits = window_bits;
1352}
1353
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001354void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001355png_set_compression_method(png_structp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001356{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001357 png_debug(1, "in png_set_compression_method\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001358 if (method != 8)
1359 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1360 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001361 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -05001362}
1363
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001364void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001365png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1366{
1367 png_ptr->write_row_fn = write_row_fn;
1368}
1369
1370#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001371void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001372png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
1373 write_user_transform_fn)
1374{
1375 png_debug(1, "in png_set_write_user_transform_fn\n");
1376 png_ptr->transformations |= PNG_USER_TRANSFORM;
1377 png_ptr->write_user_transform_fn = write_user_transform_fn;
1378}
1379#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001380
1381
1382#if defined(PNG_INFO_IMAGE_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001383void PNGAPI
1384png_write_png(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001385 int transforms, voidp params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001386{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001387#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
1388 /* invert the alpha channel from opacity to transparency */
1389 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1390 png_set_invert_alpha(png_ptr);
1391#endif
1392
1393 /* Write the file header information. */
1394 png_write_info(png_ptr, info_ptr);
1395
1396 /* ------ these transformations don't touch the info structure ------- */
1397
1398#if defined(PNG_WRITE_INVERT_SUPPORTED)
1399 /* invert monochrome pixels */
1400 if (transforms & PNG_TRANSFORM_INVERT_MONO)
1401 png_set_invert_mono(png_ptr);
1402#endif
1403
1404#if defined(PNG_WRITE_SHIFT_SUPPORTED)
1405 /* Shift the pixels up to a legal bit depth and fill in
1406 * as appropriate to correctly scale the image.
1407 */
1408 if ((transforms & PNG_TRANSFORM_SHIFT)
1409 && (info_ptr->valid & PNG_INFO_sBIT))
1410 png_set_shift(png_ptr, &info_ptr->sig_bit);
1411#endif
1412
1413#if defined(PNG_WRITE_PACK_SUPPORTED)
1414 /* pack pixels into bytes */
1415 if (transforms & PNG_TRANSFORM_PACKING)
1416 png_set_packing(png_ptr);
1417#endif
1418
1419#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
1420 /* swap location of alpha bytes from ARGB to RGBA */
1421 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1422 png_set_swap_alpha(png_ptr);
1423#endif
1424
1425#if defined(PNG_WRITE_FILLER_SUPPORTED)
1426 /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
1427 * RGB (4 channels -> 3 channels). The second parameter is not used.
1428 */
1429 if (transforms & PNG_TRANSFORM_STRIP_FILLER)
1430 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
1431#endif
1432
1433#if defined(PNG_WRITE_BGR_SUPPORTED)
1434 /* flip BGR pixels to RGB */
1435 if (transforms & PNG_TRANSFORM_BGR)
1436 png_set_bgr(png_ptr);
1437#endif
1438
1439#if defined(PNG_WRITE_SWAP_SUPPORTED)
1440 /* swap bytes of 16-bit files to most significant byte first */
1441 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1442 png_set_swap(png_ptr);
1443#endif
1444
1445#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
1446 /* swap bits of 1, 2, 4 bit packed pixel formats */
1447 if (transforms & PNG_TRANSFORM_PACKSWAP)
1448 png_set_packswap(png_ptr);
1449#endif
1450
1451 /* ----------------------- end of transformations ------------------- */
1452
1453 /* write the bits */
1454 if (info_ptr->valid & PNG_INFO_IDAT)
1455 png_write_image(png_ptr, info_ptr->row_pointers);
1456
1457 /* It is REQUIRED to call this to finish writing the rest of the file */
1458 png_write_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001459
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -05001460 if(transforms == 0 || params == NULL)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001461 /* quiet compiler warnings */ return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001462}
1463#endif
Glenn Randers-Pehrson19095602001-03-14 07:08:39 -06001464#endif /* PNG_WRITE_SUPPORTED */