blob: ab68a1e4494ceb60592415b41adba9425d5617f3 [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-Pehrsona77ef622000-02-18 13:48:52 -06004 * libpng 1.0.5s - February 18, 2000
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06008 * Copyright (c) 1998, 1999, 2000 Glenn Randers-Pehrson
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 */
Guy Schalnat0d580581995-07-20 02:43:20 -050024void
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)
60 png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_TEXT_COMPRESSION_NONE,
61 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
113void
114png_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-Pehrson61c32d92000-02-04 23:40:16 -0600168#ifdef PNG_FLOATING_POINT_SUPPORTED
169 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);
175#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600176#endif
177#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500178#if defined(PNG_WRITE_pHYs_SUPPORTED)
179 if (info_ptr->valid & PNG_INFO_pHYs)
180 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
181 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
182#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500183#if defined(PNG_WRITE_tIME_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600184 if (info_ptr->valid & PNG_INFO_tIME)
Guy Schalnate5a37791996-06-05 15:50:50 -0500185 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600186 png_write_tIME(png_ptr, &(info_ptr->mod_time));
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600187 png_ptr->mode |= PNG_WROTE_tIME;
Guy Schalnate5a37791996-06-05 15:50:50 -0500188 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500189#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600190#if defined(PNG_WRITE_sPLT_SUPPORTED)
191 if (info_ptr->valid & PNG_INFO_sPLT)
192 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
193 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
194#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600195#if defined(PNG_WRITE_TEXT_SUPPORTED)
Guy Schalnate5a37791996-06-05 15:50:50 -0500196 /* Check to see if we need to write text chunks */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500197 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500198 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500199 png_debug2(2, "Writing header text chunk %d, type %d\n", i,
200 info_ptr->text[i].compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600201 /* an internationalized chunk? */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600202 if (info_ptr->text[i].compression > 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600203 {
204#if defined(PNG_WRITE_iTXt_SUPPORTED)
205 /* write international chunk */
206 png_write_iTXt(png_ptr,
207 info_ptr->text[i].compression,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600208 info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600209 info_ptr->text[i].lang,
210 info_ptr->text[i].lang_key,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600211 info_ptr->text[i].text);
212#else
213 png_warning(png_ptr, "Unable to write international text\n");
214#endif
215 /* Mark this chunk as written */
216 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
217 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500218 /* If we want a compressed text chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600219 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500220 {
221#if defined(PNG_WRITE_zTXt_SUPPORTED)
222 /* write compressed chunk */
223 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600224 info_ptr->text[i].text, 0,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500225 info_ptr->text[i].compression);
226#else
227 png_warning(png_ptr, "Unable to write compressed text\n");
228#endif
229 /* Mark this chunk as written */
230 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
231 }
232 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
233 {
234#if defined(PNG_WRITE_tEXt_SUPPORTED)
235 /* write uncompressed chunk */
236 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600237 info_ptr->text[i].text,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600238 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500239#else
240 png_warning(png_ptr, "Unable to write uncompressed text\n");
241#endif
242 /* Mark this chunk as written */
243 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
244 }
245 }
246#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600247#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
248 if (info_ptr->unknown_chunks_num)
249 {
250 png_unknown_chunk *up;
251
252 png_debug(5, "writing extra chunks\n");
253
254 for (up = info_ptr->unknown_chunks;
255 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
256 up++)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600257 {
258 int keep=png_handle_as_unknown(png_ptr, up->name);
259 if (keep != HANDLE_CHUNK_NEVER &&
260 up->location && (up->location & PNG_HAVE_PLTE) &&
261 !(up->location & PNG_HAVE_IDAT) &&
262 ((up->name[3] & 0x20) || keep == HANDLE_CHUNK_ALWAYS ||
263 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
264 {
265 png_write_chunk(png_ptr, up->name, up->data, up->size);
266 }
267 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600268 }
269#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500270}
Guy Schalnat0d580581995-07-20 02:43:20 -0500271
Andreas Dilger47a0c421997-05-16 02:46:07 -0500272/* Writes the end of the PNG file. If you don't want to write comments or
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600273 * time information, you can pass NULL for info. If you already wrote these
274 * in png_write_info(), do not write them again here. If you have long
275 * comments, I suggest writing them here, and compressing them.
276 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500277void
278png_write_end(png_structp png_ptr, png_infop info_ptr)
279{
280 png_debug(1, "in png_write_end\n");
281 if (!(png_ptr->mode & PNG_HAVE_IDAT))
282 png_error(png_ptr, "No IDATs written into file");
283
284 /* see if user wants us to write information chunks */
285 if (info_ptr != NULL)
286 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600287#if defined(PNG_WRITE_TEXT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500288 int i; /* local index variable */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600289#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500290#if defined(PNG_WRITE_tIME_SUPPORTED)
291 /* check to see if user has supplied a time chunk */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600292 if ((info_ptr->valid & PNG_INFO_tIME) &&
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600293 !(png_ptr->mode & PNG_WROTE_tIME))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500294 png_write_tIME(png_ptr, &(info_ptr->mod_time));
295#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600296#if defined(PNG_WRITE_TEXT_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500297 /* loop through comment chunks */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600298 for (i = 0; i < info_ptr->num_text; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500299 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500300 png_debug2(2, "Writing trailer text chunk %d, type %d\n", i,
301 info_ptr->text[i].compression);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600302 /* an internationalized chunk? */
303 if (info_ptr->text[i].compression > 0)
304 {
305#if defined(PNG_WRITE_iTXt_SUPPORTED)
306 /* write international chunk */
307 png_write_iTXt(png_ptr,
308 info_ptr->text[i].compression,
309 info_ptr->text[i].key,
310 info_ptr->text[i].lang,
311 info_ptr->text[i].lang_key,
312 info_ptr->text[i].text);
313#else
314 png_warning(png_ptr, "Unable to write international text\n");
315#endif
316 /* Mark this chunk as written */
317 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
318 }
319 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
Guy Schalnat0d580581995-07-20 02:43:20 -0500320 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500321#if defined(PNG_WRITE_zTXt_SUPPORTED)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600322 /* write compressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600323 png_write_zTXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600324 info_ptr->text[i].text, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600325 info_ptr->text[i].compression);
Guy Schalnate5a37791996-06-05 15:50:50 -0500326#else
327 png_warning(png_ptr, "Unable to write compressed text\n");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500328#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500329 /* Mark this chunk as written */
330 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500331 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500332 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -0500333 {
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500334#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500335 /* write uncompressed chunk */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600336 png_write_tEXt(png_ptr, info_ptr->text[i].key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600337 info_ptr->text[i].text, 0);
Guy Schalnate5a37791996-06-05 15:50:50 -0500338#else
339 png_warning(png_ptr, "Unable to write uncompressed text\n");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500340#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500341
Andreas Dilger47a0c421997-05-16 02:46:07 -0500342 /* Mark this chunk as written */
343 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500344 }
345 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600346#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600347#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
348 if (info_ptr->unknown_chunks_num)
349 {
350 png_unknown_chunk *up;
351
352 png_debug(5, "writing extra chunks\n");
353
354 for (up = info_ptr->unknown_chunks;
355 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
356 up++)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600357 {
358 int keep=png_handle_as_unknown(png_ptr, up->name);
359 if (keep != HANDLE_CHUNK_NEVER &&
360 up->location && (up->location & PNG_AFTER_IDAT) &&
361 ((up->name[3] & 0x20) || keep == HANDLE_CHUNK_ALWAYS ||
362 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
363 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600364 png_write_chunk(png_ptr, up->name, up->data, up->size);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600365 }
366 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600367 }
368#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500369 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500370
371 png_ptr->mode |= PNG_AFTER_IDAT;
372
Andreas Dilger47a0c421997-05-16 02:46:07 -0500373 /* write end of PNG file */
Guy Schalnat0d580581995-07-20 02:43:20 -0500374 png_write_IEND(png_ptr);
375}
376
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500377#if defined(PNG_WRITE_tIME_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500378void
Guy Schalnat6d764711995-12-19 03:22:19 -0600379png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500380{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500381 png_debug(1, "in png_convert_from_struct_tm\n");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600382 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
383 ptime->month = (png_byte)(ttime->tm_mon + 1);
384 ptime->day = (png_byte)ttime->tm_mday;
385 ptime->hour = (png_byte)ttime->tm_hour;
386 ptime->minute = (png_byte)ttime->tm_min;
387 ptime->second = (png_byte)ttime->tm_sec;
Guy Schalnat0d580581995-07-20 02:43:20 -0500388}
389
390void
Guy Schalnat6d764711995-12-19 03:22:19 -0600391png_convert_from_time_t(png_timep ptime, time_t ttime)
Guy Schalnat0d580581995-07-20 02:43:20 -0500392{
393 struct tm *tbuf;
394
Andreas Dilger47a0c421997-05-16 02:46:07 -0500395 png_debug(1, "in png_convert_from_time_t\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500396 tbuf = gmtime(&ttime);
397 png_convert_from_struct_tm(ptime, tbuf);
398}
Guy Schalnat6d764711995-12-19 03:22:19 -0600399#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500400
Andreas Dilger47a0c421997-05-16 02:46:07 -0500401/* Initialize png_ptr structure, and allocate any memory needed */
Guy Schalnate5a37791996-06-05 15:50:50 -0500402png_structp
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500403png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600404 png_error_ptr error_fn, png_error_ptr warn_fn)
Guy Schalnat0d580581995-07-20 02:43:20 -0500405{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500406#ifdef PNG_USER_MEM_SUPPORTED
407 return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
408 warn_fn, NULL, NULL, NULL));
409}
410
411/* Alternate initialize png_ptr structure, and allocate any memory needed */
412png_structp
413png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
414 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
415 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
416{
417#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500418 png_structp png_ptr;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600419#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600420#ifdef USE_FAR_KEYWORD
421 jmp_buf jmpbuf;
422#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600423#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500424 png_debug(1, "in png_create_write_struct\n");
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500425#ifdef PNG_USER_MEM_SUPPORTED
426 if ((png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
427 (png_malloc_ptr)malloc_fn)) == NULL)
428#else
Guy Schalnate5a37791996-06-05 15:50:50 -0500429 if ((png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG)) == NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500430#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500431 {
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600432 return ((png_structp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500433 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600434
435#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600436#ifdef USE_FAR_KEYWORD
437 if (setjmp(jmpbuf))
438#else
Guy Schalnate5a37791996-06-05 15:50:50 -0500439 if (setjmp(png_ptr->jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600440#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500441 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600442 png_free(png_ptr, png_ptr->zbuf);
Guy Schalnate5a37791996-06-05 15:50:50 -0500443 png_destroy_struct(png_ptr);
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600444 return ((png_structp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500445 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600446#ifdef USE_FAR_KEYWORD
447 png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
448#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600449#endif
450
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500451#ifdef PNG_USER_MEM_SUPPORTED
452 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
453#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600454 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnate5a37791996-06-05 15:50:50 -0500455
Andreas Dilger47a0c421997-05-16 02:46:07 -0500456 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
457 * we must recompile any applications that use any older library version.
458 * For versions after libpng 1.0, we will be compatible, so we need
459 * only check the first digit.
460 */
461 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
462 (png_libpng_ver[0] == '0' && user_png_ver[2] < '9'))
Guy Schalnate5a37791996-06-05 15:50:50 -0500463 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500464 png_error(png_ptr,
465 "Incompatible libpng version in application and library");
Guy Schalnate5a37791996-06-05 15:50:50 -0500466 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500467
468 /* initialize zbuf - compression buffer */
469 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600470 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
471 (png_uint_32)png_ptr->zbuf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500472
473 png_set_write_fn(png_ptr, NULL, NULL, NULL);
474
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600475#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
476 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
477 1, NULL, NULL);
478#endif
479
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600480 return ((png_structp)png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500481}
482
Andreas Dilger47a0c421997-05-16 02:46:07 -0500483/* Initialize png_ptr structure, and allocate any memory needed */
Guy Schalnate5a37791996-06-05 15:50:50 -0500484void
485png_write_init(png_structp png_ptr)
486{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600487#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500488 jmp_buf tmp_jmp; /* to save current jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600489#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500490
Andreas Dilger47a0c421997-05-16 02:46:07 -0500491 png_debug(1, "in png_write_init\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600492#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500493 /* save jump buffer and error functions */
494 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600495#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500496
497 /* reset all variables to 0 */
498 png_memset(png_ptr, 0, sizeof (png_struct));
499
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600500#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500501 /* restore jump buffer */
502 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600503#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500504
505 /* initialize zbuf - compression buffer */
506 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600507 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
508 (png_uint_32)png_ptr->zbuf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -0500509 png_set_write_fn(png_ptr, NULL, NULL, NULL);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500510
511#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
512 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
513 1, NULL, NULL);
514#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500515}
516
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600517/* Write a few rows of image data. If the image is interlaced,
518 * either you will have to write the 7 sub images, or, if you
519 * have called png_set_interlace_handling(), you will have to
520 * "write" the image seven times.
521 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500522void
Guy Schalnat6d764711995-12-19 03:22:19 -0600523png_write_rows(png_structp png_ptr, png_bytepp row,
Guy Schalnat0d580581995-07-20 02:43:20 -0500524 png_uint_32 num_rows)
525{
526 png_uint_32 i; /* row counter */
Guy Schalnat6d764711995-12-19 03:22:19 -0600527 png_bytepp rp; /* row pointer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500528
Andreas Dilger47a0c421997-05-16 02:46:07 -0500529 png_debug(1, "in png_write_rows\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500530 /* loop through the rows */
531 for (i = 0, rp = row; i < num_rows; i++, rp++)
532 {
533 png_write_row(png_ptr, *rp);
534 }
535}
536
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600537/* Write the image. You only need to call this function once, even
538 * if you are writing an interlaced image.
539 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500540void
Guy Schalnat6d764711995-12-19 03:22:19 -0600541png_write_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500542{
543 png_uint_32 i; /* row index */
544 int pass, num_pass; /* pass variables */
Guy Schalnat6d764711995-12-19 03:22:19 -0600545 png_bytepp rp; /* points to current row */
Guy Schalnat0d580581995-07-20 02:43:20 -0500546
Andreas Dilger47a0c421997-05-16 02:46:07 -0500547 png_debug(1, "in png_write_image\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600548#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500549 /* intialize interlace handling. If image is not interlaced,
550 this will set pass to 1 */
551 num_pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600552#else
553 num_pass = 1;
554#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500555 /* loop through passes */
556 for (pass = 0; pass < num_pass; pass++)
557 {
558 /* loop through image */
559 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
560 {
561 png_write_row(png_ptr, *rp);
562 }
563 }
564}
565
Guy Schalnate5a37791996-06-05 15:50:50 -0500566/* called by user to write a row of image data */
Guy Schalnat0d580581995-07-20 02:43:20 -0500567void
Guy Schalnat6d764711995-12-19 03:22:19 -0600568png_write_row(png_structp png_ptr, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500569{
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600570 png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
571 png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500572 /* initialize transformations and other stuff if first time */
Guy Schalnat6d764711995-12-19 03:22:19 -0600573 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500574 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500575 /* check for transforms that have been set but were defined out */
576#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
577 if (png_ptr->transformations & PNG_INVERT_MONO)
578 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
579#endif
580#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
581 if (png_ptr->transformations & PNG_FILLER)
582 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
583#endif
584#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
585 if (png_ptr->transformations & PNG_PACKSWAP)
586 png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
587#endif
588#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
589 if (png_ptr->transformations & PNG_PACK)
590 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined.");
591#endif
592#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
593 if (png_ptr->transformations & PNG_SHIFT)
594 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
595#endif
596#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
597 if (png_ptr->transformations & PNG_BGR)
598 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined.");
599#endif
600#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
601 if (png_ptr->transformations & PNG_SWAP_BYTES)
602 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
603#endif
604
Guy Schalnat0d580581995-07-20 02:43:20 -0500605 png_write_start_row(png_ptr);
606 }
607
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500608#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500609 /* if interlaced and not interested in row, return */
610 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
611 {
612 switch (png_ptr->pass)
613 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600614 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600615 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500616 {
617 png_write_finish_row(png_ptr);
618 return;
619 }
620 break;
621 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600622 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500623 {
624 png_write_finish_row(png_ptr);
625 return;
626 }
627 break;
628 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600629 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500630 {
631 png_write_finish_row(png_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600632 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500633 }
634 break;
635 case 3:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600636 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
Guy Schalnat0d580581995-07-20 02:43:20 -0500637 {
638 png_write_finish_row(png_ptr);
639 return;
640 }
641 break;
642 case 4:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600643 if ((png_ptr->row_number & 0x03) != 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500644 {
645 png_write_finish_row(png_ptr);
646 return;
647 }
648 break;
649 case 5:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600650 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
Guy Schalnat0d580581995-07-20 02:43:20 -0500651 {
652 png_write_finish_row(png_ptr);
653 return;
654 }
655 break;
656 case 6:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600657 if (!(png_ptr->row_number & 0x01))
Guy Schalnat0d580581995-07-20 02:43:20 -0500658 {
659 png_write_finish_row(png_ptr);
660 return;
661 }
662 break;
663 }
664 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600665#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500666
667 /* set up row info for transformations */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600668 png_ptr->row_info.color_type = png_ptr->color_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500669 png_ptr->row_info.width = png_ptr->usr_width;
670 png_ptr->row_info.channels = png_ptr->usr_channels;
671 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600672 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
673 png_ptr->row_info.channels);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600674
Guy Schalnat0d580581995-07-20 02:43:20 -0500675 png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
676 (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
677
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600678 png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
679 png_debug1(3, "row_info->width = %d\n", png_ptr->row_info.width);
680 png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels);
681 png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth);
682 png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth);
683 png_debug1(3, "row_info->rowbytes = %d\n", png_ptr->row_info.rowbytes);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500684
685 /* Copy user's row into buffer, leaving room for filter byte. */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600686 png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600687 png_ptr->row_info.rowbytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500688
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500689#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500690 /* handle interlacing */
691 if (png_ptr->interlaced && png_ptr->pass < 6 &&
692 (png_ptr->transformations & PNG_INTERLACE))
693 {
694 png_do_write_interlace(&(png_ptr->row_info),
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600695 png_ptr->row_buf + 1, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500696 /* this should always get caught above, but still ... */
697 if (!(png_ptr->row_info.width))
698 {
699 png_write_finish_row(png_ptr);
700 return;
701 }
702 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600703#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500704
705 /* handle other transformations */
706 if (png_ptr->transformations)
707 png_do_write_transformations(png_ptr);
708
Andreas Dilger47a0c421997-05-16 02:46:07 -0500709 /* Find a filter if necessary, filter the row and write it out. */
Guy Schalnate5a37791996-06-05 15:50:50 -0500710 png_write_find_filter(png_ptr, &(png_ptr->row_info));
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600711
712 if (png_ptr->write_row_fn != NULL)
713 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500714}
715
Guy Schalnat0f716451995-11-28 11:22:13 -0600716#if defined(PNG_WRITE_FLUSH_SUPPORTED)
717/* Set the automatic flush interval or 0 to turn flushing off */
718void
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600719png_set_flush(png_structp png_ptr, int nrows)
Guy Schalnat0f716451995-11-28 11:22:13 -0600720{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500721 png_debug(1, "in png_set_flush\n");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600722 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
Guy Schalnat0f716451995-11-28 11:22:13 -0600723}
724
725/* flush the current output buffers now */
726void
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600727png_write_flush(png_structp png_ptr)
Guy Schalnat0f716451995-11-28 11:22:13 -0600728{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600729 int wrote_IDAT;
Guy Schalnat0f716451995-11-28 11:22:13 -0600730
Andreas Dilger47a0c421997-05-16 02:46:07 -0500731 png_debug(1, "in png_write_flush\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500732 /* We have already written out all of the data */
733 if (png_ptr->row_number >= png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600734 return;
Guy Schalnat0f716451995-11-28 11:22:13 -0600735
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600736 do
737 {
738 int ret;
Guy Schalnat0f716451995-11-28 11:22:13 -0600739
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600740 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600741 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600742 wrote_IDAT = 0;
Guy Schalnat0f716451995-11-28 11:22:13 -0600743
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600744 /* check for compression errors */
745 if (ret != Z_OK)
746 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500747 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600748 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600749 else
750 png_error(png_ptr, "zlib error");
751 }
Guy Schalnat0f716451995-11-28 11:22:13 -0600752
Andreas Dilger47a0c421997-05-16 02:46:07 -0500753 if (!(png_ptr->zstream.avail_out))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600754 {
755 /* write the IDAT and reset the zlib output buffer */
756 png_write_IDAT(png_ptr, png_ptr->zbuf,
757 png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600758 png_ptr->zstream.next_out = png_ptr->zbuf;
759 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600760 wrote_IDAT = 1;
761 }
762 } while(wrote_IDAT == 1);
Guy Schalnat0f716451995-11-28 11:22:13 -0600763
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600764 /* If there is any data left to be output, write it into a new IDAT */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600765 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600766 {
767 /* write the IDAT and reset the zlib output buffer */
768 png_write_IDAT(png_ptr, png_ptr->zbuf,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600769 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
770 png_ptr->zstream.next_out = png_ptr->zbuf;
771 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600772 }
773 png_ptr->flush_rows = 0;
774 png_flush(png_ptr);
Guy Schalnat0f716451995-11-28 11:22:13 -0600775}
776#endif /* PNG_WRITE_FLUSH_SUPPORTED */
777
Guy Schalnate5a37791996-06-05 15:50:50 -0500778/* free all memory used by the write */
779void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600780png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500781{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600782 png_structp png_ptr = NULL;
783 png_infop info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500784#ifdef PNG_USER_MEM_SUPPORTED
785 png_free_ptr free_fn = NULL;
786#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600787
Andreas Dilger47a0c421997-05-16 02:46:07 -0500788 png_debug(1, "in png_destroy_write_struct\n");
789 if (png_ptr_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500790 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600791 png_ptr = *png_ptr_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500792#ifdef PNG_USER_MEM_SUPPORTED
793 free_fn = png_ptr->free_fn;
794#endif
795 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600796
Andreas Dilger47a0c421997-05-16 02:46:07 -0500797 if (info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600798 info_ptr = *info_ptr_ptr;
799
Andreas Dilger47a0c421997-05-16 02:46:07 -0500800 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500801 {
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600802 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
803
804 if (png_ptr->num_chunk_list)
805 {
806 png_free(png_ptr, png_ptr->chunk_list);
807 png_ptr->num_chunk_list=0;
808 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600809
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500810#ifdef PNG_USER_MEM_SUPPORTED
811 png_destroy_struct_2((png_voidp)info_ptr, free_fn);
812#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600813 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500814#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600815 *info_ptr_ptr = (png_infop)NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500816 }
Guy Schalnat6d764711995-12-19 03:22:19 -0600817
Andreas Dilger47a0c421997-05-16 02:46:07 -0500818 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500819 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600820 png_write_destroy(png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500821#ifdef PNG_USER_MEM_SUPPORTED
822 png_destroy_struct_2((png_voidp)png_ptr, free_fn);
823#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600824 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500825#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600826 *png_ptr_ptr = (png_structp)NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500827 }
828}
829
830
Andreas Dilger47a0c421997-05-16 02:46:07 -0500831/* Free any memory used in png_ptr struct (old method) */
Guy Schalnat0d580581995-07-20 02:43:20 -0500832void
Guy Schalnat6d764711995-12-19 03:22:19 -0600833png_write_destroy(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500834{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600835#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600836 jmp_buf tmp_jmp; /* save jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600837#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500838 png_error_ptr error_fn;
839 png_error_ptr warning_fn;
840 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500841#ifdef PNG_USER_MEM_SUPPORTED
842 png_free_ptr free_fn;
843#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500844
Andreas Dilger47a0c421997-05-16 02:46:07 -0500845 png_debug(1, "in png_write_destroy\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500846 /* free any memory zlib uses */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600847 deflateEnd(&png_ptr->zstream);
Guy Schalnate5a37791996-06-05 15:50:50 -0500848
Guy Schalnat0d580581995-07-20 02:43:20 -0500849 /* free our memory. png_free checks NULL for us. */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600850 png_free(png_ptr, png_ptr->zbuf);
851 png_free(png_ptr, png_ptr->row_buf);
852 png_free(png_ptr, png_ptr->prev_row);
853 png_free(png_ptr, png_ptr->sub_row);
854 png_free(png_ptr, png_ptr->up_row);
855 png_free(png_ptr, png_ptr->avg_row);
856 png_free(png_ptr, png_ptr->paeth_row);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600857
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600858#if defined(PNG_TIME_RFC1123_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600859 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600860#endif
861
Andreas Dilger47a0c421997-05-16 02:46:07 -0500862#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
863 png_free(png_ptr, png_ptr->prev_filters);
864 png_free(png_ptr, png_ptr->filter_weights);
865 png_free(png_ptr, png_ptr->inv_filter_weights);
866 png_free(png_ptr, png_ptr->filter_costs);
867 png_free(png_ptr, png_ptr->inv_filter_costs);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600868#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500869
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600870#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -0500871 /* reset structure */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500872 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600873#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500874
875 error_fn = png_ptr->error_fn;
876 warning_fn = png_ptr->warning_fn;
877 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500878#ifdef PNG_USER_MEM_SUPPORTED
879 free_fn = png_ptr->free_fn;
880#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500881
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500882 png_memset(png_ptr, 0, sizeof (png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -0500883
884 png_ptr->error_fn = error_fn;
885 png_ptr->warning_fn = warning_fn;
886 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500887#ifdef PNG_USER_MEM_SUPPORTED
888 png_ptr->free_fn = free_fn;
889#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500890
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600891#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500892 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600893#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500894}
Guy Schalnate5a37791996-06-05 15:50:50 -0500895
Andreas Dilger47a0c421997-05-16 02:46:07 -0500896/* Allow the application to select one or more row filters to use. */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500897void
Guy Schalnate5a37791996-06-05 15:50:50 -0500898png_set_filter(png_structp png_ptr, int method, int filters)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500899{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500900 png_debug(1, "in png_set_filter\n");
901 /* We allow 'method' only for future expansion of the base filter method. */
902 if (method == PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500903 {
904 switch (filters & (PNG_ALL_FILTERS | 0x07))
905 {
906 case 5:
907 case 6:
Andreas Dilger47a0c421997-05-16 02:46:07 -0500908 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
909 case PNG_FILTER_VALUE_NONE: png_ptr->do_filter=PNG_FILTER_NONE; break;
910 case PNG_FILTER_VALUE_SUB: png_ptr->do_filter=PNG_FILTER_SUB; break;
911 case PNG_FILTER_VALUE_UP: png_ptr->do_filter=PNG_FILTER_UP; break;
912 case PNG_FILTER_VALUE_AVG: png_ptr->do_filter=PNG_FILTER_AVG; break;
913 case PNG_FILTER_VALUE_PAETH: png_ptr->do_filter=PNG_FILTER_PAETH;break;
Guy Schalnate5a37791996-06-05 15:50:50 -0500914 default: png_ptr->do_filter = (png_byte)filters; break;
915 }
916
Andreas Dilger47a0c421997-05-16 02:46:07 -0500917 /* If we have allocated the row_buf, this means we have already started
918 * with the image and we should have allocated all of the filter buffers
919 * that have been selected. If prev_row isn't already allocated, then
920 * it is too late to start using the filters that need it, since we
921 * will be missing the data in the previous row. If an application
922 * wants to start and stop using particular filters during compression,
923 * it should start out with all of the filters, and then add and
924 * remove them after the start of compression.
Guy Schalnate5a37791996-06-05 15:50:50 -0500925 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500926 if (png_ptr->row_buf != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500927 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600928 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500929 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500930 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600931 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500932 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -0500933 }
934
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600935 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500936 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500937 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500938 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500939 png_warning(png_ptr, "Can't add Up filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -0500940 png_ptr->do_filter &= ~PNG_FILTER_UP;
941 }
942 else
943 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500944 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600945 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500946 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -0500947 }
948 }
949
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600950 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500951 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500952 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500953 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500954 png_warning(png_ptr, "Can't add Average filter after starting");
Guy Schalnate5a37791996-06-05 15:50:50 -0500955 png_ptr->do_filter &= ~PNG_FILTER_AVG;
956 }
957 else
958 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500959 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600960 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500961 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -0500962 }
963 }
964
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600965 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
Andreas Dilger47a0c421997-05-16 02:46:07 -0500966 png_ptr->paeth_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500967 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500968 if (png_ptr->prev_row == NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500969 {
970 png_warning(png_ptr, "Can't add Paeth filter after starting");
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500971 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
Guy Schalnate5a37791996-06-05 15:50:50 -0500972 }
973 else
974 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600975 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600976 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500977 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -0500978 }
979 }
980
981 if (png_ptr->do_filter == PNG_NO_FILTERS)
982 png_ptr->do_filter = PNG_FILTER_NONE;
983 }
984 }
985 else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500986 png_error(png_ptr, "Unknown custom filter method");
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500987}
988
Andreas Dilger47a0c421997-05-16 02:46:07 -0500989/* This allows us to influence the way in which libpng chooses the "best"
990 * filter for the current scanline. While the "minimum-sum-of-absolute-
991 * differences metric is relatively fast and effective, there is some
992 * question as to whether it can be improved upon by trying to keep the
993 * filtered data going to zlib more consistent, hopefully resulting in
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600994 * better compression.
995 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500996#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
997void
998png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
999 int num_weights, png_doublep filter_weights,
1000 png_doublep filter_costs)
1001{
1002 int i;
1003
1004 png_debug(1, "in png_set_filter_heuristics\n");
1005 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
1006 {
1007 png_warning(png_ptr, "Unknown filter heuristic method");
1008 return;
1009 }
1010
1011 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
1012 {
1013 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1014 }
1015
1016 if (num_weights < 0 || filter_weights == NULL ||
1017 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
1018 {
1019 num_weights = 0;
1020 }
1021
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001022 png_ptr->num_prev_filters = (png_byte)num_weights;
1023 png_ptr->heuristic_method = (png_byte)heuristic_method;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001024
1025 if (num_weights > 0)
1026 {
1027 if (png_ptr->prev_filters == NULL)
1028 {
1029 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001030 (png_uint_32)(sizeof(png_byte) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001031
1032 /* To make sure that the weighting starts out fairly */
1033 for (i = 0; i < num_weights; i++)
1034 {
1035 png_ptr->prev_filters[i] = 255;
1036 }
1037 }
1038
1039 if (png_ptr->filter_weights == NULL)
1040 {
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -06001041 png_ptr->filter_weights = (png_uint_16p) png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001042 (png_uint_32)(sizeof(png_uint_16) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001043
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -06001044 png_ptr->inv_filter_weights = (png_uint_16p) png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001045 (png_uint_32)(sizeof(png_uint_16) * num_weights));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001046
1047 for (i = 0; i < num_weights; i++)
1048 {
1049 png_ptr->inv_filter_weights[i] =
1050 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1051 }
1052 }
1053
1054 for (i = 0; i < num_weights; i++)
1055 {
1056 if (filter_weights[i] < 0.0)
1057 {
1058 png_ptr->inv_filter_weights[i] =
1059 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1060 }
1061 else
1062 {
1063 png_ptr->inv_filter_weights[i] =
1064 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
1065 png_ptr->filter_weights[i] =
1066 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
1067 }
1068 }
1069 }
1070
1071 /* If, in the future, there are other filter methods, this would
1072 * need to be based on png_ptr->filter.
1073 */
1074 if (png_ptr->filter_costs == NULL)
1075 {
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -06001076 png_ptr->filter_costs = (png_uint_16p) png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001077 (png_uint_32)(sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001078
Glenn Randers-Pehrson983ec161998-03-07 11:24:03 -06001079 png_ptr->inv_filter_costs = (png_uint_16p) png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001080 (png_uint_32)(sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001081
1082 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1083 {
1084 png_ptr->inv_filter_costs[i] =
1085 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1086 }
1087 }
1088
1089 /* Here is where we set the relative costs of the different filters. We
1090 * should take the desired compression level into account when setting
1091 * the costs, so that Paeth, for instance, has a high relative cost at low
1092 * compression levels, while it has a lower relative cost at higher
1093 * compression settings. The filter types are in order of increasing
1094 * relative cost, so it would be possible to do this with an algorithm.
1095 */
1096 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1097 {
1098 if (filter_costs == NULL || filter_costs[i] < 0.0)
1099 {
1100 png_ptr->inv_filter_costs[i] =
1101 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1102 }
1103 else if (filter_costs[i] >= 1.0)
1104 {
1105 png_ptr->inv_filter_costs[i] =
1106 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
1107 png_ptr->filter_costs[i] =
1108 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
1109 }
1110 }
1111}
1112#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1113
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001114void
Guy Schalnat6d764711995-12-19 03:22:19 -06001115png_set_compression_level(png_structp png_ptr, int level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001116{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001117 png_debug(1, "in png_set_compression_level\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001118 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001119 png_ptr->zlib_level = level;
1120}
1121
1122void
Guy Schalnat6d764711995-12-19 03:22:19 -06001123png_set_compression_mem_level(png_structp png_ptr, int mem_level)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001124{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001125 png_debug(1, "in png_set_compression_mem_level\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001126 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001127 png_ptr->zlib_mem_level = mem_level;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001128}
1129
1130void
Guy Schalnat6d764711995-12-19 03:22:19 -06001131png_set_compression_strategy(png_structp png_ptr, int strategy)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001132{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001133 png_debug(1, "in png_set_compression_strategy\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001134 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001135 png_ptr->zlib_strategy = strategy;
1136}
1137
1138void
Guy Schalnat6d764711995-12-19 03:22:19 -06001139png_set_compression_window_bits(png_structp png_ptr, int window_bits)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001140{
Guy Schalnate5a37791996-06-05 15:50:50 -05001141 if (window_bits > 15)
1142 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001143 else if (window_bits < 8)
1144 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001145#ifndef WBITS_8_OK
1146 /* avoid libpng bug with 256-byte windows */
1147 if (window_bits == 8)
1148 {
1149 png_warning(png_ptr, "Compression window is being reset to 512");
1150 window_bits=9;
1151 }
1152#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001153 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001154 png_ptr->zlib_window_bits = window_bits;
1155}
1156
1157void
Guy Schalnat6d764711995-12-19 03:22:19 -06001158png_set_compression_method(png_structp png_ptr, int method)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001159{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001160 png_debug(1, "in png_set_compression_method\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001161 if (method != 8)
1162 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1163 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001164 png_ptr->zlib_method = method;
Guy Schalnat0d580581995-07-20 02:43:20 -05001165}
1166
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001167void
1168png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1169{
1170 png_ptr->write_row_fn = write_row_fn;
1171}
1172
1173#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1174void
1175png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
1176 write_user_transform_fn)
1177{
1178 png_debug(1, "in png_set_write_user_transform_fn\n");
1179 png_ptr->transformations |= PNG_USER_TRANSFORM;
1180 png_ptr->write_user_transform_fn = write_user_transform_fn;
1181}
1182#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001183
1184
1185#if defined(PNG_INFO_IMAGE_SUPPORTED)
1186void png_write_png(png_structp png_ptr, png_infop info_ptr,
1187 int transforms,
1188 voidp params)
1189{
1190 if(transforms == 0 || params == (voidp)NULL)
1191 /* quiet compiler warnings */ ;
1192
1193#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
1194 /* invert the alpha channel from opacity to transparency */
1195 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1196 png_set_invert_alpha(png_ptr);
1197#endif
1198
1199 /* Write the file header information. */
1200 png_write_info(png_ptr, info_ptr);
1201
1202 /* ------ these transformations don't touch the info structure ------- */
1203
1204#if defined(PNG_WRITE_INVERT_SUPPORTED)
1205 /* invert monochrome pixels */
1206 if (transforms & PNG_TRANSFORM_INVERT_MONO)
1207 png_set_invert_mono(png_ptr);
1208#endif
1209
1210#if defined(PNG_WRITE_SHIFT_SUPPORTED)
1211 /* Shift the pixels up to a legal bit depth and fill in
1212 * as appropriate to correctly scale the image.
1213 */
1214 if ((transforms & PNG_TRANSFORM_SHIFT)
1215 && (info_ptr->valid & PNG_INFO_sBIT))
1216 png_set_shift(png_ptr, &info_ptr->sig_bit);
1217#endif
1218
1219#if defined(PNG_WRITE_PACK_SUPPORTED)
1220 /* pack pixels into bytes */
1221 if (transforms & PNG_TRANSFORM_PACKING)
1222 png_set_packing(png_ptr);
1223#endif
1224
1225#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
1226 /* swap location of alpha bytes from ARGB to RGBA */
1227 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1228 png_set_swap_alpha(png_ptr);
1229#endif
1230
1231#if defined(PNG_WRITE_FILLER_SUPPORTED)
1232 /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
1233 * RGB (4 channels -> 3 channels). The second parameter is not used.
1234 */
1235 if (transforms & PNG_TRANSFORM_STRIP_FILLER)
1236 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
1237#endif
1238
1239#if defined(PNG_WRITE_BGR_SUPPORTED)
1240 /* flip BGR pixels to RGB */
1241 if (transforms & PNG_TRANSFORM_BGR)
1242 png_set_bgr(png_ptr);
1243#endif
1244
1245#if defined(PNG_WRITE_SWAP_SUPPORTED)
1246 /* swap bytes of 16-bit files to most significant byte first */
1247 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1248 png_set_swap(png_ptr);
1249#endif
1250
1251#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
1252 /* swap bits of 1, 2, 4 bit packed pixel formats */
1253 if (transforms & PNG_TRANSFORM_PACKSWAP)
1254 png_set_packswap(png_ptr);
1255#endif
1256
1257 /* ----------------------- end of transformations ------------------- */
1258
1259 /* write the bits */
1260 if (info_ptr->valid & PNG_INFO_IDAT)
1261 png_write_image(png_ptr, info_ptr->row_pointers);
1262
1263 /* It is REQUIRED to call this to finish writing the rest of the file */
1264 png_write_end(png_ptr, info_ptr);
1265}
1266#endif