blob: 8e0dc219152d88fe1773092d3968e8410bb298ba [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-Pehrson8686fff1998-05-21 09:27:50 -05004 * 1.0.1d
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06008 * Copyright (c) 1998, Glenn Randers-Pehrson
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05009 * May 21, 1998
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060010 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050011
Guy Schalnat0d580581995-07-20 02:43:20 -050012#define PNG_INTERNAL
13#include "png.h"
14
Andreas Dilger47a0c421997-05-16 02:46:07 -050015/* Place a 32-bit number into a buffer in PNG byte order. We work
16 * with unsigned numbers for convenience, although one supported
17 * ancillary chunk uses signed (two's complement) numbers.
18 */
Guy Schalnat0d580581995-07-20 02:43:20 -050019void
Guy Schalnat6d764711995-12-19 03:22:19 -060020png_save_uint_32(png_bytep buf, png_uint_32 i)
Guy Schalnat0d580581995-07-20 02:43:20 -050021{
22 buf[0] = (png_byte)((i >> 24) & 0xff);
23 buf[1] = (png_byte)((i >> 16) & 0xff);
24 buf[2] = (png_byte)((i >> 8) & 0xff);
25 buf[3] = (png_byte)(i & 0xff);
26}
27
Andreas Dilger47a0c421997-05-16 02:46:07 -050028#if defined(PNG_WRITE_pCAL_SUPPORTED)
29/* The png_save_int_32 function assumes integers are stored in two's
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060030 * complement format. If this isn't the case, then this routine needs to
31 * be modified to write data in two's complement format.
32 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050033void
34png_save_int_32(png_bytep buf, png_int_32 i)
35{
36 buf[0] = (png_byte)((i >> 24) & 0xff);
37 buf[1] = (png_byte)((i >> 16) & 0xff);
38 buf[2] = (png_byte)((i >> 8) & 0xff);
39 buf[3] = (png_byte)(i & 0xff);
40}
41#endif
42
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060043/* Place a 16-bit number into a buffer in PNG byte order.
44 * The parameter is declared unsigned int, not png_uint_16,
45 * just to avoid potential problems on pre-ANSI C compilers.
46 */
Guy Schalnat0d580581995-07-20 02:43:20 -050047void
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060048png_save_uint_16(png_bytep buf, unsigned int i)
Guy Schalnat0d580581995-07-20 02:43:20 -050049{
50 buf[0] = (png_byte)((i >> 8) & 0xff);
51 buf[1] = (png_byte)(i & 0xff);
52}
53
Andreas Dilger47a0c421997-05-16 02:46:07 -050054/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060055 * representing the chunk name. The array must be at least 4 bytes in
56 * length, and does not need to be null terminated. To be safe, pass the
57 * pre-defined chunk names here, and if you need a new one, define it
58 * where the others are defined. The length is the length of the data.
59 * All the data must be present. If that is not possible, use the
60 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
61 * functions instead.
62 */
Guy Schalnat0d580581995-07-20 02:43:20 -050063void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060064png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050065 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050066{
Andreas Dilger47a0c421997-05-16 02:46:07 -050067 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060068 png_write_chunk_data(png_ptr, data, length);
69 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050070}
71
Andreas Dilger47a0c421997-05-16 02:46:07 -050072/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060073 * The total_length is the sum of the lengths of all the data you will be
74 * passing in png_write_chunk_data().
75 */
Guy Schalnat0d580581995-07-20 02:43:20 -050076void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060077png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
78 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -050079{
Andreas Dilger47a0c421997-05-16 02:46:07 -050080 png_byte buf[4];
81 png_debug2(0, "Writing %s chunk (%d bytes)\n", chunk_name, length);
82
Guy Schalnat0d580581995-07-20 02:43:20 -050083 /* write the length */
Andreas Dilger47a0c421997-05-16 02:46:07 -050084 png_save_uint_32(buf, length);
85 png_write_data(png_ptr, buf, (png_size_t)4);
86
Guy Schalnat0d580581995-07-20 02:43:20 -050087 /* write the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -050088 png_write_data(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050089 /* reset the crc and run it over the chunk name */
90 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -050091 png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050092}
93
Andreas Dilger47a0c421997-05-16 02:46:07 -050094/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060095 * Note that multiple calls to this function are allowed, and that the
96 * sum of the lengths from these calls *must* add up to the total_length
97 * given to png_write_chunk_start().
98 */
Guy Schalnat0d580581995-07-20 02:43:20 -050099void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500100png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500101{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500102 /* write the data, and run the CRC over it */
103 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500104 {
105 png_calculate_crc(png_ptr, data, length);
Guy Schalnat6d764711995-12-19 03:22:19 -0600106 png_write_data(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500107 }
108}
109
Andreas Dilger47a0c421997-05-16 02:46:07 -0500110/* Finish a chunk started with png_write_chunk_start(). */
Guy Schalnat0d580581995-07-20 02:43:20 -0500111void
Guy Schalnat6d764711995-12-19 03:22:19 -0600112png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500113{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500114 png_byte buf[4];
115
Guy Schalnat0d580581995-07-20 02:43:20 -0500116 /* write the crc */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500117 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500118
119 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500120}
121
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600122/* Simple function to write the signature. If we have already written
123 * the magic bytes of the signature, or more likely, the PNG stream is
124 * being embedded into another stream and doesn't need its own signature,
125 * we should call png_set_sig_bytes() to tell libpng how many of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600126 * bytes have already been written.
127 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500128void
Guy Schalnat6d764711995-12-19 03:22:19 -0600129png_write_sig(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500130{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600131 /* write the rest of the 8 byte signature */
132 png_write_data(png_ptr, &png_sig[png_ptr->sig_bytes],
Andreas Dilger47a0c421997-05-16 02:46:07 -0500133 (png_size_t)8 - png_ptr->sig_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -0500134}
135
136/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600137 * information. Note that the rest of this code depends upon this
138 * information being correct.
139 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500140void
Guy Schalnat6d764711995-12-19 03:22:19 -0600141png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500142 int bit_depth, int color_type, int compression_type, int filter_type,
143 int interlace_type)
144{
145 png_byte buf[13]; /* buffer to store the IHDR info */
146
Andreas Dilger47a0c421997-05-16 02:46:07 -0500147 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500148 /* Check that we have valid input data from the application info */
149 switch (color_type)
150 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500151 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500152 switch (bit_depth)
153 {
154 case 1:
155 case 2:
156 case 4:
157 case 8:
158 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500159 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500160 }
161 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500162 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500163 if (bit_depth != 8 && bit_depth != 16)
164 png_error(png_ptr, "Invalid bit depth for RGB image");
165 png_ptr->channels = 3;
166 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500167 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500168 switch (bit_depth)
169 {
170 case 1:
171 case 2:
172 case 4:
173 case 8: png_ptr->channels = 1; break;
174 default: png_error(png_ptr, "Invalid bit depth for paletted image");
175 }
176 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500177 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500178 if (bit_depth != 8 && bit_depth != 16)
179 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
180 png_ptr->channels = 2;
181 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500182 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500183 if (bit_depth != 8 && bit_depth != 16)
184 png_error(png_ptr, "Invalid bit depth for RGBA image");
185 png_ptr->channels = 4;
186 break;
187 default:
188 png_error(png_ptr, "Invalid image color type specified");
189 }
190
Andreas Dilger47a0c421997-05-16 02:46:07 -0500191 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500192 {
193 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500194 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500195 }
196
Andreas Dilger47a0c421997-05-16 02:46:07 -0500197 if (filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500198 {
199 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500200 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500201 }
202
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600203#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500204 if (interlace_type != PNG_INTERLACE_NONE &&
205 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500206 {
207 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500208 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500209 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600210#else
211 interlace_type=PNG_INTERLACE_NONE;
212#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500213
214 /* save off the relevent information */
215 png_ptr->bit_depth = (png_byte)bit_depth;
216 png_ptr->color_type = (png_byte)color_type;
217 png_ptr->interlaced = (png_byte)interlace_type;
218 png_ptr->width = width;
219 png_ptr->height = height;
220
221 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500222 png_ptr->rowbytes = ((width * (png_size_t)png_ptr->pixel_depth + 7) >> 3);
Guy Schalnate5a37791996-06-05 15:50:50 -0500223 /* set the usr info, so any transformations can modify it */
224 png_ptr->usr_width = png_ptr->width;
225 png_ptr->usr_bit_depth = png_ptr->bit_depth;
226 png_ptr->usr_channels = png_ptr->channels;
227
Guy Schalnat0d580581995-07-20 02:43:20 -0500228 /* pack the header information into the buffer */
229 png_save_uint_32(buf, width);
230 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600231 buf[8] = (png_byte)bit_depth;
232 buf[9] = (png_byte)color_type;
233 buf[10] = (png_byte)compression_type;
234 buf[11] = (png_byte)filter_type;
235 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500236
237 /* write the chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500238 png_write_chunk(png_ptr, png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500239
Andreas Dilger47a0c421997-05-16 02:46:07 -0500240 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600241 png_ptr->zstream.zalloc = png_zalloc;
242 png_ptr->zstream.zfree = png_zfree;
243 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500244 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500245 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500246 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
247 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500248 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500249 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500250 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500251 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500252 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500253 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500254 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500255 png_ptr->zlib_strategy = Z_FILTERED;
256 else
257 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
258 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500259 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500260 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500261 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500262 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500263 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500264 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500265 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500266 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600267 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500268 png_ptr->zlib_method, png_ptr->zlib_window_bits,
269 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600270 png_ptr->zstream.next_out = png_ptr->zbuf;
271 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500272
Guy Schalnate5a37791996-06-05 15:50:50 -0500273 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500274}
275
276/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600277 * correct order for PNG, so people can redefine it to any convient
278 * structure.
279 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500280void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500281png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500282{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500283 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600284 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500285 png_byte buf[3];
286
Andreas Dilger47a0c421997-05-16 02:46:07 -0500287 png_debug(1, "in png_write_PLTE\n");
288 if (num_pal == 0 || num_pal > 256)
Guy Schalnate5a37791996-06-05 15:50:50 -0500289 {
290 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
291 {
292 png_error(png_ptr, "Invalid number of colors in palette");
293 }
294 else
295 {
296 png_warning(png_ptr, "Invalid number of colors in palette");
297 return;
298 }
299 }
300
Andreas Dilger47a0c421997-05-16 02:46:07 -0500301 png_ptr->num_palette = (png_uint_16)num_pal;
302 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500303
Andreas Dilger47a0c421997-05-16 02:46:07 -0500304 png_write_chunk_start(png_ptr, png_PLTE, num_pal * 3);
305 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500306 {
307 buf[0] = pal_ptr->red;
308 buf[1] = pal_ptr->green;
309 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500310 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500311 }
312 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500313 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500314}
315
316/* write an IDAT chunk */
317void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500318png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500319{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500320 png_debug(1, "in png_write_IDAT\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500321 png_write_chunk(png_ptr, png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500322 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500323}
324
325/* write an IEND chunk */
326void
Guy Schalnat6d764711995-12-19 03:22:19 -0600327png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500328{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500329 png_debug(1, "in png_write_IEND\n");
330 png_write_chunk(png_ptr, png_IEND, NULL, (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600331 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500332}
333
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500334#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500335/* write a gAMA chunk */
336void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500337png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500338{
339 png_uint_32 igamma;
340 png_byte buf[4];
341
Andreas Dilger47a0c421997-05-16 02:46:07 -0500342 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -0600343 /* file_gamma is saved in 1/1000000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600344 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500345 png_save_uint_32(buf, igamma);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500346 png_write_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500347}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500348#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500349
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600350#if defined(PNG_WRITE_sRGB_SUPPORTED)
351/* write a sRGB chunk */
352void
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600353png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600354{
355 png_byte buf[1];
356
357 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600358 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600359 png_warning(png_ptr,
360 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600361 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600362 png_write_chunk(png_ptr, png_sRGB, buf, (png_size_t)1);
363}
364#endif
365
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500366#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500367/* write the sBIT chunk */
368void
Guy Schalnat6d764711995-12-19 03:22:19 -0600369png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500370{
371 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500372 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500373
Andreas Dilger47a0c421997-05-16 02:46:07 -0500374 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600375 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500376 if (color_type & PNG_COLOR_MASK_COLOR)
377 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600378 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500379
380 maxbits = color_type==PNG_COLOR_TYPE_PALETTE ? 8:png_ptr->usr_bit_depth;
381 if (sbit->red == 0 || sbit->red > maxbits ||
382 sbit->green == 0 || sbit->green > maxbits ||
383 sbit->blue == 0 || sbit->blue > maxbits)
384 {
385 png_warning(png_ptr, "Invalid sBIT depth specified");
386 return;
387 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500388 buf[0] = sbit->red;
389 buf[1] = sbit->green;
390 buf[2] = sbit->blue;
391 size = 3;
392 }
393 else
394 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500395 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
396 {
397 png_warning(png_ptr, "Invalid sBIT depth specified");
398 return;
399 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500400 buf[0] = sbit->gray;
401 size = 1;
402 }
403
404 if (color_type & PNG_COLOR_MASK_ALPHA)
405 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500406 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
407 {
408 png_warning(png_ptr, "Invalid sBIT depth specified");
409 return;
410 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500411 buf[size++] = sbit->alpha;
412 }
413
Andreas Dilger47a0c421997-05-16 02:46:07 -0500414 png_write_chunk(png_ptr, png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500415}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500416#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500417
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500418#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500419/* write the cHRM chunk */
420void
Andreas Dilger47a0c421997-05-16 02:46:07 -0500421png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600422 double red_x, double red_y, double green_x, double green_y,
423 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500424{
425 png_uint_32 itemp;
426 png_byte buf[32];
427
Andreas Dilger47a0c421997-05-16 02:46:07 -0500428 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -0600429 /* each value is saved int 1/1000000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500430 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
431 white_x + white_y > 1.0)
432 {
433 png_warning(png_ptr, "Invalid cHRM white point specified");
434 return;
435 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600436 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500437 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600438 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500439 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500440
441 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
442 red_x + red_y > 1.0)
443 {
444 png_warning(png_ptr, "Invalid cHRM red point specified");
445 return;
446 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600447 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500448 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600449 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500450 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500451
452 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
453 green_x + green_y > 1.0)
454 {
455 png_warning(png_ptr, "Invalid cHRM green point specified");
456 return;
457 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600458 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500459 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600460 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500461 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500462
463 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
464 blue_x + blue_y > 1.0)
465 {
466 png_warning(png_ptr, "Invalid cHRM blue point specified");
467 return;
468 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600469 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500470 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600471 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500472 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500473
Andreas Dilger47a0c421997-05-16 02:46:07 -0500474 png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500475}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500476#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500477
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500478#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500479/* write the tRNS chunk */
480void
Guy Schalnat6d764711995-12-19 03:22:19 -0600481png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -0500482 int num_trans, int color_type)
483{
484 png_byte buf[6];
485
Andreas Dilger47a0c421997-05-16 02:46:07 -0500486 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500487 if (color_type == PNG_COLOR_TYPE_PALETTE)
488 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600489 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500490 {
491 png_warning(png_ptr,"Invalid number of transparent colors specified");
492 return;
493 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500494 /* write the chunk out as it is */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500495 png_write_chunk(png_ptr, png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -0500496 }
497 else if (color_type == PNG_COLOR_TYPE_GRAY)
498 {
499 /* one 16 bit value */
500 png_save_uint_16(buf, tran->gray);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500501 png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500502 }
503 else if (color_type == PNG_COLOR_TYPE_RGB)
504 {
505 /* three 16 bit values */
506 png_save_uint_16(buf, tran->red);
507 png_save_uint_16(buf + 2, tran->green);
508 png_save_uint_16(buf + 4, tran->blue);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500509 png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500510 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500511 else
512 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600513 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -0500514 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500515}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500516#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500517
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500518#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500519/* write the background chunk */
520void
Guy Schalnat6d764711995-12-19 03:22:19 -0600521png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500522{
523 png_byte buf[6];
524
Andreas Dilger47a0c421997-05-16 02:46:07 -0500525 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -0500526 if (color_type == PNG_COLOR_TYPE_PALETTE)
527 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500528 if (back->index > png_ptr->num_palette)
529 {
530 png_warning(png_ptr, "Invalid background palette index");
531 return;
532 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500533 buf[0] = back->index;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500534 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500535 }
536 else if (color_type & PNG_COLOR_MASK_COLOR)
537 {
538 png_save_uint_16(buf, back->red);
539 png_save_uint_16(buf + 2, back->green);
540 png_save_uint_16(buf + 4, back->blue);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500541 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -0500542 }
543 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600544 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500545 png_save_uint_16(buf, back->gray);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500546 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500547 }
548}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500549#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500550
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500551#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500552/* write the histogram */
553void
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600554png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -0500555{
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600556 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500557 png_byte buf[3];
558
Andreas Dilger47a0c421997-05-16 02:46:07 -0500559 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600560 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -0500561 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500562 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
563 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500564 png_warning(png_ptr, "Invalid number of histogram entries specified");
565 return;
566 }
567
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600568 png_write_chunk_start(png_ptr, png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500569 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500570 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600571 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500572 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -0500573 }
574 png_write_chunk_end(png_ptr);
575}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500576#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500577
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600578#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
579/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
580 * and if invalid, correct the keyword rather than discarding the entire
581 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
582 * length, forbids leading or trailing whitespace, multiple internal spaces,
583 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -0500584 *
585 * The new_key is allocated to hold the corrected keyword and must be freed
586 * by the calling routine. This avoids problems with trying to write to
587 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600588 */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500589png_size_t
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600590png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600591{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500592 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600593 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600594 int kflag;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600595
Andreas Dilger47a0c421997-05-16 02:46:07 -0500596 png_debug(1, "in png_check_keyword\n");
597 *new_key = NULL;
598
599 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600600 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600601 png_chunk_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600602 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600603 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600604
Andreas Dilger47a0c421997-05-16 02:46:07 -0500605 png_debug1(2, "Keyword to be checked is '%s'\n", key);
606
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600607 *new_key = (png_charp)png_malloc(png_ptr, (png_uint_32)(key_len + 1));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600608
609 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500610 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600611 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600612 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -0500613 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600614#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500615 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600616
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600617 sprintf(msg, "invalid keyword character 0x%02X", *kp);
618 png_chunk_warning(png_ptr, msg);
619#else
620 png_chunk_warning(png_ptr, "invalid character in keyword");
621#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500622 *dp = ' ';
623 }
624 else
625 {
626 *dp = *kp;
627 }
628 }
629 *dp = '\0';
630
631 /* Remove any trailing white space. */
632 kp = *new_key + key_len - 1;
633 if (*kp == ' ')
634 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600635 png_chunk_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500636
637 while (*kp == ' ')
638 {
639 *(kp--) = '\0';
640 key_len--;
641 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600642 }
643
644 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500645 kp = *new_key;
646 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600647 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600648 png_chunk_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500649
650 while (*kp == ' ')
651 {
652 kp++;
653 key_len--;
654 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600655 }
656
Andreas Dilger47a0c421997-05-16 02:46:07 -0500657 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600658
Andreas Dilger47a0c421997-05-16 02:46:07 -0500659 /* Remove multiple internal spaces. */
660 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600661 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500662 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600663 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500664 *(dp++) = *kp;
665 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600666 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500667 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600668 {
669 key_len--;
670 }
671 else
672 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500673 *(dp++) = *kp;
674 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600675 }
676 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500677 *dp = '\0';
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600678
679 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500680 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600681 png_chunk_warning(png_ptr, "zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500682 }
683
684 if (key_len > 79)
685 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600686 png_chunk_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500687 new_key[79] = '\0';
688 key_len = 79;
689 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600690
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600691 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600692}
693#endif
694
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500695#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500696/* write a tEXt chunk */
697void
Guy Schalnat6d764711995-12-19 03:22:19 -0600698png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500699 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -0500700{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500701 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600702 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -0500703
Andreas Dilger47a0c421997-05-16 02:46:07 -0500704 png_debug(1, "in png_write_tEXt\n");
705 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
706 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600707 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -0500708 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500709 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500710
Andreas Dilger47a0c421997-05-16 02:46:07 -0500711 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600712 text_len = 0;
713
714 /* make sure we include the 0 after the key */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500715 png_write_chunk_start(png_ptr, png_tEXt, (png_uint_32)key_len+text_len+1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600716 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600717 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500718 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600719
Guy Schalnat0d580581995-07-20 02:43:20 -0500720 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500721 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -0500722}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500723#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500724
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500725#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500726/* write a compressed text chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -0500727void
Guy Schalnat6d764711995-12-19 03:22:19 -0600728png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500729 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -0500730{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500731 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -0500732 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600733 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -0500734 int i, ret;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600735 png_charpp output_ptr = NULL; /* array of pointers to output */
Guy Schalnat0d580581995-07-20 02:43:20 -0500736 int num_output_ptr = 0; /* number of output pointers used */
737 int max_output_ptr = 0; /* size of output_ptr */
738
Andreas Dilger47a0c421997-05-16 02:46:07 -0500739 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600740
Andreas Dilger47a0c421997-05-16 02:46:07 -0500741 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -0500742 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600743 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500744 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500745 }
746
Andreas Dilger47a0c421997-05-16 02:46:07 -0500747 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
748 {
749 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
750 png_free(png_ptr, new_key);
751 return;
752 }
753
754 png_free(png_ptr, new_key);
755
756 if (compression >= PNG_TEXT_COMPRESSION_LAST)
757 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600758#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500759 char msg[50];
760 sprintf(msg, "Unknown zTXt compression type %d", compression);
761 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -0600762#else
763 png_warning(png_ptr, "Unknown zTXt compression type");
764#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500765 compression = PNG_TEXT_COMPRESSION_zTXt;
766 }
767
768 /* We can't write the chunk until we find out how much data we have,
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500769 * which means we need to run the compressor first and save the
Andreas Dilger47a0c421997-05-16 02:46:07 -0500770 * output. This shouldn't be a problem, as the vast majority of
771 * comments should be reasonable, but we will set up an array of
772 * malloc'd pointers to be sure.
773 *
774 * If we knew the application was well behaved, we could simplify this
775 * greatly by assuming we can always malloc an output buffer large
776 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
777 * and malloc this directly. The only time this would be a bad idea is
778 * if we can't malloc more than 64K and we have 64K of random input
779 * data, or if the input string is incredibly large (although this
780 * wouldn't cause a failure, just a slowdown due to swapping).
781 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500782
783 /* set up the compression buffers */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600784 png_ptr->zstream.avail_in = (uInt)text_len;
785 png_ptr->zstream.next_in = (Bytef *)text;
786 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
787 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500788
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600789 /* this is the same compression loop as in png_write_row() */
Guy Schalnat0d580581995-07-20 02:43:20 -0500790 do
791 {
792 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600793 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnat0d580581995-07-20 02:43:20 -0500794 if (ret != Z_OK)
795 {
796 /* error */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500797 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600798 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -0500799 else
Guy Schalnat6d764711995-12-19 03:22:19 -0600800 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -0500801 }
802 /* check to see if we need more room */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600803 if (!png_ptr->zstream.avail_out && png_ptr->zstream.avail_in)
Guy Schalnat0d580581995-07-20 02:43:20 -0500804 {
805 /* make sure the output array has room */
806 if (num_output_ptr >= max_output_ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600807 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500808 int old_max;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500809
810 old_max = max_output_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500811 max_output_ptr = num_output_ptr + 4;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500812 if (output_ptr != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600813 {
814 png_charpp old_ptr;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600815
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600816 old_ptr = output_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600817 output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600818 (png_uint_32)(max_output_ptr * sizeof (png_charpp)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500819 png_memcpy(output_ptr, old_ptr, old_max * sizeof (png_charp));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600820 png_free(png_ptr, old_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600821 }
822 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600823 output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600824 (png_uint_32)(max_output_ptr * sizeof (png_charp)));
Guy Schalnat0d580581995-07-20 02:43:20 -0500825 }
826
827 /* save the data */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600828 output_ptr[num_output_ptr] = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600829 (png_uint_32)png_ptr->zbuf_size);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500830 png_memcpy(output_ptr[num_output_ptr], png_ptr->zbuf,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500831 png_ptr->zbuf_size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500832 num_output_ptr++;
833
834 /* and reset the buffer */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600835 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
836 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500837 }
838 /* continue until we don't have anymore to compress */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600839 } while (png_ptr->zstream.avail_in);
Guy Schalnat0d580581995-07-20 02:43:20 -0500840
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600841 /* finish the compression */
Guy Schalnat0d580581995-07-20 02:43:20 -0500842 do
843 {
844 /* tell zlib we are finished */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600845 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -0500846 if (ret != Z_OK && ret != Z_STREAM_END)
847 {
848 /* we got an error */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500849 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600850 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -0500851 else
Guy Schalnat6d764711995-12-19 03:22:19 -0600852 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -0500853 }
854
855 /* check to see if we need more room */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500856 if (!(png_ptr->zstream.avail_out) && ret == Z_OK)
Guy Schalnat0d580581995-07-20 02:43:20 -0500857 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600858 /* check to make sure our output array has room */
859 if (num_output_ptr >= max_output_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500860 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500861 int old_max;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500862
863 old_max = max_output_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500864 max_output_ptr = num_output_ptr + 4;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500865 if (output_ptr != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600866 {
867 png_charpp old_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500868
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600869 old_ptr = output_ptr;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500870 /* This could be optimized to realloc() */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600871 output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600872 (png_uint_32)(max_output_ptr * sizeof (png_charpp)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500873 png_memcpy(output_ptr, old_ptr, old_max * sizeof (png_charp));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600874 png_free(png_ptr, old_ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600875 }
876 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600877 output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600878 (png_uint_32)(max_output_ptr * sizeof (png_charp)));
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600879 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600880
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600881 /* save off the data */
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600882 output_ptr[num_output_ptr] = (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600883 (png_uint_32)png_ptr->zbuf_size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600884 png_memcpy(output_ptr[num_output_ptr], png_ptr->zbuf,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500885 png_ptr->zbuf_size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500886 num_output_ptr++;
887
888 /* and reset the buffer pointers */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600889 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
890 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500891 }
892 } while (ret != Z_STREAM_END);
893
894 /* text length is number of buffers plus last buffer */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600895 text_len = png_ptr->zbuf_size * num_output_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600896 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500897 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
Guy Schalnat0d580581995-07-20 02:43:20 -0500898
899 /* write start of chunk */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500900 png_write_chunk_start(png_ptr, png_zTXt, (png_uint_32)(key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -0500901 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500902 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600903 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -0500904 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500905 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500906
907 /* write saved output buffers, if any */
908 for (i = 0; i < num_output_ptr; i++)
909 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500910 png_write_chunk_data(png_ptr,(png_bytep)output_ptr[i],png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600911 png_free(png_ptr, output_ptr[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500912 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500913 if (max_output_ptr != 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600914 png_free(png_ptr, output_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500915 /* write anything left in zbuf */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500916 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -0500917 png_write_chunk_data(png_ptr, png_ptr->zbuf,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600918 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -0500919 /* close the chunk */
920 png_write_chunk_end(png_ptr);
921
922 /* reset zlib for another zTXt or the image data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600923 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -0500924}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500925#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500926
Andreas Dilger47a0c421997-05-16 02:46:07 -0500927
928#if defined(PNG_WRITE_oFFs_SUPPORTED)
929/* write the oFFs chunk */
930void
931png_write_oFFs(png_structp png_ptr, png_uint_32 x_offset,
932 png_uint_32 y_offset,
933 int unit_type)
934{
935 png_byte buf[9];
936
937 png_debug(1, "in png_write_oFFs\n");
938 if (unit_type >= PNG_OFFSET_LAST)
939 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
940
941 png_save_uint_32(buf, x_offset);
942 png_save_uint_32(buf + 4, y_offset);
943 buf[8] = (png_byte)unit_type;
944
945 png_write_chunk(png_ptr, png_oFFs, buf, (png_size_t)9);
946}
947#endif
948
949#if defined(PNG_WRITE_pCAL_SUPPORTED)
950/* write the pCAL chunk (png-scivis-19970203) */
951void
952png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
953 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
954{
955 png_size_t purpose_len, units_len, total_len;
956 png_uint_32p params_len;
957 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600958 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500959 int i;
960
961 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
962 if (type >= PNG_EQUATION_LAST)
963 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
964
965 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
966 png_debug1(3, "pCAL purpose length = %d\n", purpose_len);
967 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
968 png_debug1(3, "pCAL units length = %d\n", units_len);
969 total_len = purpose_len + units_len + 10;
970
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600971 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
972 *sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -0500973
974 /* Find the length of each parameter, making sure we don't count the
975 null terminator for the last parameter. */
976 for (i = 0; i < nparams; i++)
977 {
978 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
979 png_debug2(3, "pCAL parameter %d length = %d\n", i, params_len[i]);
980 total_len += (png_size_t)params_len[i];
981 }
982
983 png_debug1(3, "pCAL total length = %d\n", total_len);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600984 png_write_chunk_start(png_ptr, png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600985 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500986 png_save_int_32(buf, X0);
987 png_save_int_32(buf + 4, X1);
988 buf[8] = (png_byte)type;
989 buf[9] = (png_byte)nparams;
990 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
991 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
992
993 png_free(png_ptr, new_purpose);
994
995 for (i = 0; i < nparams; i++)
996 {
997 png_write_chunk_data(png_ptr, (png_bytep)params[i],
998 (png_size_t)params_len[i]);
999 }
1000
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001001 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001002 png_write_chunk_end(png_ptr);
1003}
1004#endif
1005
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001006#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001007/* write the pHYs chunk */
1008void
Guy Schalnat6d764711995-12-19 03:22:19 -06001009png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001010 png_uint_32 y_pixels_per_unit,
1011 int unit_type)
1012{
1013 png_byte buf[9];
1014
Andreas Dilger47a0c421997-05-16 02:46:07 -05001015 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001016 if (unit_type >= PNG_RESOLUTION_LAST)
1017 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1018
Guy Schalnat0d580581995-07-20 02:43:20 -05001019 png_save_uint_32(buf, x_pixels_per_unit);
1020 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001021 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001022
Andreas Dilger47a0c421997-05-16 02:46:07 -05001023 png_write_chunk(png_ptr, png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001024}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001025#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001026
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001027#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001028/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1029 * or png_convert_from_time_t(), or fill in the structure yourself.
1030 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001031void
Guy Schalnat6d764711995-12-19 03:22:19 -06001032png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001033{
1034 png_byte buf[7];
1035
Andreas Dilger47a0c421997-05-16 02:46:07 -05001036 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001037 if (mod_time->month > 12 || mod_time->month < 1 ||
1038 mod_time->day > 31 || mod_time->day < 1 ||
1039 mod_time->hour > 23 || mod_time->second > 60)
1040 {
1041 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1042 return;
1043 }
1044
Guy Schalnat0d580581995-07-20 02:43:20 -05001045 png_save_uint_16(buf, mod_time->year);
1046 buf[2] = mod_time->month;
1047 buf[3] = mod_time->day;
1048 buf[4] = mod_time->hour;
1049 buf[5] = mod_time->minute;
1050 buf[6] = mod_time->second;
1051
Andreas Dilger47a0c421997-05-16 02:46:07 -05001052 png_write_chunk(png_ptr, png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001053}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001054#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001055
1056/* initializes the row writing capability of libpng */
1057void
Guy Schalnat6d764711995-12-19 03:22:19 -06001058png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001059{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001060 png_size_t buf_size;
1061
1062 png_debug(1, "in png_write_start_row\n");
1063 buf_size = (png_size_t)(((png_ptr->width * png_ptr->usr_channels *
1064 png_ptr->usr_bit_depth + 7) >> 3) + 1);
1065
Guy Schalnat0d580581995-07-20 02:43:20 -05001066 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001067 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001068 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001069
1070 /* set up filtering buffer, if using this filter */
1071 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001072 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001073 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001074 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001075 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001076 }
1077
Andreas Dilger47a0c421997-05-16 02:46:07 -05001078 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001079 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1080 {
1081 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001082 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001083 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001084
1085 if (png_ptr->do_filter & PNG_FILTER_UP)
1086 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001087 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001088 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001089 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001090 }
1091
1092 if (png_ptr->do_filter & PNG_FILTER_AVG)
1093 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001094 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1095 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001096 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001097 }
1098
1099 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1100 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001101 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001102 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001103 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001104 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001105 }
1106
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001107#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001108 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001109 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001110 {
1111 if (!(png_ptr->transformations & PNG_INTERLACE))
1112 {
1113 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1114 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001115 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1116 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001117 }
1118 else
1119 {
1120 png_ptr->num_rows = png_ptr->height;
1121 png_ptr->usr_width = png_ptr->width;
1122 }
1123 }
1124 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001125#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001126 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001127 png_ptr->num_rows = png_ptr->height;
1128 png_ptr->usr_width = png_ptr->width;
1129 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001130 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1131 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001132}
1133
Andreas Dilger47a0c421997-05-16 02:46:07 -05001134/* Internal use only. Called when finished processing a row of data. */
Guy Schalnat0d580581995-07-20 02:43:20 -05001135void
Guy Schalnat6d764711995-12-19 03:22:19 -06001136png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001137{
1138 int ret;
1139
Andreas Dilger47a0c421997-05-16 02:46:07 -05001140 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001141 /* next row */
1142 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001143
Guy Schalnat0d580581995-07-20 02:43:20 -05001144 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001145 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001146 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001147
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001148#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001149 /* if interlaced, go to next pass */
1150 if (png_ptr->interlaced)
1151 {
1152 png_ptr->row_number = 0;
1153 if (png_ptr->transformations & PNG_INTERLACE)
1154 {
1155 png_ptr->pass++;
1156 }
1157 else
1158 {
1159 /* loop until we find a non-zero width or height pass */
1160 do
1161 {
1162 png_ptr->pass++;
1163 if (png_ptr->pass >= 7)
1164 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001165 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001166 png_pass_inc[png_ptr->pass] - 1 -
1167 png_pass_start[png_ptr->pass]) /
1168 png_pass_inc[png_ptr->pass];
1169 png_ptr->num_rows = (png_ptr->height +
1170 png_pass_yinc[png_ptr->pass] - 1 -
1171 png_pass_ystart[png_ptr->pass]) /
1172 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001173 if (png_ptr->transformations & PNG_INTERLACE)
1174 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001175 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1176
1177 }
1178
Guy Schalnate5a37791996-06-05 15:50:50 -05001179 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001180 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001181 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001182 if (png_ptr->prev_row != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001183 png_memset(png_ptr->prev_row, 0,
1184 (png_size_t) (((png_uint_32)png_ptr->usr_channels *
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001185 (png_uint_32)png_ptr->usr_bit_depth *
1186 png_ptr->width + 7) >> 3) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001187 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001188 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001189 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001190#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001191
1192 /* if we get here, we've just written the last row, so we need
1193 to flush the compressor */
1194 do
1195 {
1196 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001197 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001198 /* check for an error */
1199 if (ret != Z_OK && ret != Z_STREAM_END)
1200 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001201 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001202 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001203 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001204 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001205 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001206 /* check to see if we need more room */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001207 if (!(png_ptr->zstream.avail_out) && ret == Z_OK)
Guy Schalnat0d580581995-07-20 02:43:20 -05001208 {
1209 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001210 png_ptr->zstream.next_out = png_ptr->zbuf;
1211 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat0d580581995-07-20 02:43:20 -05001212 }
1213 } while (ret != Z_STREAM_END);
1214
1215 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001216 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001217 {
1218 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001219 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001220 }
1221
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001222 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05001223}
1224
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001225#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001226/* Pick out the correct pixels for the interlace pass.
1227 * The basic idea here is to go through the row with a source
1228 * pointer and a destination pointer (sp and dp), and copy the
1229 * correct pixels for the pass. As the row gets compacted,
1230 * sp will always be >= dp, so we should never overwrite anything.
1231 * See the default: case for the easiest code to understand.
1232 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001233void
Guy Schalnat6d764711995-12-19 03:22:19 -06001234png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001235{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001236 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001237 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001238#if defined(PNG_USELESS_TESTS_SUPPORTED)
1239 if (row != NULL && row_info != NULL && pass < 6)
1240#else
1241 if (pass < 6)
1242#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001243 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001244 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001245 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001246 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001247 case 1:
1248 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001249 png_bytep sp;
1250 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001251 int shift;
1252 int d;
1253 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001254 png_uint_32 i;
1255 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001256
1257 dp = row;
1258 d = 0;
1259 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001260 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001261 i += png_pass_inc[pass])
1262 {
1263 sp = row + (png_size_t)(i >> 3);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001264 value = (int)(*sp >> (7 - (int)(i & 7))) & 0x1;
Guy Schalnat0d580581995-07-20 02:43:20 -05001265 d |= (value << shift);
1266
1267 if (shift == 0)
1268 {
1269 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001270 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001271 d = 0;
1272 }
1273 else
1274 shift--;
1275
1276 }
1277 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001278 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001279 break;
1280 }
1281 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001282 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001283 png_bytep sp;
1284 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001285 int shift;
1286 int d;
1287 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001288 png_uint_32 i;
1289 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001290
1291 dp = row;
1292 shift = 6;
1293 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001294 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001295 i += png_pass_inc[pass])
1296 {
1297 sp = row + (png_size_t)(i >> 2);
1298 value = (*sp >> ((3 - (int)(i & 3)) << 1)) & 0x3;
1299 d |= (value << shift);
1300
1301 if (shift == 0)
1302 {
1303 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001304 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001305 d = 0;
1306 }
1307 else
1308 shift -= 2;
1309 }
1310 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001311 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001312 break;
1313 }
1314 case 4:
1315 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001316 png_bytep sp;
1317 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001318 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001319 int d;
1320 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001321 png_uint_32 i;
1322 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001323
1324 dp = row;
1325 shift = 4;
1326 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001327 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001328 i += png_pass_inc[pass])
1329 {
1330 sp = row + (png_size_t)(i >> 1);
1331 value = (*sp >> ((1 - (int)(i & 1)) << 2)) & 0xf;
1332 d |= (value << shift);
1333
1334 if (shift == 0)
1335 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001336 shift = 4;
1337 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001338 d = 0;
1339 }
1340 else
1341 shift -= 4;
1342 }
1343 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001344 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001345 break;
1346 }
1347 default:
1348 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001349 png_bytep sp;
1350 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001351 png_uint_32 i;
1352 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001353 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001354
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001355 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05001356 dp = row;
1357 /* find out how many bytes each pixel takes up */
1358 pixel_bytes = (row_info->pixel_depth >> 3);
1359 /* loop through the row, only looking at the pixels that
1360 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001361 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001362 i += png_pass_inc[pass])
1363 {
1364 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06001365 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05001366 /* move the pixel */
1367 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001368 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05001369 /* next pixel */
1370 dp += pixel_bytes;
1371 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001372 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001373 }
1374 }
1375 /* set new row width */
1376 row_info->width = (row_info->width +
1377 png_pass_inc[pass] - 1 -
1378 png_pass_start[pass]) /
1379 png_pass_inc[pass];
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001380 row_info->rowbytes = ((row_info->width *
1381 row_info->pixel_depth + 7) >> 3);
Guy Schalnat0d580581995-07-20 02:43:20 -05001382 }
1383}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001384#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001385
Andreas Dilger47a0c421997-05-16 02:46:07 -05001386/* This filters the row, chooses which filter to use, if it has not already
1387 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001388 * chosen filter.
1389 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001390#define PNG_MAXSUM (~((png_uint_32)0) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001391#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06001392#define PNG_LOMASK ((png_uint_32)0xffffL)
1393#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Guy Schalnat0d580581995-07-20 02:43:20 -05001394void
Guy Schalnate5a37791996-06-05 15:50:50 -05001395png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05001396{
Guy Schalnate5a37791996-06-05 15:50:50 -05001397 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001398 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001399 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001400 png_uint_32 row_bytes = row_info->rowbytes;
1401#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1402 int num_p_filters = (int)png_ptr->num_prev_filters;
1403#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001404
Andreas Dilger47a0c421997-05-16 02:46:07 -05001405 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001406 /* find out how many bytes offset each pixel is */
1407 bpp = (row_info->pixel_depth + 7) / 8;
Guy Schalnate5a37791996-06-05 15:50:50 -05001408
1409 prev_row = png_ptr->prev_row;
1410 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001411 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05001412
Andreas Dilger47a0c421997-05-16 02:46:07 -05001413 /* The prediction method we use is to find which method provides the
1414 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05001415 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05001416 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001417 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05001418 * (experimental and can in theory improve compression), and the "zlib
1419 * predictive" method (not implemented in libpng 0.95), which does test
1420 * compressions of lines using different filter methods, and then chooses
1421 * the (series of) filter(s) which give minimum compressed data size (VERY
1422 * computationally expensive).
1423 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001424
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001425
Guy Schalnate5a37791996-06-05 15:50:50 -05001426 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05001427 * that has been chosen, as it doesn't actually do anything to the data.
1428 */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001429 if (filter_to_do & PNG_FILTER_NONE &&
1430 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05001431 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001432 png_bytep rp;
1433 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001434 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001435 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05001436
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001437 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001438 {
1439 v = *rp;
1440 sum += (v < 128) ? v : 256 - v;
1441 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001442
1443#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1444 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1445 {
1446 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001447 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001448 sumlo = sum & PNG_LOMASK;
1449 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
1450
1451 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001452 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001453 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001454 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001455 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001456 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001457 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001458 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001459 PNG_WEIGHT_SHIFT;
1460 }
1461 }
1462
1463 /* Factor in the cost of this filter (this is here for completeness,
1464 * but it makes no sense to have a "cost" for the NONE filter, as
1465 * it has the minimum possible computational cost - none).
1466 */
1467 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
1468 PNG_COST_SHIFT;
1469 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
1470 PNG_COST_SHIFT;
1471
1472 if (sumhi > PNG_HIMASK)
1473 sum = PNG_MAXSUM;
1474 else
1475 sum = (sumhi << PNG_HISHIFT) + sumlo;
1476 }
1477#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001478 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05001479 }
1480
Guy Schalnate5a37791996-06-05 15:50:50 -05001481 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001482 if (filter_to_do == PNG_FILTER_SUB)
1483 /* it's the only filter so no testing is needed */
1484 {
1485 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001486 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001487 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
1488 i++, rp++, dp++)
1489 {
1490 *dp = *rp;
1491 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001492 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001493 i++, rp++, lp++, dp++)
1494 {
1495 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
1496 }
1497 best_row = png_ptr->sub_row;
1498 }
1499
1500 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001501 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001502 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001503 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001504 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001505 int v;
1506
1507#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001508 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05001509 * would reduce the sum of this filter, so that we can do the
1510 * early exit comparison without scaling the sum each time.
1511 */
1512 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1513 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001514 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001515 png_uint_32 lmhi, lmlo;
1516 lmlo = lmins & PNG_LOMASK;
1517 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1518
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001519 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001520 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001521 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001522 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001523 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001524 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001525 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001526 PNG_WEIGHT_SHIFT;
1527 }
1528 }
1529
1530 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1531 PNG_COST_SHIFT;
1532 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1533 PNG_COST_SHIFT;
1534
1535 if (lmhi > PNG_HIMASK)
1536 lmins = PNG_MAXSUM;
1537 else
1538 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1539 }
1540#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001541
Guy Schalnate5a37791996-06-05 15:50:50 -05001542 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
1543 i++, rp++, dp++)
1544 {
1545 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001546
Guy Schalnate5a37791996-06-05 15:50:50 -05001547 sum += (v < 128) ? v : 256 - v;
1548 }
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001549 for (lp = row_buf + 1; i < row_info->rowbytes;
1550 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001551 {
1552 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001553
Guy Schalnate5a37791996-06-05 15:50:50 -05001554 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001555
1556 if (sum > lmins) /* We are already worse, don't continue. */
1557 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001558 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001559
1560#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1561 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1562 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001563 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001564 png_uint_32 sumhi, sumlo;
1565 sumlo = sum & PNG_LOMASK;
1566 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1567
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001568 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001569 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001570 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001571 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001572 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001573 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001574 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001575 PNG_WEIGHT_SHIFT;
1576 }
1577 }
1578
1579 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1580 PNG_COST_SHIFT;
1581 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
1582 PNG_COST_SHIFT;
1583
1584 if (sumhi > PNG_HIMASK)
1585 sum = PNG_MAXSUM;
1586 else
1587 sum = (sumhi << PNG_HISHIFT) + sumlo;
1588 }
1589#endif
1590
Guy Schalnate5a37791996-06-05 15:50:50 -05001591 if (sum < mins)
1592 {
1593 mins = sum;
1594 best_row = png_ptr->sub_row;
1595 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001596 }
1597
Guy Schalnate5a37791996-06-05 15:50:50 -05001598 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001599 if (filter_to_do == PNG_FILTER_UP)
1600 {
1601 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001602 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001603
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001604 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001605 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001606 i++, rp++, pp++, dp++)
1607 {
1608 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
1609 }
1610 best_row = png_ptr->up_row;
1611 }
1612
1613 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05001614 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001615 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001616 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001617 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001618 int v;
1619
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001620
Andreas Dilger47a0c421997-05-16 02:46:07 -05001621#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1622 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1623 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001624 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001625 png_uint_32 lmhi, lmlo;
1626 lmlo = lmins & PNG_LOMASK;
1627 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1628
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001629 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001630 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001631 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001632 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001633 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001634 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001635 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001636 PNG_WEIGHT_SHIFT;
1637 }
1638 }
1639
1640 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
1641 PNG_COST_SHIFT;
1642 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
1643 PNG_COST_SHIFT;
1644
1645 if (lmhi > PNG_HIMASK)
1646 lmins = PNG_MAXSUM;
1647 else
1648 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1649 }
1650#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001651
1652 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001653 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001654 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001655 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05001656
1657 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001658
1659 if (sum > lmins) /* We are already worse, don't continue. */
1660 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001661 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001662
1663#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1664 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1665 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001666 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001667 png_uint_32 sumhi, sumlo;
1668 sumlo = sum & PNG_LOMASK;
1669 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1670
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001671 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001672 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001673 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001674 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001675 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001676 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001677 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001678 PNG_WEIGHT_SHIFT;
1679 }
1680 }
1681
1682 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
1683 PNG_COST_SHIFT;
1684 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
1685 PNG_COST_SHIFT;
1686
1687 if (sumhi > PNG_HIMASK)
1688 sum = PNG_MAXSUM;
1689 else
1690 sum = (sumhi << PNG_HISHIFT) + sumlo;
1691 }
1692#endif
1693
Guy Schalnate5a37791996-06-05 15:50:50 -05001694 if (sum < mins)
1695 {
1696 mins = sum;
1697 best_row = png_ptr->up_row;
1698 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001699 }
1700
Guy Schalnate5a37791996-06-05 15:50:50 -05001701 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001702 if (filter_to_do == PNG_FILTER_AVG)
1703 {
1704 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001705 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001706 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001707 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001708 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001709 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001710 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001711 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001712 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001713 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
1714 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001715 }
1716 best_row = png_ptr->avg_row;
1717 }
1718
1719 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001720 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001721 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001722 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001723 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001724 int v;
1725
1726#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1727 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1728 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001729 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001730 png_uint_32 lmhi, lmlo;
1731 lmlo = lmins & PNG_LOMASK;
1732 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1733
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001734 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001735 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001736 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001737 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001738 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001739 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001740 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001741 PNG_WEIGHT_SHIFT;
1742 }
1743 }
1744
1745 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
1746 PNG_COST_SHIFT;
1747 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
1748 PNG_COST_SHIFT;
1749
1750 if (lmhi > PNG_HIMASK)
1751 lmins = PNG_MAXSUM;
1752 else
1753 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1754 }
1755#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001756
1757 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001758 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001759 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001760 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05001761
1762 sum += (v < 128) ? v : 256 - v;
1763 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001764 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001765 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001766 v = *dp++ =
1767 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05001768
1769 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001770
1771 if (sum > lmins) /* We are already worse, don't continue. */
1772 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001773 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001774
1775#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1776 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1777 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001778 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001779 png_uint_32 sumhi, sumlo;
1780 sumlo = sum & PNG_LOMASK;
1781 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1782
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001783 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001784 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001785 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001786 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001787 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001788 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001789 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001790 PNG_WEIGHT_SHIFT;
1791 }
1792 }
1793
1794 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
1795 PNG_COST_SHIFT;
1796 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
1797 PNG_COST_SHIFT;
1798
1799 if (sumhi > PNG_HIMASK)
1800 sum = PNG_MAXSUM;
1801 else
1802 sum = (sumhi << PNG_HISHIFT) + sumlo;
1803 }
1804#endif
1805
Guy Schalnate5a37791996-06-05 15:50:50 -05001806 if (sum < mins)
1807 {
1808 mins = sum;
1809 best_row = png_ptr->avg_row;
1810 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001811 }
1812
Andreas Dilger47a0c421997-05-16 02:46:07 -05001813 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001814 if (filter_to_do == PNG_FILTER_PAETH)
1815 {
1816 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001817 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001818 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001819 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001820 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001821 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001822 }
1823
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001824 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001825 {
1826 int a, b, c, pa, pb, pc, p;
1827
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001828 b = *pp++;
1829 c = *cp++;
1830 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001831
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001832 p = b - c;
1833 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001834
1835#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001836 pa = abs(p);
1837 pb = abs(pc);
1838 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001839#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001840 pa = p < 0 ? -p : p;
1841 pb = pc < 0 ? -pc : pc;
1842 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001843#endif
1844
1845 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
1846
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001847 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001848 }
1849 best_row = png_ptr->paeth_row;
1850 }
1851
1852 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001853 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001854 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001855 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001856 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001857 int v;
1858
1859#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1860 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1861 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001862 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001863 png_uint_32 lmhi, lmlo;
1864 lmlo = lmins & PNG_LOMASK;
1865 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
1866
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001867 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001868 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001869 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001870 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001871 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001872 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001873 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001874 PNG_WEIGHT_SHIFT;
1875 }
1876 }
1877
1878 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1879 PNG_COST_SHIFT;
1880 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1881 PNG_COST_SHIFT;
1882
1883 if (lmhi > PNG_HIMASK)
1884 lmins = PNG_MAXSUM;
1885 else
1886 lmins = (lmhi << PNG_HISHIFT) + lmlo;
1887 }
1888#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001889
1890 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001891 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001892 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001893 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05001894
1895 sum += (v < 128) ? v : 256 - v;
1896 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001897
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001898 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001899 {
1900 int a, b, c, pa, pb, pc, p;
1901
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001902 b = *pp++;
1903 c = *cp++;
1904 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001905
1906#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001907 p = b - c;
1908 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001909#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001910 pa = abs(p);
1911 pb = abs(pc);
1912 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001913#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001914 pa = p < 0 ? -p : p;
1915 pb = pc < 0 ? -pc : pc;
1916 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001917#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001918 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
1919#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001920 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001921 pa = abs(p - a);
1922 pb = abs(p - b);
1923 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05001924 if (pa <= pb && pa <= pc)
1925 p = a;
1926 else if (pb <= pc)
1927 p = b;
1928 else
1929 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001930#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05001931
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001932 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05001933
1934 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001935
1936 if (sum > lmins) /* We are already worse, don't continue. */
1937 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05001938 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001939
1940#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1941 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
1942 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05001943 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001944 png_uint_32 sumhi, sumlo;
1945 sumlo = sum & PNG_LOMASK;
1946 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
1947
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001948 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001949 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001950 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001951 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001952 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001953 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001954 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05001955 PNG_WEIGHT_SHIFT;
1956 }
1957 }
1958
1959 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1960 PNG_COST_SHIFT;
1961 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
1962 PNG_COST_SHIFT;
1963
1964 if (sumhi > PNG_HIMASK)
1965 sum = PNG_MAXSUM;
1966 else
1967 sum = (sumhi << PNG_HISHIFT) + sumlo;
1968 }
1969#endif
1970
Guy Schalnate5a37791996-06-05 15:50:50 -05001971 if (sum < mins)
1972 {
1973 best_row = png_ptr->paeth_row;
1974 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001975 }
1976
Andreas Dilger47a0c421997-05-16 02:46:07 -05001977 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001978
Guy Schalnate5a37791996-06-05 15:50:50 -05001979 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001980
1981#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1982 /* Save the type of filter we picked this time for future calculations */
1983 if (png_ptr->num_prev_filters > 0)
1984 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001985 int j;
1986 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001987 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001988 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001989 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001990 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001991 }
1992#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001993}
1994
1995
Andreas Dilger47a0c421997-05-16 02:46:07 -05001996/* Do the actual writing of a previously filtered row. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001997void
1998png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
1999{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002000 png_debug(1, "in png_write_filtered_row\n");
2001 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002002 /* set up the zlib input buffer */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002003 png_ptr->zstream.next_in = filtered_row;
2004 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002005 /* repeat until we have compressed all the data */
2006 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002007 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002008 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002009
Guy Schalnate5a37791996-06-05 15:50:50 -05002010 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002011 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002012 /* check for compression errors */
2013 if (ret != Z_OK)
2014 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002015 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002016 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002017 else
2018 png_error(png_ptr, "zlib error");
2019 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002020
Guy Schalnate5a37791996-06-05 15:50:50 -05002021 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002022 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002023 {
2024 /* write the IDAT and reset the zlib output buffer */
2025 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002026 png_ptr->zstream.next_out = png_ptr->zbuf;
2027 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002028 }
2029 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002030 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002031
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002032 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002033 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002034 {
2035 png_bytep tptr;
2036
2037 tptr = png_ptr->prev_row;
2038 png_ptr->prev_row = png_ptr->row_buf;
2039 png_ptr->row_buf = tptr;
2040 }
2041
Guy Schalnate5a37791996-06-05 15:50:50 -05002042 /* finish row - updates counters and flushes zlib if last row */
2043 png_write_finish_row(png_ptr);
2044
2045#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2046 png_ptr->flush_rows++;
2047
2048 if (png_ptr->flush_dist > 0 &&
2049 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002050 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002051 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002052 }
Guy Schalnate5a37791996-06-05 15:50:50 -05002053#endif /* PNG_WRITE_FLUSH_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05002054}