blob: fccd7b68d4ecdec045a21e0cc29f40ccc9695f8c [file] [log] [blame]
Guy Schalnat0d580581995-07-20 02:43:20 -05001
Andreas Dilger47a0c421997-05-16 02:46:07 -05002/* pngwutil.c - utilities to write a PNG file
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06004 * libpng 0.97
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-Pehrson2687fcc1998-01-07 20:54:20 -06008 * Copyright (c) 1998, Glenn Randers-Pehrson
9 * January 7, 1998
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060010 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050011
Guy Schalnat0d580581995-07-20 02:43:20 -050012#define PNG_INTERNAL
13#include "png.h"
14
Andreas Dilger47a0c421997-05-16 02:46:07 -050015/* Place a 32-bit number into a buffer in PNG byte order. We work
16 * with unsigned numbers for convenience, although one supported
17 * ancillary chunk uses signed (two's complement) numbers.
18 */
Guy Schalnat0d580581995-07-20 02:43:20 -050019void
Guy Schalnat6d764711995-12-19 03:22:19 -060020png_save_uint_32(png_bytep buf, png_uint_32 i)
Guy Schalnat0d580581995-07-20 02:43:20 -050021{
22 buf[0] = (png_byte)((i >> 24) & 0xff);
23 buf[1] = (png_byte)((i >> 16) & 0xff);
24 buf[2] = (png_byte)((i >> 8) & 0xff);
25 buf[3] = (png_byte)(i & 0xff);
26}
27
Andreas Dilger47a0c421997-05-16 02:46:07 -050028#if defined(PNG_WRITE_pCAL_SUPPORTED)
29/* The png_save_int_32 function assumes integers are stored in two's
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060030 * complement format. If this isn't the case, then this routine needs to
31 * be modified to write data in two's complement format.
32 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050033void
34png_save_int_32(png_bytep buf, png_int_32 i)
35{
36 buf[0] = (png_byte)((i >> 24) & 0xff);
37 buf[1] = (png_byte)((i >> 16) & 0xff);
38 buf[2] = (png_byte)((i >> 8) & 0xff);
39 buf[3] = (png_byte)(i & 0xff);
40}
41#endif
42
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060043/* Place a 16-bit number into a buffer in PNG byte order.
44 * The parameter is declared unsigned int, not png_uint_16,
45 * just to avoid potential problems on pre-ANSI C compilers.
46 */
Guy Schalnat0d580581995-07-20 02:43:20 -050047void
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060048png_save_uint_16(png_bytep buf, unsigned int i)
Guy Schalnat0d580581995-07-20 02:43:20 -050049{
50 buf[0] = (png_byte)((i >> 8) & 0xff);
51 buf[1] = (png_byte)(i & 0xff);
52}
53
Andreas Dilger47a0c421997-05-16 02:46:07 -050054/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060055 * representing the chunk name. The array must be at least 4 bytes in
56 * length, and does not need to be null terminated. To be safe, pass the
57 * pre-defined chunk names here, and if you need a new one, define it
58 * where the others are defined. The length is the length of the data.
59 * All the data must be present. If that is not possible, use the
60 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
61 * functions instead.
62 */
Guy Schalnat0d580581995-07-20 02:43:20 -050063void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060064png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050065 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050066{
Andreas Dilger47a0c421997-05-16 02:46:07 -050067 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060068 png_write_chunk_data(png_ptr, data, length);
69 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050070}
71
Andreas Dilger47a0c421997-05-16 02:46:07 -050072/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060073 * The total_length is the sum of the lengths of all the data you will be
74 * passing in png_write_chunk_data().
75 */
Guy Schalnat0d580581995-07-20 02:43:20 -050076void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060077png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
78 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -050079{
Andreas Dilger47a0c421997-05-16 02:46:07 -050080 png_byte buf[4];
81 png_debug2(0, "Writing %s chunk (%d bytes)\n", chunk_name, length);
82
Guy Schalnat0d580581995-07-20 02:43:20 -050083 /* write the length */
Andreas Dilger47a0c421997-05-16 02:46:07 -050084 png_save_uint_32(buf, length);
85 png_write_data(png_ptr, buf, (png_size_t)4);
86
Guy Schalnat0d580581995-07-20 02:43:20 -050087 /* write the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -050088 png_write_data(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050089 /* reset the crc and run it over the chunk name */
90 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -050091 png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050092}
93
Andreas Dilger47a0c421997-05-16 02:46:07 -050094/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060095 * Note that multiple calls to this function are allowed, and that the
96 * sum of the lengths from these calls *must* add up to the total_length
97 * given to png_write_chunk_start().
98 */
Guy Schalnat0d580581995-07-20 02:43:20 -050099void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500100png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500101{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500102 /* write the data, and run the CRC over it */
103 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500104 {
105 png_calculate_crc(png_ptr, data, length);
Guy Schalnat6d764711995-12-19 03:22:19 -0600106 png_write_data(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500107 }
108}
109
Andreas Dilger47a0c421997-05-16 02:46:07 -0500110/* Finish a chunk started with png_write_chunk_start(). */
Guy Schalnat0d580581995-07-20 02:43:20 -0500111void
Guy Schalnat6d764711995-12-19 03:22:19 -0600112png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500113{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500114 png_byte buf[4];
115
Guy Schalnat0d580581995-07-20 02:43:20 -0500116 /* write the crc */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500117 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500118
119 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500120}
121
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600122/* Simple function to write the signature. If we have already written
123 * the magic bytes of the signature, or more likely, the PNG stream is
124 * being embedded into another stream and doesn't need its own signature,
125 * we should call png_set_sig_bytes() to tell libpng how many of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600126 * bytes have already been written.
127 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500128void
Guy Schalnat6d764711995-12-19 03:22:19 -0600129png_write_sig(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500130{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600131 /* write the rest of the 8 byte signature */
132 png_write_data(png_ptr, &png_sig[png_ptr->sig_bytes],
Andreas Dilger47a0c421997-05-16 02:46:07 -0500133 (png_size_t)8 - png_ptr->sig_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500134}
135
136/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600137 * information. Note that the rest of this code depends upon this
138 * information being correct.
139 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500140void
Guy Schalnat6d764711995-12-19 03:22:19 -0600141png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500142 int bit_depth, int color_type, int compression_type, int filter_type,
143 int interlace_type)
144{
145 png_byte buf[13]; /* buffer to store the IHDR info */
146
Andreas Dilger47a0c421997-05-16 02:46:07 -0500147 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500148 /* Check that we have valid input data from the application info */
149 switch (color_type)
150 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500151 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500152 switch (bit_depth)
153 {
154 case 1:
155 case 2:
156 case 4:
157 case 8:
158 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500159 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500160 }
161 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500162 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500163 if (bit_depth != 8 && bit_depth != 16)
164 png_error(png_ptr, "Invalid bit depth for RGB image");
165 png_ptr->channels = 3;
166 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500167 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500168 switch (bit_depth)
169 {
170 case 1:
171 case 2:
172 case 4:
173 case 8: png_ptr->channels = 1; break;
174 default: png_error(png_ptr, "Invalid bit depth for paletted image");
175 }
176 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500177 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500178 if (bit_depth != 8 && bit_depth != 16)
179 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
180 png_ptr->channels = 2;
181 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500182 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500183 if (bit_depth != 8 && bit_depth != 16)
184 png_error(png_ptr, "Invalid bit depth for RGBA image");
185 png_ptr->channels = 4;
186 break;
187 default:
188 png_error(png_ptr, "Invalid image color type specified");
189 }
190
Andreas Dilger47a0c421997-05-16 02:46:07 -0500191 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500192 {
193 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500194 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500195 }
196
Andreas Dilger47a0c421997-05-16 02:46:07 -0500197 if (filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500198 {
199 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500200 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500201 }
202
Andreas Dilger47a0c421997-05-16 02:46:07 -0500203 if (interlace_type != PNG_INTERLACE_NONE &&
204 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500205 {
206 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500207 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500208 }
209
210 /* save off the relevent information */
211 png_ptr->bit_depth = (png_byte)bit_depth;
212 png_ptr->color_type = (png_byte)color_type;
213 png_ptr->interlaced = (png_byte)interlace_type;
214 png_ptr->width = width;
215 png_ptr->height = height;
216
217 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500218 png_ptr->rowbytes = ((width * (png_size_t)png_ptr->pixel_depth + 7) >> 3);
Guy Schalnate5a37791996-06-05 15:50:50 -0500219 /* set the usr info, so any transformations can modify it */
220 png_ptr->usr_width = png_ptr->width;
221 png_ptr->usr_bit_depth = png_ptr->bit_depth;
222 png_ptr->usr_channels = png_ptr->channels;
223
Guy Schalnat0d580581995-07-20 02:43:20 -0500224 /* pack the header information into the buffer */
225 png_save_uint_32(buf, width);
226 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600227 buf[8] = (png_byte)bit_depth;
228 buf[9] = (png_byte)color_type;
229 buf[10] = (png_byte)compression_type;
230 buf[11] = (png_byte)filter_type;
231 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500232
233 /* write the chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500234 png_write_chunk(png_ptr, png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500235
Andreas Dilger47a0c421997-05-16 02:46:07 -0500236 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600237 png_ptr->zstream.zalloc = png_zalloc;
238 png_ptr->zstream.zfree = png_zfree;
239 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500240 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500241 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500242 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
243 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500244 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500245 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500246 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500247 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500248 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500249 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500250 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500251 png_ptr->zlib_strategy = Z_FILTERED;
252 else
253 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
254 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500255 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500256 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500257 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500258 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500259 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500260 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500261 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500262 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600263 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500264 png_ptr->zlib_method, png_ptr->zlib_window_bits,
265 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600266 png_ptr->zstream.next_out = png_ptr->zbuf;
267 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500268
Guy Schalnate5a37791996-06-05 15:50:50 -0500269 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500270}
271
272/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600273 * correct order for PNG, so people can redefine it to any convient
274 * structure.
275 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500276void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500277png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500278{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500279 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600280 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500281 png_byte buf[3];
282
Andreas Dilger47a0c421997-05-16 02:46:07 -0500283 png_debug(1, "in png_write_PLTE\n");
284 if (num_pal == 0 || num_pal > 256)
Guy Schalnate5a37791996-06-05 15:50:50 -0500285 {
286 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
287 {
288 png_error(png_ptr, "Invalid number of colors in palette");
289 }
290 else
291 {
292 png_warning(png_ptr, "Invalid number of colors in palette");
293 return;
294 }
295 }
296
Andreas Dilger47a0c421997-05-16 02:46:07 -0500297 png_ptr->num_palette = (png_uint_16)num_pal;
298 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500299
Andreas Dilger47a0c421997-05-16 02:46:07 -0500300 png_write_chunk_start(png_ptr, png_PLTE, num_pal * 3);
301 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500302 {
303 buf[0] = pal_ptr->red;
304 buf[1] = pal_ptr->green;
305 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500306 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500307 }
308 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500309 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500310}
311
312/* write an IDAT chunk */
313void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500314png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500315{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500316 png_debug(1, "in png_write_IDAT\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500317 png_write_chunk(png_ptr, png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500318 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500319}
320
321/* write an IEND chunk */
322void
Guy Schalnat6d764711995-12-19 03:22:19 -0600323png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500324{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500325 png_debug(1, "in png_write_IEND\n");
326 png_write_chunk(png_ptr, png_IEND, NULL, (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600327 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500328}
329
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500330#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500331/* write a gAMA chunk */
332void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500333png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500334{
335 png_uint_32 igamma;
336 png_byte buf[4];
337
Andreas Dilger47a0c421997-05-16 02:46:07 -0500338 png_debug(1, "in png_write_gAMA\n");
339 /* file_gamma is saved in 1/100,000ths */
340 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500341 png_save_uint_32(buf, igamma);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500342 png_write_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500343}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500344#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500345
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600346#if defined(PNG_WRITE_sRGB_SUPPORTED)
347/* write a sRGB chunk */
348void
349png_write_sRGB(png_structp png_ptr, png_byte srgb_intent)
350{
351 png_byte buf[1];
352
353 png_debug(1, "in png_write_sRGB\n");
354 if(srgb_intent > 3)
355 png_warning(png_ptr,
356 "Invalid sRGB rendering intent specified");
357 buf[0]=srgb_intent;
358 png_write_chunk(png_ptr, png_sRGB, buf, (png_size_t)1);
359}
360#endif
361
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500362#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500363/* write the sBIT chunk */
364void
Guy Schalnat6d764711995-12-19 03:22:19 -0600365png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500366{
367 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500368 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500369
Andreas Dilger47a0c421997-05-16 02:46:07 -0500370 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600371 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500372 if (color_type & PNG_COLOR_MASK_COLOR)
373 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500374 int maxbits;
375
376 maxbits = color_type==PNG_COLOR_TYPE_PALETTE ? 8:png_ptr->usr_bit_depth;
377 if (sbit->red == 0 || sbit->red > maxbits ||
378 sbit->green == 0 || sbit->green > maxbits ||
379 sbit->blue == 0 || sbit->blue > maxbits)
380 {
381 png_warning(png_ptr, "Invalid sBIT depth specified");
382 return;
383 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500384 buf[0] = sbit->red;
385 buf[1] = sbit->green;
386 buf[2] = sbit->blue;
387 size = 3;
388 }
389 else
390 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500391 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
392 {
393 png_warning(png_ptr, "Invalid sBIT depth specified");
394 return;
395 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500396 buf[0] = sbit->gray;
397 size = 1;
398 }
399
400 if (color_type & PNG_COLOR_MASK_ALPHA)
401 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500402 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
403 {
404 png_warning(png_ptr, "Invalid sBIT depth specified");
405 return;
406 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500407 buf[size++] = sbit->alpha;
408 }
409
Andreas Dilger47a0c421997-05-16 02:46:07 -0500410 png_write_chunk(png_ptr, png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500411}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500412#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500413
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500414#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500415/* write the cHRM chunk */
416void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500417png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600418 double red_x, double red_y, double green_x, double green_y,
419 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500420{
421 png_uint_32 itemp;
422 png_byte buf[32];
423
Andreas Dilger47a0c421997-05-16 02:46:07 -0500424 png_debug(1, "in png_write_cHRM\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500425 /* each value is saved int 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500426 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
427 white_x + white_y > 1.0)
428 {
429 png_warning(png_ptr, "Invalid cHRM white point specified");
430 return;
431 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500432 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
433 png_save_uint_32(buf, itemp);
434 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
435 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500436
437 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
438 red_x + red_y > 1.0)
439 {
440 png_warning(png_ptr, "Invalid cHRM red point specified");
441 return;
442 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500443 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
444 png_save_uint_32(buf + 8, itemp);
445 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
446 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500447
448 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
449 green_x + green_y > 1.0)
450 {
451 png_warning(png_ptr, "Invalid cHRM green point specified");
452 return;
453 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500454 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
455 png_save_uint_32(buf + 16, itemp);
456 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
457 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500458
459 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
460 blue_x + blue_y > 1.0)
461 {
462 png_warning(png_ptr, "Invalid cHRM blue point specified");
463 return;
464 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500465 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
466 png_save_uint_32(buf + 24, itemp);
467 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
468 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500469
Andreas Dilger47a0c421997-05-16 02:46:07 -0500470 png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500471}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500472#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500473
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500474#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500475/* write the tRNS chunk */
476void
Guy Schalnat6d764711995-12-19 03:22:19 -0600477png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -0500478 int num_trans, int color_type)
479{
480 png_byte buf[6];
481
Andreas Dilger47a0c421997-05-16 02:46:07 -0500482 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500483 if (color_type == PNG_COLOR_TYPE_PALETTE)
484 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500485 if (num_trans <= 0 || num_trans > png_ptr->num_palette)
486 {
487 png_warning(png_ptr,"Invalid number of transparent colors specified");
488 return;
489 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500490 /* write the chunk out as it is */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500491 png_write_chunk(png_ptr, png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -0500492 }
493 else if (color_type == PNG_COLOR_TYPE_GRAY)
494 {
495 /* one 16 bit value */
496 png_save_uint_16(buf, tran->gray);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500497 png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500498 }
499 else if (color_type == PNG_COLOR_TYPE_RGB)
500 {
501 /* three 16 bit values */
502 png_save_uint_16(buf, tran->red);
503 png_save_uint_16(buf + 2, tran->green);
504 png_save_uint_16(buf + 4, tran->blue);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500505 png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500506 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500507 else
508 {
509 png_warning(png_ptr, "Can't write tRNS with and alpha channel");
510 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500511}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500512#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500513
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500514#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500515/* write the background chunk */
516void
Guy Schalnat6d764711995-12-19 03:22:19 -0600517png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500518{
519 png_byte buf[6];
520
Andreas Dilger47a0c421997-05-16 02:46:07 -0500521 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500522 if (color_type == PNG_COLOR_TYPE_PALETTE)
523 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500524 if (back->index > png_ptr->num_palette)
525 {
526 png_warning(png_ptr, "Invalid background palette index");
527 return;
528 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500529 buf[0] = back->index;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500530 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500531 }
532 else if (color_type & PNG_COLOR_MASK_COLOR)
533 {
534 png_save_uint_16(buf, back->red);
535 png_save_uint_16(buf + 2, back->green);
536 png_save_uint_16(buf + 4, back->blue);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500537 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500538 }
539 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600540 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500541 png_save_uint_16(buf, back->gray);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500542 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500543 }
544}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500545#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500546
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500547#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500548/* write the histogram */
549void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500550png_write_hIST(png_structp png_ptr, png_uint_16p hist, png_uint_32 num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -0500551{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500552 png_uint_32 i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500553 png_byte buf[3];
554
Andreas Dilger47a0c421997-05-16 02:46:07 -0500555 png_debug(1, "in png_write_hIST\n");
556 if (num_hist > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500557 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500558 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
559 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500560 png_warning(png_ptr, "Invalid number of histogram entries specified");
561 return;
562 }
563
Andreas Dilger47a0c421997-05-16 02:46:07 -0500564 png_write_chunk_start(png_ptr, png_hIST, num_hist * 2);
565 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500566 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600567 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500568 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500569 }
570 png_write_chunk_end(png_ptr);
571}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500572#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500573
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600574#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
575/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
576 * and if invalid, correct the keyword rather than discarding the entire
577 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
578 * length, forbids leading or trailing whitespace, multiple internal spaces,
579 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -0500580 *
581 * The new_key is allocated to hold the corrected keyword and must be freed
582 * by the calling routine. This avoids problems with trying to write to
583 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600584 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500585png_size_t
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600586png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600587{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500588 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600589 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600590 int kflag;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600591
Andreas Dilger47a0c421997-05-16 02:46:07 -0500592 png_debug(1, "in png_check_keyword\n");
593 *new_key = NULL;
594
595 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600596 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600597 png_chunk_warning(png_ptr, "zero length keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600598 return 0;
599 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600600
Andreas Dilger47a0c421997-05-16 02:46:07 -0500601 png_debug1(2, "Keyword to be checked is '%s'\n", key);
602
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600603 *new_key = (png_charp)png_malloc(png_ptr, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600604
605 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500606 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600607 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600608 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500609 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600610#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500611 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600612
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600613 sprintf(msg, "invalid keyword character 0x%02X", *kp);
614 png_chunk_warning(png_ptr, msg);
615#else
616 png_chunk_warning(png_ptr, "invalid character in keyword");
617#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500618 *dp = ' ';
619 }
620 else
621 {
622 *dp = *kp;
623 }
624 }
625 *dp = '\0';
626
627 /* Remove any trailing white space. */
628 kp = *new_key + key_len - 1;
629 if (*kp == ' ')
630 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600631 png_chunk_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500632
633 while (*kp == ' ')
634 {
635 *(kp--) = '\0';
636 key_len--;
637 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600638 }
639
640 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500641 kp = *new_key;
642 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600643 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600644 png_chunk_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500645
646 while (*kp == ' ')
647 {
648 kp++;
649 key_len--;
650 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600651 }
652
Andreas Dilger47a0c421997-05-16 02:46:07 -0500653 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600654
Andreas Dilger47a0c421997-05-16 02:46:07 -0500655 /* Remove multiple internal spaces. */
656 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600657 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500658 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600659 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500660 *(dp++) = *kp;
661 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600662 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500663 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600664 {
665 key_len--;
666 }
667 else
668 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500669 *(dp++) = *kp;
670 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600671 }
672 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500673 *dp = '\0';
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600674
675 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500676 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600677 png_chunk_warning(png_ptr, "zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500678 }
679
680 if (key_len > 79)
681 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600682 png_chunk_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500683 new_key[79] = '\0';
684 key_len = 79;
685 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600686
687 return key_len;
688}
689#endif
690
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500691#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500692/* write a tEXt chunk */
693void
Guy Schalnat6d764711995-12-19 03:22:19 -0600694png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500695 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -0500696{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500697 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600698 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -0500699
Andreas Dilger47a0c421997-05-16 02:46:07 -0500700 png_debug(1, "in png_write_tEXt\n");
701 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
702 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600703 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -0500704 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500705 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500706
Andreas Dilger47a0c421997-05-16 02:46:07 -0500707 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600708 text_len = 0;
709
710 /* make sure we include the 0 after the key */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500711 png_write_chunk_start(png_ptr, png_tEXt, (png_uint_32)key_len+text_len+1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600712 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600713 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500714 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600715
Guy Schalnat0d580581995-07-20 02:43:20 -0500716 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500717 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -0500718}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500719#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500720
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500721#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500722/* write a compressed text chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -0500723void
Guy Schalnat6d764711995-12-19 03:22:19 -0600724png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500725 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -0500726{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500727 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -0500728 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600729 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -0500730 int i, ret;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600731 png_charpp output_ptr = NULL; /* array of pointers to output */
Guy Schalnat0d580581995-07-20 02:43:20 -0500732 int num_output_ptr = 0; /* number of output pointers used */
733 int max_output_ptr = 0; /* size of output_ptr */
734
Andreas Dilger47a0c421997-05-16 02:46:07 -0500735 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600736
Andreas Dilger47a0c421997-05-16 02:46:07 -0500737 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -0500738 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600739 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500740 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500741 }
742
Andreas Dilger47a0c421997-05-16 02:46:07 -0500743 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
744 {
745 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
746 png_free(png_ptr, new_key);
747 return;
748 }
749
750 png_free(png_ptr, new_key);
751
752 if (compression >= PNG_TEXT_COMPRESSION_LAST)
753 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600754#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500755 char msg[50];
756 sprintf(msg, "Unknown zTXt compression type %d", compression);
757 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600758#else
759 png_warning(png_ptr, "Unknown zTXt compression type");
760#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500761 compression = PNG_TEXT_COMPRESSION_zTXt;
762 }
763
764 /* We can't write the chunk until we find out how much data we have,
765 * which means we need to run the compressor first, and save the
766 * output. This shouldn't be a problem, as the vast majority of
767 * comments should be reasonable, but we will set up an array of
768 * malloc'd pointers to be sure.
769 *
770 * If we knew the application was well behaved, we could simplify this
771 * greatly by assuming we can always malloc an output buffer large
772 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
773 * and malloc this directly. The only time this would be a bad idea is
774 * if we can't malloc more than 64K and we have 64K of random input
775 * data, or if the input string is incredibly large (although this
776 * wouldn't cause a failure, just a slowdown due to swapping).
777 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500778
779 /* set up the compression buffers */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600780 png_ptr->zstream.avail_in = (uInt)text_len;
781 png_ptr->zstream.next_in = (Bytef *)text;
782 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
783 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500784
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600785 /* this is the same compression loop as in png_write_row() */
Guy Schalnat0d580581995-07-20 02:43:20 -0500786 do
787 {
788 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600789 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnat0d580581995-07-20 02:43:20 -0500790 if (ret != Z_OK)
791 {
792 /* error */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500793 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600794 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -0500795 else
Guy Schalnat6d764711995-12-19 03:22:19 -0600796 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -0500797 }
798 /* check to see if we need more room */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600799 if (!png_ptr->zstream.avail_out && png_ptr->zstream.avail_in)
Guy Schalnat0d580581995-07-20 02:43:20 -0500800 {
801 /* make sure the output array has room */
802 if (num_output_ptr >= max_output_ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600803 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500804 int old_max;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500805
806 old_max = max_output_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500807 max_output_ptr = num_output_ptr + 4;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500808 if (output_ptr != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600809 {
810 png_charpp old_ptr;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600811
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600812 old_ptr = output_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600813 output_ptr = (png_charpp)png_malloc(png_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600814 max_output_ptr * sizeof (png_charpp));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500815 png_memcpy(output_ptr, old_ptr, old_max * sizeof (png_charp));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600816 png_free(png_ptr, old_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600817 }
818 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600819 output_ptr = (png_charpp)png_malloc(png_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600820 max_output_ptr * sizeof (png_charp));
Guy Schalnat0d580581995-07-20 02:43:20 -0500821 }
822
823 /* save the data */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500824 output_ptr[num_output_ptr] = png_malloc(png_ptr, png_ptr->zbuf_size);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500825 png_memcpy(output_ptr[num_output_ptr], png_ptr->zbuf,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500826 png_ptr->zbuf_size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500827 num_output_ptr++;
828
829 /* and reset the buffer */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600830 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
831 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500832 }
833 /* continue until we don't have anymore to compress */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600834 } while (png_ptr->zstream.avail_in);
Guy Schalnat0d580581995-07-20 02:43:20 -0500835
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600836 /* finish the compression */
Guy Schalnat0d580581995-07-20 02:43:20 -0500837 do
838 {
839 /* tell zlib we are finished */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600840 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -0500841 if (ret != Z_OK && ret != Z_STREAM_END)
842 {
843 /* we got an error */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500844 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600845 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -0500846 else
Guy Schalnat6d764711995-12-19 03:22:19 -0600847 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -0500848 }
849
850 /* check to see if we need more room */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500851 if (!(png_ptr->zstream.avail_out) && ret == Z_OK)
Guy Schalnat0d580581995-07-20 02:43:20 -0500852 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600853 /* check to make sure our output array has room */
854 if (num_output_ptr >= max_output_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500855 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500856 int old_max;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500857
858 old_max = max_output_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500859 max_output_ptr = num_output_ptr + 4;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500860 if (output_ptr != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600861 {
862 png_charpp old_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500863
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600864 old_ptr = output_ptr;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500865 /* This could be optimized to realloc() */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600866 output_ptr = (png_charpp)png_malloc(png_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600867 max_output_ptr * sizeof (png_charpp));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500868 png_memcpy(output_ptr, old_ptr, old_max * sizeof (png_charp));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600869 png_free(png_ptr, old_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600870 }
871 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600872 output_ptr = (png_charpp)png_malloc(png_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600873 max_output_ptr * sizeof (png_charp));
874 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600875
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600876 /* save off the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600877 output_ptr[num_output_ptr] = png_malloc(png_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600878 png_ptr->zbuf_size);
879 png_memcpy(output_ptr[num_output_ptr], png_ptr->zbuf,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500880 png_ptr->zbuf_size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500881 num_output_ptr++;
882
883 /* and reset the buffer pointers */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600884 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
885 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500886 }
887 } while (ret != Z_STREAM_END);
888
889 /* text length is number of buffers plus last buffer */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600890 text_len = png_ptr->zbuf_size * num_output_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600891 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500892 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
Guy Schalnat0d580581995-07-20 02:43:20 -0500893
894 /* write start of chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500895 png_write_chunk_start(png_ptr, png_zTXt, (png_uint_32)(key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -0500896 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500897 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600898 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -0500899 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500900 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500901
902 /* write saved output buffers, if any */
903 for (i = 0; i < num_output_ptr; i++)
904 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500905 png_write_chunk_data(png_ptr,(png_bytep)output_ptr[i],png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600906 png_free(png_ptr, output_ptr[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500907 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500908 if (max_output_ptr != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600909 png_free(png_ptr, output_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500910 /* write anything left in zbuf */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500911 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -0500912 png_write_chunk_data(png_ptr, png_ptr->zbuf,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600913 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -0500914 /* close the chunk */
915 png_write_chunk_end(png_ptr);
916
917 /* reset zlib for another zTXt or the image data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600918 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -0500919}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500920#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500921
Andreas Dilger47a0c421997-05-16 02:46:07 -0500922
923#if defined(PNG_WRITE_oFFs_SUPPORTED)
924/* write the oFFs chunk */
925void
926png_write_oFFs(png_structp png_ptr, png_uint_32 x_offset,
927 png_uint_32 y_offset,
928 int unit_type)
929{
930 png_byte buf[9];
931
932 png_debug(1, "in png_write_oFFs\n");
933 if (unit_type >= PNG_OFFSET_LAST)
934 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
935
936 png_save_uint_32(buf, x_offset);
937 png_save_uint_32(buf + 4, y_offset);
938 buf[8] = (png_byte)unit_type;
939
940 png_write_chunk(png_ptr, png_oFFs, buf, (png_size_t)9);
941}
942#endif
943
944#if defined(PNG_WRITE_pCAL_SUPPORTED)
945/* write the pCAL chunk (png-scivis-19970203) */
946void
947png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
948 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
949{
950 png_size_t purpose_len, units_len, total_len;
951 png_uint_32p params_len;
952 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600953 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500954 int i;
955
956 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
957 if (type >= PNG_EQUATION_LAST)
958 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
959
960 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
961 png_debug1(3, "pCAL purpose length = %d\n", purpose_len);
962 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
963 png_debug1(3, "pCAL units length = %d\n", units_len);
964 total_len = purpose_len + units_len + 10;
965
966 params_len = (png_uint_32p)png_malloc(png_ptr, nparams*sizeof(png_uint_32));
967
968 /* Find the length of each parameter, making sure we don't count the
969 null terminator for the last parameter. */
970 for (i = 0; i < nparams; i++)
971 {
972 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
973 png_debug2(3, "pCAL parameter %d length = %d\n", i, params_len[i]);
974 total_len += (png_size_t)params_len[i];
975 }
976
977 png_debug1(3, "pCAL total length = %d\n", total_len);
978 png_write_chunk_start(png_ptr, png_pCAL, total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600979 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500980 png_save_int_32(buf, X0);
981 png_save_int_32(buf + 4, X1);
982 buf[8] = (png_byte)type;
983 buf[9] = (png_byte)nparams;
984 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
985 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
986
987 png_free(png_ptr, new_purpose);
988
989 for (i = 0; i < nparams; i++)
990 {
991 png_write_chunk_data(png_ptr, (png_bytep)params[i],
992 (png_size_t)params_len[i]);
993 }
994
995 png_write_chunk_end(png_ptr);
996}
997#endif
998
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500999#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001000/* write the pHYs chunk */
1001void
Guy Schalnat6d764711995-12-19 03:22:19 -06001002png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001003 png_uint_32 y_pixels_per_unit,
1004 int unit_type)
1005{
1006 png_byte buf[9];
1007
Andreas Dilger47a0c421997-05-16 02:46:07 -05001008 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001009 if (unit_type >= PNG_RESOLUTION_LAST)
1010 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1011
Guy Schalnat0d580581995-07-20 02:43:20 -05001012 png_save_uint_32(buf, x_pixels_per_unit);
1013 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001014 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001015
Andreas Dilger47a0c421997-05-16 02:46:07 -05001016 png_write_chunk(png_ptr, png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001017}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001018#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001019
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001020#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001021/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1022 * or png_convert_from_time_t(), or fill in the structure yourself.
1023 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001024void
Guy Schalnat6d764711995-12-19 03:22:19 -06001025png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001026{
1027 png_byte buf[7];
1028
Andreas Dilger47a0c421997-05-16 02:46:07 -05001029 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001030 if (mod_time->month > 12 || mod_time->month < 1 ||
1031 mod_time->day > 31 || mod_time->day < 1 ||
1032 mod_time->hour > 23 || mod_time->second > 60)
1033 {
1034 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1035 return;
1036 }
1037
Guy Schalnat0d580581995-07-20 02:43:20 -05001038 png_save_uint_16(buf, mod_time->year);
1039 buf[2] = mod_time->month;
1040 buf[3] = mod_time->day;
1041 buf[4] = mod_time->hour;
1042 buf[5] = mod_time->minute;
1043 buf[6] = mod_time->second;
1044
Andreas Dilger47a0c421997-05-16 02:46:07 -05001045 png_write_chunk(png_ptr, png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001046}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001047#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001048
1049/* initializes the row writing capability of libpng */
1050void
Guy Schalnat6d764711995-12-19 03:22:19 -06001051png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001052{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001053 png_size_t buf_size;
1054
1055 png_debug(1, "in png_write_start_row\n");
1056 buf_size = (png_size_t)(((png_ptr->width * png_ptr->usr_channels *
1057 png_ptr->usr_bit_depth + 7) >> 3) + 1);
1058
Guy Schalnat0d580581995-07-20 02:43:20 -05001059 /* set up row buffer */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001060 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, buf_size);
1061 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001062
1063 /* set up filtering buffer, if using this filter */
1064 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001065 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001066 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Guy Schalnate5a37791996-06-05 15:50:50 -05001067 png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001068 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001069 }
1070
Andreas Dilger47a0c421997-05-16 02:46:07 -05001071 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001072 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1073 {
1074 /* set up previous row buffer */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001075 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, buf_size);
1076 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001077
1078 if (png_ptr->do_filter & PNG_FILTER_UP)
1079 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001080 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Guy Schalnate5a37791996-06-05 15:50:50 -05001081 png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001082 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001083 }
1084
1085 if (png_ptr->do_filter & PNG_FILTER_AVG)
1086 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001087 png_ptr->avg_row = (png_bytep )png_malloc(png_ptr,
Guy Schalnate5a37791996-06-05 15:50:50 -05001088 png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001089 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001090 }
1091
1092 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1093 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001094 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Guy Schalnate5a37791996-06-05 15:50:50 -05001095 png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001096 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001097 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001098 }
1099
1100 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001101 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001102 {
1103 if (!(png_ptr->transformations & PNG_INTERLACE))
1104 {
1105 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1106 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001107 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1108 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001109 }
1110 else
1111 {
1112 png_ptr->num_rows = png_ptr->height;
1113 png_ptr->usr_width = png_ptr->width;
1114 }
1115 }
1116 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001117 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001118 png_ptr->num_rows = png_ptr->height;
1119 png_ptr->usr_width = png_ptr->width;
1120 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001121 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1122 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001123}
1124
Andreas Dilger47a0c421997-05-16 02:46:07 -05001125/* Internal use only. Called when finished processing a row of data. */
Guy Schalnat0d580581995-07-20 02:43:20 -05001126void
Guy Schalnat6d764711995-12-19 03:22:19 -06001127png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001128{
1129 int ret;
1130
Andreas Dilger47a0c421997-05-16 02:46:07 -05001131 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001132 /* next row */
1133 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001134
Guy Schalnat0d580581995-07-20 02:43:20 -05001135 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001136 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001137 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001138
1139 /* if interlaced, go to next pass */
1140 if (png_ptr->interlaced)
1141 {
1142 png_ptr->row_number = 0;
1143 if (png_ptr->transformations & PNG_INTERLACE)
1144 {
1145 png_ptr->pass++;
1146 }
1147 else
1148 {
1149 /* loop until we find a non-zero width or height pass */
1150 do
1151 {
1152 png_ptr->pass++;
1153 if (png_ptr->pass >= 7)
1154 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001155 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001156 png_pass_inc[png_ptr->pass] - 1 -
1157 png_pass_start[png_ptr->pass]) /
1158 png_pass_inc[png_ptr->pass];
1159 png_ptr->num_rows = (png_ptr->height +
1160 png_pass_yinc[png_ptr->pass] - 1 -
1161 png_pass_ystart[png_ptr->pass]) /
1162 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001163 if (png_ptr->transformations & PNG_INTERLACE)
1164 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001165 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1166
1167 }
1168
Guy Schalnate5a37791996-06-05 15:50:50 -05001169 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001170 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001171 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001172 if (png_ptr->prev_row != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001173 png_memset(png_ptr->prev_row, 0,
1174 (png_size_t) (((png_uint_32)png_ptr->usr_channels *
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001175 (png_uint_32)png_ptr->usr_bit_depth *
1176 png_ptr->width + 7) >> 3) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001177 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001178 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001179 }
1180
1181 /* if we get here, we've just written the last row, so we need
1182 to flush the compressor */
1183 do
1184 {
1185 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001186 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001187 /* check for an error */
1188 if (ret != Z_OK && ret != Z_STREAM_END)
1189 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001190 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001191 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001192 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001193 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001194 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001195 /* check to see if we need more room */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001196 if (!(png_ptr->zstream.avail_out) && ret == Z_OK)
Guy Schalnat0d580581995-07-20 02:43:20 -05001197 {
1198 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001199 png_ptr->zstream.next_out = png_ptr->zbuf;
1200 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat0d580581995-07-20 02:43:20 -05001201 }
1202 } while (ret != Z_STREAM_END);
1203
1204 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001205 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001206 {
1207 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001208 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001209 }
1210
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001211 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05001212}
1213
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001214#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001215/* Pick out the correct pixels for the interlace pass.
1216 * The basic idea here is to go through the row with a source
1217 * pointer and a destination pointer (sp and dp), and copy the
1218 * correct pixels for the pass. As the row gets compacted,
1219 * sp will always be >= dp, so we should never overwrite anything.
1220 * See the default: case for the easiest code to understand.
1221 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001222void
Guy Schalnat6d764711995-12-19 03:22:19 -06001223png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001224{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001225 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001226 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001227#if defined(PNG_USELESS_TESTS_SUPPORTED)
1228 if (row != NULL && row_info != NULL && pass < 6)
1229#else
1230 if (pass < 6)
1231#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001232 {
1233 /* each pixel depth is handled seperately */
1234 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001235 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001236 case 1:
1237 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001238 png_bytep sp;
1239 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001240 int shift;
1241 int d;
1242 int value;
1243 png_uint_32 i;
1244
1245 dp = row;
1246 d = 0;
1247 shift = 7;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001248 for (i = png_pass_start[pass]; i < row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001249 i += png_pass_inc[pass])
1250 {
1251 sp = row + (png_size_t)(i >> 3);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001252 value = (int)(*sp >> (7 - (int)(i & 7))) & 0x1;
Guy Schalnat0d580581995-07-20 02:43:20 -05001253 d |= (value << shift);
1254
1255 if (shift == 0)
1256 {
1257 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001258 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001259 d = 0;
1260 }
1261 else
1262 shift--;
1263
1264 }
1265 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001266 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001267 break;
1268 }
1269 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001270 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001271 png_bytep sp;
1272 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001273 int shift;
1274 int d;
1275 int value;
1276 png_uint_32 i;
1277
1278 dp = row;
1279 shift = 6;
1280 d = 0;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001281 for (i = png_pass_start[pass]; i < row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001282 i += png_pass_inc[pass])
1283 {
1284 sp = row + (png_size_t)(i >> 2);
1285 value = (*sp >> ((3 - (int)(i & 3)) << 1)) & 0x3;
1286 d |= (value << shift);
1287
1288 if (shift == 0)
1289 {
1290 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001291 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001292 d = 0;
1293 }
1294 else
1295 shift -= 2;
1296 }
1297 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001298 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001299 break;
1300 }
1301 case 4:
1302 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001303 png_bytep sp;
1304 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001305 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001306 int d;
1307 int value;
1308 png_uint_32 i;
1309
1310 dp = row;
1311 shift = 4;
1312 d = 0;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001313 for (i = png_pass_start[pass]; i < row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001314 i += png_pass_inc[pass])
1315 {
1316 sp = row + (png_size_t)(i >> 1);
1317 value = (*sp >> ((1 - (int)(i & 1)) << 2)) & 0xf;
1318 d |= (value << shift);
1319
1320 if (shift == 0)
1321 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001322 shift = 4;
1323 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001324 d = 0;
1325 }
1326 else
1327 shift -= 4;
1328 }
1329 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001330 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001331 break;
1332 }
1333 default:
1334 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001335 png_bytep sp;
1336 png_bytep dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001337 png_uint_32 i;
1338 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001339
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001340 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05001341 dp = row;
1342 /* find out how many bytes each pixel takes up */
1343 pixel_bytes = (row_info->pixel_depth >> 3);
1344 /* loop through the row, only looking at the pixels that
1345 matter */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001346 for (i = png_pass_start[pass]; i < row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001347 i += png_pass_inc[pass])
1348 {
1349 /* find out where the original pixel is */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001350 sp = row + i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001351 /* move the pixel */
1352 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001353 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05001354 /* next pixel */
1355 dp += pixel_bytes;
1356 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001357 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001358 }
1359 }
1360 /* set new row width */
1361 row_info->width = (row_info->width +
1362 png_pass_inc[pass] - 1 -
1363 png_pass_start[pass]) /
1364 png_pass_inc[pass];
1365 row_info->rowbytes = ((row_info->width *
1366 row_info->pixel_depth + 7) >> 3);
1367
1368 }
1369}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001370#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001371
Andreas Dilger47a0c421997-05-16 02:46:07 -05001372/* This filters the row, chooses which filter to use, if it has not already
1373 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001374 * chosen filter.
1375 */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001376#define PNG_MAXSUM (png_uint_32)(~0x0UL >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001377#define PNG_HISHIFT 10
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001378#define PNG_LOMASK (png_uint_32)0xffffL
1379#define PNG_HIMASK (png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT)
Guy Schalnat0d580581995-07-20 02:43:20 -05001380void
Guy Schalnate5a37791996-06-05 15:50:50 -05001381png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05001382{
Guy Schalnate5a37791996-06-05 15:50:50 -05001383 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001384 png_uint_32 mins, bpp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001385
Andreas Dilger47a0c421997-05-16 02:46:07 -05001386 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001387 /* find out how many bytes offset each pixel is */
1388 bpp = (row_info->pixel_depth + 7) / 8;
Guy Schalnate5a37791996-06-05 15:50:50 -05001389
1390 prev_row = png_ptr->prev_row;
1391 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001392 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05001393
Andreas Dilger47a0c421997-05-16 02:46:07 -05001394 /* The prediction method we use is to find which method provides the
1395 * smallest value when summing the absolute values of the distances
1396 * from zero using anything >= 128 as negative numbers. This is known
1397 * as the "minimum sum of absolute differences" heuristic. Other
1398 * heruistics are the "weighted minumum sum of absolute differences"
1399 * (experimental and can in theory improve compression), and the "zlib
1400 * predictive" method (not implemented in libpng 0.95), which does test
1401 * compressions of lines using different filter methods, and then chooses
1402 * the (series of) filter(s) which give minimum compressed data size (VERY
1403 * computationally expensive).
1404 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001405
Guy Schalnate5a37791996-06-05 15:50:50 -05001406 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05001407 * that has been chosen, as it doesn't actually do anything to the data.
1408 */
Guy Schalnate5a37791996-06-05 15:50:50 -05001409 if (png_ptr->do_filter & PNG_FILTER_NONE &&
1410 png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05001411 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001412 png_bytep rp;
1413 png_uint_32 sum = 0;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001414 png_size_t i;
1415 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05001416
Guy Schalnate5a37791996-06-05 15:50:50 -05001417 for (i = 0, rp = row_buf + 1; i < row_info->rowbytes; i++, rp++)
1418 {
1419 v = *rp;
1420 sum += (v < 128) ? v : 256 - v;
1421 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001422
1423#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1424 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1425 {
1426 png_uint_32 sumhi, sumlo;
1427 sumlo = sum & PNG_LOMASK;
1428 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
1429
1430 /* Reduce the sum if we match any of the previous rows */
1431 for (i = 0; i < png_ptr->num_prev_filters; i++)
1432 {
1433 if (png_ptr->prev_filters[i] == PNG_FILTER_NONE)
1434 {
1435 sumlo = (sumlo * png_ptr->filter_weights[i]) >>
1436 PNG_WEIGHT_SHIFT;
1437 sumhi = (sumhi * png_ptr->filter_weights[i]) >>
1438 PNG_WEIGHT_SHIFT;
1439 }
1440 }
1441
1442 /* Factor in the cost of this filter (this is here for completeness,
1443 * but it makes no sense to have a "cost" for the NONE filter, as
1444 * it has the minimum possible computational cost - none).
1445 */
1446 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
1447 PNG_COST_SHIFT;
1448 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
1449 PNG_COST_SHIFT;
1450
1451 if (sumhi > PNG_HIMASK)
1452 sum = PNG_MAXSUM;
1453 else
1454 sum = (sumhi << PNG_HISHIFT) + sumlo;
1455 }
1456#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001457 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05001458 }
1459
Guy Schalnate5a37791996-06-05 15:50:50 -05001460 /* sub filter */
1461 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001462 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001463 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001464 png_uint_32 sum = 0, lmins = mins;
1465 png_size_t i;
1466 int v;
1467
1468#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1469 /* We temporarily increase the "minumum sum" by the factor we
1470 * would reduce the sum of this filter, so that we can do the
1471 * early exit comparison without scaling the sum each time.
1472 */
1473 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1474 {
1475 png_uint_32 lmhi, lmlo;
1476 lmlo = lmins & PNG_LOMASK;
1477 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1478
1479 for (i = 0; i < png_ptr->num_prev_filters; i++)
1480 {
1481 if (png_ptr->prev_filters[i] == PNG_FILTER_VALUE_SUB)
1482 {
1483 lmlo = (lmlo * png_ptr->inv_filter_weights[i]) >>
1484 PNG_WEIGHT_SHIFT;
1485 lmhi = (lmhi * png_ptr->inv_filter_weights[i]) >>
1486 PNG_WEIGHT_SHIFT;
1487 }
1488 }
1489
1490 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1491 PNG_COST_SHIFT;
1492 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1493 PNG_COST_SHIFT;
1494
1495 if (lmhi > PNG_HIMASK)
1496 lmins = PNG_MAXSUM;
1497 else
1498 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1499 }
1500#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001501
Guy Schalnate5a37791996-06-05 15:50:50 -05001502 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
1503 i++, rp++, dp++)
1504 {
1505 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001506
Guy Schalnate5a37791996-06-05 15:50:50 -05001507 sum += (v < 128) ? v : 256 - v;
1508 }
1509 for (lp = row_buf + 1; i < row_info->rowbytes; i++, rp++, lp++, dp++)
1510 {
1511 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001512
Guy Schalnate5a37791996-06-05 15:50:50 -05001513 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001514
1515 if (sum > lmins) /* We are already worse, don't continue. */
1516 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001517 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001518
1519#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1520 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1521 {
1522 png_uint_32 sumhi, sumlo;
1523 sumlo = sum & PNG_LOMASK;
1524 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1525
1526 for (i = 0; i < png_ptr->num_prev_filters; i++)
1527 {
1528 if (png_ptr->prev_filters[i] == PNG_FILTER_VALUE_SUB)
1529 {
1530 sumlo = (sumlo * png_ptr->inv_filter_weights[i]) >>
1531 PNG_WEIGHT_SHIFT;
1532 sumhi = (sumhi * png_ptr->inv_filter_weights[i]) >>
1533 PNG_WEIGHT_SHIFT;
1534 }
1535 }
1536
1537 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1538 PNG_COST_SHIFT;
1539 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1540 PNG_COST_SHIFT;
1541
1542 if (sumhi > PNG_HIMASK)
1543 sum = PNG_MAXSUM;
1544 else
1545 sum = (sumhi << PNG_HISHIFT) + sumlo;
1546 }
1547#endif
1548
Guy Schalnate5a37791996-06-05 15:50:50 -05001549 if (sum < mins)
1550 {
1551 mins = sum;
1552 best_row = png_ptr->sub_row;
1553 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001554 }
1555
Guy Schalnate5a37791996-06-05 15:50:50 -05001556 /* up filter */
1557 if (png_ptr->do_filter & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05001558 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001559 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001560 png_uint_32 sum = 0, lmins = mins;
1561 png_size_t i;
1562 int v;
1563
1564#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1565 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1566 {
1567 png_uint_32 lmhi, lmlo;
1568 lmlo = lmins & PNG_LOMASK;
1569 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1570
1571 for (i = 0; i < png_ptr->num_prev_filters; i++)
1572 {
1573 if (png_ptr->prev_filters[i] == PNG_FILTER_VALUE_UP)
1574 {
1575 lmlo = (lmlo * png_ptr->inv_filter_weights[i]) >>
1576 PNG_WEIGHT_SHIFT;
1577 lmhi = (lmhi * png_ptr->inv_filter_weights[i]) >>
1578 PNG_WEIGHT_SHIFT;
1579 }
1580 }
1581
1582 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
1583 PNG_COST_SHIFT;
1584 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
1585 PNG_COST_SHIFT;
1586
1587 if (lmhi > PNG_HIMASK)
1588 lmins = PNG_MAXSUM;
1589 else
1590 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1591 }
1592#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001593
1594 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
1595 pp = prev_row + 1; i < row_info->rowbytes; i++, rp++, pp++, dp++)
1596 {
1597 v = *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
1598
1599 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001600
1601 if (sum > lmins) /* We are already worse, don't continue. */
1602 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001603 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001604
1605#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1606 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1607 {
1608 png_uint_32 sumhi, sumlo;
1609 sumlo = sum & PNG_LOMASK;
1610 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1611
1612 for (i = 0; i < png_ptr->num_prev_filters; i++)
1613 {
1614 if (png_ptr->prev_filters[i] == PNG_FILTER_UP)
1615 {
1616 sumlo = (sumlo * png_ptr->filter_weights[i]) >>
1617 PNG_WEIGHT_SHIFT;
1618 sumhi = (sumhi * png_ptr->filter_weights[i]) >>
1619 PNG_WEIGHT_SHIFT;
1620 }
1621 }
1622
1623 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
1624 PNG_COST_SHIFT;
1625 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
1626 PNG_COST_SHIFT;
1627
1628 if (sumhi > PNG_HIMASK)
1629 sum = PNG_MAXSUM;
1630 else
1631 sum = (sumhi << PNG_HISHIFT) + sumlo;
1632 }
1633#endif
1634
Guy Schalnate5a37791996-06-05 15:50:50 -05001635 if (sum < mins)
1636 {
1637 mins = sum;
1638 best_row = png_ptr->up_row;
1639 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001640 }
1641
Guy Schalnate5a37791996-06-05 15:50:50 -05001642 /* avg filter */
1643 if (png_ptr->do_filter & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001644 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001645 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001646 png_uint_32 sum = 0, lmins = mins;
1647 png_size_t i;
1648 int v;
1649
1650#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1651 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1652 {
1653 png_uint_32 lmhi, lmlo;
1654 lmlo = lmins & PNG_LOMASK;
1655 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1656
1657 for (i = 0; i < png_ptr->num_prev_filters; i++)
1658 {
1659 if (png_ptr->prev_filters[i] == PNG_FILTER_VALUE_AVG)
1660 {
1661 lmlo = (lmlo * png_ptr->inv_filter_weights[i]) >>
1662 PNG_WEIGHT_SHIFT;
1663 lmhi = (lmhi * png_ptr->inv_filter_weights[i]) >>
1664 PNG_WEIGHT_SHIFT;
1665 }
1666 }
1667
1668 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
1669 PNG_COST_SHIFT;
1670 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
1671 PNG_COST_SHIFT;
1672
1673 if (lmhi > PNG_HIMASK)
1674 lmins = PNG_MAXSUM;
1675 else
1676 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1677 }
1678#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001679
1680 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
1681 pp = prev_row + 1; i < bpp; i++, rp++, pp++, dp++)
1682 {
1683 v = *dp = (png_byte)(((int)*rp - ((int)*pp / 2)) & 0xff);
1684
1685 sum += (v < 128) ? v : 256 - v;
1686 }
1687 for (lp = row_buf + 1; i < row_info->rowbytes;
1688 i++, rp++, pp++, lp++, dp++)
1689 {
1690 v = *dp = (png_byte)(((int)*rp - (((int)*pp + (int)*lp) / 2)) & 0xff);
1691
1692 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001693
1694 if (sum > lmins) /* We are already worse, don't continue. */
1695 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001696 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001697
1698#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1699 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1700 {
1701 png_uint_32 sumhi, sumlo;
1702 sumlo = sum & PNG_LOMASK;
1703 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1704
1705 for (i = 0; i < png_ptr->num_prev_filters; i++)
1706 {
1707 if (png_ptr->prev_filters[i] == PNG_FILTER_NONE)
1708 {
1709 sumlo = (sumlo * png_ptr->filter_weights[i]) >>
1710 PNG_WEIGHT_SHIFT;
1711 sumhi = (sumhi * png_ptr->filter_weights[i]) >>
1712 PNG_WEIGHT_SHIFT;
1713 }
1714 }
1715
1716 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
1717 PNG_COST_SHIFT;
1718 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
1719 PNG_COST_SHIFT;
1720
1721 if (sumhi > PNG_HIMASK)
1722 sum = PNG_MAXSUM;
1723 else
1724 sum = (sumhi << PNG_HISHIFT) + sumlo;
1725 }
1726#endif
1727
Guy Schalnate5a37791996-06-05 15:50:50 -05001728 if (sum < mins)
1729 {
1730 mins = sum;
1731 best_row = png_ptr->avg_row;
1732 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001733 }
1734
Andreas Dilger47a0c421997-05-16 02:46:07 -05001735 /* Paeth filter */
Guy Schalnate5a37791996-06-05 15:50:50 -05001736 if (png_ptr->do_filter & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001737 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001738 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001739 png_uint_32 sum = 0, lmins = mins;
1740 png_size_t i;
1741 int v;
1742
1743#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1744 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1745 {
1746 png_uint_32 lmhi, lmlo;
1747 lmlo = lmins & PNG_LOMASK;
1748 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1749
1750 for (i = 0; i < png_ptr->num_prev_filters; i++)
1751 {
1752 if (png_ptr->prev_filters[i] == PNG_FILTER_VALUE_PAETH)
1753 {
1754 lmlo = (lmlo * png_ptr->inv_filter_weights[i]) >>
1755 PNG_WEIGHT_SHIFT;
1756 lmhi = (lmhi * png_ptr->inv_filter_weights[i]) >>
1757 PNG_WEIGHT_SHIFT;
1758 }
1759 }
1760
1761 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1762 PNG_COST_SHIFT;
1763 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1764 PNG_COST_SHIFT;
1765
1766 if (lmhi > PNG_HIMASK)
1767 lmins = PNG_MAXSUM;
1768 else
1769 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1770 }
1771#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001772
1773 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
1774 pp = prev_row + 1; i < bpp; i++, rp++, pp++, dp++)
1775 {
1776 v = *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
1777
1778 sum += (v < 128) ? v : 256 - v;
1779 }
1780 for (lp = row_buf + 1, cp = prev_row + 1; i < row_info->rowbytes;
1781 i++, rp++, pp++, lp++, dp++, cp++)
1782 {
1783 int a, b, c, pa, pb, pc, p;
1784
1785 b = *pp;
1786 c = *cp;
1787 a = *lp;
1788
1789 p = a + b - c;
1790 pa = abs(p - a);
1791 pb = abs(p - b);
1792 pc = abs(p - c);
1793
1794 if (pa <= pb && pa <= pc)
1795 p = a;
1796 else if (pb <= pc)
1797 p = b;
1798 else
1799 p = c;
1800
1801 v = *dp = (png_byte)(((int)*rp - p) & 0xff);
1802
1803 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001804
1805 if (sum > lmins) /* We are already worse, don't continue. */
1806 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001807 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001808
1809#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1810 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1811 {
1812 png_uint_32 sumhi, sumlo;
1813 sumlo = sum & PNG_LOMASK;
1814 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1815
1816 for (i = 0; i < png_ptr->num_prev_filters; i++)
1817 {
1818 if (png_ptr->prev_filters[i] == PNG_FILTER_PAETH)
1819 {
1820 sumlo = (sumlo * png_ptr->filter_weights[i]) >>
1821 PNG_WEIGHT_SHIFT;
1822 sumhi = (sumhi * png_ptr->filter_weights[i]) >>
1823 PNG_WEIGHT_SHIFT;
1824 }
1825 }
1826
1827 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1828 PNG_COST_SHIFT;
1829 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1830 PNG_COST_SHIFT;
1831
1832 if (sumhi > PNG_HIMASK)
1833 sum = PNG_MAXSUM;
1834 else
1835 sum = (sumhi << PNG_HISHIFT) + sumlo;
1836 }
1837#endif
1838
Guy Schalnate5a37791996-06-05 15:50:50 -05001839 if (sum < mins)
1840 {
1841 best_row = png_ptr->paeth_row;
1842 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001843 }
1844
Andreas Dilger47a0c421997-05-16 02:46:07 -05001845 /* Do the actual writing of the filtered row data from the chosen filter. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001846 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001847
1848#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1849 /* Save the type of filter we picked this time for future calculations */
1850 if (png_ptr->num_prev_filters > 0)
1851 {
1852 png_byte i;
1853
1854 for (i = 1; i < png_ptr->num_prev_filters; i++)
1855 {
1856 png_ptr->prev_filters[i] = png_ptr->prev_filters[i - 1];
1857 }
1858 png_ptr->prev_filters[i] = best_row[0];
1859 }
1860#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001861}
1862
1863
Andreas Dilger47a0c421997-05-16 02:46:07 -05001864/* Do the actual writing of a previously filtered row. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001865void
1866png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
1867{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001868 png_debug(1, "in png_write_filtered_row\n");
1869 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05001870 /* set up the zlib input buffer */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001871 png_ptr->zstream.next_in = filtered_row;
1872 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05001873 /* repeat until we have compressed all the data */
1874 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001875 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001876 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05001877
Guy Schalnate5a37791996-06-05 15:50:50 -05001878 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001879 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05001880 /* check for compression errors */
1881 if (ret != Z_OK)
1882 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001883 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001884 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05001885 else
1886 png_error(png_ptr, "zlib error");
1887 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001888
Guy Schalnate5a37791996-06-05 15:50:50 -05001889 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001890 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05001891 {
1892 /* write the IDAT and reset the zlib output buffer */
1893 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001894 png_ptr->zstream.next_out = png_ptr->zbuf;
1895 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05001896 }
1897 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001898 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05001899
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001900 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001901 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001902 {
1903 png_bytep tptr;
1904
1905 tptr = png_ptr->prev_row;
1906 png_ptr->prev_row = png_ptr->row_buf;
1907 png_ptr->row_buf = tptr;
1908 }
1909
Guy Schalnate5a37791996-06-05 15:50:50 -05001910 /* finish row - updates counters and flushes zlib if last row */
1911 png_write_finish_row(png_ptr);
1912
1913#if defined(PNG_WRITE_FLUSH_SUPPORTED)
1914 png_ptr->flush_rows++;
1915
1916 if (png_ptr->flush_dist > 0 &&
1917 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001918 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001919 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001920 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001921#endif /* PNG_WRITE_FLUSH_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05001922}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001923