blob: 934c74981b71f2f2558bd3ce0f048febc270e128 [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-Pehrson76e5fd62000-12-28 07:50:05 -06004 * libpng 1.0.9beta7 - December 28, 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 */
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -060031#if defined(PNG_MNG_FEATURES_SUPPORTED)
32 if((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&(png_ptr->mng_features_permitted))
33 {
34 png_warning(png_ptr,"MNG features are not allowed in a PNG datastream\n");
35 png_ptr->mng_features_permitted=0;
36 }
37#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050038 /* write IHDR information. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060039 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
40 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060041 info_ptr->filter_type,
42#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
43 info_ptr->interlace_type);
44#else
45 0);
46#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050047 /* the rest of these check to see if the valid field has the appropriate
48 flag set, and if it does, writes the chunk. */
Guy Schalnat51f0eb41995-09-26 05:22:39 -050049#if defined(PNG_WRITE_gAMA_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060050 if (info_ptr->valid & PNG_INFO_gAMA)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060051 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060052# ifdef PNG_FLOATING_POINT_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060053 png_write_gAMA(png_ptr, info_ptr->gamma);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060054#else
55#ifdef PNG_FIXED_POINT_SUPPORTED
56 png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060057# endif
58#endif
59 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -050060#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060061#if defined(PNG_WRITE_sRGB_SUPPORTED)
62 if (info_ptr->valid & PNG_INFO_sRGB)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060063 png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060064#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060065#if defined(PNG_WRITE_iCCP_SUPPORTED)
66 if (info_ptr->valid & PNG_INFO_iCCP)
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -060067 png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060068 info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
69#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -050070#if defined(PNG_WRITE_sBIT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060071 if (info_ptr->valid & PNG_INFO_sBIT)
72 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -050073#endif
74#if defined(PNG_WRITE_cHRM_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060075 if (info_ptr->valid & PNG_INFO_cHRM)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060076 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060077#ifdef PNG_FLOATING_POINT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050078 png_write_cHRM(png_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060079 info_ptr->x_white, info_ptr->y_white,
80 info_ptr->x_red, info_ptr->y_red,
81 info_ptr->x_green, info_ptr->y_green,
82 info_ptr->x_blue, info_ptr->y_blue);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060083#else
84# ifdef PNG_FIXED_POINT_SUPPORTED
85 png_write_cHRM_fixed(png_ptr,
86 info_ptr->int_x_white, info_ptr->int_y_white,
87 info_ptr->int_x_red, info_ptr->int_y_red,
88 info_ptr->int_x_green, info_ptr->int_y_green,
89 info_ptr->int_x_blue, info_ptr->int_y_blue);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060090# endif
91#endif
92 }
93#endif
94#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
95 if (info_ptr->unknown_chunks_num)
96 {
97 png_unknown_chunk *up;
98
99 png_debug(5, "writing extra chunks\n");
100
101 for (up = info_ptr->unknown_chunks;
102 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
103 up++)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600104 {
105 int keep=png_handle_as_unknown(png_ptr, up->name);
106 if (keep != HANDLE_CHUNK_NEVER &&
107 up->location && (!(up->location & PNG_HAVE_PLTE)) &&
108 ((up->name[3] & 0x20) || keep == HANDLE_CHUNK_ALWAYS ||
109 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
110 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600111 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600112 }
113 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600114 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500115#endif
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600116 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
117 }
118}
119
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500120void PNGAPI
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600121png_write_info(png_structp png_ptr, png_infop info_ptr)
122{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600123#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600124 int i;
125#endif
126
127 png_debug(1, "in png_write_info\n");
128
129 png_write_info_before_PLTE(png_ptr, info_ptr);
130
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600131 if (info_ptr->valid & PNG_INFO_PLTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500132 png_write_PLTE(png_ptr, info_ptr->palette,
133 (png_uint_32)info_ptr->num_palette);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600134 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500135 png_error(png_ptr, "Valid palette required for paletted images\n");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600136
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500137#if defined(PNG_WRITE_tRNS_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600138 if (info_ptr->valid & PNG_INFO_tRNS)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600139 {
140#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
141 /* invert the alpha channel (in tRNS) */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600142 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600143 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
144 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600145 int j;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600146 for (j=0; j<(int)info_ptr->num_trans; j++)
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500147 info_ptr->trans[j] = (png_byte)(255 - info_ptr->trans[j]);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600148 }
149#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600150 png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values),
151 info_ptr->num_trans, info_ptr->color_type);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600152 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500153#endif
154#if defined(PNG_WRITE_bKGD_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600155 if (info_ptr->valid & PNG_INFO_bKGD)
156 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500157#endif
158#if defined(PNG_WRITE_hIST_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600159 if (info_ptr->valid & PNG_INFO_hIST)
160 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500161#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500162#if defined(PNG_WRITE_oFFs_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600163 if (info_ptr->valid & PNG_INFO_oFFs)
164 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
165 info_ptr->offset_unit_type);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500166#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500167#if defined(PNG_WRITE_pCAL_SUPPORTED)
168 if (info_ptr->valid & PNG_INFO_pCAL)
169 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
170 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
171 info_ptr->pcal_units, info_ptr->pcal_params);
172#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600173#if defined(PNG_WRITE_sCAL_SUPPORTED)
174 if (info_ptr->valid & PNG_INFO_sCAL)
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600175#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600176 png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,
177 info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);
178#else
179#ifdef PNG_FIXED_POINT_SUPPORTED
180 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600181 info_ptr->scal_s_width, info_ptr->scal_s_height);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600182#else
183 png_warning(png_ptr,
184 "png_write_sCAL not supported; sCAL chunk not written.\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600185#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600186#endif
187#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500188#if defined(PNG_WRITE_pHYs_SUPPORTED)
189 if (info_ptr->valid & PNG_INFO_pHYs)
190 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
191 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
192#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500193#if defined(PNG_WRITE_tIME_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600194 if (info_ptr->valid & PNG_INFO_tIME)
Guy Schalnate5a37791996-06-05 15:50:50 -0500195 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600196 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600197 png_ptr->mode |= PNG_WROTE_tIME;
Guy Schalnate5a37791996-06-05 15:50:50 -0500198 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500199#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600200#if defined(PNG_WRITE_sPLT_SUPPORTED)
201 if (info_ptr->valid & PNG_INFO_sPLT)
202 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
203 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
204#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600205#if defined(PNG_WRITE_TEXT_SUPPORTED)
Guy Schalnate5a37791996-06-05 15:50:50 -0500206 /* Check to see if we need to write text chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500207 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500208 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500209 png_debug2(2, "Writing header text chunk %d, type %d\n", i,
210 info_ptr->text[i].compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600211 /* an internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600212 if (info_ptr->text[i].compression > 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600213 {
214#if defined(PNG_WRITE_iTXt_SUPPORTED)
215 /* write international chunk */
216 png_write_iTXt(png_ptr,
217 info_ptr->text[i].compression,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600218 info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600219 info_ptr->text[i].lang,
220 info_ptr->text[i].lang_key,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600221 info_ptr->text[i].text);
222#else
223 png_warning(png_ptr, "Unable to write international text\n");
224#endif
225 /* Mark this chunk as written */
226 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
227 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500228 /* If we want a compressed text chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600229 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500230 {
231#if defined(PNG_WRITE_zTXt_SUPPORTED)
232 /* write compressed chunk */
233 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600234 info_ptr->text[i].text, 0,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500235 info_ptr->text[i].compression);
236#else
237 png_warning(png_ptr, "Unable to write compressed text\n");
238#endif
239 /* Mark this chunk as written */
240 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
241 }
242 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
243 {
244#if defined(PNG_WRITE_tEXt_SUPPORTED)
245 /* write uncompressed chunk */
246 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600247 info_ptr->text[i].text,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600248 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500249#else
250 png_warning(png_ptr, "Unable to write uncompressed text\n");
251#endif
252 /* Mark this chunk as written */
253 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
254 }
255 }
256#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600257#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
258 if (info_ptr->unknown_chunks_num)
259 {
260 png_unknown_chunk *up;
261
262 png_debug(5, "writing extra chunks\n");
263
264 for (up = info_ptr->unknown_chunks;
265 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
266 up++)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600267 {
268 int keep=png_handle_as_unknown(png_ptr, up->name);
269 if (keep != HANDLE_CHUNK_NEVER &&
270 up->location && (up->location & PNG_HAVE_PLTE) &&
271 !(up->location & PNG_HAVE_IDAT) &&
272 ((up->name[3] & 0x20) || keep == HANDLE_CHUNK_ALWAYS ||
273 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
274 {
275 png_write_chunk(png_ptr, up->name, up->data, up->size);
276 }
277 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600278 }
279#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500280}
Guy Schalnat0d580581995-07-20 02:43:20 -0500281
Andreas Dilger47a0c421997-05-16 02:46:07 -0500282/* Writes the end of the PNG file. If you don't want to write comments or
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600283 * time information, you can pass NULL for info. If you already wrote these
284 * in png_write_info(), do not write them again here. If you have long
285 * comments, I suggest writing them here, and compressing them.
286 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500287void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500288png_write_end(png_structp png_ptr, png_infop info_ptr)
289{
290 png_debug(1, "in png_write_end\n");
291 if (!(png_ptr->mode & PNG_HAVE_IDAT))
292 png_error(png_ptr, "No IDATs written into file");
293
294 /* see if user wants us to write information chunks */
295 if (info_ptr != NULL)
296 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600297#if defined(PNG_WRITE_TEXT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500298 int i; /* local index variable */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600299#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500300#if defined(PNG_WRITE_tIME_SUPPORTED)
301 /* check to see if user has supplied a time chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600302 if ((info_ptr->valid & PNG_INFO_tIME) &&
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600303 !(png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500304 png_write_tIME(png_ptr, &(info_ptr->mod_time));
305#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600306#if defined(PNG_WRITE_TEXT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500307 /* loop through comment chunks */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600308 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500309 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500310 png_debug2(2, "Writing trailer text chunk %d, type %d\n", i,
311 info_ptr->text[i].compression);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600312 /* an internationalized chunk? */
313 if (info_ptr->text[i].compression > 0)
314 {
315#if defined(PNG_WRITE_iTXt_SUPPORTED)
316 /* write international chunk */
317 png_write_iTXt(png_ptr,
318 info_ptr->text[i].compression,
319 info_ptr->text[i].key,
320 info_ptr->text[i].lang,
321 info_ptr->text[i].lang_key,
322 info_ptr->text[i].text);
323#else
324 png_warning(png_ptr, "Unable to write international text\n");
325#endif
326 /* Mark this chunk as written */
327 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
328 }
329 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
Guy Schalnat0d580581995-07-20 02:43:20 -0500330 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500331#if defined(PNG_WRITE_zTXt_SUPPORTED)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600332 /* write compressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600333 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600334 info_ptr->text[i].text, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600335 info_ptr->text[i].compression);
Guy Schalnate5a37791996-06-05 15:50:50 -0500336#else
337 png_warning(png_ptr, "Unable to write compressed text\n");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500338#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500339 /* Mark this chunk as written */
340 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500341 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500342 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500343 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500344#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500345 /* write uncompressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600346 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600347 info_ptr->text[i].text, 0);
Guy Schalnate5a37791996-06-05 15:50:50 -0500348#else
349 png_warning(png_ptr, "Unable to write uncompressed text\n");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500350#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500351
Andreas Dilger47a0c421997-05-16 02:46:07 -0500352 /* Mark this chunk as written */
353 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500354 }
355 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600356#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600357#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
358 if (info_ptr->unknown_chunks_num)
359 {
360 png_unknown_chunk *up;
361
362 png_debug(5, "writing extra chunks\n");
363
364 for (up = info_ptr->unknown_chunks;
365 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
366 up++)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600367 {
368 int keep=png_handle_as_unknown(png_ptr, up->name);
369 if (keep != HANDLE_CHUNK_NEVER &&
370 up->location && (up->location & PNG_AFTER_IDAT) &&
371 ((up->name[3] & 0x20) || keep == HANDLE_CHUNK_ALWAYS ||
372 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
373 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600374 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600375 }
376 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600377 }
378#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500379 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500380
381 png_ptr->mode |= PNG_AFTER_IDAT;
382
Andreas Dilger47a0c421997-05-16 02:46:07 -0500383 /* write end of PNG file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500384 png_write_IEND(png_ptr);
Glenn Randers-Pehrsond56aca72000-11-23 11:51:42 -0600385#if 0
386/* This flush, added in libpng-1.0.8, causes some applications to crash
387 because they do not set png_ptr->output_flush_fn */
Glenn Randers-Pehrson32fc5ce2000-07-24 06:34:14 -0500388 png_flush(png_ptr);
Glenn Randers-Pehrsond56aca72000-11-23 11:51:42 -0600389#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500390}
391
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500392#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500393#if !defined(_WIN32_WCE)
394/* "time.h" functions are not supported on WindowsCE */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500395void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600396png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500397{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500398 png_debug(1, "in png_convert_from_struct_tm\n");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600399 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
400 ptime->month = (png_byte)(ttime->tm_mon + 1);
401 ptime->day = (png_byte)ttime->tm_mday;
402 ptime->hour = (png_byte)ttime->tm_hour;
403 ptime->minute = (png_byte)ttime->tm_min;
404 ptime->second = (png_byte)ttime->tm_sec;
Guy Schalnat0d580581995-07-20 02:43:20 -0500405}
406
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500407void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600408png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500409{
410 struct tm *tbuf;
411
Andreas Dilger47a0c421997-05-16 02:46:07 -0500412 png_debug(1, "in png_convert_from_time_t\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500413 tbuf = gmtime(&ttime);
414 png_convert_from_struct_tm(ptime, tbuf);
415}
Guy Schalnat6d764711995-12-19 03:22:19 -0600416#endif
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500417#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500418
Andreas Dilger47a0c421997-05-16 02:46:07 -0500419/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500420png_structp PNGAPI
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500421png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600422 png_error_ptr error_fn, png_error_ptr warn_fn)
Guy Schalnat0d580581995-07-20 02:43:20 -0500423{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500424#ifdef PNG_USER_MEM_SUPPORTED
425 return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
426 warn_fn, NULL, NULL, NULL));
427}
428
429/* Alternate initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500430png_structp PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500431png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
432 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
433 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
434{
435#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500436 png_structp png_ptr;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600437#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600438#ifdef USE_FAR_KEYWORD
439 jmp_buf jmpbuf;
440#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600441#endif
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500442 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500443 png_debug(1, "in png_create_write_struct\n");
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500444#ifdef PNG_USER_MEM_SUPPORTED
445 if ((png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
446 (png_malloc_ptr)malloc_fn)) == NULL)
447#else
Guy Schalnate5a37791996-06-05 15:50:50 -0500448 if ((png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG)) == NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500449#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500450 {
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600451 return ((png_structp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500452 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600453
454#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600455#ifdef USE_FAR_KEYWORD
456 if (setjmp(jmpbuf))
457#else
Guy Schalnate5a37791996-06-05 15:50:50 -0500458 if (setjmp(png_ptr->jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600459#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500460 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600461 png_free(png_ptr, png_ptr->zbuf);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500462 png_ptr->zbuf=NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500463 png_destroy_struct(png_ptr);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600464 return ((png_structp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500465 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600466#ifdef USE_FAR_KEYWORD
467 png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
468#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600469#endif
470
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500471#ifdef PNG_USER_MEM_SUPPORTED
472 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
473#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600474 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnate5a37791996-06-05 15:50:50 -0500475
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500476 i=0;
477 do
Guy Schalnate5a37791996-06-05 15:50:50 -0500478 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500479 if(user_png_ver[i] != png_libpng_ver[i])
480 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
481 } while (png_libpng_ver[i++]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500482
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500483 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500484 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500485 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
486 * we must recompile any applications that use any older library version.
487 * For versions after libpng 1.0, we will be compatible, so we need
488 * only check the first digit.
489 */
490 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
491 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
492 {
493 png_error(png_ptr,
494 "Incompatible libpng version in application and library");
495 }
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500496
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500497 /* Libpng 1.0.6 was not binary compatible, due to insertion of the
498 info_ptr->free_me member. Note to maintainer: this test can be
499 removed from version 2.0.0 and beyond because the previous test
500 would have already rejected it. */
501
Glenn Randers-Pehrsond56aca72000-11-23 11:51:42 -0600502 if (user_png_ver[4] == '6' && user_png_ver[2] == '0' &&
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500503 user_png_ver[0] == '1' && user_png_ver[5] == '\0')
504 {
505 png_error(png_ptr,
506 "Application must be recompiled; version 1.0.6 was incompatible");
507 }
508 }
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500509
Guy Schalnat0d580581995-07-20 02:43:20 -0500510 /* initialize zbuf - compression buffer */
511 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600512 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
513 (png_uint_32)png_ptr->zbuf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500514
515 png_set_write_fn(png_ptr, NULL, NULL, NULL);
516
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600517#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
518 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
519 1, NULL, NULL);
520#endif
521
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600522 return ((png_structp)png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500523}
524
Andreas Dilger47a0c421997-05-16 02:46:07 -0500525/* Initialize png_ptr structure, and allocate any memory needed */
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500526#undef png_write_init
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500527void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500528png_write_init(png_structp png_ptr)
529{
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500530 /* We only come here via pre-1.0.7-compiled applications */
531 png_write_init_2(png_ptr, "1.0.0", 10000, 10000);
532}
533
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500534void PNGAPI
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500535png_write_init_2(png_structp png_ptr, png_const_charp user_png_ver,
536 png_size_t png_struct_size, png_size_t png_info_size)
537{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600538#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500539 jmp_buf tmp_jmp; /* to save current jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600540#endif
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500541 int i = 0;
542 do
543 {
544 if (user_png_ver[i] != png_libpng_ver[i])
545 {
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500546#ifdef PNG_LEGACY_SUPPORTED
547 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
548#else
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500549 png_ptr->error_fn=(png_error_ptr)NULL;
550 png_error(png_ptr,
551 "Application uses deprecated png_write_init() and must be recompiled.");
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500552#endif
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500553 }
554 } while (png_libpng_ver[i++]);
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500555
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500556 if (sizeof(png_struct) > png_struct_size ||
557 sizeof(png_info) > png_info_size)
558 {
559 png_ptr->error_fn=(png_error_ptr)NULL;
560 png_error(png_ptr,
561 "Application and library have different sized structs. Please recompile.");
562 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500563
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500564 png_debug(1, "in png_write_init_2\n");
565
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600566#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500567 /* save jump buffer and error functions */
568 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600569#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500570
571 /* reset all variables to 0 */
572 png_memset(png_ptr, 0, sizeof (png_struct));
573
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600574#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500575 /* restore jump buffer */
576 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600577#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500578
579 /* initialize zbuf - compression buffer */
580 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600581 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
582 (png_uint_32)png_ptr->zbuf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500583 png_set_write_fn(png_ptr, NULL, NULL, NULL);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500584
585#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
586 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
587 1, NULL, NULL);
588#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500589}
590
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600591/* Write a few rows of image data. If the image is interlaced,
592 * either you will have to write the 7 sub images, or, if you
593 * have called png_set_interlace_handling(), you will have to
594 * "write" the image seven times.
595 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500596void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600597png_write_rows(png_structp png_ptr, png_bytepp row,
Guy Schalnat0d580581995-07-20 02:43:20 -0500598 png_uint_32 num_rows)
599{
600 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600601 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500602
Andreas Dilger47a0c421997-05-16 02:46:07 -0500603 png_debug(1, "in png_write_rows\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500604 /* loop through the rows */
605 for (i = 0, rp = row; i < num_rows; i++, rp++)
606 {
607 png_write_row(png_ptr, *rp);
608 }
609}
610
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600611/* Write the image. You only need to call this function once, even
612 * if you are writing an interlaced image.
613 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500614void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600615png_write_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500616{
617 png_uint_32 i; /* row index */
618 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600619 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500620
Andreas Dilger47a0c421997-05-16 02:46:07 -0500621 png_debug(1, "in png_write_image\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600622#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500623 /* intialize interlace handling. If image is not interlaced,
624 this will set pass to 1 */
625 num_pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600626#else
627 num_pass = 1;
628#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500629 /* loop through passes */
630 for (pass = 0; pass < num_pass; pass++)
631 {
632 /* loop through image */
633 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
634 {
635 png_write_row(png_ptr, *rp);
636 }
637 }
638}
639
Guy Schalnate5a37791996-06-05 15:50:50 -0500640/* called by user to write a row of image data */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500641void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600642png_write_row(png_structp png_ptr, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500643{
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600644 png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
645 png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500646 /* initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600647 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500648 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500649 /* check for transforms that have been set but were defined out */
650#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
651 if (png_ptr->transformations & PNG_INVERT_MONO)
652 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
653#endif
654#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
655 if (png_ptr->transformations & PNG_FILLER)
656 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
657#endif
658#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
659 if (png_ptr->transformations & PNG_PACKSWAP)
660 png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
661#endif
662#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
663 if (png_ptr->transformations & PNG_PACK)
664 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined.");
665#endif
666#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
667 if (png_ptr->transformations & PNG_SHIFT)
668 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
669#endif
670#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
671 if (png_ptr->transformations & PNG_BGR)
672 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined.");
673#endif
674#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
675 if (png_ptr->transformations & PNG_SWAP_BYTES)
676 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
677#endif
678
Guy Schalnat0d580581995-07-20 02:43:20 -0500679 png_write_start_row(png_ptr);
680 }
681
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500682#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500683 /* if interlaced and not interested in row, return */
684 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
685 {
686 switch (png_ptr->pass)
687 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600688 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600689 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500690 {
691 png_write_finish_row(png_ptr);
692 return;
693 }
694 break;
695 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600696 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500697 {
698 png_write_finish_row(png_ptr);
699 return;
700 }
701 break;
702 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600703 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500704 {
705 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600706 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500707 }
708 break;
709 case 3:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600710 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
Guy Schalnat0d580581995-07-20 02:43:20 -0500711 {
712 png_write_finish_row(png_ptr);
713 return;
714 }
715 break;
716 case 4:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600717 if ((png_ptr->row_number & 0x03) != 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500718 {
719 png_write_finish_row(png_ptr);
720 return;
721 }
722 break;
723 case 5:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600724 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500725 {
726 png_write_finish_row(png_ptr);
727 return;
728 }
729 break;
730 case 6:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600731 if (!(png_ptr->row_number & 0x01))
Guy Schalnat0d580581995-07-20 02:43:20 -0500732 {
733 png_write_finish_row(png_ptr);
734 return;
735 }
736 break;
737 }
738 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600739#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500740
741 /* set up row info for transformations */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600742 png_ptr->row_info.color_type = png_ptr->color_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500743 png_ptr->row_info.width = png_ptr->usr_width;
744 png_ptr->row_info.channels = png_ptr->usr_channels;
745 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600746 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
747 png_ptr->row_info.channels);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600748
Guy Schalnat0d580581995-07-20 02:43:20 -0500749 png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
750 (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
751
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600752 png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500753 png_debug1(3, "row_info->width = %lu\n", png_ptr->row_info.width);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600754 png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels);
755 png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth);
756 png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500757 png_debug1(3, "row_info->rowbytes = %lu\n", png_ptr->row_info.rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500758
759 /* Copy user's row into buffer, leaving room for filter byte. */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600760 png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600761 png_ptr->row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500762
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500763#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500764 /* handle interlacing */
765 if (png_ptr->interlaced && png_ptr->pass < 6 &&
766 (png_ptr->transformations & PNG_INTERLACE))
767 {
768 png_do_write_interlace(&(png_ptr->row_info),
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600769 png_ptr->row_buf + 1, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500770 /* this should always get caught above, but still ... */
771 if (!(png_ptr->row_info.width))
772 {
773 png_write_finish_row(png_ptr);
774 return;
775 }
776 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600777#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500778
779 /* handle other transformations */
780 if (png_ptr->transformations)
781 png_do_write_transformations(png_ptr);
782
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600783#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600784 /* Write filter_method 64 (intrapixel differencing) only if
785 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
786 * 2. Libpng did not write a PNG signature (this filter_method is only
787 * used in PNG datastreams that are embedded in MNG datastreams) and
788 * 3. The application called png_permit_mng_features with a mask that
789 * included PNG_FLAG_MNG_FILTER_64 and
790 * 4. The filter_method is 64 and
791 * 5. The color_type is RGB or RGBA
792 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600793 if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
794 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
795 {
796 /* Intrapixel differencing */
797 png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
798 }
799#endif
800
Andreas Dilger47a0c421997-05-16 02:46:07 -0500801 /* Find a filter if necessary, filter the row and write it out. */
Guy Schalnate5a37791996-06-05 15:50:50 -0500802 png_write_find_filter(png_ptr, &(png_ptr->row_info));
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600803
804 if (png_ptr->write_row_fn != NULL)
805 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500806}
807
Guy Schalnat0f716451995-11-28 11:22:13 -0600808#if defined(PNG_WRITE_FLUSH_SUPPORTED)
809/* Set the automatic flush interval or 0 to turn flushing off */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500810void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600811png_set_flush(png_structp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600812{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500813 png_debug(1, "in png_set_flush\n");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600814 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600815}
816
817/* flush the current output buffers now */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500818void PNGAPI
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600819png_write_flush(png_structp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600820{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600821 int wrote_IDAT;
Guy Schalnat0f716451995-11-28 11:22:13 -0600822
Andreas Dilger47a0c421997-05-16 02:46:07 -0500823 png_debug(1, "in png_write_flush\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500824 /* We have already written out all of the data */
825 if (png_ptr->row_number >= png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600826 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600827
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600828 do
829 {
830 int ret;
Guy Schalnat0f716451995-11-28 11:22:13 -0600831
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600832 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600833 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600834 wrote_IDAT = 0;
Guy Schalnat0f716451995-11-28 11:22:13 -0600835
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600836 /* check for compression errors */
837 if (ret != Z_OK)
838 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500839 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600840 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600841 else
842 png_error(png_ptr, "zlib error");
843 }
Guy Schalnat0f716451995-11-28 11:22:13 -0600844
Andreas Dilger47a0c421997-05-16 02:46:07 -0500845 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600846 {
847 /* write the IDAT and reset the zlib output buffer */
848 png_write_IDAT(png_ptr, png_ptr->zbuf,
849 png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600850 png_ptr->zstream.next_out = png_ptr->zbuf;
851 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600852 wrote_IDAT = 1;
853 }
854 } while(wrote_IDAT == 1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600855
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600856 /* If there is any data left to be output, write it into a new IDAT */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600857 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600858 {
859 /* write the IDAT and reset the zlib output buffer */
860 png_write_IDAT(png_ptr, png_ptr->zbuf,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600861 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
862 png_ptr->zstream.next_out = png_ptr->zbuf;
863 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600864 }
865 png_ptr->flush_rows = 0;
866 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600867}
868#endif /* PNG_WRITE_FLUSH_SUPPORTED */
869
Guy Schalnate5a37791996-06-05 15:50:50 -0500870/* free all memory used by the write */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500871void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600872png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500873{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600874 png_structp png_ptr = NULL;
875 png_infop info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500876#ifdef PNG_USER_MEM_SUPPORTED
877 png_free_ptr free_fn = NULL;
878#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600879
Andreas Dilger47a0c421997-05-16 02:46:07 -0500880 png_debug(1, "in png_destroy_write_struct\n");
881 if (png_ptr_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500882 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600883 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500884#ifdef PNG_USER_MEM_SUPPORTED
885 free_fn = png_ptr->free_fn;
886#endif
887 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600888
Andreas Dilger47a0c421997-05-16 02:46:07 -0500889 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600890 info_ptr = *info_ptr_ptr;
891
Andreas Dilger47a0c421997-05-16 02:46:07 -0500892 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500893 {
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600894 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
895
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500896#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600897 if (png_ptr->num_chunk_list)
898 {
899 png_free(png_ptr, png_ptr->chunk_list);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500900 png_ptr->chunk_list=NULL;
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600901 png_ptr->num_chunk_list=0;
902 }
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500903#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600904
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500905#ifdef PNG_USER_MEM_SUPPORTED
906 png_destroy_struct_2((png_voidp)info_ptr, free_fn);
907#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600908 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500909#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600910 *info_ptr_ptr = (png_infop)NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500911 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600912
Andreas Dilger47a0c421997-05-16 02:46:07 -0500913 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500914 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600915 png_write_destroy(png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500916#ifdef PNG_USER_MEM_SUPPORTED
917 png_destroy_struct_2((png_voidp)png_ptr, free_fn);
918#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600919 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500920#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600921 *png_ptr_ptr = (png_structp)NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500922 }
923}
924
925
Andreas Dilger47a0c421997-05-16 02:46:07 -0500926/* Free any memory used in png_ptr struct (old method) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500927void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600928png_write_destroy(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500929{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600930#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600931 jmp_buf tmp_jmp; /* save jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600932#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500933 png_error_ptr error_fn;
934 png_error_ptr warning_fn;
935 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500936#ifdef PNG_USER_MEM_SUPPORTED
937 png_free_ptr free_fn;
938#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500939
Andreas Dilger47a0c421997-05-16 02:46:07 -0500940 png_debug(1, "in png_write_destroy\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500941 /* free any memory zlib uses */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600942 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -0500943
Guy Schalnat0d580581995-07-20 02:43:20 -0500944 /* free our memory. png_free checks NULL for us. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600945 png_free(png_ptr, png_ptr->zbuf);
946 png_free(png_ptr, png_ptr->row_buf);
947 png_free(png_ptr, png_ptr->prev_row);
948 png_free(png_ptr, png_ptr->sub_row);
949 png_free(png_ptr, png_ptr->up_row);
950 png_free(png_ptr, png_ptr->avg_row);
951 png_free(png_ptr, png_ptr->paeth_row);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600952
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600953#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600954 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600955#endif
956
Andreas Dilger47a0c421997-05-16 02:46:07 -0500957#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
958 png_free(png_ptr, png_ptr->prev_filters);
959 png_free(png_ptr, png_ptr->filter_weights);
960 png_free(png_ptr, png_ptr->inv_filter_weights);
961 png_free(png_ptr, png_ptr->filter_costs);
962 png_free(png_ptr, png_ptr->inv_filter_costs);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600963#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500964
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600965#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -0500966 /* reset structure */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500967 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600968#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500969
970 error_fn = png_ptr->error_fn;
971 warning_fn = png_ptr->warning_fn;
972 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500973#ifdef PNG_USER_MEM_SUPPORTED
974 free_fn = png_ptr->free_fn;
975#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500976
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500977 png_memset(png_ptr, 0, sizeof (png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -0500978
979 png_ptr->error_fn = error_fn;
980 png_ptr->warning_fn = warning_fn;
981 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500982#ifdef PNG_USER_MEM_SUPPORTED
983 png_ptr->free_fn = free_fn;
984#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500985
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600986#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500987 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600988#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500989}
Guy Schalnate5a37791996-06-05 15:50:50 -0500990
Andreas Dilger47a0c421997-05-16 02:46:07 -0500991/* Allow the application to select one or more row filters to use. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500992void PNGAPI
Guy Schalnate5a37791996-06-05 15:50:50 -0500993png_set_filter(png_structp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500994{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500995 png_debug(1, "in png_set_filter\n");
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600996#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrsonf05f8032000-12-23 14:27:39 -0600997 if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600998 (method == PNG_INTRAPIXEL_DIFFERENCING))
999 method = PNG_FILTER_TYPE_BASE;
1000#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001001 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001002 {
1003 switch (filters & (PNG_ALL_FILTERS | 0x07))
1004 {
1005 case 5:
1006 case 6:
Andreas Dilger47a0c421997-05-16 02:46:07 -05001007 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
1008 case PNG_FILTER_VALUE_NONE: png_ptr->do_filter=PNG_FILTER_NONE; break;
1009 case PNG_FILTER_VALUE_SUB: png_ptr->do_filter=PNG_FILTER_SUB; break;
1010 case PNG_FILTER_VALUE_UP: png_ptr->do_filter=PNG_FILTER_UP; break;
1011 case PNG_FILTER_VALUE_AVG: png_ptr->do_filter=PNG_FILTER_AVG; break;
1012 case PNG_FILTER_VALUE_PAETH: png_ptr->do_filter=PNG_FILTER_PAETH;break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001013 default: png_ptr->do_filter = (png_byte)filters; break;
1014 }
1015
Andreas Dilger47a0c421997-05-16 02:46:07 -05001016 /* If we have allocated the row_buf, this means we have already started
1017 * with the image and we should have allocated all of the filter buffers
1018 * that have been selected. If prev_row isn't already allocated, then
1019 * it is too late to start using the filters that need it, since we
1020 * will be missing the data in the previous row. If an application
1021 * wants to start and stop using particular filters during compression,
1022 * it should start out with all of the filters, and then add and
1023 * remove them after the start of compression.
Guy Schalnate5a37791996-06-05 15:50:50 -05001024 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001025 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001026 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001027 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001028 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001029 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001030 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001031 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001032 }
1033
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001034 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001035 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001036 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001037 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001038 png_warning(png_ptr, "Can't add Up filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -05001039 png_ptr->do_filter &= ~PNG_FILTER_UP;
1040 }
1041 else
1042 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001043 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001044 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001045 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001046 }
1047 }
1048
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001049 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001050 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001051 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001052 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001053 png_warning(png_ptr, "Can't add Average filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -05001054 png_ptr->do_filter &= ~PNG_FILTER_AVG;
1055 }
1056 else
1057 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001058 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001059 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001060 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001061 }
1062 }
1063
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001064 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
Andreas Dilger47a0c421997-05-16 02:46:07 -05001065 png_ptr->paeth_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001066 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001067 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001068 {
1069 png_warning(png_ptr, "Can't add Paeth filter after starting");
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001070 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
Guy Schalnate5a37791996-06-05 15:50:50 -05001071 }
1072 else
1073 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001074 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001075 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001076 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001077 }
1078 }
1079
1080 if (png_ptr->do_filter == PNG_NO_FILTERS)
1081 png_ptr->do_filter = PNG_FILTER_NONE;
1082 }
1083 }
1084 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05001085 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001086}
1087
Andreas Dilger47a0c421997-05-16 02:46:07 -05001088/* This allows us to influence the way in which libpng chooses the "best"
1089 * filter for the current scanline. While the "minimum-sum-of-absolute-
1090 * differences metric is relatively fast and effective, there is some
1091 * question as to whether it can be improved upon by trying to keep the
1092 * filtered data going to zlib more consistent, hopefully resulting in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001093 * better compression.
1094 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001095#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001096void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -05001097png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
1098 int num_weights, png_doublep filter_weights,
1099 png_doublep filter_costs)
1100{
1101 int i;
1102
1103 png_debug(1, "in png_set_filter_heuristics\n");
1104 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
1105 {
1106 png_warning(png_ptr, "Unknown filter heuristic method");
1107 return;
1108 }
1109
1110 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
1111 {
1112 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1113 }
1114
1115 if (num_weights < 0 || filter_weights == NULL ||
1116 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
1117 {
1118 num_weights = 0;
1119 }
1120
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001121 png_ptr->num_prev_filters = (png_byte)num_weights;
1122 png_ptr->heuristic_method = (png_byte)heuristic_method;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001123
1124 if (num_weights > 0)
1125 {
1126 if (png_ptr->prev_filters == NULL)
1127 {
1128 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001129 (png_uint_32)(sizeof(png_byte) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001130
1131 /* To make sure that the weighting starts out fairly */
1132 for (i = 0; i < num_weights; i++)
1133 {
1134 png_ptr->prev_filters[i] = 255;
1135 }
1136 }
1137
1138 if (png_ptr->filter_weights == NULL)
1139 {
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -06001140 png_ptr->filter_weights = (png_uint_16p) png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001141 (png_uint_32)(sizeof(png_uint_16) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001142
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -06001143 png_ptr->inv_filter_weights = (png_uint_16p) png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001144 (png_uint_32)(sizeof(png_uint_16) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001145
1146 for (i = 0; i < num_weights; i++)
1147 {
1148 png_ptr->inv_filter_weights[i] =
1149 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1150 }
1151 }
1152
1153 for (i = 0; i < num_weights; i++)
1154 {
1155 if (filter_weights[i] < 0.0)
1156 {
1157 png_ptr->inv_filter_weights[i] =
1158 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1159 }
1160 else
1161 {
1162 png_ptr->inv_filter_weights[i] =
1163 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
1164 png_ptr->filter_weights[i] =
1165 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
1166 }
1167 }
1168 }
1169
1170 /* If, in the future, there are other filter methods, this would
1171 * need to be based on png_ptr->filter.
1172 */
1173 if (png_ptr->filter_costs == NULL)
1174 {
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -06001175 png_ptr->filter_costs = (png_uint_16p) png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001176 (png_uint_32)(sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001177
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -06001178 png_ptr->inv_filter_costs = (png_uint_16p) png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001179 (png_uint_32)(sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001180
1181 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1182 {
1183 png_ptr->inv_filter_costs[i] =
1184 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1185 }
1186 }
1187
1188 /* Here is where we set the relative costs of the different filters. We
1189 * should take the desired compression level into account when setting
1190 * the costs, so that Paeth, for instance, has a high relative cost at low
1191 * compression levels, while it has a lower relative cost at higher
1192 * compression settings. The filter types are in order of increasing
1193 * relative cost, so it would be possible to do this with an algorithm.
1194 */
1195 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1196 {
1197 if (filter_costs == NULL || filter_costs[i] < 0.0)
1198 {
1199 png_ptr->inv_filter_costs[i] =
1200 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1201 }
1202 else if (filter_costs[i] >= 1.0)
1203 {
1204 png_ptr->inv_filter_costs[i] =
1205 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
1206 png_ptr->filter_costs[i] =
1207 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
1208 }
1209 }
1210}
1211#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1212
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001213void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001214png_set_compression_level(png_structp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001215{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001216 png_debug(1, "in png_set_compression_level\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001217 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001218 png_ptr->zlib_level = level;
1219}
1220
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001221void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001222png_set_compression_mem_level(png_structp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001223{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001224 png_debug(1, "in png_set_compression_mem_level\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001225 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001226 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001227}
1228
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001229void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001230png_set_compression_strategy(png_structp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001231{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001232 png_debug(1, "in png_set_compression_strategy\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001233 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001234 png_ptr->zlib_strategy = strategy;
1235}
1236
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001237void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001238png_set_compression_window_bits(png_structp png_ptr, int window_bits)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001239{
Guy Schalnate5a37791996-06-05 15:50:50 -05001240 if (window_bits > 15)
1241 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001242 else if (window_bits < 8)
1243 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001244#ifndef WBITS_8_OK
1245 /* avoid libpng bug with 256-byte windows */
1246 if (window_bits == 8)
1247 {
1248 png_warning(png_ptr, "Compression window is being reset to 512");
1249 window_bits=9;
1250 }
1251#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001252 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001253 png_ptr->zlib_window_bits = window_bits;
1254}
1255
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001256void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06001257png_set_compression_method(png_structp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001258{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001259 png_debug(1, "in png_set_compression_method\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001260 if (method != 8)
1261 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1262 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001263 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -05001264}
1265
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001266void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001267png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1268{
1269 png_ptr->write_row_fn = write_row_fn;
1270}
1271
1272#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001273void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001274png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
1275 write_user_transform_fn)
1276{
1277 png_debug(1, "in png_set_write_user_transform_fn\n");
1278 png_ptr->transformations |= PNG_USER_TRANSFORM;
1279 png_ptr->write_user_transform_fn = write_user_transform_fn;
1280}
1281#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001282
1283
1284#if defined(PNG_INFO_IMAGE_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001285void PNGAPI
1286png_write_png(png_structp png_ptr, png_infop info_ptr,
Glenn Randers-Pehrson1ef65b62000-05-12 06:19:53 -05001287 int transforms, voidp params)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001288{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001289#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
1290 /* invert the alpha channel from opacity to transparency */
1291 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1292 png_set_invert_alpha(png_ptr);
1293#endif
1294
1295 /* Write the file header information. */
1296 png_write_info(png_ptr, info_ptr);
1297
1298 /* ------ these transformations don't touch the info structure ------- */
1299
1300#if defined(PNG_WRITE_INVERT_SUPPORTED)
1301 /* invert monochrome pixels */
1302 if (transforms & PNG_TRANSFORM_INVERT_MONO)
1303 png_set_invert_mono(png_ptr);
1304#endif
1305
1306#if defined(PNG_WRITE_SHIFT_SUPPORTED)
1307 /* Shift the pixels up to a legal bit depth and fill in
1308 * as appropriate to correctly scale the image.
1309 */
1310 if ((transforms & PNG_TRANSFORM_SHIFT)
1311 && (info_ptr->valid & PNG_INFO_sBIT))
1312 png_set_shift(png_ptr, &info_ptr->sig_bit);
1313#endif
1314
1315#if defined(PNG_WRITE_PACK_SUPPORTED)
1316 /* pack pixels into bytes */
1317 if (transforms & PNG_TRANSFORM_PACKING)
1318 png_set_packing(png_ptr);
1319#endif
1320
1321#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
1322 /* swap location of alpha bytes from ARGB to RGBA */
1323 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1324 png_set_swap_alpha(png_ptr);
1325#endif
1326
1327#if defined(PNG_WRITE_FILLER_SUPPORTED)
1328 /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
1329 * RGB (4 channels -> 3 channels). The second parameter is not used.
1330 */
1331 if (transforms & PNG_TRANSFORM_STRIP_FILLER)
1332 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
1333#endif
1334
1335#if defined(PNG_WRITE_BGR_SUPPORTED)
1336 /* flip BGR pixels to RGB */
1337 if (transforms & PNG_TRANSFORM_BGR)
1338 png_set_bgr(png_ptr);
1339#endif
1340
1341#if defined(PNG_WRITE_SWAP_SUPPORTED)
1342 /* swap bytes of 16-bit files to most significant byte first */
1343 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1344 png_set_swap(png_ptr);
1345#endif
1346
1347#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
1348 /* swap bits of 1, 2, 4 bit packed pixel formats */
1349 if (transforms & PNG_TRANSFORM_PACKSWAP)
1350 png_set_packswap(png_ptr);
1351#endif
1352
1353 /* ----------------------- end of transformations ------------------- */
1354
1355 /* write the bits */
1356 if (info_ptr->valid & PNG_INFO_IDAT)
1357 png_write_image(png_ptr, info_ptr->row_pointers);
1358
1359 /* It is REQUIRED to call this to finish writing the rest of the file */
1360 png_write_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001361
1362 if(transforms == 0 || params == (voidp)NULL)
1363 /* quiet compiler warnings */ return;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001364}
1365#endif