blob: a34201e3dbc6e7dbdd33f59d110689d78a9cd65a [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-Pehrson5379b241999-11-27 10:22:33 -06004 * libpng 1.0.5c - November 27, 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{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600130 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600131 /* write the rest of the 8 byte signature */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600132 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
Andreas Dilger47a0c421997-05-16 02:46:07 -0500133 (png_size_t)8 - png_ptr->sig_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500134}
135
136/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600137 * information. Note that the rest of this code depends upon this
138 * information being correct.
139 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500140void
Guy Schalnat6d764711995-12-19 03:22:19 -0600141png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500142 int bit_depth, int color_type, int compression_type, int filter_type,
143 int interlace_type)
144{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600145 const png_byte png_IHDR[5] = { 73, 72, 68, 82, '\0'};
Guy Schalnat0d580581995-07-20 02:43:20 -0500146 png_byte buf[13]; /* buffer to store the IHDR info */
147
Andreas Dilger47a0c421997-05-16 02:46:07 -0500148 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500149 /* Check that we have valid input data from the application info */
150 switch (color_type)
151 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500152 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500153 switch (bit_depth)
154 {
155 case 1:
156 case 2:
157 case 4:
158 case 8:
159 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500160 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500161 }
162 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500163 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500164 if (bit_depth != 8 && bit_depth != 16)
165 png_error(png_ptr, "Invalid bit depth for RGB image");
166 png_ptr->channels = 3;
167 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500168 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500169 switch (bit_depth)
170 {
171 case 1:
172 case 2:
173 case 4:
174 case 8: png_ptr->channels = 1; break;
175 default: png_error(png_ptr, "Invalid bit depth for paletted image");
176 }
177 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500178 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500179 if (bit_depth != 8 && bit_depth != 16)
180 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
181 png_ptr->channels = 2;
182 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500183 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500184 if (bit_depth != 8 && bit_depth != 16)
185 png_error(png_ptr, "Invalid bit depth for RGBA image");
186 png_ptr->channels = 4;
187 break;
188 default:
189 png_error(png_ptr, "Invalid image color type specified");
190 }
191
Andreas Dilger47a0c421997-05-16 02:46:07 -0500192 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500193 {
194 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500195 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500196 }
197
Andreas Dilger47a0c421997-05-16 02:46:07 -0500198 if (filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500199 {
200 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500201 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500202 }
203
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600204#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500205 if (interlace_type != PNG_INTERLACE_NONE &&
206 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500207 {
208 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500209 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500210 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600211#else
212 interlace_type=PNG_INTERLACE_NONE;
213#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500214
215 /* save off the relevent information */
216 png_ptr->bit_depth = (png_byte)bit_depth;
217 png_ptr->color_type = (png_byte)color_type;
218 png_ptr->interlaced = (png_byte)interlace_type;
219 png_ptr->width = width;
220 png_ptr->height = height;
221
222 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500223 png_ptr->rowbytes = ((width * (png_size_t)png_ptr->pixel_depth + 7) >> 3);
Guy Schalnate5a37791996-06-05 15:50:50 -0500224 /* set the usr info, so any transformations can modify it */
225 png_ptr->usr_width = png_ptr->width;
226 png_ptr->usr_bit_depth = png_ptr->bit_depth;
227 png_ptr->usr_channels = png_ptr->channels;
228
Guy Schalnat0d580581995-07-20 02:43:20 -0500229 /* pack the header information into the buffer */
230 png_save_uint_32(buf, width);
231 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600232 buf[8] = (png_byte)bit_depth;
233 buf[9] = (png_byte)color_type;
234 buf[10] = (png_byte)compression_type;
235 buf[11] = (png_byte)filter_type;
236 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500237
238 /* write the chunk */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600239 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500240
Andreas Dilger47a0c421997-05-16 02:46:07 -0500241 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600242 png_ptr->zstream.zalloc = png_zalloc;
243 png_ptr->zstream.zfree = png_zfree;
244 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500245 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500246 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500247 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
248 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500249 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500250 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500251 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500252 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500253 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500254 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500255 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500256 png_ptr->zlib_strategy = Z_FILTERED;
257 else
258 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
259 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500260 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500261 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500262 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500263 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500264 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500265 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500266 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500267 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600268 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500269 png_ptr->zlib_method, png_ptr->zlib_window_bits,
270 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600271 png_ptr->zstream.next_out = png_ptr->zbuf;
272 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500273
Guy Schalnate5a37791996-06-05 15:50:50 -0500274 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500275}
276
277/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500278 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600279 * structure.
280 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500281void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500282png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500283{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600284 const png_byte png_PLTE[5] = { 80, 76, 84, 69, '\0'};
Andreas Dilger47a0c421997-05-16 02:46:07 -0500285 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600286 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500287 png_byte buf[3];
288
Andreas Dilger47a0c421997-05-16 02:46:07 -0500289 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500290 if ((
291#ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
292 !png_ptr->empty_plte_permitted &&
293#endif
294 num_pal == 0) || num_pal > 256)
295 {
296 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
297 {
298 png_error(png_ptr, "Invalid number of colors in palette");
299 }
300 else
301 {
302 png_warning(png_ptr, "Invalid number of colors in palette");
303 return;
304 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500305 }
306
Andreas Dilger47a0c421997-05-16 02:46:07 -0500307 png_ptr->num_palette = (png_uint_16)num_pal;
308 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500309
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600310 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500311 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500312 {
313 buf[0] = pal_ptr->red;
314 buf[1] = pal_ptr->green;
315 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500316 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500317 }
318 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500319 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500320}
321
322/* write an IDAT chunk */
323void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500324png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500325{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600326 const png_byte png_IDAT[5] = { 73, 68, 65, 84, '\0'};
Andreas Dilger47a0c421997-05-16 02:46:07 -0500327 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600328 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500329 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500330}
331
332/* write an IEND chunk */
333void
Guy Schalnat6d764711995-12-19 03:22:19 -0600334png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500335{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600336 const png_byte png_IEND[5] = { 73, 69, 78, 68, '\0'};
Andreas Dilger47a0c421997-05-16 02:46:07 -0500337 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600338 png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL, (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600339 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500340}
341
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500342#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500343/* write a gAMA chunk */
344void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500345png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500346{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600347 const png_byte png_gAMA[5] = {103, 65, 77, 65, '\0'};
Guy Schalnat0d580581995-07-20 02:43:20 -0500348 png_uint_32 igamma;
349 png_byte buf[4];
350
Andreas Dilger47a0c421997-05-16 02:46:07 -0500351 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -0600352 /* file_gamma is saved in 1/1000000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600353 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500354 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600355 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500356}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500357#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500358
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600359#if defined(PNG_WRITE_sRGB_SUPPORTED)
360/* write a sRGB chunk */
361void
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600362png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600363{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600364 const png_byte png_sRGB[5] = {115, 82, 71, 66, '\0'};
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600365 png_byte buf[1];
366
367 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600368 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600369 png_warning(png_ptr,
370 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600371 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600372 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600373}
374#endif
375
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500376#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500377/* write the sBIT chunk */
378void
Guy Schalnat6d764711995-12-19 03:22:19 -0600379png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500380{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600381 const png_byte png_sBIT[5] = {115, 66, 73, 84, '\0'};
Guy Schalnat0d580581995-07-20 02:43:20 -0500382 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500383 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500384
Andreas Dilger47a0c421997-05-16 02:46:07 -0500385 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600386 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500387 if (color_type & PNG_COLOR_MASK_COLOR)
388 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600389 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500390
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500391 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
392 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600393 if (sbit->red == 0 || sbit->red > maxbits ||
394 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500395 sbit->blue == 0 || sbit->blue > maxbits)
396 {
397 png_warning(png_ptr, "Invalid sBIT depth specified");
398 return;
399 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500400 buf[0] = sbit->red;
401 buf[1] = sbit->green;
402 buf[2] = sbit->blue;
403 size = 3;
404 }
405 else
406 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500407 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
408 {
409 png_warning(png_ptr, "Invalid sBIT depth specified");
410 return;
411 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500412 buf[0] = sbit->gray;
413 size = 1;
414 }
415
416 if (color_type & PNG_COLOR_MASK_ALPHA)
417 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500418 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
419 {
420 png_warning(png_ptr, "Invalid sBIT depth specified");
421 return;
422 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500423 buf[size++] = sbit->alpha;
424 }
425
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600426 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500427}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500428#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500429
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500430#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500431/* write the cHRM chunk */
432void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500433png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600434 double red_x, double red_y, double green_x, double green_y,
435 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500436{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600437 const png_byte png_cHRM[5] = { 99, 72, 82, 77, '\0'};
Guy Schalnat0d580581995-07-20 02:43:20 -0500438 png_uint_32 itemp;
439 png_byte buf[32];
440
Andreas Dilger47a0c421997-05-16 02:46:07 -0500441 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -0600442 /* each value is saved int 1/1000000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500443 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
444 white_x + white_y > 1.0)
445 {
446 png_warning(png_ptr, "Invalid cHRM white point specified");
447 return;
448 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600449 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500450 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600451 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500452 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500453
454 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
455 red_x + red_y > 1.0)
456 {
457 png_warning(png_ptr, "Invalid cHRM red point specified");
458 return;
459 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600460 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500461 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600462 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500463 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500464
465 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
466 green_x + green_y > 1.0)
467 {
468 png_warning(png_ptr, "Invalid cHRM green point specified");
469 return;
470 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600471 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500472 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600473 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500474 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500475
476 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
477 blue_x + blue_y > 1.0)
478 {
479 png_warning(png_ptr, "Invalid cHRM blue point specified");
480 return;
481 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600482 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500483 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600484 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500485 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500486
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600487 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500488}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500489#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500490
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500491#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500492/* write the tRNS chunk */
493void
Guy Schalnat6d764711995-12-19 03:22:19 -0600494png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -0500495 int num_trans, int color_type)
496{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600497 const png_byte png_tRNS[5] = {116, 82, 78, 83, '\0'};
Guy Schalnat0d580581995-07-20 02:43:20 -0500498 png_byte buf[6];
499
Andreas Dilger47a0c421997-05-16 02:46:07 -0500500 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500501 if (color_type == PNG_COLOR_TYPE_PALETTE)
502 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600503 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500504 {
505 png_warning(png_ptr,"Invalid number of transparent colors specified");
506 return;
507 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500508 /* write the chunk out as it is */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600509 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -0500510 }
511 else if (color_type == PNG_COLOR_TYPE_GRAY)
512 {
513 /* one 16 bit value */
514 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600515 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500516 }
517 else if (color_type == PNG_COLOR_TYPE_RGB)
518 {
519 /* three 16 bit values */
520 png_save_uint_16(buf, tran->red);
521 png_save_uint_16(buf + 2, tran->green);
522 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600523 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500524 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500525 else
526 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600527 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -0500528 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500529}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500530#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500531
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500532#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500533/* write the background chunk */
534void
Guy Schalnat6d764711995-12-19 03:22:19 -0600535png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500536{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600537 const png_byte png_bKGD[5] = { 98, 75, 71, 68, '\0'};
Guy Schalnat0d580581995-07-20 02:43:20 -0500538 png_byte buf[6];
539
Andreas Dilger47a0c421997-05-16 02:46:07 -0500540 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500541 if (color_type == PNG_COLOR_TYPE_PALETTE)
542 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500543 if (
544#ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
545 (!png_ptr->empty_plte_permitted ||
546 (png_ptr->empty_plte_permitted && png_ptr->num_palette)) &&
547#endif
548 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500549 {
550 png_warning(png_ptr, "Invalid background palette index");
551 return;
552 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500553 buf[0] = back->index;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600554 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500555 }
556 else if (color_type & PNG_COLOR_MASK_COLOR)
557 {
558 png_save_uint_16(buf, back->red);
559 png_save_uint_16(buf + 2, back->green);
560 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600561 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500562 }
563 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600564 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500565 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600566 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500567 }
568}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500569#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500570
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500571#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500572/* write the histogram */
573void
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600574png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -0500575{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600576 const png_byte png_hIST[5] = {104, 73, 83, 84, '\0'};
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600577 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500578 png_byte buf[3];
579
Andreas Dilger47a0c421997-05-16 02:46:07 -0500580 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600581 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500582 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500583 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
584 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500585 png_warning(png_ptr, "Invalid number of histogram entries specified");
586 return;
587 }
588
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600589 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500590 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500591 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600592 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500593 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500594 }
595 png_write_chunk_end(png_ptr);
596}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500597#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500598
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500599#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED) || \
600 defined(PNG_WRITE_pCAL_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600601/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
602 * and if invalid, correct the keyword rather than discarding the entire
603 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
604 * length, forbids leading or trailing whitespace, multiple internal spaces,
605 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -0500606 *
607 * The new_key is allocated to hold the corrected keyword and must be freed
608 * by the calling routine. This avoids problems with trying to write to
609 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600610 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500611png_size_t
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600612png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600613{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500614 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600615 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600616 int kflag;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600617
Andreas Dilger47a0c421997-05-16 02:46:07 -0500618 png_debug(1, "in png_check_keyword\n");
619 *new_key = NULL;
620
621 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600622 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600623 png_chunk_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600624 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600625 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600626
Andreas Dilger47a0c421997-05-16 02:46:07 -0500627 png_debug1(2, "Keyword to be checked is '%s'\n", key);
628
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600629 *new_key = (png_charp)png_malloc(png_ptr, (png_uint_32)(key_len + 1));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600630
631 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500632 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600633 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600634 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500635 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600636#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500637 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600638
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600639 sprintf(msg, "invalid keyword character 0x%02X", *kp);
640 png_chunk_warning(png_ptr, msg);
641#else
642 png_chunk_warning(png_ptr, "invalid character in keyword");
643#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500644 *dp = ' ';
645 }
646 else
647 {
648 *dp = *kp;
649 }
650 }
651 *dp = '\0';
652
653 /* Remove any trailing white space. */
654 kp = *new_key + key_len - 1;
655 if (*kp == ' ')
656 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600657 png_chunk_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500658
659 while (*kp == ' ')
660 {
661 *(kp--) = '\0';
662 key_len--;
663 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600664 }
665
666 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500667 kp = *new_key;
668 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600669 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600670 png_chunk_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500671
672 while (*kp == ' ')
673 {
674 kp++;
675 key_len--;
676 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600677 }
678
Andreas Dilger47a0c421997-05-16 02:46:07 -0500679 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600680
Andreas Dilger47a0c421997-05-16 02:46:07 -0500681 /* Remove multiple internal spaces. */
682 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600683 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500684 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600685 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500686 *(dp++) = *kp;
687 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600688 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500689 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600690 {
691 key_len--;
692 }
693 else
694 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500695 *(dp++) = *kp;
696 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600697 }
698 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500699 *dp = '\0';
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600700
701 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500702 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500703 png_free(png_ptr, *new_key);
704 *new_key=NULL;
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600705 png_chunk_warning(png_ptr, "zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500706 }
707
708 if (key_len > 79)
709 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600710 png_chunk_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500711 new_key[79] = '\0';
712 key_len = 79;
713 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600714
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600715 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600716}
717#endif
718
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500719#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500720/* write a tEXt chunk */
721void
Guy Schalnat6d764711995-12-19 03:22:19 -0600722png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500723 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -0500724{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600725 const png_byte png_tEXt[5] = {116, 69, 88, 116, '\0'};
Andreas Dilger47a0c421997-05-16 02:46:07 -0500726 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600727 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -0500728
Andreas Dilger47a0c421997-05-16 02:46:07 -0500729 png_debug(1, "in png_write_tEXt\n");
730 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
731 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600732 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -0500733 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500734 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500735
Andreas Dilger47a0c421997-05-16 02:46:07 -0500736 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600737 text_len = 0;
738
739 /* make sure we include the 0 after the key */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600740 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt, (png_uint_32)key_len+text_len+1);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500741 /*
742 * We leave it to the application to meet PNG-1.0 requirements on the
743 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
744 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
745 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600746 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600747 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500748 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600749
Guy Schalnat0d580581995-07-20 02:43:20 -0500750 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500751 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -0500752}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500753#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500754
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500755#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500756/* write a compressed text chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -0500757void
Guy Schalnat6d764711995-12-19 03:22:19 -0600758png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500759 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -0500760{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600761 const png_byte png_zTXt[5] = {122, 84, 88, 116, '\0'};
Andreas Dilger47a0c421997-05-16 02:46:07 -0500762 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -0500763 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600764 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -0500765 int i, ret;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600766 png_charpp output_ptr = NULL; /* array of pointers to output */
Guy Schalnat0d580581995-07-20 02:43:20 -0500767 int num_output_ptr = 0; /* number of output pointers used */
768 int max_output_ptr = 0; /* size of output_ptr */
769
Andreas Dilger47a0c421997-05-16 02:46:07 -0500770 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600771
Andreas Dilger47a0c421997-05-16 02:46:07 -0500772 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -0500773 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600774 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500775 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500776 }
777
Andreas Dilger47a0c421997-05-16 02:46:07 -0500778 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
779 {
780 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
781 png_free(png_ptr, new_key);
782 return;
783 }
784
785 png_free(png_ptr, new_key);
786
787 if (compression >= PNG_TEXT_COMPRESSION_LAST)
788 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600789#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500790 char msg[50];
791 sprintf(msg, "Unknown zTXt compression type %d", compression);
792 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600793#else
794 png_warning(png_ptr, "Unknown zTXt compression type");
795#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500796 compression = PNG_TEXT_COMPRESSION_zTXt;
797 }
798
799 /* We can't write the chunk until we find out how much data we have,
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500800 * which means we need to run the compressor first and save the
Andreas Dilger47a0c421997-05-16 02:46:07 -0500801 * output. This shouldn't be a problem, as the vast majority of
802 * comments should be reasonable, but we will set up an array of
803 * malloc'd pointers to be sure.
804 *
805 * If we knew the application was well behaved, we could simplify this
806 * greatly by assuming we can always malloc an output buffer large
807 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
808 * and malloc this directly. The only time this would be a bad idea is
809 * if we can't malloc more than 64K and we have 64K of random input
810 * data, or if the input string is incredibly large (although this
811 * wouldn't cause a failure, just a slowdown due to swapping).
812 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500813
814 /* set up the compression buffers */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600815 png_ptr->zstream.avail_in = (uInt)text_len;
816 png_ptr->zstream.next_in = (Bytef *)text;
817 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
818 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500819
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600820 /* this is the same compression loop as in png_write_row() */
Guy Schalnat0d580581995-07-20 02:43:20 -0500821 do
822 {
823 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600824 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnat0d580581995-07-20 02:43:20 -0500825 if (ret != Z_OK)
826 {
827 /* error */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500828 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600829 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -0500830 else
Guy Schalnat6d764711995-12-19 03:22:19 -0600831 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -0500832 }
833 /* check to see if we need more room */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600834 if (!png_ptr->zstream.avail_out && png_ptr->zstream.avail_in)
Guy Schalnat0d580581995-07-20 02:43:20 -0500835 {
836 /* make sure the output array has room */
837 if (num_output_ptr >= max_output_ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600838 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500839 int old_max;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500840
841 old_max = max_output_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500842 max_output_ptr = num_output_ptr + 4;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500843 if (output_ptr != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600844 {
845 png_charpp old_ptr;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600846
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600847 old_ptr = output_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600848 output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600849 (png_uint_32)(max_output_ptr * sizeof (png_charpp)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500850 png_memcpy(output_ptr, old_ptr, old_max * sizeof (png_charp));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600851 png_free(png_ptr, old_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600852 }
853 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600854 output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600855 (png_uint_32)(max_output_ptr * sizeof (png_charp)));
Guy Schalnat0d580581995-07-20 02:43:20 -0500856 }
857
858 /* save the data */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600859 output_ptr[num_output_ptr] = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600860 (png_uint_32)png_ptr->zbuf_size);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500861 png_memcpy(output_ptr[num_output_ptr], png_ptr->zbuf,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500862 png_ptr->zbuf_size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500863 num_output_ptr++;
864
865 /* and reset the buffer */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600866 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
867 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500868 }
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500869 /* continue until we don't have any more to compress */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600870 } while (png_ptr->zstream.avail_in);
Guy Schalnat0d580581995-07-20 02:43:20 -0500871
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600872 /* finish the compression */
Guy Schalnat0d580581995-07-20 02:43:20 -0500873 do
874 {
875 /* tell zlib we are finished */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600876 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -0500877 if (ret != Z_OK && ret != Z_STREAM_END)
878 {
879 /* we got an error */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500880 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600881 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -0500882 else
Guy Schalnat6d764711995-12-19 03:22:19 -0600883 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -0500884 }
885
886 /* check to see if we need more room */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500887 if (!(png_ptr->zstream.avail_out) && ret == Z_OK)
Guy Schalnat0d580581995-07-20 02:43:20 -0500888 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600889 /* check to make sure our output array has room */
890 if (num_output_ptr >= max_output_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500891 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500892 int old_max;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500893
894 old_max = max_output_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500895 max_output_ptr = num_output_ptr + 4;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500896 if (output_ptr != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600897 {
898 png_charpp old_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500899
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600900 old_ptr = output_ptr;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500901 /* This could be optimized to realloc() */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600902 output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600903 (png_uint_32)(max_output_ptr * sizeof (png_charpp)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500904 png_memcpy(output_ptr, old_ptr, old_max * sizeof (png_charp));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600905 png_free(png_ptr, old_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600906 }
907 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600908 output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600909 (png_uint_32)(max_output_ptr * sizeof (png_charp)));
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600910 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600911
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600912 /* save off the data */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600913 output_ptr[num_output_ptr] = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600914 (png_uint_32)png_ptr->zbuf_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600915 png_memcpy(output_ptr[num_output_ptr], png_ptr->zbuf,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500916 png_ptr->zbuf_size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500917 num_output_ptr++;
918
919 /* and reset the buffer pointers */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600920 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
921 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500922 }
923 } while (ret != Z_STREAM_END);
924
925 /* text length is number of buffers plus last buffer */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600926 text_len = png_ptr->zbuf_size * num_output_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600927 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500928 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
Guy Schalnat0d580581995-07-20 02:43:20 -0500929
930 /* write start of chunk */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600931 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)(key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -0500932 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500933 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600934 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -0500935 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500936 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500937
938 /* write saved output buffers, if any */
939 for (i = 0; i < num_output_ptr; i++)
940 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500941 png_write_chunk_data(png_ptr,(png_bytep)output_ptr[i],png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600942 png_free(png_ptr, output_ptr[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500943 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500944 if (max_output_ptr != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600945 png_free(png_ptr, output_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500946 /* write anything left in zbuf */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500947 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -0500948 png_write_chunk_data(png_ptr, png_ptr->zbuf,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600949 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -0500950 /* close the chunk */
951 png_write_chunk_end(png_ptr);
952
953 /* reset zlib for another zTXt or the image data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600954 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -0500955}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500956#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500957
Andreas Dilger47a0c421997-05-16 02:46:07 -0500958
959#if defined(PNG_WRITE_oFFs_SUPPORTED)
960/* write the oFFs chunk */
961void
962png_write_oFFs(png_structp png_ptr, png_uint_32 x_offset,
963 png_uint_32 y_offset,
964 int unit_type)
965{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600966 const png_byte png_oFFs[5] = {111, 70, 70, 115, '\0'};
Andreas Dilger47a0c421997-05-16 02:46:07 -0500967 png_byte buf[9];
968
969 png_debug(1, "in png_write_oFFs\n");
970 if (unit_type >= PNG_OFFSET_LAST)
971 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
972
973 png_save_uint_32(buf, x_offset);
974 png_save_uint_32(buf + 4, y_offset);
975 buf[8] = (png_byte)unit_type;
976
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600977 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500978}
979#endif
980
981#if defined(PNG_WRITE_pCAL_SUPPORTED)
982/* write the pCAL chunk (png-scivis-19970203) */
983void
984png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
985 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
986{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600987 const png_byte png_pCAL[5] = {112, 67, 65, 76, '\0'};
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600988 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500989 png_uint_32p params_len;
990 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600991 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500992 int i;
993
994 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
995 if (type >= PNG_EQUATION_LAST)
996 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
997
998 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
999 png_debug1(3, "pCAL purpose length = %d\n", purpose_len);
1000 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
1001 png_debug1(3, "pCAL units length = %d\n", units_len);
1002 total_len = purpose_len + units_len + 10;
1003
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001004 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
1005 *sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001006
1007 /* Find the length of each parameter, making sure we don't count the
1008 null terminator for the last parameter. */
1009 for (i = 0; i < nparams; i++)
1010 {
1011 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
1012 png_debug2(3, "pCAL parameter %d length = %d\n", i, params_len[i]);
1013 total_len += (png_size_t)params_len[i];
1014 }
1015
1016 png_debug1(3, "pCAL total length = %d\n", total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001017 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001018 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001019 png_save_int_32(buf, X0);
1020 png_save_int_32(buf + 4, X1);
1021 buf[8] = (png_byte)type;
1022 buf[9] = (png_byte)nparams;
1023 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1024 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1025
1026 png_free(png_ptr, new_purpose);
1027
1028 for (i = 0; i < nparams; i++)
1029 {
1030 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1031 (png_size_t)params_len[i]);
1032 }
1033
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001034 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001035 png_write_chunk_end(png_ptr);
1036}
1037#endif
1038
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001039#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001040/* write the pHYs chunk */
1041void
Guy Schalnat6d764711995-12-19 03:22:19 -06001042png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001043 png_uint_32 y_pixels_per_unit,
1044 int unit_type)
1045{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001046 const png_byte png_pHYs[5] = {112, 72, 89, 115, '\0'};
Guy Schalnat0d580581995-07-20 02:43:20 -05001047 png_byte buf[9];
1048
Andreas Dilger47a0c421997-05-16 02:46:07 -05001049 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001050 if (unit_type >= PNG_RESOLUTION_LAST)
1051 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1052
Guy Schalnat0d580581995-07-20 02:43:20 -05001053 png_save_uint_32(buf, x_pixels_per_unit);
1054 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001055 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001056
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001057 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001058}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001059#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001060
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001061#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001062/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1063 * or png_convert_from_time_t(), or fill in the structure yourself.
1064 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001065void
Guy Schalnat6d764711995-12-19 03:22:19 -06001066png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001067{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001068 const png_byte png_tIME[5] = {116, 73, 77, 69, '\0'};
Guy Schalnat0d580581995-07-20 02:43:20 -05001069 png_byte buf[7];
1070
Andreas Dilger47a0c421997-05-16 02:46:07 -05001071 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001072 if (mod_time->month > 12 || mod_time->month < 1 ||
1073 mod_time->day > 31 || mod_time->day < 1 ||
1074 mod_time->hour > 23 || mod_time->second > 60)
1075 {
1076 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1077 return;
1078 }
1079
Guy Schalnat0d580581995-07-20 02:43:20 -05001080 png_save_uint_16(buf, mod_time->year);
1081 buf[2] = mod_time->month;
1082 buf[3] = mod_time->day;
1083 buf[4] = mod_time->hour;
1084 buf[5] = mod_time->minute;
1085 buf[6] = mod_time->second;
1086
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001087 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001088}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001089#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001090
1091/* initializes the row writing capability of libpng */
1092void
Guy Schalnat6d764711995-12-19 03:22:19 -06001093png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001094{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001095 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1096
1097 /* start of interlace block */
1098 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1099
1100 /* offset to next interlace block */
1101 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1102
1103 /* start of interlace block in the y direction */
1104 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
1105
1106 /* offset to next interlace block in the y direction */
1107 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
1108
Andreas Dilger47a0c421997-05-16 02:46:07 -05001109 png_size_t buf_size;
1110
1111 png_debug(1, "in png_write_start_row\n");
1112 buf_size = (png_size_t)(((png_ptr->width * png_ptr->usr_channels *
1113 png_ptr->usr_bit_depth + 7) >> 3) + 1);
1114
Guy Schalnat0d580581995-07-20 02:43:20 -05001115 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001116 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001117 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001118
1119 /* set up filtering buffer, if using this filter */
1120 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001121 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001122 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001123 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001124 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001125 }
1126
Andreas Dilger47a0c421997-05-16 02:46:07 -05001127 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001128 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1129 {
1130 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001131 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001132 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001133
1134 if (png_ptr->do_filter & PNG_FILTER_UP)
1135 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001136 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001137 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001138 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001139 }
1140
1141 if (png_ptr->do_filter & PNG_FILTER_AVG)
1142 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001143 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1144 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001145 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001146 }
1147
1148 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1149 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001150 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001151 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001152 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001153 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001154 }
1155
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001156#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001157 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001158 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001159 {
1160 if (!(png_ptr->transformations & PNG_INTERLACE))
1161 {
1162 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1163 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001164 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1165 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001166 }
1167 else
1168 {
1169 png_ptr->num_rows = png_ptr->height;
1170 png_ptr->usr_width = png_ptr->width;
1171 }
1172 }
1173 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001174#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001175 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001176 png_ptr->num_rows = png_ptr->height;
1177 png_ptr->usr_width = png_ptr->width;
1178 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001179 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1180 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001181}
1182
Andreas Dilger47a0c421997-05-16 02:46:07 -05001183/* Internal use only. Called when finished processing a row of data. */
Guy Schalnat0d580581995-07-20 02:43:20 -05001184void
Guy Schalnat6d764711995-12-19 03:22:19 -06001185png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001186{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001187 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1188
1189 /* start of interlace block */
1190 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1191
1192 /* offset to next interlace block */
1193 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1194
1195 /* start of interlace block in the y direction */
1196 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
1197
1198 /* offset to next interlace block in the y direction */
1199 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
1200
Guy Schalnat0d580581995-07-20 02:43:20 -05001201 int ret;
1202
Andreas Dilger47a0c421997-05-16 02:46:07 -05001203 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001204 /* next row */
1205 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001206
Guy Schalnat0d580581995-07-20 02:43:20 -05001207 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001208 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001209 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001210
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001211#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001212 /* if interlaced, go to next pass */
1213 if (png_ptr->interlaced)
1214 {
1215 png_ptr->row_number = 0;
1216 if (png_ptr->transformations & PNG_INTERLACE)
1217 {
1218 png_ptr->pass++;
1219 }
1220 else
1221 {
1222 /* loop until we find a non-zero width or height pass */
1223 do
1224 {
1225 png_ptr->pass++;
1226 if (png_ptr->pass >= 7)
1227 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001228 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001229 png_pass_inc[png_ptr->pass] - 1 -
1230 png_pass_start[png_ptr->pass]) /
1231 png_pass_inc[png_ptr->pass];
1232 png_ptr->num_rows = (png_ptr->height +
1233 png_pass_yinc[png_ptr->pass] - 1 -
1234 png_pass_ystart[png_ptr->pass]) /
1235 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001236 if (png_ptr->transformations & PNG_INTERLACE)
1237 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001238 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1239
1240 }
1241
Guy Schalnate5a37791996-06-05 15:50:50 -05001242 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001243 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001244 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001245 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001246 png_memset(png_ptr->prev_row, 0,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001247 (png_size_t) (((png_uint_32)png_ptr->usr_channels *
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001248 (png_uint_32)png_ptr->usr_bit_depth *
1249 png_ptr->width + 7) >> 3) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001250 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001251 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001252 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001253#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001254
1255 /* if we get here, we've just written the last row, so we need
1256 to flush the compressor */
1257 do
1258 {
1259 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001260 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001261 /* check for an error */
1262 if (ret != Z_OK && ret != Z_STREAM_END)
1263 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001264 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001265 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001266 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001267 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001268 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001269 /* check to see if we need more room */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001270 if (!(png_ptr->zstream.avail_out) && ret == Z_OK)
Guy Schalnat0d580581995-07-20 02:43:20 -05001271 {
1272 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001273 png_ptr->zstream.next_out = png_ptr->zbuf;
1274 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat0d580581995-07-20 02:43:20 -05001275 }
1276 } while (ret != Z_STREAM_END);
1277
1278 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001279 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001280 {
1281 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001282 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001283 }
1284
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001285 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05001286}
1287
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001288#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001289/* Pick out the correct pixels for the interlace pass.
1290 * The basic idea here is to go through the row with a source
1291 * pointer and a destination pointer (sp and dp), and copy the
1292 * correct pixels for the pass. As the row gets compacted,
1293 * sp will always be >= dp, so we should never overwrite anything.
1294 * See the default: case for the easiest code to understand.
1295 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001296void
Guy Schalnat6d764711995-12-19 03:22:19 -06001297png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001298{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001299 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1300
1301 /* start of interlace block */
1302 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1303
1304 /* offset to next interlace block */
1305 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1306
Andreas Dilger47a0c421997-05-16 02:46:07 -05001307 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001308 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001309#if defined(PNG_USELESS_TESTS_SUPPORTED)
1310 if (row != NULL && row_info != NULL && pass < 6)
1311#else
1312 if (pass < 6)
1313#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001314 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001315 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001316 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001317 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001318 case 1:
1319 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001320 png_bytep sp;
1321 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001322 int shift;
1323 int d;
1324 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001325 png_uint_32 i;
1326 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001327
1328 dp = row;
1329 d = 0;
1330 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001331 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001332 i += png_pass_inc[pass])
1333 {
1334 sp = row + (png_size_t)(i >> 3);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001335 value = (int)(*sp >> (7 - (int)(i & 7))) & 0x1;
Guy Schalnat0d580581995-07-20 02:43:20 -05001336 d |= (value << shift);
1337
1338 if (shift == 0)
1339 {
1340 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001341 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001342 d = 0;
1343 }
1344 else
1345 shift--;
1346
1347 }
1348 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001349 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001350 break;
1351 }
1352 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001353 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001354 png_bytep sp;
1355 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001356 int shift;
1357 int d;
1358 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001359 png_uint_32 i;
1360 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001361
1362 dp = row;
1363 shift = 6;
1364 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001365 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001366 i += png_pass_inc[pass])
1367 {
1368 sp = row + (png_size_t)(i >> 2);
1369 value = (*sp >> ((3 - (int)(i & 3)) << 1)) & 0x3;
1370 d |= (value << shift);
1371
1372 if (shift == 0)
1373 {
1374 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001375 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001376 d = 0;
1377 }
1378 else
1379 shift -= 2;
1380 }
1381 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001382 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001383 break;
1384 }
1385 case 4:
1386 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001387 png_bytep sp;
1388 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001389 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001390 int d;
1391 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001392 png_uint_32 i;
1393 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001394
1395 dp = row;
1396 shift = 4;
1397 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001398 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001399 i += png_pass_inc[pass])
1400 {
1401 sp = row + (png_size_t)(i >> 1);
1402 value = (*sp >> ((1 - (int)(i & 1)) << 2)) & 0xf;
1403 d |= (value << shift);
1404
1405 if (shift == 0)
1406 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001407 shift = 4;
1408 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001409 d = 0;
1410 }
1411 else
1412 shift -= 4;
1413 }
1414 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001415 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001416 break;
1417 }
1418 default:
1419 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001420 png_bytep sp;
1421 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001422 png_uint_32 i;
1423 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001424 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001425
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001426 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05001427 dp = row;
1428 /* find out how many bytes each pixel takes up */
1429 pixel_bytes = (row_info->pixel_depth >> 3);
1430 /* loop through the row, only looking at the pixels that
1431 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001432 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001433 i += png_pass_inc[pass])
1434 {
1435 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001436 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001437 /* move the pixel */
1438 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001439 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05001440 /* next pixel */
1441 dp += pixel_bytes;
1442 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001443 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001444 }
1445 }
1446 /* set new row width */
1447 row_info->width = (row_info->width +
1448 png_pass_inc[pass] - 1 -
1449 png_pass_start[pass]) /
1450 png_pass_inc[pass];
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001451 row_info->rowbytes = ((row_info->width *
1452 row_info->pixel_depth + 7) >> 3);
Guy Schalnat0d580581995-07-20 02:43:20 -05001453 }
1454}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001455#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001456
Andreas Dilger47a0c421997-05-16 02:46:07 -05001457/* This filters the row, chooses which filter to use, if it has not already
1458 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001459 * chosen filter.
1460 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001461#define PNG_MAXSUM (~((png_uint_32)0) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001462#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001463#define PNG_LOMASK ((png_uint_32)0xffffL)
1464#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Guy Schalnat0d580581995-07-20 02:43:20 -05001465void
Guy Schalnate5a37791996-06-05 15:50:50 -05001466png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05001467{
Guy Schalnate5a37791996-06-05 15:50:50 -05001468 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001469 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001470 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001471 png_uint_32 row_bytes = row_info->rowbytes;
1472#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1473 int num_p_filters = (int)png_ptr->num_prev_filters;
1474#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001475
Andreas Dilger47a0c421997-05-16 02:46:07 -05001476 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001477 /* find out how many bytes offset each pixel is */
1478 bpp = (row_info->pixel_depth + 7) / 8;
Guy Schalnate5a37791996-06-05 15:50:50 -05001479
1480 prev_row = png_ptr->prev_row;
1481 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001482 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05001483
Andreas Dilger47a0c421997-05-16 02:46:07 -05001484 /* The prediction method we use is to find which method provides the
1485 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05001486 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05001487 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001488 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05001489 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001490 * predictive" method (not implemented yet), which does test compressions
1491 * of lines using different filter methods, and then chooses the
1492 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05001493 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001494 *
1495 * GRR 980525: consider also
1496 * (1) minimum sum of absolute differences from running average (i.e.,
1497 * keep running sum of non-absolute differences & count of bytes)
1498 * [track dispersion, too? restart average if dispersion too large?]
1499 * (1b) minimum sum of absolute differences from sliding average, probably
1500 * with window size <= deflate window (usually 32K)
1501 * (2) minimum sum of squared differences from zero or running average
1502 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001503 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001504
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001505
Guy Schalnate5a37791996-06-05 15:50:50 -05001506 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05001507 * that has been chosen, as it doesn't actually do anything to the data.
1508 */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001509 if (filter_to_do & PNG_FILTER_NONE &&
1510 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05001511 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001512 png_bytep rp;
1513 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001514 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001515 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05001516
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001517 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001518 {
1519 v = *rp;
1520 sum += (v < 128) ? v : 256 - v;
1521 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001522
1523#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1524 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1525 {
1526 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001527 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001528 sumlo = sum & PNG_LOMASK;
1529 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
1530
1531 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001532 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001533 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001534 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001535 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001536 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001537 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001538 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001539 PNG_WEIGHT_SHIFT;
1540 }
1541 }
1542
1543 /* Factor in the cost of this filter (this is here for completeness,
1544 * but it makes no sense to have a "cost" for the NONE filter, as
1545 * it has the minimum possible computational cost - none).
1546 */
1547 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
1548 PNG_COST_SHIFT;
1549 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
1550 PNG_COST_SHIFT;
1551
1552 if (sumhi > PNG_HIMASK)
1553 sum = PNG_MAXSUM;
1554 else
1555 sum = (sumhi << PNG_HISHIFT) + sumlo;
1556 }
1557#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001558 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05001559 }
1560
Guy Schalnate5a37791996-06-05 15:50:50 -05001561 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001562 if (filter_to_do == PNG_FILTER_SUB)
1563 /* it's the only filter so no testing is needed */
1564 {
1565 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001566 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001567 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
1568 i++, rp++, dp++)
1569 {
1570 *dp = *rp;
1571 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001572 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001573 i++, rp++, lp++, dp++)
1574 {
1575 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
1576 }
1577 best_row = png_ptr->sub_row;
1578 }
1579
1580 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001581 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001582 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001583 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001584 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001585 int v;
1586
1587#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001588 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05001589 * would reduce the sum of this filter, so that we can do the
1590 * early exit comparison without scaling the sum each time.
1591 */
1592 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1593 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001594 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001595 png_uint_32 lmhi, lmlo;
1596 lmlo = lmins & PNG_LOMASK;
1597 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1598
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001599 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001600 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001601 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001602 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001603 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001604 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001605 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001606 PNG_WEIGHT_SHIFT;
1607 }
1608 }
1609
1610 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1611 PNG_COST_SHIFT;
1612 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1613 PNG_COST_SHIFT;
1614
1615 if (lmhi > PNG_HIMASK)
1616 lmins = PNG_MAXSUM;
1617 else
1618 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1619 }
1620#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001621
Guy Schalnate5a37791996-06-05 15:50:50 -05001622 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
1623 i++, rp++, dp++)
1624 {
1625 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001626
Guy Schalnate5a37791996-06-05 15:50:50 -05001627 sum += (v < 128) ? v : 256 - v;
1628 }
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001629 for (lp = row_buf + 1; i < row_info->rowbytes;
1630 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001631 {
1632 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001633
Guy Schalnate5a37791996-06-05 15:50:50 -05001634 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001635
1636 if (sum > lmins) /* We are already worse, don't continue. */
1637 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001638 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001639
1640#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1641 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1642 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001643 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001644 png_uint_32 sumhi, sumlo;
1645 sumlo = sum & PNG_LOMASK;
1646 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1647
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001648 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001649 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001650 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001651 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001652 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001653 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001654 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001655 PNG_WEIGHT_SHIFT;
1656 }
1657 }
1658
1659 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1660 PNG_COST_SHIFT;
1661 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1662 PNG_COST_SHIFT;
1663
1664 if (sumhi > PNG_HIMASK)
1665 sum = PNG_MAXSUM;
1666 else
1667 sum = (sumhi << PNG_HISHIFT) + sumlo;
1668 }
1669#endif
1670
Guy Schalnate5a37791996-06-05 15:50:50 -05001671 if (sum < mins)
1672 {
1673 mins = sum;
1674 best_row = png_ptr->sub_row;
1675 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001676 }
1677
Guy Schalnate5a37791996-06-05 15:50:50 -05001678 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001679 if (filter_to_do == PNG_FILTER_UP)
1680 {
1681 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001682 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001683
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001684 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001685 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001686 i++, rp++, pp++, dp++)
1687 {
1688 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
1689 }
1690 best_row = png_ptr->up_row;
1691 }
1692
1693 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05001694 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001695 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001696 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001697 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001698 int v;
1699
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001700
Andreas Dilger47a0c421997-05-16 02:46:07 -05001701#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1702 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1703 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001704 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001705 png_uint_32 lmhi, lmlo;
1706 lmlo = lmins & PNG_LOMASK;
1707 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1708
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001709 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001710 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001711 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001712 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001713 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001714 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001715 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001716 PNG_WEIGHT_SHIFT;
1717 }
1718 }
1719
1720 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
1721 PNG_COST_SHIFT;
1722 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
1723 PNG_COST_SHIFT;
1724
1725 if (lmhi > PNG_HIMASK)
1726 lmins = PNG_MAXSUM;
1727 else
1728 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1729 }
1730#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001731
1732 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001733 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001734 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001735 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05001736
1737 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001738
1739 if (sum > lmins) /* We are already worse, don't continue. */
1740 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001741 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001742
1743#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1744 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1745 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001746 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001747 png_uint_32 sumhi, sumlo;
1748 sumlo = sum & PNG_LOMASK;
1749 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1750
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001751 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001752 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001753 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001754 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001755 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001756 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001757 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001758 PNG_WEIGHT_SHIFT;
1759 }
1760 }
1761
1762 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
1763 PNG_COST_SHIFT;
1764 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
1765 PNG_COST_SHIFT;
1766
1767 if (sumhi > PNG_HIMASK)
1768 sum = PNG_MAXSUM;
1769 else
1770 sum = (sumhi << PNG_HISHIFT) + sumlo;
1771 }
1772#endif
1773
Guy Schalnate5a37791996-06-05 15:50:50 -05001774 if (sum < mins)
1775 {
1776 mins = sum;
1777 best_row = png_ptr->up_row;
1778 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001779 }
1780
Guy Schalnate5a37791996-06-05 15:50:50 -05001781 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001782 if (filter_to_do == PNG_FILTER_AVG)
1783 {
1784 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001785 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001786 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001787 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001788 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001789 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001790 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001791 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001792 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001793 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
1794 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001795 }
1796 best_row = png_ptr->avg_row;
1797 }
1798
1799 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001800 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001801 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001802 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001803 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001804 int v;
1805
1806#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1807 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1808 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001809 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001810 png_uint_32 lmhi, lmlo;
1811 lmlo = lmins & PNG_LOMASK;
1812 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1813
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001814 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001815 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001816 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001817 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001818 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001819 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001820 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001821 PNG_WEIGHT_SHIFT;
1822 }
1823 }
1824
1825 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
1826 PNG_COST_SHIFT;
1827 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
1828 PNG_COST_SHIFT;
1829
1830 if (lmhi > PNG_HIMASK)
1831 lmins = PNG_MAXSUM;
1832 else
1833 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1834 }
1835#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001836
1837 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001838 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001839 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001840 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05001841
1842 sum += (v < 128) ? v : 256 - v;
1843 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001844 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001845 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001846 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001847 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05001848
1849 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001850
1851 if (sum > lmins) /* We are already worse, don't continue. */
1852 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001853 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001854
1855#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1856 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1857 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001858 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001859 png_uint_32 sumhi, sumlo;
1860 sumlo = sum & PNG_LOMASK;
1861 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1862
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001863 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001864 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001865 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001866 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001867 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001868 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001869 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001870 PNG_WEIGHT_SHIFT;
1871 }
1872 }
1873
1874 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
1875 PNG_COST_SHIFT;
1876 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
1877 PNG_COST_SHIFT;
1878
1879 if (sumhi > PNG_HIMASK)
1880 sum = PNG_MAXSUM;
1881 else
1882 sum = (sumhi << PNG_HISHIFT) + sumlo;
1883 }
1884#endif
1885
Guy Schalnate5a37791996-06-05 15:50:50 -05001886 if (sum < mins)
1887 {
1888 mins = sum;
1889 best_row = png_ptr->avg_row;
1890 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001891 }
1892
Andreas Dilger47a0c421997-05-16 02:46:07 -05001893 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001894 if (filter_to_do == PNG_FILTER_PAETH)
1895 {
1896 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001897 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001898 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001899 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001900 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001901 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001902 }
1903
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001904 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001905 {
1906 int a, b, c, pa, pb, pc, p;
1907
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001908 b = *pp++;
1909 c = *cp++;
1910 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001911
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001912 p = b - c;
1913 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001914
1915#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001916 pa = abs(p);
1917 pb = abs(pc);
1918 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001919#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001920 pa = p < 0 ? -p : p;
1921 pb = pc < 0 ? -pc : pc;
1922 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001923#endif
1924
1925 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
1926
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001927 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001928 }
1929 best_row = png_ptr->paeth_row;
1930 }
1931
1932 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001933 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001934 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001935 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001936 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001937 int v;
1938
1939#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1940 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1941 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001942 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001943 png_uint_32 lmhi, lmlo;
1944 lmlo = lmins & PNG_LOMASK;
1945 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1946
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001947 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001948 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001949 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001950 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001951 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001952 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001953 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001954 PNG_WEIGHT_SHIFT;
1955 }
1956 }
1957
1958 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1959 PNG_COST_SHIFT;
1960 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1961 PNG_COST_SHIFT;
1962
1963 if (lmhi > PNG_HIMASK)
1964 lmins = PNG_MAXSUM;
1965 else
1966 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1967 }
1968#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001969
1970 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001971 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001972 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001973 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05001974
1975 sum += (v < 128) ? v : 256 - v;
1976 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001977
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001978 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001979 {
1980 int a, b, c, pa, pb, pc, p;
1981
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001982 b = *pp++;
1983 c = *cp++;
1984 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001985
1986#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001987 p = b - c;
1988 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001989#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001990 pa = abs(p);
1991 pb = abs(pc);
1992 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001993#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001994 pa = p < 0 ? -p : p;
1995 pb = pc < 0 ? -pc : pc;
1996 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001997#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001998 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
1999#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002000 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002001 pa = abs(p - a);
2002 pb = abs(p - b);
2003 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002004 if (pa <= pb && pa <= pc)
2005 p = a;
2006 else if (pb <= pc)
2007 p = b;
2008 else
2009 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002010#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002011
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002012 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002013
2014 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002015
2016 if (sum > lmins) /* We are already worse, don't continue. */
2017 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002018 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002019
2020#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2021 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2022 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002023 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002024 png_uint_32 sumhi, sumlo;
2025 sumlo = sum & PNG_LOMASK;
2026 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2027
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002028 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002029 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002030 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002031 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002032 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002033 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002034 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002035 PNG_WEIGHT_SHIFT;
2036 }
2037 }
2038
2039 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2040 PNG_COST_SHIFT;
2041 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2042 PNG_COST_SHIFT;
2043
2044 if (sumhi > PNG_HIMASK)
2045 sum = PNG_MAXSUM;
2046 else
2047 sum = (sumhi << PNG_HISHIFT) + sumlo;
2048 }
2049#endif
2050
Guy Schalnate5a37791996-06-05 15:50:50 -05002051 if (sum < mins)
2052 {
2053 best_row = png_ptr->paeth_row;
2054 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002055 }
2056
Andreas Dilger47a0c421997-05-16 02:46:07 -05002057 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002058
Guy Schalnate5a37791996-06-05 15:50:50 -05002059 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002060
2061#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2062 /* Save the type of filter we picked this time for future calculations */
2063 if (png_ptr->num_prev_filters > 0)
2064 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002065 int j;
2066 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002067 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002068 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002069 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002070 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002071 }
2072#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002073}
2074
2075
Andreas Dilger47a0c421997-05-16 02:46:07 -05002076/* Do the actual writing of a previously filtered row. */
Guy Schalnate5a37791996-06-05 15:50:50 -05002077void
2078png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2079{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002080 png_debug(1, "in png_write_filtered_row\n");
2081 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002082 /* set up the zlib input buffer */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002083 png_ptr->zstream.next_in = filtered_row;
2084 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002085 /* repeat until we have compressed all the data */
2086 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002087 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002088 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002089
Guy Schalnate5a37791996-06-05 15:50:50 -05002090 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002091 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002092 /* check for compression errors */
2093 if (ret != Z_OK)
2094 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002095 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002096 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002097 else
2098 png_error(png_ptr, "zlib error");
2099 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002100
Guy Schalnate5a37791996-06-05 15:50:50 -05002101 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002102 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002103 {
2104 /* write the IDAT and reset the zlib output buffer */
2105 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002106 png_ptr->zstream.next_out = png_ptr->zbuf;
2107 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002108 }
2109 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002110 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002111
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002112 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002113 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002114 {
2115 png_bytep tptr;
2116
2117 tptr = png_ptr->prev_row;
2118 png_ptr->prev_row = png_ptr->row_buf;
2119 png_ptr->row_buf = tptr;
2120 }
2121
Guy Schalnate5a37791996-06-05 15:50:50 -05002122 /* finish row - updates counters and flushes zlib if last row */
2123 png_write_finish_row(png_ptr);
2124
2125#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2126 png_ptr->flush_rows++;
2127
2128 if (png_ptr->flush_dist > 0 &&
2129 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002130 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002131 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002132 }
Guy Schalnate5a37791996-06-05 15:50:50 -05002133#endif /* PNG_WRITE_FLUSH_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05002134}