blob: 0db9cc395cdbce4c602dfa85dfda29416946edbb [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-Pehrson860ab2b1999-10-14 07:43:10 -05004 * libpng 1.0.5 - October 15, 1999
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-Pehrsonc9442291999-01-06 21:50:16 -06008 * Copyright (c) 1998, 1999 Glenn Randers-Pehrson
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 */
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
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600202#ifdef PNG_WRITE_INTERLACING_SUPPORTED
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 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600209#else
210 interlace_type=PNG_INTERLACE_NONE;
211#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500212
213 /* save off the relevent information */
214 png_ptr->bit_depth = (png_byte)bit_depth;
215 png_ptr->color_type = (png_byte)color_type;
216 png_ptr->interlaced = (png_byte)interlace_type;
217 png_ptr->width = width;
218 png_ptr->height = height;
219
220 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500221 png_ptr->rowbytes = ((width * (png_size_t)png_ptr->pixel_depth + 7) >> 3);
Guy Schalnate5a37791996-06-05 15:50:50 -0500222 /* set the usr info, so any transformations can modify it */
223 png_ptr->usr_width = png_ptr->width;
224 png_ptr->usr_bit_depth = png_ptr->bit_depth;
225 png_ptr->usr_channels = png_ptr->channels;
226
Guy Schalnat0d580581995-07-20 02:43:20 -0500227 /* pack the header information into the buffer */
228 png_save_uint_32(buf, width);
229 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600230 buf[8] = (png_byte)bit_depth;
231 buf[9] = (png_byte)color_type;
232 buf[10] = (png_byte)compression_type;
233 buf[11] = (png_byte)filter_type;
234 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500235
236 /* write the chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500237 png_write_chunk(png_ptr, png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500238
Andreas Dilger47a0c421997-05-16 02:46:07 -0500239 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600240 png_ptr->zstream.zalloc = png_zalloc;
241 png_ptr->zstream.zfree = png_zfree;
242 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500243 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500244 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500245 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
246 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500247 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500248 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500249 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500250 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500251 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500252 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500253 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500254 png_ptr->zlib_strategy = Z_FILTERED;
255 else
256 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
257 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500258 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500259 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500260 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500261 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500262 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500263 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500264 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500265 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600266 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500267 png_ptr->zlib_method, png_ptr->zlib_window_bits,
268 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600269 png_ptr->zstream.next_out = png_ptr->zbuf;
270 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500271
Guy Schalnate5a37791996-06-05 15:50:50 -0500272 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500273}
274
275/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500276 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600277 * structure.
278 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500279void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500280png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500281{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500282 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600283 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500284 png_byte buf[3];
285
Andreas Dilger47a0c421997-05-16 02:46:07 -0500286 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500287 if ((
288#ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
289 !png_ptr->empty_plte_permitted &&
290#endif
291 num_pal == 0) || num_pal > 256)
292 {
293 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
294 {
295 png_error(png_ptr, "Invalid number of colors in palette");
296 }
297 else
298 {
299 png_warning(png_ptr, "Invalid number of colors in palette");
300 return;
301 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500302 }
303
Andreas Dilger47a0c421997-05-16 02:46:07 -0500304 png_ptr->num_palette = (png_uint_16)num_pal;
305 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500306
Andreas Dilger47a0c421997-05-16 02:46:07 -0500307 png_write_chunk_start(png_ptr, png_PLTE, num_pal * 3);
308 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500309 {
310 buf[0] = pal_ptr->red;
311 buf[1] = pal_ptr->green;
312 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500313 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500314 }
315 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500316 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500317}
318
319/* write an IDAT chunk */
320void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500321png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500322{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500323 png_debug(1, "in png_write_IDAT\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500324 png_write_chunk(png_ptr, png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500325 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500326}
327
328/* write an IEND chunk */
329void
Guy Schalnat6d764711995-12-19 03:22:19 -0600330png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500331{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500332 png_debug(1, "in png_write_IEND\n");
333 png_write_chunk(png_ptr, png_IEND, NULL, (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600334 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500335}
336
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500337#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500338/* write a gAMA chunk */
339void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500340png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500341{
342 png_uint_32 igamma;
343 png_byte buf[4];
344
Andreas Dilger47a0c421997-05-16 02:46:07 -0500345 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -0600346 /* file_gamma is saved in 1/1000000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600347 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500348 png_save_uint_32(buf, igamma);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500349 png_write_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500350}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500351#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500352
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600353#if defined(PNG_WRITE_sRGB_SUPPORTED)
354/* write a sRGB chunk */
355void
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600356png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600357{
358 png_byte buf[1];
359
360 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600361 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600362 png_warning(png_ptr,
363 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600364 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600365 png_write_chunk(png_ptr, png_sRGB, buf, (png_size_t)1);
366}
367#endif
368
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500369#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500370/* write the sBIT chunk */
371void
Guy Schalnat6d764711995-12-19 03:22:19 -0600372png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500373{
374 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500375 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500376
Andreas Dilger47a0c421997-05-16 02:46:07 -0500377 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600378 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500379 if (color_type & PNG_COLOR_MASK_COLOR)
380 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600381 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500382
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500383 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
384 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600385 if (sbit->red == 0 || sbit->red > maxbits ||
386 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500387 sbit->blue == 0 || sbit->blue > maxbits)
388 {
389 png_warning(png_ptr, "Invalid sBIT depth specified");
390 return;
391 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500392 buf[0] = sbit->red;
393 buf[1] = sbit->green;
394 buf[2] = sbit->blue;
395 size = 3;
396 }
397 else
398 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500399 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
400 {
401 png_warning(png_ptr, "Invalid sBIT depth specified");
402 return;
403 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500404 buf[0] = sbit->gray;
405 size = 1;
406 }
407
408 if (color_type & PNG_COLOR_MASK_ALPHA)
409 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500410 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
411 {
412 png_warning(png_ptr, "Invalid sBIT depth specified");
413 return;
414 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500415 buf[size++] = sbit->alpha;
416 }
417
Andreas Dilger47a0c421997-05-16 02:46:07 -0500418 png_write_chunk(png_ptr, png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500419}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500420#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500421
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500422#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500423/* write the cHRM chunk */
424void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500425png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600426 double red_x, double red_y, double green_x, double green_y,
427 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500428{
429 png_uint_32 itemp;
430 png_byte buf[32];
431
Andreas Dilger47a0c421997-05-16 02:46:07 -0500432 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -0600433 /* each value is saved int 1/1000000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500434 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
435 white_x + white_y > 1.0)
436 {
437 png_warning(png_ptr, "Invalid cHRM white point specified");
438 return;
439 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600440 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500441 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600442 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500443 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500444
445 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
446 red_x + red_y > 1.0)
447 {
448 png_warning(png_ptr, "Invalid cHRM red point specified");
449 return;
450 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600451 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500452 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600453 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500454 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500455
456 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
457 green_x + green_y > 1.0)
458 {
459 png_warning(png_ptr, "Invalid cHRM green point specified");
460 return;
461 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600462 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500463 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600464 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500465 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500466
467 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
468 blue_x + blue_y > 1.0)
469 {
470 png_warning(png_ptr, "Invalid cHRM blue point specified");
471 return;
472 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600473 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500474 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600475 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500476 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500477
Andreas Dilger47a0c421997-05-16 02:46:07 -0500478 png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500479}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500480#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500481
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500482#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500483/* write the tRNS chunk */
484void
Guy Schalnat6d764711995-12-19 03:22:19 -0600485png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -0500486 int num_trans, int color_type)
487{
488 png_byte buf[6];
489
Andreas Dilger47a0c421997-05-16 02:46:07 -0500490 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500491 if (color_type == PNG_COLOR_TYPE_PALETTE)
492 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600493 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500494 {
495 png_warning(png_ptr,"Invalid number of transparent colors specified");
496 return;
497 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500498 /* write the chunk out as it is */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500499 png_write_chunk(png_ptr, png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -0500500 }
501 else if (color_type == PNG_COLOR_TYPE_GRAY)
502 {
503 /* one 16 bit value */
504 png_save_uint_16(buf, tran->gray);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500505 png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500506 }
507 else if (color_type == PNG_COLOR_TYPE_RGB)
508 {
509 /* three 16 bit values */
510 png_save_uint_16(buf, tran->red);
511 png_save_uint_16(buf + 2, tran->green);
512 png_save_uint_16(buf + 4, tran->blue);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500513 png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500514 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500515 else
516 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600517 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -0500518 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500519}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500520#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500521
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500522#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500523/* write the background chunk */
524void
Guy Schalnat6d764711995-12-19 03:22:19 -0600525png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500526{
527 png_byte buf[6];
528
Andreas Dilger47a0c421997-05-16 02:46:07 -0500529 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500530 if (color_type == PNG_COLOR_TYPE_PALETTE)
531 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500532 if (
533#ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
534 (!png_ptr->empty_plte_permitted ||
535 (png_ptr->empty_plte_permitted && png_ptr->num_palette)) &&
536#endif
537 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500538 {
539 png_warning(png_ptr, "Invalid background palette index");
540 return;
541 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500542 buf[0] = back->index;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500543 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500544 }
545 else if (color_type & PNG_COLOR_MASK_COLOR)
546 {
547 png_save_uint_16(buf, back->red);
548 png_save_uint_16(buf + 2, back->green);
549 png_save_uint_16(buf + 4, back->blue);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500550 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500551 }
552 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600553 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500554 png_save_uint_16(buf, back->gray);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500555 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500556 }
557}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500558#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500559
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500560#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500561/* write the histogram */
562void
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600563png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -0500564{
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600565 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500566 png_byte buf[3];
567
Andreas Dilger47a0c421997-05-16 02:46:07 -0500568 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600569 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500570 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500571 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
572 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500573 png_warning(png_ptr, "Invalid number of histogram entries specified");
574 return;
575 }
576
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600577 png_write_chunk_start(png_ptr, png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500578 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500579 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600580 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500581 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500582 }
583 png_write_chunk_end(png_ptr);
584}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500585#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500586
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500587#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED) || \
588 defined(PNG_WRITE_pCAL_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600589/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
590 * and if invalid, correct the keyword rather than discarding the entire
591 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
592 * length, forbids leading or trailing whitespace, multiple internal spaces,
593 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -0500594 *
595 * The new_key is allocated to hold the corrected keyword and must be freed
596 * by the calling routine. This avoids problems with trying to write to
597 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600598 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500599png_size_t
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600600png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600601{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500602 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600603 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600604 int kflag;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600605
Andreas Dilger47a0c421997-05-16 02:46:07 -0500606 png_debug(1, "in png_check_keyword\n");
607 *new_key = NULL;
608
609 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600610 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600611 png_chunk_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600612 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600613 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600614
Andreas Dilger47a0c421997-05-16 02:46:07 -0500615 png_debug1(2, "Keyword to be checked is '%s'\n", key);
616
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600617 *new_key = (png_charp)png_malloc(png_ptr, (png_uint_32)(key_len + 1));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600618
619 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500620 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600621 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600622 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500623 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600624#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500625 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600626
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600627 sprintf(msg, "invalid keyword character 0x%02X", *kp);
628 png_chunk_warning(png_ptr, msg);
629#else
630 png_chunk_warning(png_ptr, "invalid character in keyword");
631#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500632 *dp = ' ';
633 }
634 else
635 {
636 *dp = *kp;
637 }
638 }
639 *dp = '\0';
640
641 /* Remove any trailing white space. */
642 kp = *new_key + key_len - 1;
643 if (*kp == ' ')
644 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600645 png_chunk_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500646
647 while (*kp == ' ')
648 {
649 *(kp--) = '\0';
650 key_len--;
651 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600652 }
653
654 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500655 kp = *new_key;
656 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600657 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600658 png_chunk_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500659
660 while (*kp == ' ')
661 {
662 kp++;
663 key_len--;
664 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600665 }
666
Andreas Dilger47a0c421997-05-16 02:46:07 -0500667 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600668
Andreas Dilger47a0c421997-05-16 02:46:07 -0500669 /* Remove multiple internal spaces. */
670 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600671 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500672 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600673 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500674 *(dp++) = *kp;
675 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600676 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500677 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600678 {
679 key_len--;
680 }
681 else
682 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500683 *(dp++) = *kp;
684 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600685 }
686 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500687 *dp = '\0';
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600688
689 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500690 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600691 png_chunk_warning(png_ptr, "zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500692 }
693
694 if (key_len > 79)
695 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600696 png_chunk_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500697 new_key[79] = '\0';
698 key_len = 79;
699 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600700
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600701 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600702}
703#endif
704
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500705#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500706/* write a tEXt chunk */
707void
Guy Schalnat6d764711995-12-19 03:22:19 -0600708png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500709 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -0500710{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500711 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600712 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -0500713
Andreas Dilger47a0c421997-05-16 02:46:07 -0500714 png_debug(1, "in png_write_tEXt\n");
715 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
716 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600717 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -0500718 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500719 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500720
Andreas Dilger47a0c421997-05-16 02:46:07 -0500721 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600722 text_len = 0;
723
724 /* make sure we include the 0 after the key */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500725 png_write_chunk_start(png_ptr, png_tEXt, (png_uint_32)key_len+text_len+1);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500726 /*
727 * We leave it to the application to meet PNG-1.0 requirements on the
728 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
729 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
730 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600731 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600732 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500733 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600734
Guy Schalnat0d580581995-07-20 02:43:20 -0500735 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500736 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -0500737}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500738#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500739
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500740#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500741/* write a compressed text chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -0500742void
Guy Schalnat6d764711995-12-19 03:22:19 -0600743png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500744 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -0500745{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500746 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -0500747 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600748 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -0500749 int i, ret;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600750 png_charpp output_ptr = NULL; /* array of pointers to output */
Guy Schalnat0d580581995-07-20 02:43:20 -0500751 int num_output_ptr = 0; /* number of output pointers used */
752 int max_output_ptr = 0; /* size of output_ptr */
753
Andreas Dilger47a0c421997-05-16 02:46:07 -0500754 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600755
Andreas Dilger47a0c421997-05-16 02:46:07 -0500756 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -0500757 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600758 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500759 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500760 }
761
Andreas Dilger47a0c421997-05-16 02:46:07 -0500762 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
763 {
764 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
765 png_free(png_ptr, new_key);
766 return;
767 }
768
769 png_free(png_ptr, new_key);
770
771 if (compression >= PNG_TEXT_COMPRESSION_LAST)
772 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600773#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500774 char msg[50];
775 sprintf(msg, "Unknown zTXt compression type %d", compression);
776 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600777#else
778 png_warning(png_ptr, "Unknown zTXt compression type");
779#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500780 compression = PNG_TEXT_COMPRESSION_zTXt;
781 }
782
783 /* We can't write the chunk until we find out how much data we have,
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500784 * which means we need to run the compressor first and save the
Andreas Dilger47a0c421997-05-16 02:46:07 -0500785 * output. This shouldn't be a problem, as the vast majority of
786 * comments should be reasonable, but we will set up an array of
787 * malloc'd pointers to be sure.
788 *
789 * If we knew the application was well behaved, we could simplify this
790 * greatly by assuming we can always malloc an output buffer large
791 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
792 * and malloc this directly. The only time this would be a bad idea is
793 * if we can't malloc more than 64K and we have 64K of random input
794 * data, or if the input string is incredibly large (although this
795 * wouldn't cause a failure, just a slowdown due to swapping).
796 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500797
798 /* set up the compression buffers */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600799 png_ptr->zstream.avail_in = (uInt)text_len;
800 png_ptr->zstream.next_in = (Bytef *)text;
801 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
802 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500803
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600804 /* this is the same compression loop as in png_write_row() */
Guy Schalnat0d580581995-07-20 02:43:20 -0500805 do
806 {
807 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600808 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnat0d580581995-07-20 02:43:20 -0500809 if (ret != Z_OK)
810 {
811 /* error */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500812 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600813 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -0500814 else
Guy Schalnat6d764711995-12-19 03:22:19 -0600815 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -0500816 }
817 /* check to see if we need more room */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600818 if (!png_ptr->zstream.avail_out && png_ptr->zstream.avail_in)
Guy Schalnat0d580581995-07-20 02:43:20 -0500819 {
820 /* make sure the output array has room */
821 if (num_output_ptr >= max_output_ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600822 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500823 int old_max;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500824
825 old_max = max_output_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500826 max_output_ptr = num_output_ptr + 4;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500827 if (output_ptr != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600828 {
829 png_charpp old_ptr;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600830
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600831 old_ptr = output_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600832 output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600833 (png_uint_32)(max_output_ptr * sizeof (png_charpp)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500834 png_memcpy(output_ptr, old_ptr, old_max * sizeof (png_charp));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600835 png_free(png_ptr, old_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600836 }
837 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600838 output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600839 (png_uint_32)(max_output_ptr * sizeof (png_charp)));
Guy Schalnat0d580581995-07-20 02:43:20 -0500840 }
841
842 /* save the data */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600843 output_ptr[num_output_ptr] = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600844 (png_uint_32)png_ptr->zbuf_size);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500845 png_memcpy(output_ptr[num_output_ptr], png_ptr->zbuf,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500846 png_ptr->zbuf_size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500847 num_output_ptr++;
848
849 /* and reset the buffer */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600850 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
851 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500852 }
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500853 /* continue until we don't have any more to compress */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600854 } while (png_ptr->zstream.avail_in);
Guy Schalnat0d580581995-07-20 02:43:20 -0500855
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600856 /* finish the compression */
Guy Schalnat0d580581995-07-20 02:43:20 -0500857 do
858 {
859 /* tell zlib we are finished */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600860 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -0500861 if (ret != Z_OK && ret != Z_STREAM_END)
862 {
863 /* we got an error */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500864 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600865 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -0500866 else
Guy Schalnat6d764711995-12-19 03:22:19 -0600867 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -0500868 }
869
870 /* check to see if we need more room */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500871 if (!(png_ptr->zstream.avail_out) && ret == Z_OK)
Guy Schalnat0d580581995-07-20 02:43:20 -0500872 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600873 /* check to make sure our output array has room */
874 if (num_output_ptr >= max_output_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500875 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500876 int old_max;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500877
878 old_max = max_output_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500879 max_output_ptr = num_output_ptr + 4;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500880 if (output_ptr != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600881 {
882 png_charpp old_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500883
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600884 old_ptr = output_ptr;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500885 /* This could be optimized to realloc() */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600886 output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600887 (png_uint_32)(max_output_ptr * sizeof (png_charpp)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500888 png_memcpy(output_ptr, old_ptr, old_max * sizeof (png_charp));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600889 png_free(png_ptr, old_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600890 }
891 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600892 output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600893 (png_uint_32)(max_output_ptr * sizeof (png_charp)));
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600894 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600895
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600896 /* save off the data */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600897 output_ptr[num_output_ptr] = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600898 (png_uint_32)png_ptr->zbuf_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600899 png_memcpy(output_ptr[num_output_ptr], png_ptr->zbuf,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500900 png_ptr->zbuf_size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500901 num_output_ptr++;
902
903 /* and reset the buffer pointers */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600904 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
905 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500906 }
907 } while (ret != Z_STREAM_END);
908
909 /* text length is number of buffers plus last buffer */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600910 text_len = png_ptr->zbuf_size * num_output_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600911 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500912 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
Guy Schalnat0d580581995-07-20 02:43:20 -0500913
914 /* write start of chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500915 png_write_chunk_start(png_ptr, png_zTXt, (png_uint_32)(key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -0500916 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500917 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600918 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -0500919 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500920 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500921
922 /* write saved output buffers, if any */
923 for (i = 0; i < num_output_ptr; i++)
924 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500925 png_write_chunk_data(png_ptr,(png_bytep)output_ptr[i],png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600926 png_free(png_ptr, output_ptr[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500927 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500928 if (max_output_ptr != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600929 png_free(png_ptr, output_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500930 /* write anything left in zbuf */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500931 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -0500932 png_write_chunk_data(png_ptr, png_ptr->zbuf,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600933 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -0500934 /* close the chunk */
935 png_write_chunk_end(png_ptr);
936
937 /* reset zlib for another zTXt or the image data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600938 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -0500939}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500940#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500941
Andreas Dilger47a0c421997-05-16 02:46:07 -0500942
943#if defined(PNG_WRITE_oFFs_SUPPORTED)
944/* write the oFFs chunk */
945void
946png_write_oFFs(png_structp png_ptr, png_uint_32 x_offset,
947 png_uint_32 y_offset,
948 int unit_type)
949{
950 png_byte buf[9];
951
952 png_debug(1, "in png_write_oFFs\n");
953 if (unit_type >= PNG_OFFSET_LAST)
954 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
955
956 png_save_uint_32(buf, x_offset);
957 png_save_uint_32(buf + 4, y_offset);
958 buf[8] = (png_byte)unit_type;
959
960 png_write_chunk(png_ptr, png_oFFs, buf, (png_size_t)9);
961}
962#endif
963
964#if defined(PNG_WRITE_pCAL_SUPPORTED)
965/* write the pCAL chunk (png-scivis-19970203) */
966void
967png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
968 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
969{
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600970 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500971 png_uint_32p params_len;
972 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600973 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500974 int i;
975
976 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
977 if (type >= PNG_EQUATION_LAST)
978 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
979
980 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
981 png_debug1(3, "pCAL purpose length = %d\n", purpose_len);
982 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
983 png_debug1(3, "pCAL units length = %d\n", units_len);
984 total_len = purpose_len + units_len + 10;
985
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600986 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
987 *sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500988
989 /* Find the length of each parameter, making sure we don't count the
990 null terminator for the last parameter. */
991 for (i = 0; i < nparams; i++)
992 {
993 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
994 png_debug2(3, "pCAL parameter %d length = %d\n", i, params_len[i]);
995 total_len += (png_size_t)params_len[i];
996 }
997
998 png_debug1(3, "pCAL total length = %d\n", total_len);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600999 png_write_chunk_start(png_ptr, png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001000 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001001 png_save_int_32(buf, X0);
1002 png_save_int_32(buf + 4, X1);
1003 buf[8] = (png_byte)type;
1004 buf[9] = (png_byte)nparams;
1005 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1006 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1007
1008 png_free(png_ptr, new_purpose);
1009
1010 for (i = 0; i < nparams; i++)
1011 {
1012 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1013 (png_size_t)params_len[i]);
1014 }
1015
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001016 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001017 png_write_chunk_end(png_ptr);
1018}
1019#endif
1020
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001021#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001022/* write the pHYs chunk */
1023void
Guy Schalnat6d764711995-12-19 03:22:19 -06001024png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001025 png_uint_32 y_pixels_per_unit,
1026 int unit_type)
1027{
1028 png_byte buf[9];
1029
Andreas Dilger47a0c421997-05-16 02:46:07 -05001030 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001031 if (unit_type >= PNG_RESOLUTION_LAST)
1032 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1033
Guy Schalnat0d580581995-07-20 02:43:20 -05001034 png_save_uint_32(buf, x_pixels_per_unit);
1035 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001036 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001037
Andreas Dilger47a0c421997-05-16 02:46:07 -05001038 png_write_chunk(png_ptr, png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001039}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001040#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001041
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001042#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001043/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1044 * or png_convert_from_time_t(), or fill in the structure yourself.
1045 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001046void
Guy Schalnat6d764711995-12-19 03:22:19 -06001047png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001048{
1049 png_byte buf[7];
1050
Andreas Dilger47a0c421997-05-16 02:46:07 -05001051 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001052 if (mod_time->month > 12 || mod_time->month < 1 ||
1053 mod_time->day > 31 || mod_time->day < 1 ||
1054 mod_time->hour > 23 || mod_time->second > 60)
1055 {
1056 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1057 return;
1058 }
1059
Guy Schalnat0d580581995-07-20 02:43:20 -05001060 png_save_uint_16(buf, mod_time->year);
1061 buf[2] = mod_time->month;
1062 buf[3] = mod_time->day;
1063 buf[4] = mod_time->hour;
1064 buf[5] = mod_time->minute;
1065 buf[6] = mod_time->second;
1066
Andreas Dilger47a0c421997-05-16 02:46:07 -05001067 png_write_chunk(png_ptr, png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001068}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001069#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001070
1071/* initializes the row writing capability of libpng */
1072void
Guy Schalnat6d764711995-12-19 03:22:19 -06001073png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001074{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001075 png_size_t buf_size;
1076
1077 png_debug(1, "in png_write_start_row\n");
1078 buf_size = (png_size_t)(((png_ptr->width * png_ptr->usr_channels *
1079 png_ptr->usr_bit_depth + 7) >> 3) + 1);
1080
Guy Schalnat0d580581995-07-20 02:43:20 -05001081 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001082 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001083 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001084
1085 /* set up filtering buffer, if using this filter */
1086 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001087 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001088 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001089 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001090 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001091 }
1092
Andreas Dilger47a0c421997-05-16 02:46:07 -05001093 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001094 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1095 {
1096 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001097 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001098 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001099
1100 if (png_ptr->do_filter & PNG_FILTER_UP)
1101 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001102 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001103 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001104 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001105 }
1106
1107 if (png_ptr->do_filter & PNG_FILTER_AVG)
1108 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001109 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1110 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001111 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001112 }
1113
1114 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1115 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001116 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001117 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001118 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001119 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001120 }
1121
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001122#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001123 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001124 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001125 {
1126 if (!(png_ptr->transformations & PNG_INTERLACE))
1127 {
1128 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1129 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001130 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1131 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001132 }
1133 else
1134 {
1135 png_ptr->num_rows = png_ptr->height;
1136 png_ptr->usr_width = png_ptr->width;
1137 }
1138 }
1139 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001140#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001141 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001142 png_ptr->num_rows = png_ptr->height;
1143 png_ptr->usr_width = png_ptr->width;
1144 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001145 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1146 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001147}
1148
Andreas Dilger47a0c421997-05-16 02:46:07 -05001149/* Internal use only. Called when finished processing a row of data. */
Guy Schalnat0d580581995-07-20 02:43:20 -05001150void
Guy Schalnat6d764711995-12-19 03:22:19 -06001151png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001152{
1153 int ret;
1154
Andreas Dilger47a0c421997-05-16 02:46:07 -05001155 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001156 /* next row */
1157 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001158
Guy Schalnat0d580581995-07-20 02:43:20 -05001159 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001160 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001161 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001162
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001163#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001164 /* if interlaced, go to next pass */
1165 if (png_ptr->interlaced)
1166 {
1167 png_ptr->row_number = 0;
1168 if (png_ptr->transformations & PNG_INTERLACE)
1169 {
1170 png_ptr->pass++;
1171 }
1172 else
1173 {
1174 /* loop until we find a non-zero width or height pass */
1175 do
1176 {
1177 png_ptr->pass++;
1178 if (png_ptr->pass >= 7)
1179 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001180 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001181 png_pass_inc[png_ptr->pass] - 1 -
1182 png_pass_start[png_ptr->pass]) /
1183 png_pass_inc[png_ptr->pass];
1184 png_ptr->num_rows = (png_ptr->height +
1185 png_pass_yinc[png_ptr->pass] - 1 -
1186 png_pass_ystart[png_ptr->pass]) /
1187 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001188 if (png_ptr->transformations & PNG_INTERLACE)
1189 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001190 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1191
1192 }
1193
Guy Schalnate5a37791996-06-05 15:50:50 -05001194 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001195 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001196 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001197 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001198 png_memset(png_ptr->prev_row, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001199 (png_size_t) (((png_uint_32)png_ptr->usr_channels *
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001200 (png_uint_32)png_ptr->usr_bit_depth *
1201 png_ptr->width + 7) >> 3) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001202 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001203 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001204 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001205#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001206
1207 /* if we get here, we've just written the last row, so we need
1208 to flush the compressor */
1209 do
1210 {
1211 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001212 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001213 /* check for an error */
1214 if (ret != Z_OK && ret != Z_STREAM_END)
1215 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001216 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001217 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001218 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001219 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001220 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001221 /* check to see if we need more room */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001222 if (!(png_ptr->zstream.avail_out) && ret == Z_OK)
Guy Schalnat0d580581995-07-20 02:43:20 -05001223 {
1224 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001225 png_ptr->zstream.next_out = png_ptr->zbuf;
1226 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat0d580581995-07-20 02:43:20 -05001227 }
1228 } while (ret != Z_STREAM_END);
1229
1230 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001231 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001232 {
1233 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001234 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001235 }
1236
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001237 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05001238}
1239
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001240#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001241/* Pick out the correct pixels for the interlace pass.
1242 * The basic idea here is to go through the row with a source
1243 * pointer and a destination pointer (sp and dp), and copy the
1244 * correct pixels for the pass. As the row gets compacted,
1245 * sp will always be >= dp, so we should never overwrite anything.
1246 * See the default: case for the easiest code to understand.
1247 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001248void
Guy Schalnat6d764711995-12-19 03:22:19 -06001249png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001250{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001251 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001252 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001253#if defined(PNG_USELESS_TESTS_SUPPORTED)
1254 if (row != NULL && row_info != NULL && pass < 6)
1255#else
1256 if (pass < 6)
1257#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001258 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001259 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001260 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001261 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001262 case 1:
1263 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001264 png_bytep sp;
1265 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001266 int shift;
1267 int d;
1268 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001269 png_uint_32 i;
1270 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001271
1272 dp = row;
1273 d = 0;
1274 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001275 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001276 i += png_pass_inc[pass])
1277 {
1278 sp = row + (png_size_t)(i >> 3);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001279 value = (int)(*sp >> (7 - (int)(i & 7))) & 0x1;
Guy Schalnat0d580581995-07-20 02:43:20 -05001280 d |= (value << shift);
1281
1282 if (shift == 0)
1283 {
1284 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001285 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001286 d = 0;
1287 }
1288 else
1289 shift--;
1290
1291 }
1292 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001293 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001294 break;
1295 }
1296 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001297 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001298 png_bytep sp;
1299 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001300 int shift;
1301 int d;
1302 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001303 png_uint_32 i;
1304 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001305
1306 dp = row;
1307 shift = 6;
1308 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001309 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001310 i += png_pass_inc[pass])
1311 {
1312 sp = row + (png_size_t)(i >> 2);
1313 value = (*sp >> ((3 - (int)(i & 3)) << 1)) & 0x3;
1314 d |= (value << shift);
1315
1316 if (shift == 0)
1317 {
1318 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001319 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001320 d = 0;
1321 }
1322 else
1323 shift -= 2;
1324 }
1325 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001326 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001327 break;
1328 }
1329 case 4:
1330 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001331 png_bytep sp;
1332 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001333 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001334 int d;
1335 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001336 png_uint_32 i;
1337 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001338
1339 dp = row;
1340 shift = 4;
1341 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001342 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001343 i += png_pass_inc[pass])
1344 {
1345 sp = row + (png_size_t)(i >> 1);
1346 value = (*sp >> ((1 - (int)(i & 1)) << 2)) & 0xf;
1347 d |= (value << shift);
1348
1349 if (shift == 0)
1350 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001351 shift = 4;
1352 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001353 d = 0;
1354 }
1355 else
1356 shift -= 4;
1357 }
1358 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001359 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001360 break;
1361 }
1362 default:
1363 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001364 png_bytep sp;
1365 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001366 png_uint_32 i;
1367 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001368 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001369
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001370 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05001371 dp = row;
1372 /* find out how many bytes each pixel takes up */
1373 pixel_bytes = (row_info->pixel_depth >> 3);
1374 /* loop through the row, only looking at the pixels that
1375 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001376 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001377 i += png_pass_inc[pass])
1378 {
1379 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001380 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001381 /* move the pixel */
1382 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001383 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05001384 /* next pixel */
1385 dp += pixel_bytes;
1386 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001387 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001388 }
1389 }
1390 /* set new row width */
1391 row_info->width = (row_info->width +
1392 png_pass_inc[pass] - 1 -
1393 png_pass_start[pass]) /
1394 png_pass_inc[pass];
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001395 row_info->rowbytes = ((row_info->width *
1396 row_info->pixel_depth + 7) >> 3);
Guy Schalnat0d580581995-07-20 02:43:20 -05001397 }
1398}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001399#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001400
Andreas Dilger47a0c421997-05-16 02:46:07 -05001401/* This filters the row, chooses which filter to use, if it has not already
1402 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001403 * chosen filter.
1404 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001405#define PNG_MAXSUM (~((png_uint_32)0) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001406#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001407#define PNG_LOMASK ((png_uint_32)0xffffL)
1408#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Guy Schalnat0d580581995-07-20 02:43:20 -05001409void
Guy Schalnate5a37791996-06-05 15:50:50 -05001410png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05001411{
Guy Schalnate5a37791996-06-05 15:50:50 -05001412 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001413 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001414 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001415 png_uint_32 row_bytes = row_info->rowbytes;
1416#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1417 int num_p_filters = (int)png_ptr->num_prev_filters;
1418#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001419
Andreas Dilger47a0c421997-05-16 02:46:07 -05001420 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001421 /* find out how many bytes offset each pixel is */
1422 bpp = (row_info->pixel_depth + 7) / 8;
Guy Schalnate5a37791996-06-05 15:50:50 -05001423
1424 prev_row = png_ptr->prev_row;
1425 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001426 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05001427
Andreas Dilger47a0c421997-05-16 02:46:07 -05001428 /* The prediction method we use is to find which method provides the
1429 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05001430 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05001431 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001432 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05001433 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001434 * predictive" method (not implemented yet), which does test compressions
1435 * of lines using different filter methods, and then chooses the
1436 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05001437 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001438 *
1439 * GRR 980525: consider also
1440 * (1) minimum sum of absolute differences from running average (i.e.,
1441 * keep running sum of non-absolute differences & count of bytes)
1442 * [track dispersion, too? restart average if dispersion too large?]
1443 * (1b) minimum sum of absolute differences from sliding average, probably
1444 * with window size <= deflate window (usually 32K)
1445 * (2) minimum sum of squared differences from zero or running average
1446 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001447 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001448
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001449
Guy Schalnate5a37791996-06-05 15:50:50 -05001450 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05001451 * that has been chosen, as it doesn't actually do anything to the data.
1452 */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001453 if (filter_to_do & PNG_FILTER_NONE &&
1454 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05001455 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001456 png_bytep rp;
1457 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001458 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001459 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05001460
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001461 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001462 {
1463 v = *rp;
1464 sum += (v < 128) ? v : 256 - v;
1465 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001466
1467#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1468 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1469 {
1470 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001471 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001472 sumlo = sum & PNG_LOMASK;
1473 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
1474
1475 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001476 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001477 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001478 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001479 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001480 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001481 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001482 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001483 PNG_WEIGHT_SHIFT;
1484 }
1485 }
1486
1487 /* Factor in the cost of this filter (this is here for completeness,
1488 * but it makes no sense to have a "cost" for the NONE filter, as
1489 * it has the minimum possible computational cost - none).
1490 */
1491 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
1492 PNG_COST_SHIFT;
1493 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
1494 PNG_COST_SHIFT;
1495
1496 if (sumhi > PNG_HIMASK)
1497 sum = PNG_MAXSUM;
1498 else
1499 sum = (sumhi << PNG_HISHIFT) + sumlo;
1500 }
1501#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001502 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05001503 }
1504
Guy Schalnate5a37791996-06-05 15:50:50 -05001505 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001506 if (filter_to_do == PNG_FILTER_SUB)
1507 /* it's the only filter so no testing is needed */
1508 {
1509 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001510 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001511 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
1512 i++, rp++, dp++)
1513 {
1514 *dp = *rp;
1515 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001516 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001517 i++, rp++, lp++, dp++)
1518 {
1519 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
1520 }
1521 best_row = png_ptr->sub_row;
1522 }
1523
1524 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001525 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001526 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001527 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001528 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001529 int v;
1530
1531#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001532 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05001533 * would reduce the sum of this filter, so that we can do the
1534 * early exit comparison without scaling the sum each time.
1535 */
1536 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1537 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001538 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001539 png_uint_32 lmhi, lmlo;
1540 lmlo = lmins & PNG_LOMASK;
1541 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1542
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001543 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001544 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001545 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001546 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001547 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001548 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001549 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001550 PNG_WEIGHT_SHIFT;
1551 }
1552 }
1553
1554 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1555 PNG_COST_SHIFT;
1556 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1557 PNG_COST_SHIFT;
1558
1559 if (lmhi > PNG_HIMASK)
1560 lmins = PNG_MAXSUM;
1561 else
1562 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1563 }
1564#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001565
Guy Schalnate5a37791996-06-05 15:50:50 -05001566 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
1567 i++, rp++, dp++)
1568 {
1569 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001570
Guy Schalnate5a37791996-06-05 15:50:50 -05001571 sum += (v < 128) ? v : 256 - v;
1572 }
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001573 for (lp = row_buf + 1; i < row_info->rowbytes;
1574 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001575 {
1576 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001577
Guy Schalnate5a37791996-06-05 15:50:50 -05001578 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001579
1580 if (sum > lmins) /* We are already worse, don't continue. */
1581 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001582 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001583
1584#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1585 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1586 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001587 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001588 png_uint_32 sumhi, sumlo;
1589 sumlo = sum & PNG_LOMASK;
1590 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1591
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001592 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001593 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001594 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001595 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001596 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001597 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001598 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001599 PNG_WEIGHT_SHIFT;
1600 }
1601 }
1602
1603 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1604 PNG_COST_SHIFT;
1605 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1606 PNG_COST_SHIFT;
1607
1608 if (sumhi > PNG_HIMASK)
1609 sum = PNG_MAXSUM;
1610 else
1611 sum = (sumhi << PNG_HISHIFT) + sumlo;
1612 }
1613#endif
1614
Guy Schalnate5a37791996-06-05 15:50:50 -05001615 if (sum < mins)
1616 {
1617 mins = sum;
1618 best_row = png_ptr->sub_row;
1619 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001620 }
1621
Guy Schalnate5a37791996-06-05 15:50:50 -05001622 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001623 if (filter_to_do == PNG_FILTER_UP)
1624 {
1625 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001626 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001627
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001628 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001629 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001630 i++, rp++, pp++, dp++)
1631 {
1632 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
1633 }
1634 best_row = png_ptr->up_row;
1635 }
1636
1637 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05001638 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001639 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001640 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001641 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001642 int v;
1643
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001644
Andreas Dilger47a0c421997-05-16 02:46:07 -05001645#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1646 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1647 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001648 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001649 png_uint_32 lmhi, lmlo;
1650 lmlo = lmins & PNG_LOMASK;
1651 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1652
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001653 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001654 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001655 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001656 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001657 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001658 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001659 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001660 PNG_WEIGHT_SHIFT;
1661 }
1662 }
1663
1664 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
1665 PNG_COST_SHIFT;
1666 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
1667 PNG_COST_SHIFT;
1668
1669 if (lmhi > PNG_HIMASK)
1670 lmins = PNG_MAXSUM;
1671 else
1672 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1673 }
1674#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001675
1676 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001677 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001678 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001679 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05001680
1681 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001682
1683 if (sum > lmins) /* We are already worse, don't continue. */
1684 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001685 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001686
1687#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1688 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1689 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001690 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001691 png_uint_32 sumhi, sumlo;
1692 sumlo = sum & PNG_LOMASK;
1693 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1694
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001695 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001696 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001697 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001698 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001699 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001700 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001701 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001702 PNG_WEIGHT_SHIFT;
1703 }
1704 }
1705
1706 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
1707 PNG_COST_SHIFT;
1708 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
1709 PNG_COST_SHIFT;
1710
1711 if (sumhi > PNG_HIMASK)
1712 sum = PNG_MAXSUM;
1713 else
1714 sum = (sumhi << PNG_HISHIFT) + sumlo;
1715 }
1716#endif
1717
Guy Schalnate5a37791996-06-05 15:50:50 -05001718 if (sum < mins)
1719 {
1720 mins = sum;
1721 best_row = png_ptr->up_row;
1722 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001723 }
1724
Guy Schalnate5a37791996-06-05 15:50:50 -05001725 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001726 if (filter_to_do == PNG_FILTER_AVG)
1727 {
1728 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001729 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001730 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001731 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001732 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001733 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001734 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001735 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001736 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001737 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
1738 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001739 }
1740 best_row = png_ptr->avg_row;
1741 }
1742
1743 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001744 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001745 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001746 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001747 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001748 int v;
1749
1750#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1751 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1752 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001753 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001754 png_uint_32 lmhi, lmlo;
1755 lmlo = lmins & PNG_LOMASK;
1756 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1757
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001758 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001759 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001760 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001761 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001762 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001763 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001764 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001765 PNG_WEIGHT_SHIFT;
1766 }
1767 }
1768
1769 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
1770 PNG_COST_SHIFT;
1771 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
1772 PNG_COST_SHIFT;
1773
1774 if (lmhi > PNG_HIMASK)
1775 lmins = PNG_MAXSUM;
1776 else
1777 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1778 }
1779#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001780
1781 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001782 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001783 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001784 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05001785
1786 sum += (v < 128) ? v : 256 - v;
1787 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001788 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001789 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001790 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001791 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05001792
1793 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001794
1795 if (sum > lmins) /* We are already worse, don't continue. */
1796 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001797 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001798
1799#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1800 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1801 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001802 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001803 png_uint_32 sumhi, sumlo;
1804 sumlo = sum & PNG_LOMASK;
1805 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1806
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001807 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001808 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001809 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001810 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001811 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001812 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001813 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001814 PNG_WEIGHT_SHIFT;
1815 }
1816 }
1817
1818 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
1819 PNG_COST_SHIFT;
1820 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
1821 PNG_COST_SHIFT;
1822
1823 if (sumhi > PNG_HIMASK)
1824 sum = PNG_MAXSUM;
1825 else
1826 sum = (sumhi << PNG_HISHIFT) + sumlo;
1827 }
1828#endif
1829
Guy Schalnate5a37791996-06-05 15:50:50 -05001830 if (sum < mins)
1831 {
1832 mins = sum;
1833 best_row = png_ptr->avg_row;
1834 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001835 }
1836
Andreas Dilger47a0c421997-05-16 02:46:07 -05001837 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001838 if (filter_to_do == PNG_FILTER_PAETH)
1839 {
1840 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001841 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001842 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001843 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001844 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001845 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001846 }
1847
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001848 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001849 {
1850 int a, b, c, pa, pb, pc, p;
1851
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001852 b = *pp++;
1853 c = *cp++;
1854 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001855
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001856 p = b - c;
1857 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001858
1859#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001860 pa = abs(p);
1861 pb = abs(pc);
1862 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001863#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001864 pa = p < 0 ? -p : p;
1865 pb = pc < 0 ? -pc : pc;
1866 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001867#endif
1868
1869 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
1870
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001871 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001872 }
1873 best_row = png_ptr->paeth_row;
1874 }
1875
1876 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001877 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001878 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001879 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001880 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001881 int v;
1882
1883#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1884 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1885 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001886 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001887 png_uint_32 lmhi, lmlo;
1888 lmlo = lmins & PNG_LOMASK;
1889 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1890
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001891 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001892 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001893 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001894 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001895 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001896 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001897 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001898 PNG_WEIGHT_SHIFT;
1899 }
1900 }
1901
1902 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1903 PNG_COST_SHIFT;
1904 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1905 PNG_COST_SHIFT;
1906
1907 if (lmhi > PNG_HIMASK)
1908 lmins = PNG_MAXSUM;
1909 else
1910 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1911 }
1912#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001913
1914 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001915 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001916 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001917 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05001918
1919 sum += (v < 128) ? v : 256 - v;
1920 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001921
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001922 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001923 {
1924 int a, b, c, pa, pb, pc, p;
1925
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001926 b = *pp++;
1927 c = *cp++;
1928 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001929
1930#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001931 p = b - c;
1932 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001933#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001934 pa = abs(p);
1935 pb = abs(pc);
1936 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001937#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001938 pa = p < 0 ? -p : p;
1939 pb = pc < 0 ? -pc : pc;
1940 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001941#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001942 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
1943#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001944 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001945 pa = abs(p - a);
1946 pb = abs(p - b);
1947 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05001948 if (pa <= pb && pa <= pc)
1949 p = a;
1950 else if (pb <= pc)
1951 p = b;
1952 else
1953 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001954#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05001955
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001956 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05001957
1958 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001959
1960 if (sum > lmins) /* We are already worse, don't continue. */
1961 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001962 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001963
1964#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1965 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1966 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001967 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001968 png_uint_32 sumhi, sumlo;
1969 sumlo = sum & PNG_LOMASK;
1970 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1971
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001972 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001973 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001974 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001975 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001976 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001977 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001978 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001979 PNG_WEIGHT_SHIFT;
1980 }
1981 }
1982
1983 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1984 PNG_COST_SHIFT;
1985 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1986 PNG_COST_SHIFT;
1987
1988 if (sumhi > PNG_HIMASK)
1989 sum = PNG_MAXSUM;
1990 else
1991 sum = (sumhi << PNG_HISHIFT) + sumlo;
1992 }
1993#endif
1994
Guy Schalnate5a37791996-06-05 15:50:50 -05001995 if (sum < mins)
1996 {
1997 best_row = png_ptr->paeth_row;
1998 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001999 }
2000
Andreas Dilger47a0c421997-05-16 02:46:07 -05002001 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002002
Guy Schalnate5a37791996-06-05 15:50:50 -05002003 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002004
2005#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2006 /* Save the type of filter we picked this time for future calculations */
2007 if (png_ptr->num_prev_filters > 0)
2008 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002009 int j;
2010 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002011 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002012 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002013 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002014 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002015 }
2016#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002017}
2018
2019
Andreas Dilger47a0c421997-05-16 02:46:07 -05002020/* Do the actual writing of a previously filtered row. */
Guy Schalnate5a37791996-06-05 15:50:50 -05002021void
2022png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2023{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002024 png_debug(1, "in png_write_filtered_row\n");
2025 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002026 /* set up the zlib input buffer */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002027 png_ptr->zstream.next_in = filtered_row;
2028 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002029 /* repeat until we have compressed all the data */
2030 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002031 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002032 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002033
Guy Schalnate5a37791996-06-05 15:50:50 -05002034 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002035 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002036 /* check for compression errors */
2037 if (ret != Z_OK)
2038 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002039 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002040 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002041 else
2042 png_error(png_ptr, "zlib error");
2043 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002044
Guy Schalnate5a37791996-06-05 15:50:50 -05002045 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002046 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002047 {
2048 /* write the IDAT and reset the zlib output buffer */
2049 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002050 png_ptr->zstream.next_out = png_ptr->zbuf;
2051 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002052 }
2053 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002054 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002055
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002056 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002057 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002058 {
2059 png_bytep tptr;
2060
2061 tptr = png_ptr->prev_row;
2062 png_ptr->prev_row = png_ptr->row_buf;
2063 png_ptr->row_buf = tptr;
2064 }
2065
Guy Schalnate5a37791996-06-05 15:50:50 -05002066 /* finish row - updates counters and flushes zlib if last row */
2067 png_write_finish_row(png_ptr);
2068
2069#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2070 png_ptr->flush_rows++;
2071
2072 if (png_ptr->flush_dist > 0 &&
2073 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002074 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002075 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002076 }
Guy Schalnate5a37791996-06-05 15:50:50 -05002077#endif /* PNG_WRITE_FLUSH_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05002078}