blob: b4575e4c02042a80217b7d8ee96ef2c0180f897a [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 *
4 * libpng 1.00.97
5 * 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
8 * May 28, 1997
9 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050010
Guy Schalnat0d580581995-07-20 02:43:20 -050011#define PNG_INTERNAL
12#include "png.h"
13
Andreas Dilger47a0c421997-05-16 02:46:07 -050014/* Place a 32-bit number into a buffer in PNG byte order. We work
15 * with unsigned numbers for convenience, although one supported
16 * ancillary chunk uses signed (two's complement) numbers.
17 */
Guy Schalnat0d580581995-07-20 02:43:20 -050018void
Guy Schalnat6d764711995-12-19 03:22:19 -060019png_save_uint_32(png_bytep buf, png_uint_32 i)
Guy Schalnat0d580581995-07-20 02:43:20 -050020{
21 buf[0] = (png_byte)((i >> 24) & 0xff);
22 buf[1] = (png_byte)((i >> 16) & 0xff);
23 buf[2] = (png_byte)((i >> 8) & 0xff);
24 buf[3] = (png_byte)(i & 0xff);
25}
26
Andreas Dilger47a0c421997-05-16 02:46:07 -050027#if defined(PNG_WRITE_pCAL_SUPPORTED)
28/* The png_save_int_32 function assumes integers are stored in two's
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060029 * complement format. If this isn't the case, then this routine needs to
30 * be modified to write data in two's complement format.
31 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050032void
33png_save_int_32(png_bytep buf, png_int_32 i)
34{
35 buf[0] = (png_byte)((i >> 24) & 0xff);
36 buf[1] = (png_byte)((i >> 16) & 0xff);
37 buf[2] = (png_byte)((i >> 8) & 0xff);
38 buf[3] = (png_byte)(i & 0xff);
39}
40#endif
41
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060042/* Place a 16-bit number into a buffer in PNG byte order.
43 * The parameter is declared unsigned int, not png_uint_16,
44 * just to avoid potential problems on pre-ANSI C compilers.
45 */
Guy Schalnat0d580581995-07-20 02:43:20 -050046void
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060047png_save_uint_16(png_bytep buf, unsigned int i)
Guy Schalnat0d580581995-07-20 02:43:20 -050048{
49 buf[0] = (png_byte)((i >> 8) & 0xff);
50 buf[1] = (png_byte)(i & 0xff);
51}
52
Andreas Dilger47a0c421997-05-16 02:46:07 -050053/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060054 * representing the chunk name. The array must be at least 4 bytes in
55 * length, and does not need to be null terminated. To be safe, pass the
56 * pre-defined chunk names here, and if you need a new one, define it
57 * where the others are defined. The length is the length of the data.
58 * All the data must be present. If that is not possible, use the
59 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
60 * functions instead.
61 */
Guy Schalnat0d580581995-07-20 02:43:20 -050062void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060063png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050064 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050065{
Andreas Dilger47a0c421997-05-16 02:46:07 -050066 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060067 png_write_chunk_data(png_ptr, data, length);
68 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050069}
70
Andreas Dilger47a0c421997-05-16 02:46:07 -050071/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060072 * The total_length is the sum of the lengths of all the data you will be
73 * passing in png_write_chunk_data().
74 */
Guy Schalnat0d580581995-07-20 02:43:20 -050075void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060076png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
77 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -050078{
Andreas Dilger47a0c421997-05-16 02:46:07 -050079 png_byte buf[4];
80 png_debug2(0, "Writing %s chunk (%d bytes)\n", chunk_name, length);
81
Guy Schalnat0d580581995-07-20 02:43:20 -050082 /* write the length */
Andreas Dilger47a0c421997-05-16 02:46:07 -050083 png_save_uint_32(buf, length);
84 png_write_data(png_ptr, buf, (png_size_t)4);
85
Guy Schalnat0d580581995-07-20 02:43:20 -050086 /* write the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -050087 png_write_data(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050088 /* reset the crc and run it over the chunk name */
89 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -050090 png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050091}
92
Andreas Dilger47a0c421997-05-16 02:46:07 -050093/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060094 * Note that multiple calls to this function are allowed, and that the
95 * sum of the lengths from these calls *must* add up to the total_length
96 * given to png_write_chunk_start().
97 */
Guy Schalnat0d580581995-07-20 02:43:20 -050098void
Andreas Dilger47a0c421997-05-16 02:46:07 -050099png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500100{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500101 /* write the data, and run the CRC over it */
102 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500103 {
104 png_calculate_crc(png_ptr, data, length);
Guy Schalnat6d764711995-12-19 03:22:19 -0600105 png_write_data(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500106 }
107}
108
Andreas Dilger47a0c421997-05-16 02:46:07 -0500109/* Finish a chunk started with png_write_chunk_start(). */
Guy Schalnat0d580581995-07-20 02:43:20 -0500110void
Guy Schalnat6d764711995-12-19 03:22:19 -0600111png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500112{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500113 png_byte buf[4];
114
Guy Schalnat0d580581995-07-20 02:43:20 -0500115 /* write the crc */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500116 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500117
118 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500119}
120
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600121/* Simple function to write the signature. If we have already written
122 * the magic bytes of the signature, or more likely, the PNG stream is
123 * being embedded into another stream and doesn't need its own signature,
124 * we should call png_set_sig_bytes() to tell libpng how many of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600125 * bytes have already been written.
126 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500127void
Guy Schalnat6d764711995-12-19 03:22:19 -0600128png_write_sig(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500129{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600130 /* write the rest of the 8 byte signature */
131 png_write_data(png_ptr, &png_sig[png_ptr->sig_bytes],
Andreas Dilger47a0c421997-05-16 02:46:07 -0500132 (png_size_t)8 - png_ptr->sig_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500133}
134
135/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600136 * information. Note that the rest of this code depends upon this
137 * information being correct.
138 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500139void
Guy Schalnat6d764711995-12-19 03:22:19 -0600140png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500141 int bit_depth, int color_type, int compression_type, int filter_type,
142 int interlace_type)
143{
144 png_byte buf[13]; /* buffer to store the IHDR info */
145
Andreas Dilger47a0c421997-05-16 02:46:07 -0500146 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500147 /* Check that we have valid input data from the application info */
148 switch (color_type)
149 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500150 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500151 switch (bit_depth)
152 {
153 case 1:
154 case 2:
155 case 4:
156 case 8:
157 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500158 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500159 }
160 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500161 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500162 if (bit_depth != 8 && bit_depth != 16)
163 png_error(png_ptr, "Invalid bit depth for RGB image");
164 png_ptr->channels = 3;
165 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500166 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500167 switch (bit_depth)
168 {
169 case 1:
170 case 2:
171 case 4:
172 case 8: png_ptr->channels = 1; break;
173 default: png_error(png_ptr, "Invalid bit depth for paletted image");
174 }
175 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500176 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500177 if (bit_depth != 8 && bit_depth != 16)
178 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
179 png_ptr->channels = 2;
180 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500181 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500182 if (bit_depth != 8 && bit_depth != 16)
183 png_error(png_ptr, "Invalid bit depth for RGBA image");
184 png_ptr->channels = 4;
185 break;
186 default:
187 png_error(png_ptr, "Invalid image color type specified");
188 }
189
Andreas Dilger47a0c421997-05-16 02:46:07 -0500190 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500191 {
192 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500193 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500194 }
195
Andreas Dilger47a0c421997-05-16 02:46:07 -0500196 if (filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500197 {
198 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500199 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500200 }
201
Andreas Dilger47a0c421997-05-16 02:46:07 -0500202 if (interlace_type != PNG_INTERLACE_NONE &&
203 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500204 {
205 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500206 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500207 }
208
209 /* save off the relevent information */
210 png_ptr->bit_depth = (png_byte)bit_depth;
211 png_ptr->color_type = (png_byte)color_type;
212 png_ptr->interlaced = (png_byte)interlace_type;
213 png_ptr->width = width;
214 png_ptr->height = height;
215
216 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500217 png_ptr->rowbytes = ((width * (png_size_t)png_ptr->pixel_depth + 7) >> 3);
Guy Schalnate5a37791996-06-05 15:50:50 -0500218 /* set the usr info, so any transformations can modify it */
219 png_ptr->usr_width = png_ptr->width;
220 png_ptr->usr_bit_depth = png_ptr->bit_depth;
221 png_ptr->usr_channels = png_ptr->channels;
222
Guy Schalnat0d580581995-07-20 02:43:20 -0500223 /* pack the header information into the buffer */
224 png_save_uint_32(buf, width);
225 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600226 buf[8] = (png_byte)bit_depth;
227 buf[9] = (png_byte)color_type;
228 buf[10] = (png_byte)compression_type;
229 buf[11] = (png_byte)filter_type;
230 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500231
232 /* write the chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500233 png_write_chunk(png_ptr, png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500234
Andreas Dilger47a0c421997-05-16 02:46:07 -0500235 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600236 png_ptr->zstream.zalloc = png_zalloc;
237 png_ptr->zstream.zfree = png_zfree;
238 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500239 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500240 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500241 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
242 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500243 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500244 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500245 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500246 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500247 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500248 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500249 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500250 png_ptr->zlib_strategy = Z_FILTERED;
251 else
252 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
253 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500254 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500255 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500256 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500257 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500258 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500259 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500260 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500261 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600262 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500263 png_ptr->zlib_method, png_ptr->zlib_window_bits,
264 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600265 png_ptr->zstream.next_out = png_ptr->zbuf;
266 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500267
Guy Schalnate5a37791996-06-05 15:50:50 -0500268 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500269}
270
271/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600272 * correct order for PNG, so people can redefine it to any convient
273 * structure.
274 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500275void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500276png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500277{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500278 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600279 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500280 png_byte buf[3];
281
Andreas Dilger47a0c421997-05-16 02:46:07 -0500282 png_debug(1, "in png_write_PLTE\n");
283 if (num_pal == 0 || num_pal > 256)
Guy Schalnate5a37791996-06-05 15:50:50 -0500284 {
285 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
286 {
287 png_error(png_ptr, "Invalid number of colors in palette");
288 }
289 else
290 {
291 png_warning(png_ptr, "Invalid number of colors in palette");
292 return;
293 }
294 }
295
Andreas Dilger47a0c421997-05-16 02:46:07 -0500296 png_ptr->num_palette = (png_uint_16)num_pal;
297 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500298
Andreas Dilger47a0c421997-05-16 02:46:07 -0500299 png_write_chunk_start(png_ptr, png_PLTE, num_pal * 3);
300 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500301 {
302 buf[0] = pal_ptr->red;
303 buf[1] = pal_ptr->green;
304 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500305 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500306 }
307 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500308 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500309}
310
311/* write an IDAT chunk */
312void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500313png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500314{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500315 png_debug(1, "in png_write_IDAT\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500316 png_write_chunk(png_ptr, png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500317 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500318}
319
320/* write an IEND chunk */
321void
Guy Schalnat6d764711995-12-19 03:22:19 -0600322png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500323{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500324 png_debug(1, "in png_write_IEND\n");
325 png_write_chunk(png_ptr, png_IEND, NULL, (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600326 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500327}
328
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500329#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500330/* write a gAMA chunk */
331void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500332png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500333{
334 png_uint_32 igamma;
335 png_byte buf[4];
336
Andreas Dilger47a0c421997-05-16 02:46:07 -0500337 png_debug(1, "in png_write_gAMA\n");
338 /* file_gamma is saved in 1/100,000ths */
339 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500340 png_save_uint_32(buf, igamma);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500341 png_write_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500342}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500343#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500344
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600345#if defined(PNG_WRITE_sRGB_SUPPORTED)
346/* write a sRGB chunk */
347void
348png_write_sRGB(png_structp png_ptr, png_byte srgb_intent)
349{
350 png_byte buf[1];
351
352 png_debug(1, "in png_write_sRGB\n");
353 if(srgb_intent > 3)
354 png_warning(png_ptr,
355 "Invalid sRGB rendering intent specified");
356 buf[0]=srgb_intent;
357 png_write_chunk(png_ptr, png_sRGB, buf, (png_size_t)1);
358}
359#endif
360
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500361#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500362/* write the sBIT chunk */
363void
Guy Schalnat6d764711995-12-19 03:22:19 -0600364png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500365{
366 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500367 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500368
Andreas Dilger47a0c421997-05-16 02:46:07 -0500369 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600370 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500371 if (color_type & PNG_COLOR_MASK_COLOR)
372 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500373 int maxbits;
374
375 maxbits = color_type==PNG_COLOR_TYPE_PALETTE ? 8:png_ptr->usr_bit_depth;
376 if (sbit->red == 0 || sbit->red > maxbits ||
377 sbit->green == 0 || sbit->green > maxbits ||
378 sbit->blue == 0 || sbit->blue > maxbits)
379 {
380 png_warning(png_ptr, "Invalid sBIT depth specified");
381 return;
382 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500383 buf[0] = sbit->red;
384 buf[1] = sbit->green;
385 buf[2] = sbit->blue;
386 size = 3;
387 }
388 else
389 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500390 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
391 {
392 png_warning(png_ptr, "Invalid sBIT depth specified");
393 return;
394 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500395 buf[0] = sbit->gray;
396 size = 1;
397 }
398
399 if (color_type & PNG_COLOR_MASK_ALPHA)
400 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500401 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
402 {
403 png_warning(png_ptr, "Invalid sBIT depth specified");
404 return;
405 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500406 buf[size++] = sbit->alpha;
407 }
408
Andreas Dilger47a0c421997-05-16 02:46:07 -0500409 png_write_chunk(png_ptr, png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500410}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500411#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500412
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500413#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500414/* write the cHRM chunk */
415void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500416png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600417 double red_x, double red_y, double green_x, double green_y,
418 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500419{
420 png_uint_32 itemp;
421 png_byte buf[32];
422
Andreas Dilger47a0c421997-05-16 02:46:07 -0500423 png_debug(1, "in png_write_cHRM\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500424 /* each value is saved int 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500425 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
426 white_x + white_y > 1.0)
427 {
428 png_warning(png_ptr, "Invalid cHRM white point specified");
429 return;
430 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500431 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
432 png_save_uint_32(buf, itemp);
433 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
434 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500435
436 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
437 red_x + red_y > 1.0)
438 {
439 png_warning(png_ptr, "Invalid cHRM red point specified");
440 return;
441 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500442 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
443 png_save_uint_32(buf + 8, itemp);
444 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
445 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500446
447 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
448 green_x + green_y > 1.0)
449 {
450 png_warning(png_ptr, "Invalid cHRM green point specified");
451 return;
452 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500453 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
454 png_save_uint_32(buf + 16, itemp);
455 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
456 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500457
458 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
459 blue_x + blue_y > 1.0)
460 {
461 png_warning(png_ptr, "Invalid cHRM blue point specified");
462 return;
463 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500464 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
465 png_save_uint_32(buf + 24, itemp);
466 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
467 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500468
Andreas Dilger47a0c421997-05-16 02:46:07 -0500469 png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500470}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500471#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500472
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500473#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500474/* write the tRNS chunk */
475void
Guy Schalnat6d764711995-12-19 03:22:19 -0600476png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -0500477 int num_trans, int color_type)
478{
479 png_byte buf[6];
480
Andreas Dilger47a0c421997-05-16 02:46:07 -0500481 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500482 if (color_type == PNG_COLOR_TYPE_PALETTE)
483 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500484 if (num_trans <= 0 || num_trans > png_ptr->num_palette)
485 {
486 png_warning(png_ptr,"Invalid number of transparent colors specified");
487 return;
488 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500489 /* write the chunk out as it is */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500490 png_write_chunk(png_ptr, png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -0500491 }
492 else if (color_type == PNG_COLOR_TYPE_GRAY)
493 {
494 /* one 16 bit value */
495 png_save_uint_16(buf, tran->gray);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500496 png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500497 }
498 else if (color_type == PNG_COLOR_TYPE_RGB)
499 {
500 /* three 16 bit values */
501 png_save_uint_16(buf, tran->red);
502 png_save_uint_16(buf + 2, tran->green);
503 png_save_uint_16(buf + 4, tran->blue);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500504 png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500505 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500506 else
507 {
508 png_warning(png_ptr, "Can't write tRNS with and alpha channel");
509 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500510}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500511#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500512
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500513#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500514/* write the background chunk */
515void
Guy Schalnat6d764711995-12-19 03:22:19 -0600516png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500517{
518 png_byte buf[6];
519
Andreas Dilger47a0c421997-05-16 02:46:07 -0500520 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500521 if (color_type == PNG_COLOR_TYPE_PALETTE)
522 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500523 if (back->index > png_ptr->num_palette)
524 {
525 png_warning(png_ptr, "Invalid background palette index");
526 return;
527 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500528 buf[0] = back->index;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500529 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500530 }
531 else if (color_type & PNG_COLOR_MASK_COLOR)
532 {
533 png_save_uint_16(buf, back->red);
534 png_save_uint_16(buf + 2, back->green);
535 png_save_uint_16(buf + 4, back->blue);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500536 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500537 }
538 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600539 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500540 png_save_uint_16(buf, back->gray);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500541 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500542 }
543}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500544#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500545
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500546#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500547/* write the histogram */
548void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500549png_write_hIST(png_structp png_ptr, png_uint_16p hist, png_uint_32 num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -0500550{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500551 png_uint_32 i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500552 png_byte buf[3];
553
Andreas Dilger47a0c421997-05-16 02:46:07 -0500554 png_debug(1, "in png_write_hIST\n");
555 if (num_hist > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500556 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500557 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
558 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500559 png_warning(png_ptr, "Invalid number of histogram entries specified");
560 return;
561 }
562
Andreas Dilger47a0c421997-05-16 02:46:07 -0500563 png_write_chunk_start(png_ptr, png_hIST, num_hist * 2);
564 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500565 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600566 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500567 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500568 }
569 png_write_chunk_end(png_ptr);
570}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500571#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500572
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600573#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
574/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
575 * and if invalid, correct the keyword rather than discarding the entire
576 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
577 * length, forbids leading or trailing whitespace, multiple internal spaces,
578 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -0500579 *
580 * The new_key is allocated to hold the corrected keyword and must be freed
581 * by the calling routine. This avoids problems with trying to write to
582 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600583 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500584png_size_t
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600585png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600586{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500587 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600588 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600589 int kflag;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600590
Andreas Dilger47a0c421997-05-16 02:46:07 -0500591 png_debug(1, "in png_check_keyword\n");
592 *new_key = NULL;
593
594 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600595 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600596 png_chunk_warning(png_ptr, "zero length keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600597 return 0;
598 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600599
Andreas Dilger47a0c421997-05-16 02:46:07 -0500600 png_debug1(2, "Keyword to be checked is '%s'\n", key);
601
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600602 *new_key = (png_charp)png_malloc(png_ptr, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600603
604 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500605 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600606 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600607 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500608 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600609#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500610 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600611
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600612 sprintf(msg, "invalid keyword character 0x%02X", *kp);
613 png_chunk_warning(png_ptr, msg);
614#else
615 png_chunk_warning(png_ptr, "invalid character in keyword");
616#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500617 *dp = ' ';
618 }
619 else
620 {
621 *dp = *kp;
622 }
623 }
624 *dp = '\0';
625
626 /* Remove any trailing white space. */
627 kp = *new_key + key_len - 1;
628 if (*kp == ' ')
629 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600630 png_chunk_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500631
632 while (*kp == ' ')
633 {
634 *(kp--) = '\0';
635 key_len--;
636 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600637 }
638
639 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500640 kp = *new_key;
641 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600642 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600643 png_chunk_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500644
645 while (*kp == ' ')
646 {
647 kp++;
648 key_len--;
649 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600650 }
651
Andreas Dilger47a0c421997-05-16 02:46:07 -0500652 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600653
Andreas Dilger47a0c421997-05-16 02:46:07 -0500654 /* Remove multiple internal spaces. */
655 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600656 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500657 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600658 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500659 *(dp++) = *kp;
660 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600661 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500662 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600663 {
664 key_len--;
665 }
666 else
667 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500668 *(dp++) = *kp;
669 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600670 }
671 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500672 *dp = '\0';
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600673
674 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500675 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600676 png_chunk_warning(png_ptr, "zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500677 }
678
679 if (key_len > 79)
680 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600681 png_chunk_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500682 new_key[79] = '\0';
683 key_len = 79;
684 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600685
686 return key_len;
687}
688#endif
689
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500690#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500691/* write a tEXt chunk */
692void
Guy Schalnat6d764711995-12-19 03:22:19 -0600693png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500694 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -0500695{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500696 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600697 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -0500698
Andreas Dilger47a0c421997-05-16 02:46:07 -0500699 png_debug(1, "in png_write_tEXt\n");
700 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
701 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600702 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -0500703 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500704 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500705
Andreas Dilger47a0c421997-05-16 02:46:07 -0500706 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600707 text_len = 0;
708
709 /* make sure we include the 0 after the key */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500710 png_write_chunk_start(png_ptr, png_tEXt, (png_uint_32)key_len+text_len+1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600711 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600712 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500713 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600714
Guy Schalnat0d580581995-07-20 02:43:20 -0500715 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500716 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -0500717}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500718#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500719
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500720#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500721/* write a compressed text chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -0500722void
Guy Schalnat6d764711995-12-19 03:22:19 -0600723png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500724 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -0500725{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500726 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -0500727 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600728 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -0500729 int i, ret;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600730 png_charpp output_ptr = NULL; /* array of pointers to output */
Guy Schalnat0d580581995-07-20 02:43:20 -0500731 int num_output_ptr = 0; /* number of output pointers used */
732 int max_output_ptr = 0; /* size of output_ptr */
733
Andreas Dilger47a0c421997-05-16 02:46:07 -0500734 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600735
Andreas Dilger47a0c421997-05-16 02:46:07 -0500736 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -0500737 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600738 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500739 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500740 }
741
Andreas Dilger47a0c421997-05-16 02:46:07 -0500742 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
743 {
744 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
745 png_free(png_ptr, new_key);
746 return;
747 }
748
749 png_free(png_ptr, new_key);
750
751 if (compression >= PNG_TEXT_COMPRESSION_LAST)
752 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600753#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500754 char msg[50];
755 sprintf(msg, "Unknown zTXt compression type %d", compression);
756 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600757#else
758 png_warning(png_ptr, "Unknown zTXt compression type");
759#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500760 compression = PNG_TEXT_COMPRESSION_zTXt;
761 }
762
763 /* We can't write the chunk until we find out how much data we have,
764 * which means we need to run the compressor first, and save the
765 * output. This shouldn't be a problem, as the vast majority of
766 * comments should be reasonable, but we will set up an array of
767 * malloc'd pointers to be sure.
768 *
769 * If we knew the application was well behaved, we could simplify this
770 * greatly by assuming we can always malloc an output buffer large
771 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
772 * and malloc this directly. The only time this would be a bad idea is
773 * if we can't malloc more than 64K and we have 64K of random input
774 * data, or if the input string is incredibly large (although this
775 * wouldn't cause a failure, just a slowdown due to swapping).
776 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500777
778 /* set up the compression buffers */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600779 png_ptr->zstream.avail_in = (uInt)text_len;
780 png_ptr->zstream.next_in = (Bytef *)text;
781 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
782 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500783
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600784 /* this is the same compression loop as in png_write_row() */
Guy Schalnat0d580581995-07-20 02:43:20 -0500785 do
786 {
787 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600788 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnat0d580581995-07-20 02:43:20 -0500789 if (ret != Z_OK)
790 {
791 /* error */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500792 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600793 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -0500794 else
Guy Schalnat6d764711995-12-19 03:22:19 -0600795 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -0500796 }
797 /* check to see if we need more room */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600798 if (!png_ptr->zstream.avail_out && png_ptr->zstream.avail_in)
Guy Schalnat0d580581995-07-20 02:43:20 -0500799 {
800 /* make sure the output array has room */
801 if (num_output_ptr >= max_output_ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600802 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500803 int old_max;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500804
805 old_max = max_output_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500806 max_output_ptr = num_output_ptr + 4;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500807 if (output_ptr != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600808 {
809 png_charpp old_ptr;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600810
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600811 old_ptr = output_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600812 output_ptr = (png_charpp)png_malloc(png_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600813 max_output_ptr * sizeof (png_charpp));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500814 png_memcpy(output_ptr, old_ptr, old_max * sizeof (png_charp));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600815 png_free(png_ptr, old_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600816 }
817 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600818 output_ptr = (png_charpp)png_malloc(png_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600819 max_output_ptr * sizeof (png_charp));
Guy Schalnat0d580581995-07-20 02:43:20 -0500820 }
821
822 /* save the data */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500823 output_ptr[num_output_ptr] = png_malloc(png_ptr, png_ptr->zbuf_size);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500824 png_memcpy(output_ptr[num_output_ptr], png_ptr->zbuf,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500825 png_ptr->zbuf_size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500826 num_output_ptr++;
827
828 /* and reset the buffer */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600829 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
830 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500831 }
832 /* continue until we don't have anymore to compress */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600833 } while (png_ptr->zstream.avail_in);
Guy Schalnat0d580581995-07-20 02:43:20 -0500834
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600835 /* finish the compression */
Guy Schalnat0d580581995-07-20 02:43:20 -0500836 do
837 {
838 /* tell zlib we are finished */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600839 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -0500840 if (ret != Z_OK && ret != Z_STREAM_END)
841 {
842 /* we got an error */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500843 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600844 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -0500845 else
Guy Schalnat6d764711995-12-19 03:22:19 -0600846 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -0500847 }
848
849 /* check to see if we need more room */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500850 if (!(png_ptr->zstream.avail_out) && ret == Z_OK)
Guy Schalnat0d580581995-07-20 02:43:20 -0500851 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600852 /* check to make sure our output array has room */
853 if (num_output_ptr >= max_output_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500854 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500855 int old_max;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500856
857 old_max = max_output_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500858 max_output_ptr = num_output_ptr + 4;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500859 if (output_ptr != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600860 {
861 png_charpp old_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500862
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600863 old_ptr = output_ptr;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500864 /* This could be optimized to realloc() */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600865 output_ptr = (png_charpp)png_malloc(png_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600866 max_output_ptr * sizeof (png_charpp));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500867 png_memcpy(output_ptr, old_ptr, old_max * sizeof (png_charp));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600868 png_free(png_ptr, old_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600869 }
870 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600871 output_ptr = (png_charpp)png_malloc(png_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600872 max_output_ptr * sizeof (png_charp));
873 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600874
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600875 /* save off the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600876 output_ptr[num_output_ptr] = png_malloc(png_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600877 png_ptr->zbuf_size);
878 png_memcpy(output_ptr[num_output_ptr], png_ptr->zbuf,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500879 png_ptr->zbuf_size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500880 num_output_ptr++;
881
882 /* and reset the buffer pointers */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600883 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
884 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500885 }
886 } while (ret != Z_STREAM_END);
887
888 /* text length is number of buffers plus last buffer */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600889 text_len = png_ptr->zbuf_size * num_output_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600890 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500891 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
Guy Schalnat0d580581995-07-20 02:43:20 -0500892
893 /* write start of chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500894 png_write_chunk_start(png_ptr, png_zTXt, (png_uint_32)(key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -0500895 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500896 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600897 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -0500898 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500899 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500900
901 /* write saved output buffers, if any */
902 for (i = 0; i < num_output_ptr; i++)
903 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500904 png_write_chunk_data(png_ptr,(png_bytep)output_ptr[i],png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600905 png_free(png_ptr, output_ptr[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500906 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500907 if (max_output_ptr != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600908 png_free(png_ptr, output_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500909 /* write anything left in zbuf */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500910 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -0500911 png_write_chunk_data(png_ptr, png_ptr->zbuf,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600912 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -0500913 /* close the chunk */
914 png_write_chunk_end(png_ptr);
915
916 /* reset zlib for another zTXt or the image data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600917 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -0500918}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500919#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500920
Andreas Dilger47a0c421997-05-16 02:46:07 -0500921
922#if defined(PNG_WRITE_oFFs_SUPPORTED)
923/* write the oFFs chunk */
924void
925png_write_oFFs(png_structp png_ptr, png_uint_32 x_offset,
926 png_uint_32 y_offset,
927 int unit_type)
928{
929 png_byte buf[9];
930
931 png_debug(1, "in png_write_oFFs\n");
932 if (unit_type >= PNG_OFFSET_LAST)
933 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
934
935 png_save_uint_32(buf, x_offset);
936 png_save_uint_32(buf + 4, y_offset);
937 buf[8] = (png_byte)unit_type;
938
939 png_write_chunk(png_ptr, png_oFFs, buf, (png_size_t)9);
940}
941#endif
942
943#if defined(PNG_WRITE_pCAL_SUPPORTED)
944/* write the pCAL chunk (png-scivis-19970203) */
945void
946png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
947 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
948{
949 png_size_t purpose_len, units_len, total_len;
950 png_uint_32p params_len;
951 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600952 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500953 int i;
954
955 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
956 if (type >= PNG_EQUATION_LAST)
957 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
958
959 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
960 png_debug1(3, "pCAL purpose length = %d\n", purpose_len);
961 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
962 png_debug1(3, "pCAL units length = %d\n", units_len);
963 total_len = purpose_len + units_len + 10;
964
965 params_len = (png_uint_32p)png_malloc(png_ptr, nparams*sizeof(png_uint_32));
966
967 /* Find the length of each parameter, making sure we don't count the
968 null terminator for the last parameter. */
969 for (i = 0; i < nparams; i++)
970 {
971 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
972 png_debug2(3, "pCAL parameter %d length = %d\n", i, params_len[i]);
973 total_len += (png_size_t)params_len[i];
974 }
975
976 png_debug1(3, "pCAL total length = %d\n", total_len);
977 png_write_chunk_start(png_ptr, png_pCAL, total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600978 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500979 png_save_int_32(buf, X0);
980 png_save_int_32(buf + 4, X1);
981 buf[8] = (png_byte)type;
982 buf[9] = (png_byte)nparams;
983 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
984 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
985
986 png_free(png_ptr, new_purpose);
987
988 for (i = 0; i < nparams; i++)
989 {
990 png_write_chunk_data(png_ptr, (png_bytep)params[i],
991 (png_size_t)params_len[i]);
992 }
993
994 png_write_chunk_end(png_ptr);
995}
996#endif
997
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500998#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500999/* write the pHYs chunk */
1000void
Guy Schalnat6d764711995-12-19 03:22:19 -06001001png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001002 png_uint_32 y_pixels_per_unit,
1003 int unit_type)
1004{
1005 png_byte buf[9];
1006
Andreas Dilger47a0c421997-05-16 02:46:07 -05001007 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001008 if (unit_type >= PNG_RESOLUTION_LAST)
1009 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1010
Guy Schalnat0d580581995-07-20 02:43:20 -05001011 png_save_uint_32(buf, x_pixels_per_unit);
1012 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001013 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001014
Andreas Dilger47a0c421997-05-16 02:46:07 -05001015 png_write_chunk(png_ptr, png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001016}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001017#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001018
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001019#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001020/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1021 * or png_convert_from_time_t(), or fill in the structure yourself.
1022 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001023void
Guy Schalnat6d764711995-12-19 03:22:19 -06001024png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001025{
1026 png_byte buf[7];
1027
Andreas Dilger47a0c421997-05-16 02:46:07 -05001028 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001029 if (mod_time->month > 12 || mod_time->month < 1 ||
1030 mod_time->day > 31 || mod_time->day < 1 ||
1031 mod_time->hour > 23 || mod_time->second > 60)
1032 {
1033 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1034 return;
1035 }
1036
Guy Schalnat0d580581995-07-20 02:43:20 -05001037 png_save_uint_16(buf, mod_time->year);
1038 buf[2] = mod_time->month;
1039 buf[3] = mod_time->day;
1040 buf[4] = mod_time->hour;
1041 buf[5] = mod_time->minute;
1042 buf[6] = mod_time->second;
1043
Andreas Dilger47a0c421997-05-16 02:46:07 -05001044 png_write_chunk(png_ptr, png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001045}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001046#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001047
1048/* initializes the row writing capability of libpng */
1049void
Guy Schalnat6d764711995-12-19 03:22:19 -06001050png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001051{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001052 png_size_t buf_size;
1053
1054 png_debug(1, "in png_write_start_row\n");
1055 buf_size = (png_size_t)(((png_ptr->width * png_ptr->usr_channels *
1056 png_ptr->usr_bit_depth + 7) >> 3) + 1);
1057
Guy Schalnat0d580581995-07-20 02:43:20 -05001058 /* set up row buffer */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001059 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, buf_size);
1060 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001061
1062 /* set up filtering buffer, if using this filter */
1063 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001064 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001065 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Guy Schalnate5a37791996-06-05 15:50:50 -05001066 png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001067 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001068 }
1069
Andreas Dilger47a0c421997-05-16 02:46:07 -05001070 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001071 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1072 {
1073 /* set up previous row buffer */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001074 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, buf_size);
1075 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001076
1077 if (png_ptr->do_filter & PNG_FILTER_UP)
1078 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001079 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Guy Schalnate5a37791996-06-05 15:50:50 -05001080 png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001081 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001082 }
1083
1084 if (png_ptr->do_filter & PNG_FILTER_AVG)
1085 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001086 png_ptr->avg_row = (png_bytep )png_malloc(png_ptr,
Guy Schalnate5a37791996-06-05 15:50:50 -05001087 png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001088 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001089 }
1090
1091 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1092 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001093 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Guy Schalnate5a37791996-06-05 15:50:50 -05001094 png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001095 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001096 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001097 }
1098
1099 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001100 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001101 {
1102 if (!(png_ptr->transformations & PNG_INTERLACE))
1103 {
1104 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1105 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001106 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1107 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001108 }
1109 else
1110 {
1111 png_ptr->num_rows = png_ptr->height;
1112 png_ptr->usr_width = png_ptr->width;
1113 }
1114 }
1115 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001116 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001117 png_ptr->num_rows = png_ptr->height;
1118 png_ptr->usr_width = png_ptr->width;
1119 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001120 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1121 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001122}
1123
Andreas Dilger47a0c421997-05-16 02:46:07 -05001124/* Internal use only. Called when finished processing a row of data. */
Guy Schalnat0d580581995-07-20 02:43:20 -05001125void
Guy Schalnat6d764711995-12-19 03:22:19 -06001126png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001127{
1128 int ret;
1129
Andreas Dilger47a0c421997-05-16 02:46:07 -05001130 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001131 /* next row */
1132 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001133
Guy Schalnat0d580581995-07-20 02:43:20 -05001134 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001135 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001136 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001137
1138 /* if interlaced, go to next pass */
1139 if (png_ptr->interlaced)
1140 {
1141 png_ptr->row_number = 0;
1142 if (png_ptr->transformations & PNG_INTERLACE)
1143 {
1144 png_ptr->pass++;
1145 }
1146 else
1147 {
1148 /* loop until we find a non-zero width or height pass */
1149 do
1150 {
1151 png_ptr->pass++;
1152 if (png_ptr->pass >= 7)
1153 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001154 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001155 png_pass_inc[png_ptr->pass] - 1 -
1156 png_pass_start[png_ptr->pass]) /
1157 png_pass_inc[png_ptr->pass];
1158 png_ptr->num_rows = (png_ptr->height +
1159 png_pass_yinc[png_ptr->pass] - 1 -
1160 png_pass_ystart[png_ptr->pass]) /
1161 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001162 if (png_ptr->transformations & PNG_INTERLACE)
1163 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001164 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1165
1166 }
1167
Guy Schalnate5a37791996-06-05 15:50:50 -05001168 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001169 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001170 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001171 if (png_ptr->prev_row != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001172 png_memset(png_ptr->prev_row, 0,
1173 (png_size_t) (((png_uint_32)png_ptr->usr_channels *
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001174 (png_uint_32)png_ptr->usr_bit_depth *
1175 png_ptr->width + 7) >> 3) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001176 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001177 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001178 }
1179
1180 /* if we get here, we've just written the last row, so we need
1181 to flush the compressor */
1182 do
1183 {
1184 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001185 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001186 /* check for an error */
1187 if (ret != Z_OK && ret != Z_STREAM_END)
1188 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001189 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001190 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001191 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001192 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001193 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001194 /* check to see if we need more room */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001195 if (!(png_ptr->zstream.avail_out) && ret == Z_OK)
Guy Schalnat0d580581995-07-20 02:43:20 -05001196 {
1197 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001198 png_ptr->zstream.next_out = png_ptr->zbuf;
1199 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat0d580581995-07-20 02:43:20 -05001200 }
1201 } while (ret != Z_STREAM_END);
1202
1203 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001204 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001205 {
1206 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001207 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001208 }
1209
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001210 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05001211}
1212
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001213#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001214/* Pick out the correct pixels for the interlace pass.
1215 * The basic idea here is to go through the row with a source
1216 * pointer and a destination pointer (sp and dp), and copy the
1217 * correct pixels for the pass. As the row gets compacted,
1218 * sp will always be >= dp, so we should never overwrite anything.
1219 * See the default: case for the easiest code to understand.
1220 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001221void
Guy Schalnat6d764711995-12-19 03:22:19 -06001222png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001223{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001224 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001225 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001226#if defined(PNG_USELESS_TESTS_SUPPORTED)
1227 if (row != NULL && row_info != NULL && pass < 6)
1228#else
1229 if (pass < 6)
1230#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001231 {
1232 /* each pixel depth is handled seperately */
1233 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001234 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001235 case 1:
1236 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001237 png_bytep sp;
1238 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001239 int shift;
1240 int d;
1241 int value;
1242 png_uint_32 i;
1243
1244 dp = row;
1245 d = 0;
1246 shift = 7;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001247 for (i = png_pass_start[pass]; i < row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001248 i += png_pass_inc[pass])
1249 {
1250 sp = row + (png_size_t)(i >> 3);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001251 value = (int)(*sp >> (7 - (int)(i & 7))) & 0x1;
Guy Schalnat0d580581995-07-20 02:43:20 -05001252 d |= (value << shift);
1253
1254 if (shift == 0)
1255 {
1256 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001257 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001258 d = 0;
1259 }
1260 else
1261 shift--;
1262
1263 }
1264 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001265 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001266 break;
1267 }
1268 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001269 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001270 png_bytep sp;
1271 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001272 int shift;
1273 int d;
1274 int value;
1275 png_uint_32 i;
1276
1277 dp = row;
1278 shift = 6;
1279 d = 0;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001280 for (i = png_pass_start[pass]; i < row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001281 i += png_pass_inc[pass])
1282 {
1283 sp = row + (png_size_t)(i >> 2);
1284 value = (*sp >> ((3 - (int)(i & 3)) << 1)) & 0x3;
1285 d |= (value << shift);
1286
1287 if (shift == 0)
1288 {
1289 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001290 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001291 d = 0;
1292 }
1293 else
1294 shift -= 2;
1295 }
1296 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001297 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001298 break;
1299 }
1300 case 4:
1301 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001302 png_bytep sp;
1303 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001304 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001305 int d;
1306 int value;
1307 png_uint_32 i;
1308
1309 dp = row;
1310 shift = 4;
1311 d = 0;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001312 for (i = png_pass_start[pass]; i < row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001313 i += png_pass_inc[pass])
1314 {
1315 sp = row + (png_size_t)(i >> 1);
1316 value = (*sp >> ((1 - (int)(i & 1)) << 2)) & 0xf;
1317 d |= (value << shift);
1318
1319 if (shift == 0)
1320 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001321 shift = 4;
1322 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001323 d = 0;
1324 }
1325 else
1326 shift -= 4;
1327 }
1328 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001329 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001330 break;
1331 }
1332 default:
1333 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001334 png_bytep sp;
1335 png_bytep dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001336 png_uint_32 i;
1337 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001338
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001339 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05001340 dp = row;
1341 /* find out how many bytes each pixel takes up */
1342 pixel_bytes = (row_info->pixel_depth >> 3);
1343 /* loop through the row, only looking at the pixels that
1344 matter */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001345 for (i = png_pass_start[pass]; i < row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001346 i += png_pass_inc[pass])
1347 {
1348 /* find out where the original pixel is */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001349 sp = row + i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001350 /* move the pixel */
1351 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001352 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05001353 /* next pixel */
1354 dp += pixel_bytes;
1355 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001356 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001357 }
1358 }
1359 /* set new row width */
1360 row_info->width = (row_info->width +
1361 png_pass_inc[pass] - 1 -
1362 png_pass_start[pass]) /
1363 png_pass_inc[pass];
1364 row_info->rowbytes = ((row_info->width *
1365 row_info->pixel_depth + 7) >> 3);
1366
1367 }
1368}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001369#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001370
Andreas Dilger47a0c421997-05-16 02:46:07 -05001371/* This filters the row, chooses which filter to use, if it has not already
1372 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001373 * chosen filter.
1374 */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001375#define PNG_MAXSUM (png_uint_32)(~0x0UL >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001376#define PNG_HISHIFT 10
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001377#define PNG_LOMASK (png_uint_32)0xffffL
1378#define PNG_HIMASK (png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT)
Guy Schalnat0d580581995-07-20 02:43:20 -05001379void
Guy Schalnate5a37791996-06-05 15:50:50 -05001380png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05001381{
Guy Schalnate5a37791996-06-05 15:50:50 -05001382 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001383 png_uint_32 mins, bpp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001384
Andreas Dilger47a0c421997-05-16 02:46:07 -05001385 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001386 /* find out how many bytes offset each pixel is */
1387 bpp = (row_info->pixel_depth + 7) / 8;
Guy Schalnate5a37791996-06-05 15:50:50 -05001388
1389 prev_row = png_ptr->prev_row;
1390 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001391 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05001392
Andreas Dilger47a0c421997-05-16 02:46:07 -05001393 /* The prediction method we use is to find which method provides the
1394 * smallest value when summing the absolute values of the distances
1395 * from zero using anything >= 128 as negative numbers. This is known
1396 * as the "minimum sum of absolute differences" heuristic. Other
1397 * heruistics are the "weighted minumum sum of absolute differences"
1398 * (experimental and can in theory improve compression), and the "zlib
1399 * predictive" method (not implemented in libpng 0.95), which does test
1400 * compressions of lines using different filter methods, and then chooses
1401 * the (series of) filter(s) which give minimum compressed data size (VERY
1402 * computationally expensive).
1403 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001404
Guy Schalnate5a37791996-06-05 15:50:50 -05001405 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05001406 * that has been chosen, as it doesn't actually do anything to the data.
1407 */
Guy Schalnate5a37791996-06-05 15:50:50 -05001408 if (png_ptr->do_filter & PNG_FILTER_NONE &&
1409 png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05001410 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001411 png_bytep rp;
1412 png_uint_32 sum = 0;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001413 png_size_t i;
1414 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05001415
Guy Schalnate5a37791996-06-05 15:50:50 -05001416 for (i = 0, rp = row_buf + 1; i < row_info->rowbytes; i++, rp++)
1417 {
1418 v = *rp;
1419 sum += (v < 128) ? v : 256 - v;
1420 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001421
1422#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1423 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1424 {
1425 png_uint_32 sumhi, sumlo;
1426 sumlo = sum & PNG_LOMASK;
1427 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
1428
1429 /* Reduce the sum if we match any of the previous rows */
1430 for (i = 0; i < png_ptr->num_prev_filters; i++)
1431 {
1432 if (png_ptr->prev_filters[i] == PNG_FILTER_NONE)
1433 {
1434 sumlo = (sumlo * png_ptr->filter_weights[i]) >>
1435 PNG_WEIGHT_SHIFT;
1436 sumhi = (sumhi * png_ptr->filter_weights[i]) >>
1437 PNG_WEIGHT_SHIFT;
1438 }
1439 }
1440
1441 /* Factor in the cost of this filter (this is here for completeness,
1442 * but it makes no sense to have a "cost" for the NONE filter, as
1443 * it has the minimum possible computational cost - none).
1444 */
1445 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
1446 PNG_COST_SHIFT;
1447 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
1448 PNG_COST_SHIFT;
1449
1450 if (sumhi > PNG_HIMASK)
1451 sum = PNG_MAXSUM;
1452 else
1453 sum = (sumhi << PNG_HISHIFT) + sumlo;
1454 }
1455#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001456 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05001457 }
1458
Guy Schalnate5a37791996-06-05 15:50:50 -05001459 /* sub filter */
1460 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001461 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001462 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001463 png_uint_32 sum = 0, lmins = mins;
1464 png_size_t i;
1465 int v;
1466
1467#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1468 /* We temporarily increase the "minumum sum" by the factor we
1469 * would reduce the sum of this filter, so that we can do the
1470 * early exit comparison without scaling the sum each time.
1471 */
1472 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1473 {
1474 png_uint_32 lmhi, lmlo;
1475 lmlo = lmins & PNG_LOMASK;
1476 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1477
1478 for (i = 0; i < png_ptr->num_prev_filters; i++)
1479 {
1480 if (png_ptr->prev_filters[i] == PNG_FILTER_VALUE_SUB)
1481 {
1482 lmlo = (lmlo * png_ptr->inv_filter_weights[i]) >>
1483 PNG_WEIGHT_SHIFT;
1484 lmhi = (lmhi * png_ptr->inv_filter_weights[i]) >>
1485 PNG_WEIGHT_SHIFT;
1486 }
1487 }
1488
1489 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1490 PNG_COST_SHIFT;
1491 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1492 PNG_COST_SHIFT;
1493
1494 if (lmhi > PNG_HIMASK)
1495 lmins = PNG_MAXSUM;
1496 else
1497 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1498 }
1499#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001500
Guy Schalnate5a37791996-06-05 15:50:50 -05001501 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
1502 i++, rp++, dp++)
1503 {
1504 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001505
Guy Schalnate5a37791996-06-05 15:50:50 -05001506 sum += (v < 128) ? v : 256 - v;
1507 }
1508 for (lp = row_buf + 1; i < row_info->rowbytes; i++, rp++, lp++, dp++)
1509 {
1510 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001511
Guy Schalnate5a37791996-06-05 15:50:50 -05001512 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001513
1514 if (sum > lmins) /* We are already worse, don't continue. */
1515 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001516 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001517
1518#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1519 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1520 {
1521 png_uint_32 sumhi, sumlo;
1522 sumlo = sum & PNG_LOMASK;
1523 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1524
1525 for (i = 0; i < png_ptr->num_prev_filters; i++)
1526 {
1527 if (png_ptr->prev_filters[i] == PNG_FILTER_VALUE_SUB)
1528 {
1529 sumlo = (sumlo * png_ptr->inv_filter_weights[i]) >>
1530 PNG_WEIGHT_SHIFT;
1531 sumhi = (sumhi * png_ptr->inv_filter_weights[i]) >>
1532 PNG_WEIGHT_SHIFT;
1533 }
1534 }
1535
1536 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1537 PNG_COST_SHIFT;
1538 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1539 PNG_COST_SHIFT;
1540
1541 if (sumhi > PNG_HIMASK)
1542 sum = PNG_MAXSUM;
1543 else
1544 sum = (sumhi << PNG_HISHIFT) + sumlo;
1545 }
1546#endif
1547
Guy Schalnate5a37791996-06-05 15:50:50 -05001548 if (sum < mins)
1549 {
1550 mins = sum;
1551 best_row = png_ptr->sub_row;
1552 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001553 }
1554
Guy Schalnate5a37791996-06-05 15:50:50 -05001555 /* up filter */
1556 if (png_ptr->do_filter & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05001557 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001558 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001559 png_uint_32 sum = 0, lmins = mins;
1560 png_size_t i;
1561 int v;
1562
1563#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1564 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1565 {
1566 png_uint_32 lmhi, lmlo;
1567 lmlo = lmins & PNG_LOMASK;
1568 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1569
1570 for (i = 0; i < png_ptr->num_prev_filters; i++)
1571 {
1572 if (png_ptr->prev_filters[i] == PNG_FILTER_VALUE_UP)
1573 {
1574 lmlo = (lmlo * png_ptr->inv_filter_weights[i]) >>
1575 PNG_WEIGHT_SHIFT;
1576 lmhi = (lmhi * png_ptr->inv_filter_weights[i]) >>
1577 PNG_WEIGHT_SHIFT;
1578 }
1579 }
1580
1581 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
1582 PNG_COST_SHIFT;
1583 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
1584 PNG_COST_SHIFT;
1585
1586 if (lmhi > PNG_HIMASK)
1587 lmins = PNG_MAXSUM;
1588 else
1589 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1590 }
1591#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001592
1593 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
1594 pp = prev_row + 1; i < row_info->rowbytes; i++, rp++, pp++, dp++)
1595 {
1596 v = *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
1597
1598 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001599
1600 if (sum > lmins) /* We are already worse, don't continue. */
1601 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001602 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001603
1604#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1605 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1606 {
1607 png_uint_32 sumhi, sumlo;
1608 sumlo = sum & PNG_LOMASK;
1609 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1610
1611 for (i = 0; i < png_ptr->num_prev_filters; i++)
1612 {
1613 if (png_ptr->prev_filters[i] == PNG_FILTER_UP)
1614 {
1615 sumlo = (sumlo * png_ptr->filter_weights[i]) >>
1616 PNG_WEIGHT_SHIFT;
1617 sumhi = (sumhi * png_ptr->filter_weights[i]) >>
1618 PNG_WEIGHT_SHIFT;
1619 }
1620 }
1621
1622 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
1623 PNG_COST_SHIFT;
1624 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
1625 PNG_COST_SHIFT;
1626
1627 if (sumhi > PNG_HIMASK)
1628 sum = PNG_MAXSUM;
1629 else
1630 sum = (sumhi << PNG_HISHIFT) + sumlo;
1631 }
1632#endif
1633
Guy Schalnate5a37791996-06-05 15:50:50 -05001634 if (sum < mins)
1635 {
1636 mins = sum;
1637 best_row = png_ptr->up_row;
1638 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001639 }
1640
Guy Schalnate5a37791996-06-05 15:50:50 -05001641 /* avg filter */
1642 if (png_ptr->do_filter & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001643 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001644 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001645 png_uint_32 sum = 0, lmins = mins;
1646 png_size_t i;
1647 int v;
1648
1649#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1650 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1651 {
1652 png_uint_32 lmhi, lmlo;
1653 lmlo = lmins & PNG_LOMASK;
1654 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1655
1656 for (i = 0; i < png_ptr->num_prev_filters; i++)
1657 {
1658 if (png_ptr->prev_filters[i] == PNG_FILTER_VALUE_AVG)
1659 {
1660 lmlo = (lmlo * png_ptr->inv_filter_weights[i]) >>
1661 PNG_WEIGHT_SHIFT;
1662 lmhi = (lmhi * png_ptr->inv_filter_weights[i]) >>
1663 PNG_WEIGHT_SHIFT;
1664 }
1665 }
1666
1667 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
1668 PNG_COST_SHIFT;
1669 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
1670 PNG_COST_SHIFT;
1671
1672 if (lmhi > PNG_HIMASK)
1673 lmins = PNG_MAXSUM;
1674 else
1675 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1676 }
1677#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001678
1679 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
1680 pp = prev_row + 1; i < bpp; i++, rp++, pp++, dp++)
1681 {
1682 v = *dp = (png_byte)(((int)*rp - ((int)*pp / 2)) & 0xff);
1683
1684 sum += (v < 128) ? v : 256 - v;
1685 }
1686 for (lp = row_buf + 1; i < row_info->rowbytes;
1687 i++, rp++, pp++, lp++, dp++)
1688 {
1689 v = *dp = (png_byte)(((int)*rp - (((int)*pp + (int)*lp) / 2)) & 0xff);
1690
1691 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001692
1693 if (sum > lmins) /* We are already worse, don't continue. */
1694 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001695 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001696
1697#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1698 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1699 {
1700 png_uint_32 sumhi, sumlo;
1701 sumlo = sum & PNG_LOMASK;
1702 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1703
1704 for (i = 0; i < png_ptr->num_prev_filters; i++)
1705 {
1706 if (png_ptr->prev_filters[i] == PNG_FILTER_NONE)
1707 {
1708 sumlo = (sumlo * png_ptr->filter_weights[i]) >>
1709 PNG_WEIGHT_SHIFT;
1710 sumhi = (sumhi * png_ptr->filter_weights[i]) >>
1711 PNG_WEIGHT_SHIFT;
1712 }
1713 }
1714
1715 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
1716 PNG_COST_SHIFT;
1717 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
1718 PNG_COST_SHIFT;
1719
1720 if (sumhi > PNG_HIMASK)
1721 sum = PNG_MAXSUM;
1722 else
1723 sum = (sumhi << PNG_HISHIFT) + sumlo;
1724 }
1725#endif
1726
Guy Schalnate5a37791996-06-05 15:50:50 -05001727 if (sum < mins)
1728 {
1729 mins = sum;
1730 best_row = png_ptr->avg_row;
1731 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001732 }
1733
Andreas Dilger47a0c421997-05-16 02:46:07 -05001734 /* Paeth filter */
Guy Schalnate5a37791996-06-05 15:50:50 -05001735 if (png_ptr->do_filter & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001736 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001737 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001738 png_uint_32 sum = 0, lmins = mins;
1739 png_size_t i;
1740 int v;
1741
1742#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1743 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1744 {
1745 png_uint_32 lmhi, lmlo;
1746 lmlo = lmins & PNG_LOMASK;
1747 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1748
1749 for (i = 0; i < png_ptr->num_prev_filters; i++)
1750 {
1751 if (png_ptr->prev_filters[i] == PNG_FILTER_VALUE_PAETH)
1752 {
1753 lmlo = (lmlo * png_ptr->inv_filter_weights[i]) >>
1754 PNG_WEIGHT_SHIFT;
1755 lmhi = (lmhi * png_ptr->inv_filter_weights[i]) >>
1756 PNG_WEIGHT_SHIFT;
1757 }
1758 }
1759
1760 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1761 PNG_COST_SHIFT;
1762 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1763 PNG_COST_SHIFT;
1764
1765 if (lmhi > PNG_HIMASK)
1766 lmins = PNG_MAXSUM;
1767 else
1768 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1769 }
1770#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001771
1772 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
1773 pp = prev_row + 1; i < bpp; i++, rp++, pp++, dp++)
1774 {
1775 v = *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
1776
1777 sum += (v < 128) ? v : 256 - v;
1778 }
1779 for (lp = row_buf + 1, cp = prev_row + 1; i < row_info->rowbytes;
1780 i++, rp++, pp++, lp++, dp++, cp++)
1781 {
1782 int a, b, c, pa, pb, pc, p;
1783
1784 b = *pp;
1785 c = *cp;
1786 a = *lp;
1787
1788 p = a + b - c;
1789 pa = abs(p - a);
1790 pb = abs(p - b);
1791 pc = abs(p - c);
1792
1793 if (pa <= pb && pa <= pc)
1794 p = a;
1795 else if (pb <= pc)
1796 p = b;
1797 else
1798 p = c;
1799
1800 v = *dp = (png_byte)(((int)*rp - p) & 0xff);
1801
1802 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001803
1804 if (sum > lmins) /* We are already worse, don't continue. */
1805 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001806 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001807
1808#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1809 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1810 {
1811 png_uint_32 sumhi, sumlo;
1812 sumlo = sum & PNG_LOMASK;
1813 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1814
1815 for (i = 0; i < png_ptr->num_prev_filters; i++)
1816 {
1817 if (png_ptr->prev_filters[i] == PNG_FILTER_PAETH)
1818 {
1819 sumlo = (sumlo * png_ptr->filter_weights[i]) >>
1820 PNG_WEIGHT_SHIFT;
1821 sumhi = (sumhi * png_ptr->filter_weights[i]) >>
1822 PNG_WEIGHT_SHIFT;
1823 }
1824 }
1825
1826 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1827 PNG_COST_SHIFT;
1828 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1829 PNG_COST_SHIFT;
1830
1831 if (sumhi > PNG_HIMASK)
1832 sum = PNG_MAXSUM;
1833 else
1834 sum = (sumhi << PNG_HISHIFT) + sumlo;
1835 }
1836#endif
1837
Guy Schalnate5a37791996-06-05 15:50:50 -05001838 if (sum < mins)
1839 {
1840 best_row = png_ptr->paeth_row;
1841 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001842 }
1843
Andreas Dilger47a0c421997-05-16 02:46:07 -05001844 /* Do the actual writing of the filtered row data from the chosen filter. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001845 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001846
1847#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1848 /* Save the type of filter we picked this time for future calculations */
1849 if (png_ptr->num_prev_filters > 0)
1850 {
1851 png_byte i;
1852
1853 for (i = 1; i < png_ptr->num_prev_filters; i++)
1854 {
1855 png_ptr->prev_filters[i] = png_ptr->prev_filters[i - 1];
1856 }
1857 png_ptr->prev_filters[i] = best_row[0];
1858 }
1859#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001860}
1861
1862
Andreas Dilger47a0c421997-05-16 02:46:07 -05001863/* Do the actual writing of a previously filtered row. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001864void
1865png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
1866{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001867 png_debug(1, "in png_write_filtered_row\n");
1868 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05001869 /* set up the zlib input buffer */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001870 png_ptr->zstream.next_in = filtered_row;
1871 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05001872 /* repeat until we have compressed all the data */
1873 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001874 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001875 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05001876
Guy Schalnate5a37791996-06-05 15:50:50 -05001877 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001878 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05001879 /* check for compression errors */
1880 if (ret != Z_OK)
1881 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001882 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001883 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05001884 else
1885 png_error(png_ptr, "zlib error");
1886 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001887
Guy Schalnate5a37791996-06-05 15:50:50 -05001888 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001889 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05001890 {
1891 /* write the IDAT and reset the zlib output buffer */
1892 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001893 png_ptr->zstream.next_out = png_ptr->zbuf;
1894 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05001895 }
1896 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001897 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05001898
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001899 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001900 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001901 {
1902 png_bytep tptr;
1903
1904 tptr = png_ptr->prev_row;
1905 png_ptr->prev_row = png_ptr->row_buf;
1906 png_ptr->row_buf = tptr;
1907 }
1908
Guy Schalnate5a37791996-06-05 15:50:50 -05001909 /* finish row - updates counters and flushes zlib if last row */
1910 png_write_finish_row(png_ptr);
1911
1912#if defined(PNG_WRITE_FLUSH_SUPPORTED)
1913 png_ptr->flush_rows++;
1914
1915 if (png_ptr->flush_dist > 0 &&
1916 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001917 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001918 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001919 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001920#endif /* PNG_WRITE_FLUSH_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05001921}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001922