blob: bad95a912632137e76528810fb516fd589596154 [file] [log] [blame]
Guy Schalnat0d580581995-07-20 02:43:20 -05001
Andreas Dilger47a0c421997-05-16 02:46:07 -05002/* pngwutil.c - utilities to write a PNG file
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
4 * libpng 1.00.97
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
8 * May 28, 1997
9 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050010
Guy Schalnat0d580581995-07-20 02:43:20 -050011#define PNG_INTERNAL
12#include "png.h"
13
Andreas Dilger47a0c421997-05-16 02:46:07 -050014/* Place a 32-bit number into a buffer in PNG byte order. We work
15 * with unsigned numbers for convenience, although one supported
16 * ancillary chunk uses signed (two's complement) numbers.
17 */
Guy Schalnat0d580581995-07-20 02:43:20 -050018void
Guy Schalnat6d764711995-12-19 03:22:19 -060019png_save_uint_32(png_bytep buf, png_uint_32 i)
Guy Schalnat0d580581995-07-20 02:43:20 -050020{
21 buf[0] = (png_byte)((i >> 24) & 0xff);
22 buf[1] = (png_byte)((i >> 16) & 0xff);
23 buf[2] = (png_byte)((i >> 8) & 0xff);
24 buf[3] = (png_byte)(i & 0xff);
25}
26
Andreas Dilger47a0c421997-05-16 02:46:07 -050027#if defined(PNG_WRITE_pCAL_SUPPORTED)
28/* The png_save_int_32 function assumes integers are stored in two's
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060029 * complement format. If this isn't the case, then this routine needs to
30 * be modified to write data in two's complement format.
31 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050032void
33png_save_int_32(png_bytep buf, png_int_32 i)
34{
35 buf[0] = (png_byte)((i >> 24) & 0xff);
36 buf[1] = (png_byte)((i >> 16) & 0xff);
37 buf[2] = (png_byte)((i >> 8) & 0xff);
38 buf[3] = (png_byte)(i & 0xff);
39}
40#endif
41
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060042/* Place a 16-bit number into a buffer in PNG byte order.
43 * The parameter is declared unsigned int, not png_uint_16,
44 * just to avoid potential problems on pre-ANSI C compilers.
45 */
Guy Schalnat0d580581995-07-20 02:43:20 -050046void
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060047png_save_uint_16(png_bytep buf, unsigned int i)
Guy Schalnat0d580581995-07-20 02:43:20 -050048{
49 buf[0] = (png_byte)((i >> 8) & 0xff);
50 buf[1] = (png_byte)(i & 0xff);
51}
52
Andreas Dilger47a0c421997-05-16 02:46:07 -050053/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060054 * representing the chunk name. The array must be at least 4 bytes in
55 * length, and does not need to be null terminated. To be safe, pass the
56 * pre-defined chunk names here, and if you need a new one, define it
57 * where the others are defined. The length is the length of the data.
58 * All the data must be present. If that is not possible, use the
59 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
60 * functions instead.
61 */
Guy Schalnat0d580581995-07-20 02:43:20 -050062void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060063png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050064 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050065{
Andreas Dilger47a0c421997-05-16 02:46:07 -050066 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060067 png_write_chunk_data(png_ptr, data, length);
68 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050069}
70
Andreas Dilger47a0c421997-05-16 02:46:07 -050071/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060072 * The total_length is the sum of the lengths of all the data you will be
73 * passing in png_write_chunk_data().
74 */
Guy Schalnat0d580581995-07-20 02:43:20 -050075void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060076png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
77 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -050078{
Andreas Dilger47a0c421997-05-16 02:46:07 -050079 png_byte buf[4];
80 png_debug2(0, "Writing %s chunk (%d bytes)\n", chunk_name, length);
81
Guy Schalnat0d580581995-07-20 02:43:20 -050082 /* write the length */
Andreas Dilger47a0c421997-05-16 02:46:07 -050083 png_save_uint_32(buf, length);
84 png_write_data(png_ptr, buf, (png_size_t)4);
85
Guy Schalnat0d580581995-07-20 02:43:20 -050086 /* write the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -050087 png_write_data(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050088 /* reset the crc and run it over the chunk name */
89 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -050090 png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050091}
92
Andreas Dilger47a0c421997-05-16 02:46:07 -050093/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060094 * Note that multiple calls to this function are allowed, and that the
95 * sum of the lengths from these calls *must* add up to the total_length
96 * given to png_write_chunk_start().
97 */
Guy Schalnat0d580581995-07-20 02:43:20 -050098void
Andreas Dilger47a0c421997-05-16 02:46:07 -050099png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500100{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500101 /* write the data, and run the CRC over it */
102 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500103 {
104 png_calculate_crc(png_ptr, data, length);
Guy Schalnat6d764711995-12-19 03:22:19 -0600105 png_write_data(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500106 }
107}
108
Andreas Dilger47a0c421997-05-16 02:46:07 -0500109/* Finish a chunk started with png_write_chunk_start(). */
Guy Schalnat0d580581995-07-20 02:43:20 -0500110void
Guy Schalnat6d764711995-12-19 03:22:19 -0600111png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500112{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500113 png_byte buf[4];
114
Guy Schalnat0d580581995-07-20 02:43:20 -0500115 /* write the crc */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500116 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500117
118 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500119}
120
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600121/* Simple function to write the signature. If we have already written
122 * the magic bytes of the signature, or more likely, the PNG stream is
123 * being embedded into another stream and doesn't need its own signature,
124 * we should call png_set_sig_bytes() to tell libpng how many of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600125 * bytes have already been written.
126 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500127void
Guy Schalnat6d764711995-12-19 03:22:19 -0600128png_write_sig(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500129{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600130 /* write the rest of the 8 byte signature */
131 png_write_data(png_ptr, &png_sig[png_ptr->sig_bytes],
Andreas Dilger47a0c421997-05-16 02:46:07 -0500132 (png_size_t)8 - png_ptr->sig_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500133}
134
135/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600136 * information. Note that the rest of this code depends upon this
137 * information being correct.
138 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500139void
Guy Schalnat6d764711995-12-19 03:22:19 -0600140png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500141 int bit_depth, int color_type, int compression_type, int filter_type,
142 int interlace_type)
143{
144 png_byte buf[13]; /* buffer to store the IHDR info */
145
Andreas Dilger47a0c421997-05-16 02:46:07 -0500146 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500147 /* Check that we have valid input data from the application info */
148 switch (color_type)
149 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500150 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500151 switch (bit_depth)
152 {
153 case 1:
154 case 2:
155 case 4:
156 case 8:
157 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500158 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500159 }
160 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500161 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500162 if (bit_depth != 8 && bit_depth != 16)
163 png_error(png_ptr, "Invalid bit depth for RGB image");
164 png_ptr->channels = 3;
165 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500166 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500167 switch (bit_depth)
168 {
169 case 1:
170 case 2:
171 case 4:
172 case 8: png_ptr->channels = 1; break;
173 default: png_error(png_ptr, "Invalid bit depth for paletted image");
174 }
175 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500176 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500177 if (bit_depth != 8 && bit_depth != 16)
178 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
179 png_ptr->channels = 2;
180 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500181 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500182 if (bit_depth != 8 && bit_depth != 16)
183 png_error(png_ptr, "Invalid bit depth for RGBA image");
184 png_ptr->channels = 4;
185 break;
186 default:
187 png_error(png_ptr, "Invalid image color type specified");
188 }
189
Andreas Dilger47a0c421997-05-16 02:46:07 -0500190 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500191 {
192 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500193 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500194 }
195
Andreas Dilger47a0c421997-05-16 02:46:07 -0500196 if (filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500197 {
198 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500199 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500200 }
201
Andreas Dilger47a0c421997-05-16 02:46:07 -0500202 if (interlace_type != PNG_INTERLACE_NONE &&
203 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500204 {
205 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500206 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500207 }
208
209 /* save off the relevent information */
210 png_ptr->bit_depth = (png_byte)bit_depth;
211 png_ptr->color_type = (png_byte)color_type;
212 png_ptr->interlaced = (png_byte)interlace_type;
213 png_ptr->width = width;
214 png_ptr->height = height;
215
216 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500217 png_ptr->rowbytes = ((width * (png_size_t)png_ptr->pixel_depth + 7) >> 3);
Guy Schalnate5a37791996-06-05 15:50:50 -0500218 /* set the usr info, so any transformations can modify it */
219 png_ptr->usr_width = png_ptr->width;
220 png_ptr->usr_bit_depth = png_ptr->bit_depth;
221 png_ptr->usr_channels = png_ptr->channels;
222
Guy Schalnat0d580581995-07-20 02:43:20 -0500223 /* pack the header information into the buffer */
224 png_save_uint_32(buf, width);
225 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600226 buf[8] = (png_byte)bit_depth;
227 buf[9] = (png_byte)color_type;
228 buf[10] = (png_byte)compression_type;
229 buf[11] = (png_byte)filter_type;
230 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500231
232 /* write the chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500233 png_write_chunk(png_ptr, png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500234
Andreas Dilger47a0c421997-05-16 02:46:07 -0500235 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600236 png_ptr->zstream.zalloc = png_zalloc;
237 png_ptr->zstream.zfree = png_zfree;
238 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500239 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500240 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500241 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
242 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500243 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500244 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500245 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500246 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500247 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500248 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500249 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500250 png_ptr->zlib_strategy = Z_FILTERED;
251 else
252 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
253 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500254 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500255 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500256 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500257 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500258 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500259 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500260 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500261 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600262 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500263 png_ptr->zlib_method, png_ptr->zlib_window_bits,
264 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600265 png_ptr->zstream.next_out = png_ptr->zbuf;
266 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500267
Guy Schalnate5a37791996-06-05 15:50:50 -0500268 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500269}
270
271/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600272 * correct order for PNG, so people can redefine it to any convient
273 * structure.
274 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500275void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500276png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500277{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500278 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600279 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500280 png_byte buf[3];
281
Andreas Dilger47a0c421997-05-16 02:46:07 -0500282 png_debug(1, "in png_write_PLTE\n");
283 if (num_pal == 0 || num_pal > 256)
Guy Schalnate5a37791996-06-05 15:50:50 -0500284 {
285 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
286 {
287 png_error(png_ptr, "Invalid number of colors in palette");
288 }
289 else
290 {
291 png_warning(png_ptr, "Invalid number of colors in palette");
292 return;
293 }
294 }
295
Andreas Dilger47a0c421997-05-16 02:46:07 -0500296 png_ptr->num_palette = (png_uint_16)num_pal;
297 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500298
Andreas Dilger47a0c421997-05-16 02:46:07 -0500299 png_write_chunk_start(png_ptr, png_PLTE, num_pal * 3);
300 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500301 {
302 buf[0] = pal_ptr->red;
303 buf[1] = pal_ptr->green;
304 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500305 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500306 }
307 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500308 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500309}
310
311/* write an IDAT chunk */
312void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500313png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500314{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500315 png_debug(1, "in png_write_IDAT\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500316 png_write_chunk(png_ptr, png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500317 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500318}
319
320/* write an IEND chunk */
321void
Guy Schalnat6d764711995-12-19 03:22:19 -0600322png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500323{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500324 png_debug(1, "in png_write_IEND\n");
325 png_write_chunk(png_ptr, png_IEND, NULL, (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600326 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500327}
328
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500329#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500330/* write a gAMA chunk */
331void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500332png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500333{
334 png_uint_32 igamma;
335 png_byte buf[4];
336
Andreas Dilger47a0c421997-05-16 02:46:07 -0500337 png_debug(1, "in png_write_gAMA\n");
338 /* file_gamma is saved in 1/100,000ths */
339 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500340 png_save_uint_32(buf, igamma);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500341 png_write_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500342}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500343#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500344
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600345#if defined(PNG_WRITE_sRGB_SUPPORTED)
346/* write a sRGB chunk */
347void
348png_write_sRGB(png_structp png_ptr, png_byte srgb_intent)
349{
350 png_byte buf[1];
351
352 png_debug(1, "in png_write_sRGB\n");
353 if(srgb_intent > 3)
354 png_warning(png_ptr,
355 "Invalid sRGB rendering intent specified");
356 buf[0]=srgb_intent;
357 png_write_chunk(png_ptr, png_sRGB, buf, (png_size_t)1);
358}
359#endif
360
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500361#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500362/* write the sBIT chunk */
363void
Guy Schalnat6d764711995-12-19 03:22:19 -0600364png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500365{
366 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500367 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500368
Andreas Dilger47a0c421997-05-16 02:46:07 -0500369 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600370 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500371 if (color_type & PNG_COLOR_MASK_COLOR)
372 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500373 int maxbits;
374
375 maxbits = color_type==PNG_COLOR_TYPE_PALETTE ? 8:png_ptr->usr_bit_depth;
376 if (sbit->red == 0 || sbit->red > maxbits ||
377 sbit->green == 0 || sbit->green > maxbits ||
378 sbit->blue == 0 || sbit->blue > maxbits)
379 {
380 png_warning(png_ptr, "Invalid sBIT depth specified");
381 return;
382 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500383 buf[0] = sbit->red;
384 buf[1] = sbit->green;
385 buf[2] = sbit->blue;
386 size = 3;
387 }
388 else
389 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500390 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
391 {
392 png_warning(png_ptr, "Invalid sBIT depth specified");
393 return;
394 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500395 buf[0] = sbit->gray;
396 size = 1;
397 }
398
399 if (color_type & PNG_COLOR_MASK_ALPHA)
400 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500401 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
402 {
403 png_warning(png_ptr, "Invalid sBIT depth specified");
404 return;
405 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500406 buf[size++] = sbit->alpha;
407 }
408
Andreas Dilger47a0c421997-05-16 02:46:07 -0500409 png_write_chunk(png_ptr, png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500410}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500411#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500412
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500413#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500414/* write the cHRM chunk */
415void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500416png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600417 double red_x, double red_y, double green_x, double green_y,
418 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500419{
420 png_uint_32 itemp;
421 png_byte buf[32];
422
Andreas Dilger47a0c421997-05-16 02:46:07 -0500423 png_debug(1, "in png_write_cHRM\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500424 /* each value is saved int 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500425 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
426 white_x + white_y > 1.0)
427 {
428 png_warning(png_ptr, "Invalid cHRM white point specified");
429 return;
430 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500431 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
432 png_save_uint_32(buf, itemp);
433 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
434 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500435
436 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
437 red_x + red_y > 1.0)
438 {
439 png_warning(png_ptr, "Invalid cHRM red point specified");
440 return;
441 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500442 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
443 png_save_uint_32(buf + 8, itemp);
444 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
445 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500446
447 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
448 green_x + green_y > 1.0)
449 {
450 png_warning(png_ptr, "Invalid cHRM green point specified");
451 return;
452 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500453 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
454 png_save_uint_32(buf + 16, itemp);
455 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
456 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500457
458 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
459 blue_x + blue_y > 1.0)
460 {
461 png_warning(png_ptr, "Invalid cHRM blue point specified");
462 return;
463 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500464 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
465 png_save_uint_32(buf + 24, itemp);
466 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
467 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500468
Andreas Dilger47a0c421997-05-16 02:46:07 -0500469 png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500470}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500471#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500472
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500473#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500474/* write the tRNS chunk */
475void
Guy Schalnat6d764711995-12-19 03:22:19 -0600476png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -0500477 int num_trans, int color_type)
478{
479 png_byte buf[6];
480
Andreas Dilger47a0c421997-05-16 02:46:07 -0500481 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500482 if (color_type == PNG_COLOR_TYPE_PALETTE)
483 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500484 if (num_trans <= 0 || num_trans > png_ptr->num_palette)
485 {
486 png_warning(png_ptr,"Invalid number of transparent colors specified");
487 return;
488 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500489 /* write the chunk out as it is */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500490 png_write_chunk(png_ptr, png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -0500491 }
492 else if (color_type == PNG_COLOR_TYPE_GRAY)
493 {
494 /* one 16 bit value */
495 png_save_uint_16(buf, tran->gray);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500496 png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500497 }
498 else if (color_type == PNG_COLOR_TYPE_RGB)
499 {
500 /* three 16 bit values */
501 png_save_uint_16(buf, tran->red);
502 png_save_uint_16(buf + 2, tran->green);
503 png_save_uint_16(buf + 4, tran->blue);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500504 png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500505 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500506 else
507 {
508 png_warning(png_ptr, "Can't write tRNS with and alpha channel");
509 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500510}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500511#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500512
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500513#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500514/* write the background chunk */
515void
Guy Schalnat6d764711995-12-19 03:22:19 -0600516png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500517{
518 png_byte buf[6];
519
Andreas Dilger47a0c421997-05-16 02:46:07 -0500520 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500521 if (color_type == PNG_COLOR_TYPE_PALETTE)
522 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500523 if (back->index > png_ptr->num_palette)
524 {
525 png_warning(png_ptr, "Invalid background palette index");
526 return;
527 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500528 buf[0] = back->index;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500529 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500530 }
531 else if (color_type & PNG_COLOR_MASK_COLOR)
532 {
533 png_save_uint_16(buf, back->red);
534 png_save_uint_16(buf + 2, back->green);
535 png_save_uint_16(buf + 4, back->blue);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500536 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500537 }
538 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600539 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500540 png_save_uint_16(buf, back->gray);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500541 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500542 }
543}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500544#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500545
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500546#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500547/* write the histogram */
548void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500549png_write_hIST(png_structp png_ptr, png_uint_16p hist, png_uint_32 num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -0500550{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500551 png_uint_32 i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500552 png_byte buf[3];
553
Andreas Dilger47a0c421997-05-16 02:46:07 -0500554 png_debug(1, "in png_write_hIST\n");
555 if (num_hist > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500556 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500557 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
558 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500559 png_warning(png_ptr, "Invalid number of histogram entries specified");
560 return;
561 }
562
Andreas Dilger47a0c421997-05-16 02:46:07 -0500563 png_write_chunk_start(png_ptr, png_hIST, num_hist * 2);
564 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500565 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600566 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500567 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500568 }
569 png_write_chunk_end(png_ptr);
570}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500571#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500572
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600573#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
574/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
575 * and if invalid, correct the keyword rather than discarding the entire
576 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
577 * length, forbids leading or trailing whitespace, multiple internal spaces,
578 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -0500579 *
580 * The new_key is allocated to hold the corrected keyword and must be freed
581 * by the calling routine. This avoids problems with trying to write to
582 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600583 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500584png_size_t
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600585png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600586{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500587 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600588 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600589 int kflag;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600590
Andreas Dilger47a0c421997-05-16 02:46:07 -0500591 png_debug(1, "in png_check_keyword\n");
592 *new_key = NULL;
593
594 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600595 {
596 char msg[40];
597
598 sprintf(msg, "Zero length %s keyword", png_ptr->chunk_name);
599 png_warning(png_ptr, msg);
600 return 0;
601 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600602
Andreas Dilger47a0c421997-05-16 02:46:07 -0500603 png_debug1(2, "Keyword to be checked is '%s'\n", key);
604
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600605 *new_key = (png_charp)png_malloc(png_ptr, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600606
607 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500608 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600609 {
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600610 if (*kp < 0x20 || (*kp > 0x7E && *kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500611 {
612 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600613
Andreas Dilger47a0c421997-05-16 02:46:07 -0500614 sprintf(msg, "Invalid %s keyword character 0x%02X",
615 png_ptr->chunk_name, *kp);
616 png_warning(png_ptr, msg);
617 *dp = ' ';
618 }
619 else
620 {
621 *dp = *kp;
622 }
623 }
624 *dp = '\0';
625
626 /* Remove any trailing white space. */
627 kp = *new_key + key_len - 1;
628 if (*kp == ' ')
629 {
630 char msg[50];
631 sprintf(msg, "Trailing spaces removed from %s keyword",
632 png_ptr->chunk_name);
633
634 png_warning(png_ptr, msg);
635
636 while (*kp == ' ')
637 {
638 *(kp--) = '\0';
639 key_len--;
640 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600641 }
642
643 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500644 kp = *new_key;
645 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600646 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500647 char msg[50];
648 sprintf(msg, "Leading spaces removed from %s keyword",
649 png_ptr->chunk_name);
650
651 png_warning(png_ptr, msg);
652
653 while (*kp == ' ')
654 {
655 kp++;
656 key_len--;
657 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600658 }
659
Andreas Dilger47a0c421997-05-16 02:46:07 -0500660 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600661
Andreas Dilger47a0c421997-05-16 02:46:07 -0500662 /* Remove multiple internal spaces. */
663 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600664 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500665 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600666 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500667 *(dp++) = *kp;
668 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600669 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500670 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600671 {
672 key_len--;
673 }
674 else
675 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500676 *(dp++) = *kp;
677 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600678 }
679 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500680 *dp = '\0';
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600681
682 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500683 {
684 char msg[40];
685
686 sprintf(msg, "Zero length %s keyword", png_ptr->chunk_name);
687 png_warning(png_ptr, msg);
688 }
689
690 if (key_len > 79)
691 {
692 char msg[50];
693
694 sprintf(msg, "%s keyword length must be 1 - 79 characters",
695 png_ptr->chunk_name);
696 png_warning(png_ptr, msg);
697 new_key[79] = '\0';
698 key_len = 79;
699 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600700
701 return key_len;
702}
703#endif
704
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500705#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500706/* write a tEXt chunk */
707void
Guy Schalnat6d764711995-12-19 03:22:19 -0600708png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500709 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -0500710{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500711 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600712 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -0500713
Andreas Dilger47a0c421997-05-16 02:46:07 -0500714 png_debug(1, "in png_write_tEXt\n");
715 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
716 {
717 char msg[40];
718
719 sprintf(msg, "Empty keyword in %s chunk", "tEXt");
720 png_warning(png_ptr, msg);
Guy Schalnate5a37791996-06-05 15:50:50 -0500721 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500722 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500723
Andreas Dilger47a0c421997-05-16 02:46:07 -0500724 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600725 text_len = 0;
726
727 /* make sure we include the 0 after the key */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500728 png_write_chunk_start(png_ptr, png_tEXt, (png_uint_32)key_len+text_len+1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600729 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600730 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500731 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600732
Guy Schalnat0d580581995-07-20 02:43:20 -0500733 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500734 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -0500735}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500736#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500737
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500738#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500739/* write a compressed text chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -0500740void
Guy Schalnat6d764711995-12-19 03:22:19 -0600741png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500742 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -0500743{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500744 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -0500745 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600746 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -0500747 int i, ret;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600748 png_charpp output_ptr = NULL; /* array of pointers to output */
Guy Schalnat0d580581995-07-20 02:43:20 -0500749 int num_output_ptr = 0; /* number of output pointers used */
750 int max_output_ptr = 0; /* size of output_ptr */
751
Andreas Dilger47a0c421997-05-16 02:46:07 -0500752 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600753
Andreas Dilger47a0c421997-05-16 02:46:07 -0500754 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -0500755 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500756 char msg[40];
757
758 sprintf(msg, "Empty keyword in %s chunk", "zTXt");
759 png_warning(png_ptr, msg);
760 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500761 }
762
Andreas Dilger47a0c421997-05-16 02:46:07 -0500763 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
764 {
765 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
766 png_free(png_ptr, new_key);
767 return;
768 }
769
770 png_free(png_ptr, new_key);
771
772 if (compression >= PNG_TEXT_COMPRESSION_LAST)
773 {
774 char msg[50];
775 sprintf(msg, "Unknown zTXt compression type %d", compression);
776 png_warning(png_ptr, msg);
777 compression = PNG_TEXT_COMPRESSION_zTXt;
778 }
779
780 /* We can't write the chunk until we find out how much data we have,
781 * which means we need to run the compressor first, and save the
782 * output. This shouldn't be a problem, as the vast majority of
783 * comments should be reasonable, but we will set up an array of
784 * malloc'd pointers to be sure.
785 *
786 * If we knew the application was well behaved, we could simplify this
787 * greatly by assuming we can always malloc an output buffer large
788 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
789 * and malloc this directly. The only time this would be a bad idea is
790 * if we can't malloc more than 64K and we have 64K of random input
791 * data, or if the input string is incredibly large (although this
792 * wouldn't cause a failure, just a slowdown due to swapping).
793 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500794
795 /* set up the compression buffers */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600796 png_ptr->zstream.avail_in = (uInt)text_len;
797 png_ptr->zstream.next_in = (Bytef *)text;
798 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
799 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500800
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600801 /* this is the same compression loop as in png_write_row() */
Guy Schalnat0d580581995-07-20 02:43:20 -0500802 do
803 {
804 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600805 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnat0d580581995-07-20 02:43:20 -0500806 if (ret != Z_OK)
807 {
808 /* error */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500809 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600810 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -0500811 else
Guy Schalnat6d764711995-12-19 03:22:19 -0600812 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -0500813 }
814 /* check to see if we need more room */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600815 if (!png_ptr->zstream.avail_out && png_ptr->zstream.avail_in)
Guy Schalnat0d580581995-07-20 02:43:20 -0500816 {
817 /* make sure the output array has room */
818 if (num_output_ptr >= max_output_ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600819 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500820 int old_max;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500821
822 old_max = max_output_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500823 max_output_ptr = num_output_ptr + 4;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500824 if (output_ptr != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600825 {
826 png_charpp old_ptr;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600827
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600828 old_ptr = output_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600829 output_ptr = (png_charpp)png_malloc(png_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600830 max_output_ptr * sizeof (png_charpp));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500831 png_memcpy(output_ptr, old_ptr, old_max * sizeof (png_charp));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600832 png_free(png_ptr, old_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600833 }
834 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600835 output_ptr = (png_charpp)png_malloc(png_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600836 max_output_ptr * sizeof (png_charp));
Guy Schalnat0d580581995-07-20 02:43:20 -0500837 }
838
839 /* save the data */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500840 output_ptr[num_output_ptr] = png_malloc(png_ptr, png_ptr->zbuf_size);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500841 png_memcpy(output_ptr[num_output_ptr], png_ptr->zbuf,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500842 png_ptr->zbuf_size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500843 num_output_ptr++;
844
845 /* and reset the buffer */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600846 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
847 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500848 }
849 /* continue until we don't have anymore to compress */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600850 } while (png_ptr->zstream.avail_in);
Guy Schalnat0d580581995-07-20 02:43:20 -0500851
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600852 /* finish the compression */
Guy Schalnat0d580581995-07-20 02:43:20 -0500853 do
854 {
855 /* tell zlib we are finished */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600856 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -0500857 if (ret != Z_OK && ret != Z_STREAM_END)
858 {
859 /* we got an error */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500860 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600861 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -0500862 else
Guy Schalnat6d764711995-12-19 03:22:19 -0600863 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -0500864 }
865
866 /* check to see if we need more room */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500867 if (!(png_ptr->zstream.avail_out) && ret == Z_OK)
Guy Schalnat0d580581995-07-20 02:43:20 -0500868 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600869 /* check to make sure our output array has room */
870 if (num_output_ptr >= max_output_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500871 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500872 int old_max;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500873
874 old_max = max_output_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500875 max_output_ptr = num_output_ptr + 4;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500876 if (output_ptr != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600877 {
878 png_charpp old_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500879
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600880 old_ptr = output_ptr;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500881 /* This could be optimized to realloc() */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600882 output_ptr = (png_charpp)png_malloc(png_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600883 max_output_ptr * sizeof (png_charpp));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500884 png_memcpy(output_ptr, old_ptr, old_max * sizeof (png_charp));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600885 png_free(png_ptr, old_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600886 }
887 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600888 output_ptr = (png_charpp)png_malloc(png_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600889 max_output_ptr * sizeof (png_charp));
890 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600891
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600892 /* save off the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600893 output_ptr[num_output_ptr] = png_malloc(png_ptr,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600894 png_ptr->zbuf_size);
895 png_memcpy(output_ptr[num_output_ptr], png_ptr->zbuf,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500896 png_ptr->zbuf_size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500897 num_output_ptr++;
898
899 /* and reset the buffer pointers */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600900 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
901 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500902 }
903 } while (ret != Z_STREAM_END);
904
905 /* text length is number of buffers plus last buffer */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600906 text_len = png_ptr->zbuf_size * num_output_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600907 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500908 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
Guy Schalnat0d580581995-07-20 02:43:20 -0500909
910 /* write start of chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500911 png_write_chunk_start(png_ptr, png_zTXt, (png_uint_32)(key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -0500912 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500913 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600914 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -0500915 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500916 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500917
918 /* write saved output buffers, if any */
919 for (i = 0; i < num_output_ptr; i++)
920 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500921 png_write_chunk_data(png_ptr,(png_bytep)output_ptr[i],png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600922 png_free(png_ptr, output_ptr[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500923 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500924 if (max_output_ptr != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600925 png_free(png_ptr, output_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500926 /* write anything left in zbuf */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500927 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -0500928 png_write_chunk_data(png_ptr, png_ptr->zbuf,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600929 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -0500930 /* close the chunk */
931 png_write_chunk_end(png_ptr);
932
933 /* reset zlib for another zTXt or the image data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600934 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -0500935}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500936#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500937
Andreas Dilger47a0c421997-05-16 02:46:07 -0500938
939#if defined(PNG_WRITE_oFFs_SUPPORTED)
940/* write the oFFs chunk */
941void
942png_write_oFFs(png_structp png_ptr, png_uint_32 x_offset,
943 png_uint_32 y_offset,
944 int unit_type)
945{
946 png_byte buf[9];
947
948 png_debug(1, "in png_write_oFFs\n");
949 if (unit_type >= PNG_OFFSET_LAST)
950 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
951
952 png_save_uint_32(buf, x_offset);
953 png_save_uint_32(buf + 4, y_offset);
954 buf[8] = (png_byte)unit_type;
955
956 png_write_chunk(png_ptr, png_oFFs, buf, (png_size_t)9);
957}
958#endif
959
960#if defined(PNG_WRITE_pCAL_SUPPORTED)
961/* write the pCAL chunk (png-scivis-19970203) */
962void
963png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
964 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
965{
966 png_size_t purpose_len, units_len, total_len;
967 png_uint_32p params_len;
968 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600969 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500970 int i;
971
972 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
973 if (type >= PNG_EQUATION_LAST)
974 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
975
976 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
977 png_debug1(3, "pCAL purpose length = %d\n", purpose_len);
978 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
979 png_debug1(3, "pCAL units length = %d\n", units_len);
980 total_len = purpose_len + units_len + 10;
981
982 params_len = (png_uint_32p)png_malloc(png_ptr, nparams*sizeof(png_uint_32));
983
984 /* Find the length of each parameter, making sure we don't count the
985 null terminator for the last parameter. */
986 for (i = 0; i < nparams; i++)
987 {
988 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
989 png_debug2(3, "pCAL parameter %d length = %d\n", i, params_len[i]);
990 total_len += (png_size_t)params_len[i];
991 }
992
993 png_debug1(3, "pCAL total length = %d\n", total_len);
994 png_write_chunk_start(png_ptr, png_pCAL, total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600995 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500996 png_save_int_32(buf, X0);
997 png_save_int_32(buf + 4, X1);
998 buf[8] = (png_byte)type;
999 buf[9] = (png_byte)nparams;
1000 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1001 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1002
1003 png_free(png_ptr, new_purpose);
1004
1005 for (i = 0; i < nparams; i++)
1006 {
1007 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1008 (png_size_t)params_len[i]);
1009 }
1010
1011 png_write_chunk_end(png_ptr);
1012}
1013#endif
1014
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001015#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001016/* write the pHYs chunk */
1017void
Guy Schalnat6d764711995-12-19 03:22:19 -06001018png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001019 png_uint_32 y_pixels_per_unit,
1020 int unit_type)
1021{
1022 png_byte buf[9];
1023
Andreas Dilger47a0c421997-05-16 02:46:07 -05001024 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001025 if (unit_type >= PNG_RESOLUTION_LAST)
1026 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1027
Guy Schalnat0d580581995-07-20 02:43:20 -05001028 png_save_uint_32(buf, x_pixels_per_unit);
1029 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001030 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001031
Andreas Dilger47a0c421997-05-16 02:46:07 -05001032 png_write_chunk(png_ptr, png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001033}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001034#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001035
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001036#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001037/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1038 * or png_convert_from_time_t(), or fill in the structure yourself.
1039 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001040void
Guy Schalnat6d764711995-12-19 03:22:19 -06001041png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001042{
1043 png_byte buf[7];
1044
Andreas Dilger47a0c421997-05-16 02:46:07 -05001045 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001046 if (mod_time->month > 12 || mod_time->month < 1 ||
1047 mod_time->day > 31 || mod_time->day < 1 ||
1048 mod_time->hour > 23 || mod_time->second > 60)
1049 {
1050 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1051 return;
1052 }
1053
Guy Schalnat0d580581995-07-20 02:43:20 -05001054 png_save_uint_16(buf, mod_time->year);
1055 buf[2] = mod_time->month;
1056 buf[3] = mod_time->day;
1057 buf[4] = mod_time->hour;
1058 buf[5] = mod_time->minute;
1059 buf[6] = mod_time->second;
1060
Andreas Dilger47a0c421997-05-16 02:46:07 -05001061 png_write_chunk(png_ptr, png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001062}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001063#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001064
1065/* initializes the row writing capability of libpng */
1066void
Guy Schalnat6d764711995-12-19 03:22:19 -06001067png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001068{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001069 png_size_t buf_size;
1070
1071 png_debug(1, "in png_write_start_row\n");
1072 buf_size = (png_size_t)(((png_ptr->width * png_ptr->usr_channels *
1073 png_ptr->usr_bit_depth + 7) >> 3) + 1);
1074
Guy Schalnat0d580581995-07-20 02:43:20 -05001075 /* set up row buffer */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001076 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, buf_size);
1077 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001078
1079 /* set up filtering buffer, if using this filter */
1080 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001081 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001082 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Guy Schalnate5a37791996-06-05 15:50:50 -05001083 png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001084 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001085 }
1086
Andreas Dilger47a0c421997-05-16 02:46:07 -05001087 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001088 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1089 {
1090 /* set up previous row buffer */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001091 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, buf_size);
1092 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001093
1094 if (png_ptr->do_filter & PNG_FILTER_UP)
1095 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001096 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Guy Schalnate5a37791996-06-05 15:50:50 -05001097 png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001098 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001099 }
1100
1101 if (png_ptr->do_filter & PNG_FILTER_AVG)
1102 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001103 png_ptr->avg_row = (png_bytep )png_malloc(png_ptr,
Guy Schalnate5a37791996-06-05 15:50:50 -05001104 png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001105 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001106 }
1107
1108 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1109 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001110 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Guy Schalnate5a37791996-06-05 15:50:50 -05001111 png_ptr->rowbytes + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001112 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001113 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001114 }
1115
1116 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001117 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001118 {
1119 if (!(png_ptr->transformations & PNG_INTERLACE))
1120 {
1121 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1122 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001123 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1124 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001125 }
1126 else
1127 {
1128 png_ptr->num_rows = png_ptr->height;
1129 png_ptr->usr_width = png_ptr->width;
1130 }
1131 }
1132 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001133 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001134 png_ptr->num_rows = png_ptr->height;
1135 png_ptr->usr_width = png_ptr->width;
1136 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001137 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1138 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001139}
1140
Andreas Dilger47a0c421997-05-16 02:46:07 -05001141/* Internal use only. Called when finished processing a row of data. */
Guy Schalnat0d580581995-07-20 02:43:20 -05001142void
Guy Schalnat6d764711995-12-19 03:22:19 -06001143png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001144{
1145 int ret;
1146
Andreas Dilger47a0c421997-05-16 02:46:07 -05001147 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001148 /* next row */
1149 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001150
Guy Schalnat0d580581995-07-20 02:43:20 -05001151 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001152 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001153 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001154
1155 /* if interlaced, go to next pass */
1156 if (png_ptr->interlaced)
1157 {
1158 png_ptr->row_number = 0;
1159 if (png_ptr->transformations & PNG_INTERLACE)
1160 {
1161 png_ptr->pass++;
1162 }
1163 else
1164 {
1165 /* loop until we find a non-zero width or height pass */
1166 do
1167 {
1168 png_ptr->pass++;
1169 if (png_ptr->pass >= 7)
1170 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001171 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001172 png_pass_inc[png_ptr->pass] - 1 -
1173 png_pass_start[png_ptr->pass]) /
1174 png_pass_inc[png_ptr->pass];
1175 png_ptr->num_rows = (png_ptr->height +
1176 png_pass_yinc[png_ptr->pass] - 1 -
1177 png_pass_ystart[png_ptr->pass]) /
1178 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001179 if (png_ptr->transformations & PNG_INTERLACE)
1180 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001181 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1182
1183 }
1184
Guy Schalnate5a37791996-06-05 15:50:50 -05001185 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001186 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001187 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001188 if (png_ptr->prev_row != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001189 png_memset(png_ptr->prev_row, 0,
1190 (png_size_t) (((png_uint_32)png_ptr->usr_channels *
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001191 (png_uint_32)png_ptr->usr_bit_depth *
1192 png_ptr->width + 7) >> 3) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001193 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001194 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001195 }
1196
1197 /* if we get here, we've just written the last row, so we need
1198 to flush the compressor */
1199 do
1200 {
1201 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001202 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001203 /* check for an error */
1204 if (ret != Z_OK && ret != Z_STREAM_END)
1205 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001206 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001207 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001208 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001209 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001210 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001211 /* check to see if we need more room */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001212 if (!(png_ptr->zstream.avail_out) && ret == Z_OK)
Guy Schalnat0d580581995-07-20 02:43:20 -05001213 {
1214 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001215 png_ptr->zstream.next_out = png_ptr->zbuf;
1216 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat0d580581995-07-20 02:43:20 -05001217 }
1218 } while (ret != Z_STREAM_END);
1219
1220 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001221 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001222 {
1223 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001224 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001225 }
1226
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001227 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05001228}
1229
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001230#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001231/* Pick out the correct pixels for the interlace pass.
1232 * The basic idea here is to go through the row with a source
1233 * pointer and a destination pointer (sp and dp), and copy the
1234 * correct pixels for the pass. As the row gets compacted,
1235 * sp will always be >= dp, so we should never overwrite anything.
1236 * See the default: case for the easiest code to understand.
1237 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001238void
Guy Schalnat6d764711995-12-19 03:22:19 -06001239png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001240{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001241 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001242 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001243#if defined(PNG_USELESS_TESTS_SUPPORTED)
1244 if (row != NULL && row_info != NULL && pass < 6)
1245#else
1246 if (pass < 6)
1247#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001248 {
1249 /* each pixel depth is handled seperately */
1250 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001251 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001252 case 1:
1253 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001254 png_bytep sp;
1255 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001256 int shift;
1257 int d;
1258 int value;
1259 png_uint_32 i;
1260
1261 dp = row;
1262 d = 0;
1263 shift = 7;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001264 for (i = png_pass_start[pass]; i < row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001265 i += png_pass_inc[pass])
1266 {
1267 sp = row + (png_size_t)(i >> 3);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001268 value = (int)(*sp >> (7 - (int)(i & 7))) & 0x1;
Guy Schalnat0d580581995-07-20 02:43:20 -05001269 d |= (value << shift);
1270
1271 if (shift == 0)
1272 {
1273 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001274 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001275 d = 0;
1276 }
1277 else
1278 shift--;
1279
1280 }
1281 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001282 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001283 break;
1284 }
1285 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001286 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001287 png_bytep sp;
1288 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001289 int shift;
1290 int d;
1291 int value;
1292 png_uint_32 i;
1293
1294 dp = row;
1295 shift = 6;
1296 d = 0;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001297 for (i = png_pass_start[pass]; i < row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001298 i += png_pass_inc[pass])
1299 {
1300 sp = row + (png_size_t)(i >> 2);
1301 value = (*sp >> ((3 - (int)(i & 3)) << 1)) & 0x3;
1302 d |= (value << shift);
1303
1304 if (shift == 0)
1305 {
1306 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001307 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001308 d = 0;
1309 }
1310 else
1311 shift -= 2;
1312 }
1313 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001314 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001315 break;
1316 }
1317 case 4:
1318 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001319 png_bytep sp;
1320 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001321 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001322 int d;
1323 int value;
1324 png_uint_32 i;
1325
1326 dp = row;
1327 shift = 4;
1328 d = 0;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001329 for (i = png_pass_start[pass]; i < row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001330 i += png_pass_inc[pass])
1331 {
1332 sp = row + (png_size_t)(i >> 1);
1333 value = (*sp >> ((1 - (int)(i & 1)) << 2)) & 0xf;
1334 d |= (value << shift);
1335
1336 if (shift == 0)
1337 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001338 shift = 4;
1339 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001340 d = 0;
1341 }
1342 else
1343 shift -= 4;
1344 }
1345 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001346 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001347 break;
1348 }
1349 default:
1350 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001351 png_bytep sp;
1352 png_bytep dp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001353 png_uint_32 i;
1354 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001355
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001356 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05001357 dp = row;
1358 /* find out how many bytes each pixel takes up */
1359 pixel_bytes = (row_info->pixel_depth >> 3);
1360 /* loop through the row, only looking at the pixels that
1361 matter */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001362 for (i = png_pass_start[pass]; i < row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001363 i += png_pass_inc[pass])
1364 {
1365 /* find out where the original pixel is */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001366 sp = row + i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001367 /* move the pixel */
1368 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001369 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05001370 /* next pixel */
1371 dp += pixel_bytes;
1372 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001373 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001374 }
1375 }
1376 /* set new row width */
1377 row_info->width = (row_info->width +
1378 png_pass_inc[pass] - 1 -
1379 png_pass_start[pass]) /
1380 png_pass_inc[pass];
1381 row_info->rowbytes = ((row_info->width *
1382 row_info->pixel_depth + 7) >> 3);
1383
1384 }
1385}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001386#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001387
Andreas Dilger47a0c421997-05-16 02:46:07 -05001388/* This filters the row, chooses which filter to use, if it has not already
1389 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001390 * chosen filter.
1391 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001392#define PNG_MAXSUM (~0x0UL >> 1)
1393#define PNG_HISHIFT 10
1394#define PNG_LOMASK 0xffffL
1395#define PNG_HIMASK (~PNG_LOMASK >> PNG_HISHIFT)
Guy Schalnat0d580581995-07-20 02:43:20 -05001396void
Guy Schalnate5a37791996-06-05 15:50:50 -05001397png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05001398{
Guy Schalnate5a37791996-06-05 15:50:50 -05001399 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001400 png_uint_32 mins, bpp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001401
Andreas Dilger47a0c421997-05-16 02:46:07 -05001402 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001403 /* find out how many bytes offset each pixel is */
1404 bpp = (row_info->pixel_depth + 7) / 8;
Guy Schalnate5a37791996-06-05 15:50:50 -05001405
1406 prev_row = png_ptr->prev_row;
1407 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001408 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05001409
Andreas Dilger47a0c421997-05-16 02:46:07 -05001410 /* The prediction method we use is to find which method provides the
1411 * smallest value when summing the absolute values of the distances
1412 * from zero using anything >= 128 as negative numbers. This is known
1413 * as the "minimum sum of absolute differences" heuristic. Other
1414 * heruistics are the "weighted minumum sum of absolute differences"
1415 * (experimental and can in theory improve compression), and the "zlib
1416 * predictive" method (not implemented in libpng 0.95), which does test
1417 * compressions of lines using different filter methods, and then chooses
1418 * the (series of) filter(s) which give minimum compressed data size (VERY
1419 * computationally expensive).
1420 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001421
Guy Schalnate5a37791996-06-05 15:50:50 -05001422 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05001423 * that has been chosen, as it doesn't actually do anything to the data.
1424 */
Guy Schalnate5a37791996-06-05 15:50:50 -05001425 if (png_ptr->do_filter & PNG_FILTER_NONE &&
1426 png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05001427 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001428 png_bytep rp;
1429 png_uint_32 sum = 0;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001430 png_size_t i;
1431 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05001432
Guy Schalnate5a37791996-06-05 15:50:50 -05001433 for (i = 0, rp = row_buf + 1; i < row_info->rowbytes; i++, rp++)
1434 {
1435 v = *rp;
1436 sum += (v < 128) ? v : 256 - v;
1437 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001438
1439#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1440 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1441 {
1442 png_uint_32 sumhi, sumlo;
1443 sumlo = sum & PNG_LOMASK;
1444 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
1445
1446 /* Reduce the sum if we match any of the previous rows */
1447 for (i = 0; i < png_ptr->num_prev_filters; i++)
1448 {
1449 if (png_ptr->prev_filters[i] == PNG_FILTER_NONE)
1450 {
1451 sumlo = (sumlo * png_ptr->filter_weights[i]) >>
1452 PNG_WEIGHT_SHIFT;
1453 sumhi = (sumhi * png_ptr->filter_weights[i]) >>
1454 PNG_WEIGHT_SHIFT;
1455 }
1456 }
1457
1458 /* Factor in the cost of this filter (this is here for completeness,
1459 * but it makes no sense to have a "cost" for the NONE filter, as
1460 * it has the minimum possible computational cost - none).
1461 */
1462 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
1463 PNG_COST_SHIFT;
1464 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
1465 PNG_COST_SHIFT;
1466
1467 if (sumhi > PNG_HIMASK)
1468 sum = PNG_MAXSUM;
1469 else
1470 sum = (sumhi << PNG_HISHIFT) + sumlo;
1471 }
1472#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001473 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05001474 }
1475
Guy Schalnate5a37791996-06-05 15:50:50 -05001476 /* sub filter */
1477 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001478 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001479 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001480 png_uint_32 sum = 0, lmins = mins;
1481 png_size_t i;
1482 int v;
1483
1484#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1485 /* We temporarily increase the "minumum sum" by the factor we
1486 * would reduce the sum of this filter, so that we can do the
1487 * early exit comparison without scaling the sum each time.
1488 */
1489 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1490 {
1491 png_uint_32 lmhi, lmlo;
1492 lmlo = lmins & PNG_LOMASK;
1493 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1494
1495 for (i = 0; i < png_ptr->num_prev_filters; i++)
1496 {
1497 if (png_ptr->prev_filters[i] == PNG_FILTER_VALUE_SUB)
1498 {
1499 lmlo = (lmlo * png_ptr->inv_filter_weights[i]) >>
1500 PNG_WEIGHT_SHIFT;
1501 lmhi = (lmhi * png_ptr->inv_filter_weights[i]) >>
1502 PNG_WEIGHT_SHIFT;
1503 }
1504 }
1505
1506 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1507 PNG_COST_SHIFT;
1508 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1509 PNG_COST_SHIFT;
1510
1511 if (lmhi > PNG_HIMASK)
1512 lmins = PNG_MAXSUM;
1513 else
1514 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1515 }
1516#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001517
Guy Schalnate5a37791996-06-05 15:50:50 -05001518 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
1519 i++, rp++, dp++)
1520 {
1521 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001522
Guy Schalnate5a37791996-06-05 15:50:50 -05001523 sum += (v < 128) ? v : 256 - v;
1524 }
1525 for (lp = row_buf + 1; i < row_info->rowbytes; i++, rp++, lp++, dp++)
1526 {
1527 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001528
Guy Schalnate5a37791996-06-05 15:50:50 -05001529 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001530
1531 if (sum > lmins) /* We are already worse, don't continue. */
1532 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001533 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001534
1535#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1536 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1537 {
1538 png_uint_32 sumhi, sumlo;
1539 sumlo = sum & PNG_LOMASK;
1540 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1541
1542 for (i = 0; i < png_ptr->num_prev_filters; i++)
1543 {
1544 if (png_ptr->prev_filters[i] == PNG_FILTER_VALUE_SUB)
1545 {
1546 sumlo = (sumlo * png_ptr->inv_filter_weights[i]) >>
1547 PNG_WEIGHT_SHIFT;
1548 sumhi = (sumhi * png_ptr->inv_filter_weights[i]) >>
1549 PNG_WEIGHT_SHIFT;
1550 }
1551 }
1552
1553 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1554 PNG_COST_SHIFT;
1555 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1556 PNG_COST_SHIFT;
1557
1558 if (sumhi > PNG_HIMASK)
1559 sum = PNG_MAXSUM;
1560 else
1561 sum = (sumhi << PNG_HISHIFT) + sumlo;
1562 }
1563#endif
1564
Guy Schalnate5a37791996-06-05 15:50:50 -05001565 if (sum < mins)
1566 {
1567 mins = sum;
1568 best_row = png_ptr->sub_row;
1569 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001570 }
1571
Guy Schalnate5a37791996-06-05 15:50:50 -05001572 /* up filter */
1573 if (png_ptr->do_filter & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05001574 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001575 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001576 png_uint_32 sum = 0, lmins = mins;
1577 png_size_t i;
1578 int v;
1579
1580#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1581 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1582 {
1583 png_uint_32 lmhi, lmlo;
1584 lmlo = lmins & PNG_LOMASK;
1585 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1586
1587 for (i = 0; i < png_ptr->num_prev_filters; i++)
1588 {
1589 if (png_ptr->prev_filters[i] == PNG_FILTER_VALUE_UP)
1590 {
1591 lmlo = (lmlo * png_ptr->inv_filter_weights[i]) >>
1592 PNG_WEIGHT_SHIFT;
1593 lmhi = (lmhi * png_ptr->inv_filter_weights[i]) >>
1594 PNG_WEIGHT_SHIFT;
1595 }
1596 }
1597
1598 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
1599 PNG_COST_SHIFT;
1600 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
1601 PNG_COST_SHIFT;
1602
1603 if (lmhi > PNG_HIMASK)
1604 lmins = PNG_MAXSUM;
1605 else
1606 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1607 }
1608#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001609
1610 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
1611 pp = prev_row + 1; i < row_info->rowbytes; i++, rp++, pp++, dp++)
1612 {
1613 v = *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
1614
1615 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001616
1617 if (sum > lmins) /* We are already worse, don't continue. */
1618 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001619 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001620
1621#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1622 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1623 {
1624 png_uint_32 sumhi, sumlo;
1625 sumlo = sum & PNG_LOMASK;
1626 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1627
1628 for (i = 0; i < png_ptr->num_prev_filters; i++)
1629 {
1630 if (png_ptr->prev_filters[i] == PNG_FILTER_UP)
1631 {
1632 sumlo = (sumlo * png_ptr->filter_weights[i]) >>
1633 PNG_WEIGHT_SHIFT;
1634 sumhi = (sumhi * png_ptr->filter_weights[i]) >>
1635 PNG_WEIGHT_SHIFT;
1636 }
1637 }
1638
1639 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
1640 PNG_COST_SHIFT;
1641 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
1642 PNG_COST_SHIFT;
1643
1644 if (sumhi > PNG_HIMASK)
1645 sum = PNG_MAXSUM;
1646 else
1647 sum = (sumhi << PNG_HISHIFT) + sumlo;
1648 }
1649#endif
1650
Guy Schalnate5a37791996-06-05 15:50:50 -05001651 if (sum < mins)
1652 {
1653 mins = sum;
1654 best_row = png_ptr->up_row;
1655 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001656 }
1657
Guy Schalnate5a37791996-06-05 15:50:50 -05001658 /* avg filter */
1659 if (png_ptr->do_filter & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001660 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001661 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001662 png_uint_32 sum = 0, lmins = mins;
1663 png_size_t i;
1664 int v;
1665
1666#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1667 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1668 {
1669 png_uint_32 lmhi, lmlo;
1670 lmlo = lmins & PNG_LOMASK;
1671 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1672
1673 for (i = 0; i < png_ptr->num_prev_filters; i++)
1674 {
1675 if (png_ptr->prev_filters[i] == PNG_FILTER_VALUE_AVG)
1676 {
1677 lmlo = (lmlo * png_ptr->inv_filter_weights[i]) >>
1678 PNG_WEIGHT_SHIFT;
1679 lmhi = (lmhi * png_ptr->inv_filter_weights[i]) >>
1680 PNG_WEIGHT_SHIFT;
1681 }
1682 }
1683
1684 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
1685 PNG_COST_SHIFT;
1686 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
1687 PNG_COST_SHIFT;
1688
1689 if (lmhi > PNG_HIMASK)
1690 lmins = PNG_MAXSUM;
1691 else
1692 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1693 }
1694#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001695
1696 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
1697 pp = prev_row + 1; i < bpp; i++, rp++, pp++, dp++)
1698 {
1699 v = *dp = (png_byte)(((int)*rp - ((int)*pp / 2)) & 0xff);
1700
1701 sum += (v < 128) ? v : 256 - v;
1702 }
1703 for (lp = row_buf + 1; i < row_info->rowbytes;
1704 i++, rp++, pp++, lp++, dp++)
1705 {
1706 v = *dp = (png_byte)(((int)*rp - (((int)*pp + (int)*lp) / 2)) & 0xff);
1707
1708 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001709
1710 if (sum > lmins) /* We are already worse, don't continue. */
1711 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001712 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001713
1714#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1715 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1716 {
1717 png_uint_32 sumhi, sumlo;
1718 sumlo = sum & PNG_LOMASK;
1719 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1720
1721 for (i = 0; i < png_ptr->num_prev_filters; i++)
1722 {
1723 if (png_ptr->prev_filters[i] == PNG_FILTER_NONE)
1724 {
1725 sumlo = (sumlo * png_ptr->filter_weights[i]) >>
1726 PNG_WEIGHT_SHIFT;
1727 sumhi = (sumhi * png_ptr->filter_weights[i]) >>
1728 PNG_WEIGHT_SHIFT;
1729 }
1730 }
1731
1732 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
1733 PNG_COST_SHIFT;
1734 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
1735 PNG_COST_SHIFT;
1736
1737 if (sumhi > PNG_HIMASK)
1738 sum = PNG_MAXSUM;
1739 else
1740 sum = (sumhi << PNG_HISHIFT) + sumlo;
1741 }
1742#endif
1743
Guy Schalnate5a37791996-06-05 15:50:50 -05001744 if (sum < mins)
1745 {
1746 mins = sum;
1747 best_row = png_ptr->avg_row;
1748 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001749 }
1750
Andreas Dilger47a0c421997-05-16 02:46:07 -05001751 /* Paeth filter */
Guy Schalnate5a37791996-06-05 15:50:50 -05001752 if (png_ptr->do_filter & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001753 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001754 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001755 png_uint_32 sum = 0, lmins = mins;
1756 png_size_t i;
1757 int v;
1758
1759#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1760 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1761 {
1762 png_uint_32 lmhi, lmlo;
1763 lmlo = lmins & PNG_LOMASK;
1764 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1765
1766 for (i = 0; i < png_ptr->num_prev_filters; i++)
1767 {
1768 if (png_ptr->prev_filters[i] == PNG_FILTER_VALUE_PAETH)
1769 {
1770 lmlo = (lmlo * png_ptr->inv_filter_weights[i]) >>
1771 PNG_WEIGHT_SHIFT;
1772 lmhi = (lmhi * png_ptr->inv_filter_weights[i]) >>
1773 PNG_WEIGHT_SHIFT;
1774 }
1775 }
1776
1777 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1778 PNG_COST_SHIFT;
1779 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1780 PNG_COST_SHIFT;
1781
1782 if (lmhi > PNG_HIMASK)
1783 lmins = PNG_MAXSUM;
1784 else
1785 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1786 }
1787#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001788
1789 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
1790 pp = prev_row + 1; i < bpp; i++, rp++, pp++, dp++)
1791 {
1792 v = *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
1793
1794 sum += (v < 128) ? v : 256 - v;
1795 }
1796 for (lp = row_buf + 1, cp = prev_row + 1; i < row_info->rowbytes;
1797 i++, rp++, pp++, lp++, dp++, cp++)
1798 {
1799 int a, b, c, pa, pb, pc, p;
1800
1801 b = *pp;
1802 c = *cp;
1803 a = *lp;
1804
1805 p = a + b - c;
1806 pa = abs(p - a);
1807 pb = abs(p - b);
1808 pc = abs(p - c);
1809
1810 if (pa <= pb && pa <= pc)
1811 p = a;
1812 else if (pb <= pc)
1813 p = b;
1814 else
1815 p = c;
1816
1817 v = *dp = (png_byte)(((int)*rp - p) & 0xff);
1818
1819 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001820
1821 if (sum > lmins) /* We are already worse, don't continue. */
1822 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001823 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001824
1825#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1826 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1827 {
1828 png_uint_32 sumhi, sumlo;
1829 sumlo = sum & PNG_LOMASK;
1830 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1831
1832 for (i = 0; i < png_ptr->num_prev_filters; i++)
1833 {
1834 if (png_ptr->prev_filters[i] == PNG_FILTER_PAETH)
1835 {
1836 sumlo = (sumlo * png_ptr->filter_weights[i]) >>
1837 PNG_WEIGHT_SHIFT;
1838 sumhi = (sumhi * png_ptr->filter_weights[i]) >>
1839 PNG_WEIGHT_SHIFT;
1840 }
1841 }
1842
1843 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1844 PNG_COST_SHIFT;
1845 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1846 PNG_COST_SHIFT;
1847
1848 if (sumhi > PNG_HIMASK)
1849 sum = PNG_MAXSUM;
1850 else
1851 sum = (sumhi << PNG_HISHIFT) + sumlo;
1852 }
1853#endif
1854
Guy Schalnate5a37791996-06-05 15:50:50 -05001855 if (sum < mins)
1856 {
1857 best_row = png_ptr->paeth_row;
1858 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001859 }
1860
Andreas Dilger47a0c421997-05-16 02:46:07 -05001861 /* Do the actual writing of the filtered row data from the chosen filter. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001862 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001863
1864#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1865 /* Save the type of filter we picked this time for future calculations */
1866 if (png_ptr->num_prev_filters > 0)
1867 {
1868 png_byte i;
1869
1870 for (i = 1; i < png_ptr->num_prev_filters; i++)
1871 {
1872 png_ptr->prev_filters[i] = png_ptr->prev_filters[i - 1];
1873 }
1874 png_ptr->prev_filters[i] = best_row[0];
1875 }
1876#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001877}
1878
1879
Andreas Dilger47a0c421997-05-16 02:46:07 -05001880/* Do the actual writing of a previously filtered row. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001881void
1882png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
1883{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001884 png_debug(1, "in png_write_filtered_row\n");
1885 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05001886 /* set up the zlib input buffer */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001887 png_ptr->zstream.next_in = filtered_row;
1888 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05001889 /* repeat until we have compressed all the data */
1890 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001891 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001892 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05001893
Guy Schalnate5a37791996-06-05 15:50:50 -05001894 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001895 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05001896 /* check for compression errors */
1897 if (ret != Z_OK)
1898 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001899 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001900 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05001901 else
1902 png_error(png_ptr, "zlib error");
1903 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001904
Guy Schalnate5a37791996-06-05 15:50:50 -05001905 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001906 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05001907 {
1908 /* write the IDAT and reset the zlib output buffer */
1909 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001910 png_ptr->zstream.next_out = png_ptr->zbuf;
1911 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05001912 }
1913 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001914 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05001915
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001916 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001917 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001918 {
1919 png_bytep tptr;
1920
1921 tptr = png_ptr->prev_row;
1922 png_ptr->prev_row = png_ptr->row_buf;
1923 png_ptr->row_buf = tptr;
1924 }
1925
Guy Schalnate5a37791996-06-05 15:50:50 -05001926 /* finish row - updates counters and flushes zlib if last row */
1927 png_write_finish_row(png_ptr);
1928
1929#if defined(PNG_WRITE_FLUSH_SUPPORTED)
1930 png_ptr->flush_rows++;
1931
1932 if (png_ptr->flush_dist > 0 &&
1933 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001934 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001935 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001936 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001937#endif /* PNG_WRITE_FLUSH_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05001938}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001939