blob: f1f1d3a267c96bb6ae8693f4a525e759a2327be0 [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-Pehrson2ad31ae2000-12-15 08:54:42 -06004 * libpng 1.0.9beta5 - December 15, 2000
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrsonf5ed0e12000-11-18 18:19:14 -06006 * Copyright (c) 1998, 1999, 2000 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"
14
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 */
31 /* write IHDR information. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060032 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
33 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060034 info_ptr->filter_type,
35#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
36 info_ptr->interlace_type);
37#else
38 0);
39#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050040 /* the rest of these check to see if the valid field has the appropriate
41 flag set, and if it does, writes the chunk. */
Guy Schalnat51f0eb41995-09-26 05:22:39 -050042#if defined(PNG_WRITE_gAMA_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060043 if (info_ptr->valid & PNG_INFO_gAMA)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060044 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060045# ifdef PNG_FLOATING_POINT_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060046 png_write_gAMA(png_ptr, info_ptr->gamma);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060047#else
48#ifdef PNG_FIXED_POINT_SUPPORTED
49 png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060050# endif
51#endif
52 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -050053#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060054#if defined(PNG_WRITE_sRGB_SUPPORTED)
55 if (info_ptr->valid & PNG_INFO_sRGB)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060056 png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060057#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060058#if defined(PNG_WRITE_iCCP_SUPPORTED)
59 if (info_ptr->valid & PNG_INFO_iCCP)
Glenn Randers-Pehrsonf5ed0e12000-11-18 18:19:14 -060060 png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_TEXT_COMPRESSION_zTXt,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060061 info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
62#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050063#if defined(PNG_WRITE_sBIT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060064 if (info_ptr->valid & PNG_INFO_sBIT)
65 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050066#endif
67#if defined(PNG_WRITE_cHRM_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060068 if (info_ptr->valid & PNG_INFO_cHRM)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060069 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060070#ifdef PNG_FLOATING_POINT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050071 png_write_cHRM(png_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060072 info_ptr->x_white, info_ptr->y_white,
73 info_ptr->x_red, info_ptr->y_red,
74 info_ptr->x_green, info_ptr->y_green,
75 info_ptr->x_blue, info_ptr->y_blue);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060076#else
77# ifdef PNG_FIXED_POINT_SUPPORTED
78 png_write_cHRM_fixed(png_ptr,
79 info_ptr->int_x_white, info_ptr->int_y_white,
80 info_ptr->int_x_red, info_ptr->int_y_red,
81 info_ptr->int_x_green, info_ptr->int_y_green,
82 info_ptr->int_x_blue, info_ptr->int_y_blue);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060083# endif
84#endif
85 }
86#endif
87#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
88 if (info_ptr->unknown_chunks_num)
89 {
90 png_unknown_chunk *up;
91
92 png_debug(5, "writing extra chunks\n");
93
94 for (up = info_ptr->unknown_chunks;
95 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
96 up++)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060097 {
98 int keep=png_handle_as_unknown(png_ptr, up->name);
99 if (keep != HANDLE_CHUNK_NEVER &&
100 up->location && (!(up->location & PNG_HAVE_PLTE)) &&
101 ((up->name[3] & 0x20) || keep == HANDLE_CHUNK_ALWAYS ||
102 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
103 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600104 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600105 }
106 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600107 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500108#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600109 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
110 }
111}
112
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500113void PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600114png_write_info(png_structp png_ptr, png_infop info_ptr)
115{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600116#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600117 int i;
118#endif
119
120 png_debug(1, "in png_write_info\n");
121
122 png_write_info_before_PLTE(png_ptr, info_ptr);
123
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600124 if (info_ptr->valid & PNG_INFO_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500125 png_write_PLTE(png_ptr, info_ptr->palette,
126 (png_uint_32)info_ptr->num_palette);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600127 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500128 png_error(png_ptr, "Valid palette required for paletted images\n");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600129
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500130#if defined(PNG_WRITE_tRNS_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600131 if (info_ptr->valid & PNG_INFO_tRNS)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600132 {
133#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
134 /* invert the alpha channel (in tRNS) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600135 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600136 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
137 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600138 int j;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600139 for (j=0; j<(int)info_ptr->num_trans; j++)
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500140 info_ptr->trans[j] = (png_byte)(255 - info_ptr->trans[j]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600141 }
142#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600143 png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values),
144 info_ptr->num_trans, info_ptr->color_type);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600145 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500146#endif
147#if defined(PNG_WRITE_bKGD_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600148 if (info_ptr->valid & PNG_INFO_bKGD)
149 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500150#endif
151#if defined(PNG_WRITE_hIST_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600152 if (info_ptr->valid & PNG_INFO_hIST)
153 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500154#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500155#if defined(PNG_WRITE_oFFs_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600156 if (info_ptr->valid & PNG_INFO_oFFs)
157 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
158 info_ptr->offset_unit_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500159#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500160#if defined(PNG_WRITE_pCAL_SUPPORTED)
161 if (info_ptr->valid & PNG_INFO_pCAL)
162 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
163 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
164 info_ptr->pcal_units, info_ptr->pcal_params);
165#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600166#if defined(PNG_WRITE_sCAL_SUPPORTED)
167 if (info_ptr->valid & PNG_INFO_sCAL)
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600168#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600169 png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,
170 info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);
171#else
172#ifdef PNG_FIXED_POINT_SUPPORTED
173 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600174 info_ptr->scal_s_width, info_ptr->scal_s_height);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600175#else
176 png_warning(png_ptr,
177 "png_write_sCAL not supported; sCAL chunk not written.\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600178#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600179#endif
180#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500181#if defined(PNG_WRITE_pHYs_SUPPORTED)
182 if (info_ptr->valid & PNG_INFO_pHYs)
183 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
184 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
185#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500186#if defined(PNG_WRITE_tIME_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600187 if (info_ptr->valid & PNG_INFO_tIME)
Guy Schalnate5a37791996-06-05 15:50:50 -0500188 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600189 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600190 png_ptr->mode |= PNG_WROTE_tIME;
Guy Schalnate5a37791996-06-05 15:50:50 -0500191 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500192#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600193#if defined(PNG_WRITE_sPLT_SUPPORTED)
194 if (info_ptr->valid & PNG_INFO_sPLT)
195 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
196 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
197#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600198#if defined(PNG_WRITE_TEXT_SUPPORTED)
Guy Schalnate5a37791996-06-05 15:50:50 -0500199 /* Check to see if we need to write text chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500200 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500201 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500202 png_debug2(2, "Writing header text chunk %d, type %d\n", i,
203 info_ptr->text[i].compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600204 /* an internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600205 if (info_ptr->text[i].compression > 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600206 {
207#if defined(PNG_WRITE_iTXt_SUPPORTED)
208 /* write international chunk */
209 png_write_iTXt(png_ptr,
210 info_ptr->text[i].compression,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600211 info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600212 info_ptr->text[i].lang,
213 info_ptr->text[i].lang_key,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600214 info_ptr->text[i].text);
215#else
216 png_warning(png_ptr, "Unable to write international text\n");
217#endif
218 /* Mark this chunk as written */
219 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
220 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500221 /* If we want a compressed text chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600222 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500223 {
224#if defined(PNG_WRITE_zTXt_SUPPORTED)
225 /* write compressed chunk */
226 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600227 info_ptr->text[i].text, 0,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500228 info_ptr->text[i].compression);
229#else
230 png_warning(png_ptr, "Unable to write compressed text\n");
231#endif
232 /* Mark this chunk as written */
233 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
234 }
235 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
236 {
237#if defined(PNG_WRITE_tEXt_SUPPORTED)
238 /* write uncompressed chunk */
239 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600240 info_ptr->text[i].text,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600241 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500242#else
243 png_warning(png_ptr, "Unable to write uncompressed text\n");
244#endif
245 /* Mark this chunk as written */
246 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
247 }
248 }
249#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600250#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
251 if (info_ptr->unknown_chunks_num)
252 {
253 png_unknown_chunk *up;
254
255 png_debug(5, "writing extra chunks\n");
256
257 for (up = info_ptr->unknown_chunks;
258 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
259 up++)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600260 {
261 int keep=png_handle_as_unknown(png_ptr, up->name);
262 if (keep != HANDLE_CHUNK_NEVER &&
263 up->location && (up->location & PNG_HAVE_PLTE) &&
264 !(up->location & PNG_HAVE_IDAT) &&
265 ((up->name[3] & 0x20) || keep == HANDLE_CHUNK_ALWAYS ||
266 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
267 {
268 png_write_chunk(png_ptr, up->name, up->data, up->size);
269 }
270 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600271 }
272#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500273}
Guy Schalnat0d580581995-07-20 02:43:20 -0500274
Andreas Dilger47a0c421997-05-16 02:46:07 -0500275/* Writes the end of the PNG file. If you don't want to write comments or
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600276 * time information, you can pass NULL for info. If you already wrote these
277 * in png_write_info(), do not write them again here. If you have long
278 * comments, I suggest writing them here, and compressing them.
279 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500280void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500281png_write_end(png_structp png_ptr, png_infop info_ptr)
282{
283 png_debug(1, "in png_write_end\n");
284 if (!(png_ptr->mode & PNG_HAVE_IDAT))
285 png_error(png_ptr, "No IDATs written into file");
286
287 /* see if user wants us to write information chunks */
288 if (info_ptr != NULL)
289 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600290#if defined(PNG_WRITE_TEXT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500291 int i; /* local index variable */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600292#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500293#if defined(PNG_WRITE_tIME_SUPPORTED)
294 /* check to see if user has supplied a time chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600295 if ((info_ptr->valid & PNG_INFO_tIME) &&
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600296 !(png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500297 png_write_tIME(png_ptr, &(info_ptr->mod_time));
298#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600299#if defined(PNG_WRITE_TEXT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500300 /* loop through comment chunks */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600301 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500302 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500303 png_debug2(2, "Writing trailer text chunk %d, type %d\n", i,
304 info_ptr->text[i].compression);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600305 /* an internationalized chunk? */
306 if (info_ptr->text[i].compression > 0)
307 {
308#if defined(PNG_WRITE_iTXt_SUPPORTED)
309 /* write international chunk */
310 png_write_iTXt(png_ptr,
311 info_ptr->text[i].compression,
312 info_ptr->text[i].key,
313 info_ptr->text[i].lang,
314 info_ptr->text[i].lang_key,
315 info_ptr->text[i].text);
316#else
317 png_warning(png_ptr, "Unable to write international text\n");
318#endif
319 /* Mark this chunk as written */
320 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
321 }
322 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
Guy Schalnat0d580581995-07-20 02:43:20 -0500323 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500324#if defined(PNG_WRITE_zTXt_SUPPORTED)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600325 /* write compressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600326 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600327 info_ptr->text[i].text, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600328 info_ptr->text[i].compression);
Guy Schalnate5a37791996-06-05 15:50:50 -0500329#else
330 png_warning(png_ptr, "Unable to write compressed text\n");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500331#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500332 /* Mark this chunk as written */
333 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500334 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500335 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500336 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500337#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500338 /* write uncompressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600339 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600340 info_ptr->text[i].text, 0);
Guy Schalnate5a37791996-06-05 15:50:50 -0500341#else
342 png_warning(png_ptr, "Unable to write uncompressed text\n");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500343#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500344
Andreas Dilger47a0c421997-05-16 02:46:07 -0500345 /* Mark this chunk as written */
346 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500347 }
348 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600349#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600350#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
351 if (info_ptr->unknown_chunks_num)
352 {
353 png_unknown_chunk *up;
354
355 png_debug(5, "writing extra chunks\n");
356
357 for (up = info_ptr->unknown_chunks;
358 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
359 up++)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600360 {
361 int keep=png_handle_as_unknown(png_ptr, up->name);
362 if (keep != HANDLE_CHUNK_NEVER &&
363 up->location && (up->location & PNG_AFTER_IDAT) &&
364 ((up->name[3] & 0x20) || keep == HANDLE_CHUNK_ALWAYS ||
365 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
366 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600367 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600368 }
369 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600370 }
371#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500372 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500373
374 png_ptr->mode |= PNG_AFTER_IDAT;
375
Andreas Dilger47a0c421997-05-16 02:46:07 -0500376 /* write end of PNG file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500377 png_write_IEND(png_ptr);
Glenn Randers-Pehrsond56aca72000-11-23 11:51:42 -0600378#if 0
379/* This flush, added in libpng-1.0.8, causes some applications to crash
380 because they do not set png_ptr->output_flush_fn */
Glenn Randers-Pehrson32fc5ce2000-07-24 06:34:14 -0500381 png_flush(png_ptr);
Glenn Randers-Pehrsond56aca72000-11-23 11:51:42 -0600382#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500383}
384
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500385#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500386#if !defined(_WIN32_WCE)
387/* "time.h" functions are not supported on WindowsCE */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500388void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600389png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500390{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500391 png_debug(1, "in png_convert_from_struct_tm\n");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600392 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
393 ptime->month = (png_byte)(ttime->tm_mon + 1);
394 ptime->day = (png_byte)ttime->tm_mday;
395 ptime->hour = (png_byte)ttime->tm_hour;
396 ptime->minute = (png_byte)ttime->tm_min;
397 ptime->second = (png_byte)ttime->tm_sec;
Guy Schalnat0d580581995-07-20 02:43:20 -0500398}
399
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500400void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600401png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500402{
403 struct tm *tbuf;
404
Andreas Dilger47a0c421997-05-16 02:46:07 -0500405 png_debug(1, "in png_convert_from_time_t\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500406 tbuf = gmtime(&ttime);
407 png_convert_from_struct_tm(ptime, tbuf);
408}
Guy Schalnat6d764711995-12-19 03:22:19 -0600409#endif
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500410#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500411
Andreas Dilger47a0c421997-05-16 02:46:07 -0500412/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500413png_structp PNGAPI
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500414png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600415 png_error_ptr error_fn, png_error_ptr warn_fn)
Guy Schalnat0d580581995-07-20 02:43:20 -0500416{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500417#ifdef PNG_USER_MEM_SUPPORTED
418 return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
419 warn_fn, NULL, NULL, NULL));
420}
421
422/* Alternate initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500423png_structp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500424png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
425 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
426 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
427{
428#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500429 png_structp png_ptr;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600430#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600431#ifdef USE_FAR_KEYWORD
432 jmp_buf jmpbuf;
433#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600434#endif
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500435 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500436 png_debug(1, "in png_create_write_struct\n");
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500437#ifdef PNG_USER_MEM_SUPPORTED
438 if ((png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
439 (png_malloc_ptr)malloc_fn)) == NULL)
440#else
Guy Schalnate5a37791996-06-05 15:50:50 -0500441 if ((png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG)) == NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500442#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500443 {
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600444 return ((png_structp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500445 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600446
447#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600448#ifdef USE_FAR_KEYWORD
449 if (setjmp(jmpbuf))
450#else
Guy Schalnate5a37791996-06-05 15:50:50 -0500451 if (setjmp(png_ptr->jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600452#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500453 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600454 png_free(png_ptr, png_ptr->zbuf);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500455 png_ptr->zbuf=NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500456 png_destroy_struct(png_ptr);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600457 return ((png_structp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500458 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600459#ifdef USE_FAR_KEYWORD
460 png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
461#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600462#endif
463
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500464#ifdef PNG_USER_MEM_SUPPORTED
465 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
466#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600467 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnate5a37791996-06-05 15:50:50 -0500468
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500469 i=0;
470 do
Guy Schalnate5a37791996-06-05 15:50:50 -0500471 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500472 if(user_png_ver[i] != png_libpng_ver[i])
473 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
474 } while (png_libpng_ver[i++]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500475
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500476 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500477 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500478 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
479 * we must recompile any applications that use any older library version.
480 * For versions after libpng 1.0, we will be compatible, so we need
481 * only check the first digit.
482 */
483 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
484 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
485 {
486 png_error(png_ptr,
487 "Incompatible libpng version in application and library");
488 }
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500489
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500490 /* Libpng 1.0.6 was not binary compatible, due to insertion of the
491 info_ptr->free_me member. Note to maintainer: this test can be
492 removed from version 2.0.0 and beyond because the previous test
493 would have already rejected it. */
494
Glenn Randers-Pehrsond56aca72000-11-23 11:51:42 -0600495 if (user_png_ver[4] == '6' && user_png_ver[2] == '0' &&
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500496 user_png_ver[0] == '1' && user_png_ver[5] == '\0')
497 {
498 png_error(png_ptr,
499 "Application must be recompiled; version 1.0.6 was incompatible");
500 }
501 }
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500502
Guy Schalnat0d580581995-07-20 02:43:20 -0500503 /* initialize zbuf - compression buffer */
504 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600505 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
506 (png_uint_32)png_ptr->zbuf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500507
508 png_set_write_fn(png_ptr, NULL, NULL, NULL);
509
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600510#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
511 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
512 1, NULL, NULL);
513#endif
514
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600515 return ((png_structp)png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500516}
517
Andreas Dilger47a0c421997-05-16 02:46:07 -0500518/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500519#undef png_write_init
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500520void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500521png_write_init(png_structp png_ptr)
522{
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500523 /* We only come here via pre-1.0.7-compiled applications */
524 png_write_init_2(png_ptr, "1.0.0", 10000, 10000);
525}
526
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500527void PNGAPI
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500528png_write_init_2(png_structp png_ptr, png_const_charp user_png_ver,
529 png_size_t png_struct_size, png_size_t png_info_size)
530{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600531#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500532 jmp_buf tmp_jmp; /* to save current jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600533#endif
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500534 int i = 0;
535 do
536 {
537 if (user_png_ver[i] != png_libpng_ver[i])
538 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500539#ifdef PNG_LEGACY_SUPPORTED
540 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
541#else
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500542 png_ptr->error_fn=(png_error_ptr)NULL;
543 png_error(png_ptr,
544 "Application uses deprecated png_write_init() and must be recompiled.");
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500545#endif
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500546 }
547 } while (png_libpng_ver[i++]);
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500548
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500549 if (sizeof(png_struct) > png_struct_size ||
550 sizeof(png_info) > png_info_size)
551 {
552 png_ptr->error_fn=(png_error_ptr)NULL;
553 png_error(png_ptr,
554 "Application and library have different sized structs. Please recompile.");
555 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500556
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500557 png_debug(1, "in png_write_init_2\n");
558
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600559#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500560 /* save jump buffer and error functions */
561 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600562#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500563
564 /* reset all variables to 0 */
565 png_memset(png_ptr, 0, sizeof (png_struct));
566
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600567#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500568 /* restore jump buffer */
569 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600570#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500571
572 /* initialize zbuf - compression buffer */
573 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600574 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
575 (png_uint_32)png_ptr->zbuf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500576 png_set_write_fn(png_ptr, NULL, NULL, NULL);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500577
578#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
579 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
580 1, NULL, NULL);
581#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500582}
583
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600584/* Write a few rows of image data. If the image is interlaced,
585 * either you will have to write the 7 sub images, or, if you
586 * have called png_set_interlace_handling(), you will have to
587 * "write" the image seven times.
588 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500589void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600590png_write_rows(png_structp png_ptr, png_bytepp row,
Guy Schalnat0d580581995-07-20 02:43:20 -0500591 png_uint_32 num_rows)
592{
593 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600594 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500595
Andreas Dilger47a0c421997-05-16 02:46:07 -0500596 png_debug(1, "in png_write_rows\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500597 /* loop through the rows */
598 for (i = 0, rp = row; i < num_rows; i++, rp++)
599 {
600 png_write_row(png_ptr, *rp);
601 }
602}
603
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600604/* Write the image. You only need to call this function once, even
605 * if you are writing an interlaced image.
606 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500607void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600608png_write_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500609{
610 png_uint_32 i; /* row index */
611 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600612 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500613
Andreas Dilger47a0c421997-05-16 02:46:07 -0500614 png_debug(1, "in png_write_image\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600615#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500616 /* intialize interlace handling. If image is not interlaced,
617 this will set pass to 1 */
618 num_pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600619#else
620 num_pass = 1;
621#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500622 /* loop through passes */
623 for (pass = 0; pass < num_pass; pass++)
624 {
625 /* loop through image */
626 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
627 {
628 png_write_row(png_ptr, *rp);
629 }
630 }
631}
632
Guy Schalnate5a37791996-06-05 15:50:50 -0500633/* called by user to write a row of image data */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500634void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600635png_write_row(png_structp png_ptr, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500636{
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600637 png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
638 png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500639 /* initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600640 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500641 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500642 /* check for transforms that have been set but were defined out */
643#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
644 if (png_ptr->transformations & PNG_INVERT_MONO)
645 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
646#endif
647#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
648 if (png_ptr->transformations & PNG_FILLER)
649 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
650#endif
651#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
652 if (png_ptr->transformations & PNG_PACKSWAP)
653 png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
654#endif
655#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
656 if (png_ptr->transformations & PNG_PACK)
657 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined.");
658#endif
659#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
660 if (png_ptr->transformations & PNG_SHIFT)
661 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
662#endif
663#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
664 if (png_ptr->transformations & PNG_BGR)
665 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined.");
666#endif
667#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
668 if (png_ptr->transformations & PNG_SWAP_BYTES)
669 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
670#endif
671
Guy Schalnat0d580581995-07-20 02:43:20 -0500672 png_write_start_row(png_ptr);
673 }
674
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500675#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500676 /* if interlaced and not interested in row, return */
677 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
678 {
679 switch (png_ptr->pass)
680 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600681 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600682 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500683 {
684 png_write_finish_row(png_ptr);
685 return;
686 }
687 break;
688 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600689 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500690 {
691 png_write_finish_row(png_ptr);
692 return;
693 }
694 break;
695 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600696 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500697 {
698 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600699 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500700 }
701 break;
702 case 3:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600703 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
Guy Schalnat0d580581995-07-20 02:43:20 -0500704 {
705 png_write_finish_row(png_ptr);
706 return;
707 }
708 break;
709 case 4:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600710 if ((png_ptr->row_number & 0x03) != 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500711 {
712 png_write_finish_row(png_ptr);
713 return;
714 }
715 break;
716 case 5:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600717 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500718 {
719 png_write_finish_row(png_ptr);
720 return;
721 }
722 break;
723 case 6:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600724 if (!(png_ptr->row_number & 0x01))
Guy Schalnat0d580581995-07-20 02:43:20 -0500725 {
726 png_write_finish_row(png_ptr);
727 return;
728 }
729 break;
730 }
731 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600732#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500733
734 /* set up row info for transformations */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600735 png_ptr->row_info.color_type = png_ptr->color_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500736 png_ptr->row_info.width = png_ptr->usr_width;
737 png_ptr->row_info.channels = png_ptr->usr_channels;
738 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600739 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
740 png_ptr->row_info.channels);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600741
Guy Schalnat0d580581995-07-20 02:43:20 -0500742 png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
743 (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
744
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600745 png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500746 png_debug1(3, "row_info->width = %lu\n", png_ptr->row_info.width);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600747 png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels);
748 png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth);
749 png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500750 png_debug1(3, "row_info->rowbytes = %lu\n", png_ptr->row_info.rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500751
752 /* Copy user's row into buffer, leaving room for filter byte. */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600753 png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600754 png_ptr->row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500755
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500756#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500757 /* handle interlacing */
758 if (png_ptr->interlaced && png_ptr->pass < 6 &&
759 (png_ptr->transformations & PNG_INTERLACE))
760 {
761 png_do_write_interlace(&(png_ptr->row_info),
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600762 png_ptr->row_buf + 1, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500763 /* this should always get caught above, but still ... */
764 if (!(png_ptr->row_info.width))
765 {
766 png_write_finish_row(png_ptr);
767 return;
768 }
769 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600770#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500771
772 /* handle other transformations */
773 if (png_ptr->transformations)
774 png_do_write_transformations(png_ptr);
775
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600776#if defined(PNG_MNG_FEATURES_SUPPORTED)
777 if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
778 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
779 {
780 /* Intrapixel differencing */
781 png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
782 }
783#endif
784
Andreas Dilger47a0c421997-05-16 02:46:07 -0500785 /* Find a filter if necessary, filter the row and write it out. */
Guy Schalnate5a37791996-06-05 15:50:50 -0500786 png_write_find_filter(png_ptr, &(png_ptr->row_info));
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600787
788 if (png_ptr->write_row_fn != NULL)
789 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500790}
791
Guy Schalnat0f716451995-11-28 11:22:13 -0600792#if defined(PNG_WRITE_FLUSH_SUPPORTED)
793/* Set the automatic flush interval or 0 to turn flushing off */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500794void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600795png_set_flush(png_structp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600796{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500797 png_debug(1, "in png_set_flush\n");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600798 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600799}
800
801/* flush the current output buffers now */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500802void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600803png_write_flush(png_structp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600804{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600805 int wrote_IDAT;
Guy Schalnat0f716451995-11-28 11:22:13 -0600806
Andreas Dilger47a0c421997-05-16 02:46:07 -0500807 png_debug(1, "in png_write_flush\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500808 /* We have already written out all of the data */
809 if (png_ptr->row_number >= png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600810 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600811
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600812 do
813 {
814 int ret;
Guy Schalnat0f716451995-11-28 11:22:13 -0600815
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600816 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600817 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600818 wrote_IDAT = 0;
Guy Schalnat0f716451995-11-28 11:22:13 -0600819
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600820 /* check for compression errors */
821 if (ret != Z_OK)
822 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500823 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600824 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600825 else
826 png_error(png_ptr, "zlib error");
827 }
Guy Schalnat0f716451995-11-28 11:22:13 -0600828
Andreas Dilger47a0c421997-05-16 02:46:07 -0500829 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600830 {
831 /* write the IDAT and reset the zlib output buffer */
832 png_write_IDAT(png_ptr, png_ptr->zbuf,
833 png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600834 png_ptr->zstream.next_out = png_ptr->zbuf;
835 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600836 wrote_IDAT = 1;
837 }
838 } while(wrote_IDAT == 1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600839
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600840 /* If there is any data left to be output, write it into a new IDAT */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600841 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600842 {
843 /* write the IDAT and reset the zlib output buffer */
844 png_write_IDAT(png_ptr, png_ptr->zbuf,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600845 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
846 png_ptr->zstream.next_out = png_ptr->zbuf;
847 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600848 }
849 png_ptr->flush_rows = 0;
850 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600851}
852#endif /* PNG_WRITE_FLUSH_SUPPORTED */
853
Guy Schalnate5a37791996-06-05 15:50:50 -0500854/* free all memory used by the write */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500855void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600856png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500857{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600858 png_structp png_ptr = NULL;
859 png_infop info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500860#ifdef PNG_USER_MEM_SUPPORTED
861 png_free_ptr free_fn = NULL;
862#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600863
Andreas Dilger47a0c421997-05-16 02:46:07 -0500864 png_debug(1, "in png_destroy_write_struct\n");
865 if (png_ptr_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500866 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600867 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500868#ifdef PNG_USER_MEM_SUPPORTED
869 free_fn = png_ptr->free_fn;
870#endif
871 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600872
Andreas Dilger47a0c421997-05-16 02:46:07 -0500873 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600874 info_ptr = *info_ptr_ptr;
875
Andreas Dilger47a0c421997-05-16 02:46:07 -0500876 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500877 {
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600878 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
879
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500880#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600881 if (png_ptr->num_chunk_list)
882 {
883 png_free(png_ptr, png_ptr->chunk_list);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500884 png_ptr->chunk_list=NULL;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600885 png_ptr->num_chunk_list=0;
886 }
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500887#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600888
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500889#ifdef PNG_USER_MEM_SUPPORTED
890 png_destroy_struct_2((png_voidp)info_ptr, free_fn);
891#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600892 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500893#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600894 *info_ptr_ptr = (png_infop)NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500895 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600896
Andreas Dilger47a0c421997-05-16 02:46:07 -0500897 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500898 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600899 png_write_destroy(png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500900#ifdef PNG_USER_MEM_SUPPORTED
901 png_destroy_struct_2((png_voidp)png_ptr, free_fn);
902#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600903 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500904#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600905 *png_ptr_ptr = (png_structp)NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500906 }
907}
908
909
Andreas Dilger47a0c421997-05-16 02:46:07 -0500910/* Free any memory used in png_ptr struct (old method) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500911void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600912png_write_destroy(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500913{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600914#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600915 jmp_buf tmp_jmp; /* save jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600916#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500917 png_error_ptr error_fn;
918 png_error_ptr warning_fn;
919 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500920#ifdef PNG_USER_MEM_SUPPORTED
921 png_free_ptr free_fn;
922#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500923
Andreas Dilger47a0c421997-05-16 02:46:07 -0500924 png_debug(1, "in png_write_destroy\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500925 /* free any memory zlib uses */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600926 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -0500927
Guy Schalnat0d580581995-07-20 02:43:20 -0500928 /* free our memory. png_free checks NULL for us. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600929 png_free(png_ptr, png_ptr->zbuf);
930 png_free(png_ptr, png_ptr->row_buf);
931 png_free(png_ptr, png_ptr->prev_row);
932 png_free(png_ptr, png_ptr->sub_row);
933 png_free(png_ptr, png_ptr->up_row);
934 png_free(png_ptr, png_ptr->avg_row);
935 png_free(png_ptr, png_ptr->paeth_row);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600936
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600937#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600938 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600939#endif
940
Andreas Dilger47a0c421997-05-16 02:46:07 -0500941#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
942 png_free(png_ptr, png_ptr->prev_filters);
943 png_free(png_ptr, png_ptr->filter_weights);
944 png_free(png_ptr, png_ptr->inv_filter_weights);
945 png_free(png_ptr, png_ptr->filter_costs);
946 png_free(png_ptr, png_ptr->inv_filter_costs);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600947#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500948
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600949#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -0500950 /* reset structure */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500951 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600952#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500953
954 error_fn = png_ptr->error_fn;
955 warning_fn = png_ptr->warning_fn;
956 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500957#ifdef PNG_USER_MEM_SUPPORTED
958 free_fn = png_ptr->free_fn;
959#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500960
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500961 png_memset(png_ptr, 0, sizeof (png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -0500962
963 png_ptr->error_fn = error_fn;
964 png_ptr->warning_fn = warning_fn;
965 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500966#ifdef PNG_USER_MEM_SUPPORTED
967 png_ptr->free_fn = free_fn;
968#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500969
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600970#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500971 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600972#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500973}
Guy Schalnate5a37791996-06-05 15:50:50 -0500974
Andreas Dilger47a0c421997-05-16 02:46:07 -0500975/* Allow the application to select one or more row filters to use. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500976void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500977png_set_filter(png_structp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500978{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500979 png_debug(1, "in png_set_filter\n");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500980 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500981 {
982 switch (filters & (PNG_ALL_FILTERS | 0x07))
983 {
984 case 5:
985 case 6:
Andreas Dilger47a0c421997-05-16 02:46:07 -0500986 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
987 case PNG_FILTER_VALUE_NONE: png_ptr->do_filter=PNG_FILTER_NONE; break;
988 case PNG_FILTER_VALUE_SUB: png_ptr->do_filter=PNG_FILTER_SUB; break;
989 case PNG_FILTER_VALUE_UP: png_ptr->do_filter=PNG_FILTER_UP; break;
990 case PNG_FILTER_VALUE_AVG: png_ptr->do_filter=PNG_FILTER_AVG; break;
991 case PNG_FILTER_VALUE_PAETH: png_ptr->do_filter=PNG_FILTER_PAETH;break;
Guy Schalnate5a37791996-06-05 15:50:50 -0500992 default: png_ptr->do_filter = (png_byte)filters; break;
993 }
994
Andreas Dilger47a0c421997-05-16 02:46:07 -0500995 /* If we have allocated the row_buf, this means we have already started
996 * with the image and we should have allocated all of the filter buffers
997 * that have been selected. If prev_row isn't already allocated, then
998 * it is too late to start using the filters that need it, since we
999 * will be missing the data in the previous row. If an application
1000 * wants to start and stop using particular filters during compression,
1001 * it should start out with all of the filters, and then add and
1002 * remove them after the start of compression.
Guy Schalnate5a37791996-06-05 15:50:50 -05001003 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001004 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001005 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001006 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001007 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001008 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001009 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001010 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001011 }
1012
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001013 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001014 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001015 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001016 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001017 png_warning(png_ptr, "Can't add Up filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -05001018 png_ptr->do_filter &= ~PNG_FILTER_UP;
1019 }
1020 else
1021 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001022 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001023 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001024 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001025 }
1026 }
1027
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001028 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001029 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001030 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001031 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001032 png_warning(png_ptr, "Can't add Average filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -05001033 png_ptr->do_filter &= ~PNG_FILTER_AVG;
1034 }
1035 else
1036 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001037 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001038 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001039 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001040 }
1041 }
1042
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001043 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
Andreas Dilger47a0c421997-05-16 02:46:07 -05001044 png_ptr->paeth_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001045 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001046 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001047 {
1048 png_warning(png_ptr, "Can't add Paeth filter after starting");
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001049 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
Guy Schalnate5a37791996-06-05 15:50:50 -05001050 }
1051 else
1052 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001053 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001054 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001055 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001056 }
1057 }
1058
1059 if (png_ptr->do_filter == PNG_NO_FILTERS)
1060 png_ptr->do_filter = PNG_FILTER_NONE;
1061 }
1062 }
1063 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05001064 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001065}
1066
Andreas Dilger47a0c421997-05-16 02:46:07 -05001067/* This allows us to influence the way in which libpng chooses the "best"
1068 * filter for the current scanline. While the "minimum-sum-of-absolute-
1069 * differences metric is relatively fast and effective, there is some
1070 * question as to whether it can be improved upon by trying to keep the
1071 * filtered data going to zlib more consistent, hopefully resulting in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001072 * better compression.
1073 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001074#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001075void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -05001076png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
1077 int num_weights, png_doublep filter_weights,
1078 png_doublep filter_costs)
1079{
1080 int i;
1081
1082 png_debug(1, "in png_set_filter_heuristics\n");
1083 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
1084 {
1085 png_warning(png_ptr, "Unknown filter heuristic method");
1086 return;
1087 }
1088
1089 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
1090 {
1091 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1092 }
1093
1094 if (num_weights < 0 || filter_weights == NULL ||
1095 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
1096 {
1097 num_weights = 0;
1098 }
1099
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001100 png_ptr->num_prev_filters = (png_byte)num_weights;
1101 png_ptr->heuristic_method = (png_byte)heuristic_method;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001102
1103 if (num_weights > 0)
1104 {
1105 if (png_ptr->prev_filters == NULL)
1106 {
1107 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001108 (png_uint_32)(sizeof(png_byte) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001109
1110 /* To make sure that the weighting starts out fairly */
1111 for (i = 0; i < num_weights; i++)
1112 {
1113 png_ptr->prev_filters[i] = 255;
1114 }
1115 }
1116
1117 if (png_ptr->filter_weights == NULL)
1118 {
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -06001119 png_ptr->filter_weights = (png_uint_16p) png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001120 (png_uint_32)(sizeof(png_uint_16) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001121
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -06001122 png_ptr->inv_filter_weights = (png_uint_16p) png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001123 (png_uint_32)(sizeof(png_uint_16) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001124
1125 for (i = 0; i < num_weights; i++)
1126 {
1127 png_ptr->inv_filter_weights[i] =
1128 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1129 }
1130 }
1131
1132 for (i = 0; i < num_weights; i++)
1133 {
1134 if (filter_weights[i] < 0.0)
1135 {
1136 png_ptr->inv_filter_weights[i] =
1137 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1138 }
1139 else
1140 {
1141 png_ptr->inv_filter_weights[i] =
1142 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
1143 png_ptr->filter_weights[i] =
1144 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
1145 }
1146 }
1147 }
1148
1149 /* If, in the future, there are other filter methods, this would
1150 * need to be based on png_ptr->filter.
1151 */
1152 if (png_ptr->filter_costs == NULL)
1153 {
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -06001154 png_ptr->filter_costs = (png_uint_16p) png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001155 (png_uint_32)(sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001156
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -06001157 png_ptr->inv_filter_costs = (png_uint_16p) png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001158 (png_uint_32)(sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001159
1160 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1161 {
1162 png_ptr->inv_filter_costs[i] =
1163 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1164 }
1165 }
1166
1167 /* Here is where we set the relative costs of the different filters. We
1168 * should take the desired compression level into account when setting
1169 * the costs, so that Paeth, for instance, has a high relative cost at low
1170 * compression levels, while it has a lower relative cost at higher
1171 * compression settings. The filter types are in order of increasing
1172 * relative cost, so it would be possible to do this with an algorithm.
1173 */
1174 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1175 {
1176 if (filter_costs == NULL || filter_costs[i] < 0.0)
1177 {
1178 png_ptr->inv_filter_costs[i] =
1179 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1180 }
1181 else if (filter_costs[i] >= 1.0)
1182 {
1183 png_ptr->inv_filter_costs[i] =
1184 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
1185 png_ptr->filter_costs[i] =
1186 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
1187 }
1188 }
1189}
1190#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1191
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001192void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001193png_set_compression_level(png_structp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001194{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001195 png_debug(1, "in png_set_compression_level\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001196 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001197 png_ptr->zlib_level = level;
1198}
1199
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001200void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001201png_set_compression_mem_level(png_structp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001202{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001203 png_debug(1, "in png_set_compression_mem_level\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001204 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001205 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001206}
1207
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001208void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001209png_set_compression_strategy(png_structp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001210{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001211 png_debug(1, "in png_set_compression_strategy\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001212 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001213 png_ptr->zlib_strategy = strategy;
1214}
1215
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001216void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001217png_set_compression_window_bits(png_structp png_ptr, int window_bits)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001218{
Guy Schalnate5a37791996-06-05 15:50:50 -05001219 if (window_bits > 15)
1220 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001221 else if (window_bits < 8)
1222 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001223#ifndef WBITS_8_OK
1224 /* avoid libpng bug with 256-byte windows */
1225 if (window_bits == 8)
1226 {
1227 png_warning(png_ptr, "Compression window is being reset to 512");
1228 window_bits=9;
1229 }
1230#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001231 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001232 png_ptr->zlib_window_bits = window_bits;
1233}
1234
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001235void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001236png_set_compression_method(png_structp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001237{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001238 png_debug(1, "in png_set_compression_method\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001239 if (method != 8)
1240 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1241 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001242 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -05001243}
1244
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001245void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001246png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1247{
1248 png_ptr->write_row_fn = write_row_fn;
1249}
1250
1251#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001252void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001253png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
1254 write_user_transform_fn)
1255{
1256 png_debug(1, "in png_set_write_user_transform_fn\n");
1257 png_ptr->transformations |= PNG_USER_TRANSFORM;
1258 png_ptr->write_user_transform_fn = write_user_transform_fn;
1259}
1260#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001261
1262
1263#if defined(PNG_INFO_IMAGE_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001264void PNGAPI
1265png_write_png(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001266 int transforms, voidp params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001267{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001268#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
1269 /* invert the alpha channel from opacity to transparency */
1270 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1271 png_set_invert_alpha(png_ptr);
1272#endif
1273
1274 /* Write the file header information. */
1275 png_write_info(png_ptr, info_ptr);
1276
1277 /* ------ these transformations don't touch the info structure ------- */
1278
1279#if defined(PNG_WRITE_INVERT_SUPPORTED)
1280 /* invert monochrome pixels */
1281 if (transforms & PNG_TRANSFORM_INVERT_MONO)
1282 png_set_invert_mono(png_ptr);
1283#endif
1284
1285#if defined(PNG_WRITE_SHIFT_SUPPORTED)
1286 /* Shift the pixels up to a legal bit depth and fill in
1287 * as appropriate to correctly scale the image.
1288 */
1289 if ((transforms & PNG_TRANSFORM_SHIFT)
1290 && (info_ptr->valid & PNG_INFO_sBIT))
1291 png_set_shift(png_ptr, &info_ptr->sig_bit);
1292#endif
1293
1294#if defined(PNG_WRITE_PACK_SUPPORTED)
1295 /* pack pixels into bytes */
1296 if (transforms & PNG_TRANSFORM_PACKING)
1297 png_set_packing(png_ptr);
1298#endif
1299
1300#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
1301 /* swap location of alpha bytes from ARGB to RGBA */
1302 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1303 png_set_swap_alpha(png_ptr);
1304#endif
1305
1306#if defined(PNG_WRITE_FILLER_SUPPORTED)
1307 /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
1308 * RGB (4 channels -> 3 channels). The second parameter is not used.
1309 */
1310 if (transforms & PNG_TRANSFORM_STRIP_FILLER)
1311 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
1312#endif
1313
1314#if defined(PNG_WRITE_BGR_SUPPORTED)
1315 /* flip BGR pixels to RGB */
1316 if (transforms & PNG_TRANSFORM_BGR)
1317 png_set_bgr(png_ptr);
1318#endif
1319
1320#if defined(PNG_WRITE_SWAP_SUPPORTED)
1321 /* swap bytes of 16-bit files to most significant byte first */
1322 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1323 png_set_swap(png_ptr);
1324#endif
1325
1326#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
1327 /* swap bits of 1, 2, 4 bit packed pixel formats */
1328 if (transforms & PNG_TRANSFORM_PACKSWAP)
1329 png_set_packswap(png_ptr);
1330#endif
1331
1332 /* ----------------------- end of transformations ------------------- */
1333
1334 /* write the bits */
1335 if (info_ptr->valid & PNG_INFO_IDAT)
1336 png_write_image(png_ptr, info_ptr->row_pointers);
1337
1338 /* It is REQUIRED to call this to finish writing the rest of the file */
1339 png_write_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001340
1341 if(transforms == 0 || params == (voidp)NULL)
1342 /* quiet compiler warnings */ return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001343}
1344#endif