blob: f0dabfad8a771c70467217797a75bcc94bedf296 [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-Pehrson16e11662004-11-01 14:13:40 -06004 * libpng version 1.2.8beta1 - November 1, 2004
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05006 * Copyright (c) 1998-2004 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05007 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050010
Guy Schalnat0d580581995-07-20 02:43:20 -050011#define PNG_INTERNAL
12#include "png.h"
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050013#ifdef PNG_WRITE_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -050014
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 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050019void /* PRIVATE */
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
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -050028#if defined(PNG_WRITE_pCAL_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -050029/* 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 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050033void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -050034png_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 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050047void /* PRIVATE */
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 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050063void PNGAPI
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 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050076void PNGAPI
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];
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -050081 png_debug2(0, "Writing %s chunk (%lu bytes)\n", chunk_name, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -050082
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 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050099void PNGAPI
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(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500111void PNGAPI
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 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500128void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600129png_write_sig(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500130{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600131 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600132 /* write the rest of the 8 byte signature */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600133 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
Andreas Dilger47a0c421997-05-16 02:46:07 -0500134 (png_size_t)8 - png_ptr->sig_bytes);
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600135 if(png_ptr->sig_bytes < 3)
136 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500137}
138
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600139#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600140/*
141 * This pair of functions encapsulates the operation of (a) compressing a
142 * text string, and (b) issuing it later as a series of chunk data writes.
143 * The compression_state structure is shared context for these functions
144 * set up by the caller in order to make the whole mess thread-safe.
145 */
146
147typedef struct
148{
149 char *input; /* the uncompressed input data */
150 int input_len; /* its length */
151 int num_output_ptr; /* number of output pointers used */
152 int max_output_ptr; /* size of output_ptr */
153 png_charpp output_ptr; /* array of pointers to output */
154} compression_state;
155
156/* compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500157static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600158png_text_compress(png_structp png_ptr,
159 png_charp text, png_size_t text_len, int compression,
160 compression_state *comp)
161{
162 int ret;
163
164 comp->num_output_ptr = comp->max_output_ptr = 0;
165 comp->output_ptr = NULL;
166 comp->input = NULL;
167
168 /* we may just want to pass the text right through */
169 if (compression == PNG_TEXT_COMPRESSION_NONE)
170 {
171 comp->input = text;
172 comp->input_len = text_len;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500173 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600174 }
175
176 if (compression >= PNG_TEXT_COMPRESSION_LAST)
177 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500178#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600179 char msg[50];
180 sprintf(msg, "Unknown compression type %d", compression);
181 png_warning(png_ptr, msg);
182#else
183 png_warning(png_ptr, "Unknown compression type");
184#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600185 }
186
187 /* We can't write the chunk until we find out how much data we have,
188 * which means we need to run the compressor first and save the
189 * output. This shouldn't be a problem, as the vast majority of
190 * comments should be reasonable, but we will set up an array of
191 * malloc'd pointers to be sure.
192 *
193 * If we knew the application was well behaved, we could simplify this
194 * greatly by assuming we can always malloc an output buffer large
195 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
196 * and malloc this directly. The only time this would be a bad idea is
197 * if we can't malloc more than 64K and we have 64K of random input
198 * data, or if the input string is incredibly large (although this
199 * wouldn't cause a failure, just a slowdown due to swapping).
200 */
201
202 /* set up the compression buffers */
203 png_ptr->zstream.avail_in = (uInt)text_len;
204 png_ptr->zstream.next_in = (Bytef *)text;
205 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
206 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
207
208 /* this is the same compression loop as in png_write_row() */
209 do
210 {
211 /* compress the data */
212 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
213 if (ret != Z_OK)
214 {
215 /* error */
216 if (png_ptr->zstream.msg != NULL)
217 png_error(png_ptr, png_ptr->zstream.msg);
218 else
219 png_error(png_ptr, "zlib error");
220 }
221 /* check to see if we need more room */
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600222 if (!(png_ptr->zstream.avail_out))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600223 {
224 /* make sure the output array has room */
225 if (comp->num_output_ptr >= comp->max_output_ptr)
226 {
227 int old_max;
228
229 old_max = comp->max_output_ptr;
230 comp->max_output_ptr = comp->num_output_ptr + 4;
231 if (comp->output_ptr != NULL)
232 {
233 png_charpp old_ptr;
234
235 old_ptr = comp->output_ptr;
236 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500237 (png_uint_32)(comp->max_output_ptr *
238 png_sizeof (png_charpp)));
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500239 png_memcpy(comp->output_ptr, old_ptr, old_max
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500240 * png_sizeof (png_charp));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600241 png_free(png_ptr, old_ptr);
242 }
243 else
244 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500245 (png_uint_32)(comp->max_output_ptr *
246 png_sizeof (png_charp)));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600247 }
248
249 /* save the data */
250 comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr,
251 (png_uint_32)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500252 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
253 png_ptr->zbuf_size);
254 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600255
256 /* and reset the buffer */
257 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
258 png_ptr->zstream.next_out = png_ptr->zbuf;
259 }
260 /* continue until we don't have any more to compress */
261 } while (png_ptr->zstream.avail_in);
262
263 /* finish the compression */
264 do
265 {
266 /* tell zlib we are finished */
267 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500268
269 if (ret == Z_OK)
270 {
271 /* check to see if we need more room */
272 if (!(png_ptr->zstream.avail_out))
273 {
274 /* check to make sure our output array has room */
275 if (comp->num_output_ptr >= comp->max_output_ptr)
276 {
277 int old_max;
278
279 old_max = comp->max_output_ptr;
280 comp->max_output_ptr = comp->num_output_ptr + 4;
281 if (comp->output_ptr != NULL)
282 {
283 png_charpp old_ptr;
284
285 old_ptr = comp->output_ptr;
286 /* This could be optimized to realloc() */
287 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500288 (png_uint_32)(comp->max_output_ptr *
289 png_sizeof (png_charpp)));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500290 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500291 old_max * png_sizeof (png_charp));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500292 png_free(png_ptr, old_ptr);
293 }
294 else
295 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500296 (png_uint_32)(comp->max_output_ptr *
297 png_sizeof (png_charp)));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500298 }
299
300 /* save off the data */
301 comp->output_ptr[comp->num_output_ptr] =
302 (png_charp)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500303 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
304 png_ptr->zbuf_size);
305 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500306
307 /* and reset the buffer pointers */
308 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
309 png_ptr->zstream.next_out = png_ptr->zbuf;
310 }
311 }
312 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600313 {
314 /* we got an error */
315 if (png_ptr->zstream.msg != NULL)
316 png_error(png_ptr, png_ptr->zstream.msg);
317 else
318 png_error(png_ptr, "zlib error");
319 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600320 } while (ret != Z_STREAM_END);
321
322 /* text length is number of buffers plus last buffer */
323 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
324 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
325 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
326
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500327 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600328}
329
330/* ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500331static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600332png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
333{
334 int i;
335
336 /* handle the no-compression case */
337 if (comp->input)
338 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500339 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
340 (png_size_t)comp->input_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600341 return;
342 }
343
344 /* write saved output buffers, if any */
345 for (i = 0; i < comp->num_output_ptr; i++)
346 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600347 png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
348 png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600349 png_free(png_ptr, comp->output_ptr[i]);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500350 comp->output_ptr[i]=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600351 }
352 if (comp->max_output_ptr != 0)
353 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500354 comp->output_ptr=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600355 /* write anything left in zbuf */
356 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
357 png_write_chunk_data(png_ptr, png_ptr->zbuf,
358 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
359
360 /* reset zlib for another zTXt/iTXt or the image data */
361 deflateReset(&png_ptr->zstream);
362
363}
364#endif
365
Guy Schalnat0d580581995-07-20 02:43:20 -0500366/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600367 * information. Note that the rest of this code depends upon this
368 * information being correct.
369 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500370void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600371png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500372 int bit_depth, int color_type, int compression_type, int filter_type,
373 int interlace_type)
374{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600375#ifdef PNG_USE_LOCAL_ARRAYS
376 PNG_IHDR;
377#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500378 png_byte buf[13]; /* buffer to store the IHDR info */
379
Andreas Dilger47a0c421997-05-16 02:46:07 -0500380 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500381 /* Check that we have valid input data from the application info */
382 switch (color_type)
383 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500384 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500385 switch (bit_depth)
386 {
387 case 1:
388 case 2:
389 case 4:
390 case 8:
391 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500392 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500393 }
394 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500395 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500396 if (bit_depth != 8 && bit_depth != 16)
397 png_error(png_ptr, "Invalid bit depth for RGB image");
398 png_ptr->channels = 3;
399 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500400 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500401 switch (bit_depth)
402 {
403 case 1:
404 case 2:
405 case 4:
406 case 8: png_ptr->channels = 1; break;
407 default: png_error(png_ptr, "Invalid bit depth for paletted image");
408 }
409 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500410 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500411 if (bit_depth != 8 && bit_depth != 16)
412 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
413 png_ptr->channels = 2;
414 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500415 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500416 if (bit_depth != 8 && bit_depth != 16)
417 png_error(png_ptr, "Invalid bit depth for RGBA image");
418 png_ptr->channels = 4;
419 break;
420 default:
421 png_error(png_ptr, "Invalid image color type specified");
422 }
423
Andreas Dilger47a0c421997-05-16 02:46:07 -0500424 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500425 {
426 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500427 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500428 }
429
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600430 /* Write filter_method 64 (intrapixel differencing) only if
431 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
432 * 2. Libpng did not write a PNG signature (this filter_method is only
433 * used in PNG datastreams that are embedded in MNG datastreams) and
434 * 3. The application called png_permit_mng_features with a mask that
435 * included PNG_FLAG_MNG_FILTER_64 and
436 * 4. The filter_method is 64 and
437 * 5. The color_type is RGB or RGBA
438 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600439 if (
440#if defined(PNG_MNG_FEATURES_SUPPORTED)
441 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600442 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500443 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600444 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600445 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
446#endif
447 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500448 {
449 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500450 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500451 }
452
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600453#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500454 if (interlace_type != PNG_INTERLACE_NONE &&
455 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500456 {
457 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500458 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500459 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600460#else
461 interlace_type=PNG_INTERLACE_NONE;
462#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500463
464 /* save off the relevent information */
465 png_ptr->bit_depth = (png_byte)bit_depth;
466 png_ptr->color_type = (png_byte)color_type;
467 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500468#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600469 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500470#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500471 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500472 png_ptr->width = width;
473 png_ptr->height = height;
474
475 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500476 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
Guy Schalnate5a37791996-06-05 15:50:50 -0500477 /* set the usr info, so any transformations can modify it */
478 png_ptr->usr_width = png_ptr->width;
479 png_ptr->usr_bit_depth = png_ptr->bit_depth;
480 png_ptr->usr_channels = png_ptr->channels;
481
Guy Schalnat0d580581995-07-20 02:43:20 -0500482 /* pack the header information into the buffer */
483 png_save_uint_32(buf, width);
484 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600485 buf[8] = (png_byte)bit_depth;
486 buf[9] = (png_byte)color_type;
487 buf[10] = (png_byte)compression_type;
488 buf[11] = (png_byte)filter_type;
489 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500490
491 /* write the chunk */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600492 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500493
Andreas Dilger47a0c421997-05-16 02:46:07 -0500494 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600495 png_ptr->zstream.zalloc = png_zalloc;
496 png_ptr->zstream.zfree = png_zfree;
497 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500498 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500499 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500500 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
501 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500502 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500503 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500504 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500505 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500506 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500507 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500508 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500509 png_ptr->zlib_strategy = Z_FILTERED;
510 else
511 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
512 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500513 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500514 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500515 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500516 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500517 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500518 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500519 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500520 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600521 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500522 png_ptr->zlib_method, png_ptr->zlib_window_bits,
523 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600524 png_ptr->zstream.next_out = png_ptr->zbuf;
525 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500526
Guy Schalnate5a37791996-06-05 15:50:50 -0500527 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500528}
529
530/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500531 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600532 * structure.
533 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500534void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500535png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500536{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600537#ifdef PNG_USE_LOCAL_ARRAYS
538 PNG_PLTE;
539#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500540 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600541 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500542 png_byte buf[3];
543
Andreas Dilger47a0c421997-05-16 02:46:07 -0500544 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500545 if ((
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500546#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600547 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500548#endif
549 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500550 {
551 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500552 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500553 png_error(png_ptr, "Invalid number of colors in palette");
554 }
555 else
556 {
557 png_warning(png_ptr, "Invalid number of colors in palette");
558 return;
559 }
560 }
561
562 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
563 {
564 png_warning(png_ptr,
565 "Ignoring request to write a PLTE chunk in grayscale PNG");
566 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500567 }
568
Andreas Dilger47a0c421997-05-16 02:46:07 -0500569 png_ptr->num_palette = (png_uint_16)num_pal;
570 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500571
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600572 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500573#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500574 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500575 {
576 buf[0] = pal_ptr->red;
577 buf[1] = pal_ptr->green;
578 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500579 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500580 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500581#else
582 /* This is a little slower but some buggy compilers need to do this instead */
583 pal_ptr=palette;
584 for (i = 0; i < num_pal; i++)
585 {
586 buf[0] = pal_ptr[i].red;
587 buf[1] = pal_ptr[i].green;
588 buf[2] = pal_ptr[i].blue;
589 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
590 }
591#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500592 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500593 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500594}
595
596/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500597void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500598png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500599{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600600#ifdef PNG_USE_LOCAL_ARRAYS
601 PNG_IDAT;
602#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500603 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500604
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500605 /* Optimize the CMF field in the zlib stream. */
606 /* This hack of the zlib stream is compliant to the stream specification. */
607 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
608 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
609 {
610 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
611 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
612 {
613 /* Avoid memory underflows and multiplication overflows. */
614 /* The conditions below are practically always satisfied;
615 however, they still must be checked. */
616 if (length >= 2 &&
617 png_ptr->height < 16384 && png_ptr->width < 16384)
618 {
619 png_uint_32 uncompressed_idat_size = png_ptr->height *
620 ((png_ptr->width *
621 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
622 unsigned int z_cinfo = z_cmf >> 4;
623 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
624 while (uncompressed_idat_size <= half_z_window_size &&
625 half_z_window_size >= 256)
626 {
627 z_cinfo--;
628 half_z_window_size >>= 1;
629 }
630 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
631 if (data[0] != (png_byte)z_cmf)
632 {
633 data[0] = (png_byte)z_cmf;
634 data[1] &= 0xe0;
635 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
636 }
637 }
638 }
639 else
640 png_error(png_ptr,
641 "Invalid zlib compression method or flags in IDAT");
642 }
643
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600644 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500645 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500646}
647
648/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500649void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600650png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500651{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600652#ifdef PNG_USE_LOCAL_ARRAYS
653 PNG_IEND;
654#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500655 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500656 png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL,
Glenn Randers-Pehrson6c97ddb2001-10-24 22:01:19 -0500657 (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600658 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500659}
660
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500661#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500662/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600663#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500664void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500665png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500666{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600667#ifdef PNG_USE_LOCAL_ARRAYS
668 PNG_gAMA;
669#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500670 png_uint_32 igamma;
671 png_byte buf[4];
672
Andreas Dilger47a0c421997-05-16 02:46:07 -0500673 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600674 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600675 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500676 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600677 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500678}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500679#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600680#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500681void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600682png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600683{
684#ifdef PNG_USE_LOCAL_ARRAYS
685 PNG_gAMA;
686#endif
687 png_byte buf[4];
688
689 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600690 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500691 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600692 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
693}
694#endif
695#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500696
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600697#if defined(PNG_WRITE_sRGB_SUPPORTED)
698/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500699void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600700png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600701{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600702#ifdef PNG_USE_LOCAL_ARRAYS
703 PNG_sRGB;
704#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600705 png_byte buf[1];
706
707 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600708 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600709 png_warning(png_ptr,
710 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600711 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600712 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600713}
714#endif
715
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600716#if defined(PNG_WRITE_iCCP_SUPPORTED)
717/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500718void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600719png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
720 png_charp profile, int profile_len)
721{
722#ifdef PNG_USE_LOCAL_ARRAYS
723 PNG_iCCP;
724#endif
725 png_size_t name_len;
726 png_charp new_name;
727 compression_state comp;
728
729 png_debug(1, "in png_write_iCCP\n");
730 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
731 &new_name)) == 0)
732 {
733 png_warning(png_ptr, "Empty keyword in iCCP chunk");
734 return;
735 }
736
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600737 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600738 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600739
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500740 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600741 profile_len = 0;
742
743 if (profile_len)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500744 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600745 PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600746
747 /* make sure we include the NULL after the name and the compression type */
748 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
749 (png_uint_32)name_len+profile_len+2);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600750 new_name[name_len+1]=0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600751 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
752
753 if (profile_len)
754 png_write_compressed_data_out(png_ptr, &comp);
755
756 png_write_chunk_end(png_ptr);
757 png_free(png_ptr, new_name);
758}
759#endif
760
761#if defined(PNG_WRITE_sPLT_SUPPORTED)
762/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500763void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600764png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600765{
766#ifdef PNG_USE_LOCAL_ARRAYS
767 PNG_sPLT;
768#endif
769 png_size_t name_len;
770 png_charp new_name;
771 png_byte entrybuf[10];
772 int entry_size = (spalette->depth == 8 ? 6 : 10);
773 int palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600774 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500775#ifdef PNG_NO_POINTER_INDEXING
776 int i;
777#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600778
779 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600780 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
781 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600782 {
783 png_warning(png_ptr, "Empty keyword in sPLT chunk");
784 return;
785 }
786
787 /* make sure we include the NULL after the name */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500788 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600789 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600790 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600791 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600792
793 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500794#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600795 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
796 {
797 if (spalette->depth == 8)
798 {
799 entrybuf[0] = (png_byte)ep->red;
800 entrybuf[1] = (png_byte)ep->green;
801 entrybuf[2] = (png_byte)ep->blue;
802 entrybuf[3] = (png_byte)ep->alpha;
803 png_save_uint_16(entrybuf + 4, ep->frequency);
804 }
805 else
806 {
807 png_save_uint_16(entrybuf + 0, ep->red);
808 png_save_uint_16(entrybuf + 2, ep->green);
809 png_save_uint_16(entrybuf + 4, ep->blue);
810 png_save_uint_16(entrybuf + 6, ep->alpha);
811 png_save_uint_16(entrybuf + 8, ep->frequency);
812 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500813 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600814 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500815#else
816 ep=spalette->entries;
817 for (i=0; i>spalette->nentries; i++)
818 {
819 if (spalette->depth == 8)
820 {
821 entrybuf[0] = (png_byte)ep[i].red;
822 entrybuf[1] = (png_byte)ep[i].green;
823 entrybuf[2] = (png_byte)ep[i].blue;
824 entrybuf[3] = (png_byte)ep[i].alpha;
825 png_save_uint_16(entrybuf + 4, ep[i].frequency);
826 }
827 else
828 {
829 png_save_uint_16(entrybuf + 0, ep[i].red);
830 png_save_uint_16(entrybuf + 2, ep[i].green);
831 png_save_uint_16(entrybuf + 4, ep[i].blue);
832 png_save_uint_16(entrybuf + 6, ep[i].alpha);
833 png_save_uint_16(entrybuf + 8, ep[i].frequency);
834 }
835 png_write_chunk_data(png_ptr, entrybuf, entry_size);
836 }
837#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600838
839 png_write_chunk_end(png_ptr);
840 png_free(png_ptr, new_name);
841}
842#endif
843
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500844#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500845/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500846void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600847png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500848{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600849#ifdef PNG_USE_LOCAL_ARRAYS
850 PNG_sBIT;
851#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500852 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500853 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500854
Andreas Dilger47a0c421997-05-16 02:46:07 -0500855 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600856 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500857 if (color_type & PNG_COLOR_MASK_COLOR)
858 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600859 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500860
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500861 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
862 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600863 if (sbit->red == 0 || sbit->red > maxbits ||
864 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500865 sbit->blue == 0 || sbit->blue > maxbits)
866 {
867 png_warning(png_ptr, "Invalid sBIT depth specified");
868 return;
869 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500870 buf[0] = sbit->red;
871 buf[1] = sbit->green;
872 buf[2] = sbit->blue;
873 size = 3;
874 }
875 else
876 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500877 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
878 {
879 png_warning(png_ptr, "Invalid sBIT depth specified");
880 return;
881 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500882 buf[0] = sbit->gray;
883 size = 1;
884 }
885
886 if (color_type & PNG_COLOR_MASK_ALPHA)
887 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500888 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
889 {
890 png_warning(png_ptr, "Invalid sBIT depth specified");
891 return;
892 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500893 buf[size++] = sbit->alpha;
894 }
895
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600896 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500897}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500898#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500899
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500900#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500901/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600902#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500903void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500904png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600905 double red_x, double red_y, double green_x, double green_y,
906 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500907{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600908#ifdef PNG_USE_LOCAL_ARRAYS
909 PNG_cHRM;
910#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500911 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600912 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500913
Andreas Dilger47a0c421997-05-16 02:46:07 -0500914 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600915 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500916 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
917 white_x + white_y > 1.0)
918 {
919 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500920#if !defined(PNG_NO_CONSOLE_IO)
921 fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600922#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500923 return;
924 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600925 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500926 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600927 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500928 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500929
930 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
931 red_x + red_y > 1.0)
932 {
933 png_warning(png_ptr, "Invalid cHRM red point specified");
934 return;
935 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600936 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500937 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600938 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500939 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500940
941 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
942 green_x + green_y > 1.0)
943 {
944 png_warning(png_ptr, "Invalid cHRM green point specified");
945 return;
946 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600947 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500948 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600949 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500950 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500951
952 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
953 blue_x + blue_y > 1.0)
954 {
955 png_warning(png_ptr, "Invalid cHRM blue point specified");
956 return;
957 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600958 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500959 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600960 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500961 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500962
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600963 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500964}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500965#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600966#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500967void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600968png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
969 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
970 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
971 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600972{
973#ifdef PNG_USE_LOCAL_ARRAYS
974 PNG_cHRM;
975#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600976 png_byte buf[32];
977
978 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600979 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600980 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
981 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600982 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500983#if !defined(PNG_NO_CONSOLE_IO)
984 fprintf(stderr,"white_x=%ld, white_y=%ld\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600985#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600986 return;
987 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500988 png_save_uint_32(buf, (png_uint_32)white_x);
989 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600990
991 if (red_x > 80000L || red_y > 80000L || red_x + red_y > 100000L)
992 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600993 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600994 return;
995 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500996 png_save_uint_32(buf + 8, (png_uint_32)red_x);
997 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600998
999 if (green_x > 80000L || green_y > 80000L || green_x + green_y > 100000L)
1000 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001001 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001002 return;
1003 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001004 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1005 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001006
1007 if (blue_x > 80000L || blue_y > 80000L || blue_x + blue_y > 100000L)
1008 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001009 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001010 return;
1011 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001012 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1013 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001014
1015 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
1016}
1017#endif
1018#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001019
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001020#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001021/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001022void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001023png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -05001024 int num_trans, int color_type)
1025{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001026#ifdef PNG_USE_LOCAL_ARRAYS
1027 PNG_tRNS;
1028#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001029 png_byte buf[6];
1030
Andreas Dilger47a0c421997-05-16 02:46:07 -05001031 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001032 if (color_type == PNG_COLOR_TYPE_PALETTE)
1033 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001034 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001035 {
1036 png_warning(png_ptr,"Invalid number of transparent colors specified");
1037 return;
1038 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001039 /* write the chunk out as it is */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001040 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001041 }
1042 else if (color_type == PNG_COLOR_TYPE_GRAY)
1043 {
1044 /* one 16 bit value */
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001045 if(tran->gray >= (1 << png_ptr->bit_depth))
1046 {
1047 png_warning(png_ptr,
1048 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1049 return;
1050 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001051 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001052 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001053 }
1054 else if (color_type == PNG_COLOR_TYPE_RGB)
1055 {
1056 /* three 16 bit values */
1057 png_save_uint_16(buf, tran->red);
1058 png_save_uint_16(buf + 2, tran->green);
1059 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001060 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1061 {
1062 png_warning(png_ptr,
1063 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1064 return;
1065 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001066 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001067 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001068 else
1069 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001070 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001071 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001072}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001073#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001074
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001075#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001076/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001077void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001078png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001079{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001080#ifdef PNG_USE_LOCAL_ARRAYS
1081 PNG_bKGD;
1082#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001083 png_byte buf[6];
1084
Andreas Dilger47a0c421997-05-16 02:46:07 -05001085 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001086 if (color_type == PNG_COLOR_TYPE_PALETTE)
1087 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001088 if (
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001089#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001090 (png_ptr->num_palette ||
1091 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001092#endif
1093 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001094 {
1095 png_warning(png_ptr, "Invalid background palette index");
1096 return;
1097 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001098 buf[0] = back->index;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001099 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001100 }
1101 else if (color_type & PNG_COLOR_MASK_COLOR)
1102 {
1103 png_save_uint_16(buf, back->red);
1104 png_save_uint_16(buf + 2, back->green);
1105 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001106 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1107 {
1108 png_warning(png_ptr,
1109 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1110 return;
1111 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001112 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001113 }
1114 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001115 {
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001116 if(back->gray >= (1 << png_ptr->bit_depth))
1117 {
1118 png_warning(png_ptr,
1119 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1120 return;
1121 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001122 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001123 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001124 }
1125}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001126#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001127
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001128#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001129/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001130void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001131png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001132{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001133#ifdef PNG_USE_LOCAL_ARRAYS
1134 PNG_hIST;
1135#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001136 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001137 png_byte buf[3];
1138
Andreas Dilger47a0c421997-05-16 02:46:07 -05001139 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001140 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001141 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001142 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1143 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001144 png_warning(png_ptr, "Invalid number of histogram entries specified");
1145 return;
1146 }
1147
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001148 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001149 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001150 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001151 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001152 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001153 }
1154 png_write_chunk_end(png_ptr);
1155}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001156#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001157
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001158#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1159 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001160/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1161 * and if invalid, correct the keyword rather than discarding the entire
1162 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1163 * length, forbids leading or trailing whitespace, multiple internal spaces,
1164 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001165 *
1166 * The new_key is allocated to hold the corrected keyword and must be freed
1167 * by the calling routine. This avoids problems with trying to write to
1168 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001169 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001170png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001171png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001172{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001173 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001174 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001175 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001176 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001177
Andreas Dilger47a0c421997-05-16 02:46:07 -05001178 png_debug(1, "in png_check_keyword\n");
1179 *new_key = NULL;
1180
1181 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001182 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001183 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001184 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001185 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001186
Andreas Dilger47a0c421997-05-16 02:46:07 -05001187 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1188
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001189 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
1190 if (*new_key == NULL)
1191 {
1192 png_warning(png_ptr, "Out of memory while procesing keyword");
1193 return ((png_size_t)0);
1194 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001195
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001196 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001197 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001198 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001199 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001200 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001201#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001202 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001203
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001204 sprintf(msg, "invalid keyword character 0x%02X", *kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001205 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001206#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001207 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001208#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001209 *dp = ' ';
1210 }
1211 else
1212 {
1213 *dp = *kp;
1214 }
1215 }
1216 *dp = '\0';
1217
1218 /* Remove any trailing white space. */
1219 kp = *new_key + key_len - 1;
1220 if (*kp == ' ')
1221 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001222 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001223
1224 while (*kp == ' ')
1225 {
1226 *(kp--) = '\0';
1227 key_len--;
1228 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001229 }
1230
1231 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001232 kp = *new_key;
1233 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001234 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001235 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001236
1237 while (*kp == ' ')
1238 {
1239 kp++;
1240 key_len--;
1241 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001242 }
1243
Andreas Dilger47a0c421997-05-16 02:46:07 -05001244 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001245
Andreas Dilger47a0c421997-05-16 02:46:07 -05001246 /* Remove multiple internal spaces. */
1247 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001248 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001249 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001250 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001251 *(dp++) = *kp;
1252 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001253 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001254 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001255 {
1256 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001257 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001258 }
1259 else
1260 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001261 *(dp++) = *kp;
1262 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001263 }
1264 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001265 *dp = '\0';
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001266 if(kwarn)
1267 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001268
1269 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001270 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001271 png_free(png_ptr, *new_key);
1272 *new_key=NULL;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001273 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001274 }
1275
1276 if (key_len > 79)
1277 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001278 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001279 new_key[79] = '\0';
1280 key_len = 79;
1281 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001282
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001283 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001284}
1285#endif
1286
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001287#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001288/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001289void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001290png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001291 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001292{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001293#ifdef PNG_USE_LOCAL_ARRAYS
1294 PNG_tEXt;
1295#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001296 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001297 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001298
Andreas Dilger47a0c421997-05-16 02:46:07 -05001299 png_debug(1, "in png_write_tEXt\n");
1300 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1301 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001302 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001303 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001304 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001305
Andreas Dilger47a0c421997-05-16 02:46:07 -05001306 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001307 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001308 else
1309 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001310
1311 /* make sure we include the 0 after the key */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001312 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt, (png_uint_32)key_len+text_len+1);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001313 /*
1314 * We leave it to the application to meet PNG-1.0 requirements on the
1315 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1316 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001317 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001318 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001319 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001320 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001321 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001322
Guy Schalnat0d580581995-07-20 02:43:20 -05001323 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001324 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001325}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001326#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001327
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001328#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001329/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001330void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001331png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001332 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001333{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001334#ifdef PNG_USE_LOCAL_ARRAYS
1335 PNG_zTXt;
1336#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001337 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001338 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001339 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001340 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001341
Andreas Dilger47a0c421997-05-16 02:46:07 -05001342 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001343
Andreas Dilger47a0c421997-05-16 02:46:07 -05001344 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001345 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001346 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001347 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001348 }
1349
Andreas Dilger47a0c421997-05-16 02:46:07 -05001350 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1351 {
1352 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1353 png_free(png_ptr, new_key);
1354 return;
1355 }
1356
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001357 text_len = png_strlen(text);
1358
Andreas Dilger47a0c421997-05-16 02:46:07 -05001359 png_free(png_ptr, new_key);
1360
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001361 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001362 text_len = png_text_compress(png_ptr, text, text_len, compression,
1363 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001364
1365 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001366 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1367 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001368 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001369 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001370 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001371 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001372 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001373 /* write the compressed data */
1374 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001375
Guy Schalnat0d580581995-07-20 02:43:20 -05001376 /* close the chunk */
1377 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001378}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001379#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001380
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001381#if defined(PNG_WRITE_iTXt_SUPPORTED)
1382/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001383void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001384png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001385 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001386{
1387#ifdef PNG_USE_LOCAL_ARRAYS
1388 PNG_iTXt;
1389#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001390 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001391 png_charp new_lang, new_key;
1392 png_byte cbuf[2];
1393 compression_state comp;
1394
1395 png_debug(1, "in png_write_iTXt\n");
1396
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001397 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1398 {
1399 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1400 return;
1401 }
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001402 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001403 {
1404 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001405 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001406 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001407 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001408
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001409 if (lang_key == NULL)
1410 lang_key_len = 0;
1411 else
1412 lang_key_len = png_strlen(lang_key);
1413
1414 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001415 text_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001416 else
1417 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001418
1419 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001420 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1421 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001422
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001423
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001424 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001425 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001426
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001427 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1428 (png_uint_32)(
1429 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1430 + key_len
1431 + lang_len
1432 + lang_key_len
1433 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001434
1435 /*
1436 * We leave it to the application to meet PNG-1.0 requirements on the
1437 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1438 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1439 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1440 */
1441 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001442
1443 /* set the compression flag */
1444 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1445 compression == PNG_TEXT_COMPRESSION_NONE)
1446 cbuf[0] = 0;
1447 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1448 cbuf[0] = 1;
1449 /* set the compression method */
1450 cbuf[1] = 0;
1451 png_write_chunk_data(png_ptr, cbuf, 2);
1452
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001453 cbuf[0] = 0;
1454 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf), lang_len + 1);
1455 png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf), lang_key_len + 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001456 png_write_compressed_data_out(png_ptr, &comp);
1457
1458 png_write_chunk_end(png_ptr);
1459 png_free(png_ptr, new_key);
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001460 if (new_lang)
1461 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001462}
1463#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001464
1465#if defined(PNG_WRITE_oFFs_SUPPORTED)
1466/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001467void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001468png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001469 int unit_type)
1470{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001471#ifdef PNG_USE_LOCAL_ARRAYS
1472 PNG_oFFs;
1473#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001474 png_byte buf[9];
1475
1476 png_debug(1, "in png_write_oFFs\n");
1477 if (unit_type >= PNG_OFFSET_LAST)
1478 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1479
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001480 png_save_int_32(buf, x_offset);
1481 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001482 buf[8] = (png_byte)unit_type;
1483
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001484 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001485}
1486#endif
1487
1488#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001489/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001490void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001491png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1492 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1493{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001494#ifdef PNG_USE_LOCAL_ARRAYS
1495 PNG_pCAL;
1496#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001497 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001498 png_uint_32p params_len;
1499 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001500 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001501 int i;
1502
1503 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1504 if (type >= PNG_EQUATION_LAST)
1505 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1506
1507 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001508 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001509 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001510 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001511 total_len = purpose_len + units_len + 10;
1512
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001513 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001514 *png_sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001515
1516 /* Find the length of each parameter, making sure we don't count the
1517 null terminator for the last parameter. */
1518 for (i = 0; i < nparams; i++)
1519 {
1520 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001521 png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001522 total_len += (png_size_t)params_len[i];
1523 }
1524
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001525 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001526 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001527 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001528 png_save_int_32(buf, X0);
1529 png_save_int_32(buf + 4, X1);
1530 buf[8] = (png_byte)type;
1531 buf[9] = (png_byte)nparams;
1532 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1533 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1534
1535 png_free(png_ptr, new_purpose);
1536
1537 for (i = 0; i < nparams; i++)
1538 {
1539 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1540 (png_size_t)params_len[i]);
1541 }
1542
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001543 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001544 png_write_chunk_end(png_ptr);
1545}
1546#endif
1547
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001548#if defined(PNG_WRITE_sCAL_SUPPORTED)
1549/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001550#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001551void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001552png_write_sCAL(png_structp png_ptr, int unit, double width,double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001553{
1554#ifdef PNG_USE_LOCAL_ARRAYS
1555 PNG_sCAL;
1556#endif
1557 png_size_t total_len;
1558 char wbuf[32], hbuf[32];
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001559 png_byte bunit = unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001560
1561 png_debug(1, "in png_write_sCAL\n");
1562
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001563#if defined(_WIN32_WCE)
1564/* sprintf() function is not supported on WindowsCE */
1565 {
1566 wchar_t wc_buf[32];
1567 swprintf(wc_buf, TEXT("%12.12e"), width);
1568 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, wbuf, 32, NULL, NULL);
1569 swprintf(wc_buf, TEXT("%12.12e"), height);
1570 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, hbuf, 32, NULL, NULL);
1571 }
1572#else
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001573 sprintf(wbuf, "%12.12e", width);
1574 sprintf(hbuf, "%12.12e", height);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001575#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001576 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001577
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001578 png_debug1(3, "sCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001579 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001580 png_write_chunk_data(png_ptr, (png_bytep)&bunit, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001581 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1582 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001583
1584 png_write_chunk_end(png_ptr);
1585}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001586#else
1587#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001588void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001589png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001590 png_charp height)
1591{
1592#ifdef PNG_USE_LOCAL_ARRAYS
1593 PNG_sCAL;
1594#endif
1595 png_size_t total_len;
1596 char wbuf[32], hbuf[32];
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001597 png_byte bunit = unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001598
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001599 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001600
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001601 png_strcpy(wbuf,(const char *)width);
1602 png_strcpy(hbuf,(const char *)height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001603 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001604
1605 png_debug1(3, "sCAL total length = %d\n", total_len);
1606 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001607 png_write_chunk_data(png_ptr, (png_bytep)&bunit, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001608 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1609 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001610
1611 png_write_chunk_end(png_ptr);
1612}
1613#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001614#endif
1615#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001616
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001617#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001618/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001619void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001620png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001621 png_uint_32 y_pixels_per_unit,
1622 int unit_type)
1623{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001624#ifdef PNG_USE_LOCAL_ARRAYS
1625 PNG_pHYs;
1626#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001627 png_byte buf[9];
1628
Andreas Dilger47a0c421997-05-16 02:46:07 -05001629 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001630 if (unit_type >= PNG_RESOLUTION_LAST)
1631 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1632
Guy Schalnat0d580581995-07-20 02:43:20 -05001633 png_save_uint_32(buf, x_pixels_per_unit);
1634 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001635 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001636
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001637 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001638}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001639#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001640
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001641#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001642/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1643 * or png_convert_from_time_t(), or fill in the structure yourself.
1644 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001645void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001646png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001647{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001648#ifdef PNG_USE_LOCAL_ARRAYS
1649 PNG_tIME;
1650#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001651 png_byte buf[7];
1652
Andreas Dilger47a0c421997-05-16 02:46:07 -05001653 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001654 if (mod_time->month > 12 || mod_time->month < 1 ||
1655 mod_time->day > 31 || mod_time->day < 1 ||
1656 mod_time->hour > 23 || mod_time->second > 60)
1657 {
1658 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1659 return;
1660 }
1661
Guy Schalnat0d580581995-07-20 02:43:20 -05001662 png_save_uint_16(buf, mod_time->year);
1663 buf[2] = mod_time->month;
1664 buf[3] = mod_time->day;
1665 buf[4] = mod_time->hour;
1666 buf[5] = mod_time->minute;
1667 buf[6] = mod_time->second;
1668
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001669 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001670}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001671#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001672
1673/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001674void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001675png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001676{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001677#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001678 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001679
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001680 /* start of interlace block */
1681 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001682
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001683 /* offset to next interlace block */
1684 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001685
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001686 /* start of interlace block in the y direction */
1687 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001688
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001689 /* offset to next interlace block in the y direction */
1690 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001691#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001692
Andreas Dilger47a0c421997-05-16 02:46:07 -05001693 png_size_t buf_size;
1694
1695 png_debug(1, "in png_write_start_row\n");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001696 buf_size = (png_size_t)(PNG_ROWBYTES(
1697 png_ptr->usr_channels*png_ptr->usr_bit_depth,png_ptr->width)+1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001698
Guy Schalnat0d580581995-07-20 02:43:20 -05001699 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001700 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001701 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001702
1703 /* set up filtering buffer, if using this filter */
1704 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001705 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001706 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001707 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001708 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001709 }
1710
Andreas Dilger47a0c421997-05-16 02:46:07 -05001711 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001712 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1713 {
1714 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001715 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001716 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001717
1718 if (png_ptr->do_filter & PNG_FILTER_UP)
1719 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001720 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001721 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001722 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001723 }
1724
1725 if (png_ptr->do_filter & PNG_FILTER_AVG)
1726 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001727 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1728 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001729 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001730 }
1731
1732 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1733 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001734 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001735 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001736 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001737 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001738 }
1739
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001740#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001741 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001742 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001743 {
1744 if (!(png_ptr->transformations & PNG_INTERLACE))
1745 {
1746 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1747 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001748 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1749 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001750 }
1751 else
1752 {
1753 png_ptr->num_rows = png_ptr->height;
1754 png_ptr->usr_width = png_ptr->width;
1755 }
1756 }
1757 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001758#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001759 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001760 png_ptr->num_rows = png_ptr->height;
1761 png_ptr->usr_width = png_ptr->width;
1762 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001763 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1764 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001765}
1766
Andreas Dilger47a0c421997-05-16 02:46:07 -05001767/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001768void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001769png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001770{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001771#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001772 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001773
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001774 /* start of interlace block */
1775 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001776
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001777 /* offset to next interlace block */
1778 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001779
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001780 /* start of interlace block in the y direction */
1781 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001782
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001783 /* offset to next interlace block in the y direction */
1784 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001785#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001786
Guy Schalnat0d580581995-07-20 02:43:20 -05001787 int ret;
1788
Andreas Dilger47a0c421997-05-16 02:46:07 -05001789 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001790 /* next row */
1791 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001792
Guy Schalnat0d580581995-07-20 02:43:20 -05001793 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001794 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001795 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001796
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001797#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001798 /* if interlaced, go to next pass */
1799 if (png_ptr->interlaced)
1800 {
1801 png_ptr->row_number = 0;
1802 if (png_ptr->transformations & PNG_INTERLACE)
1803 {
1804 png_ptr->pass++;
1805 }
1806 else
1807 {
1808 /* loop until we find a non-zero width or height pass */
1809 do
1810 {
1811 png_ptr->pass++;
1812 if (png_ptr->pass >= 7)
1813 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001814 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001815 png_pass_inc[png_ptr->pass] - 1 -
1816 png_pass_start[png_ptr->pass]) /
1817 png_pass_inc[png_ptr->pass];
1818 png_ptr->num_rows = (png_ptr->height +
1819 png_pass_yinc[png_ptr->pass] - 1 -
1820 png_pass_ystart[png_ptr->pass]) /
1821 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001822 if (png_ptr->transformations & PNG_INTERLACE)
1823 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001824 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1825
1826 }
1827
Guy Schalnate5a37791996-06-05 15:50:50 -05001828 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001829 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001830 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001831 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001832 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001833 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
1834 png_ptr->usr_bit_depth,png_ptr->width))+1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001835 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001836 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001837 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001838#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001839
1840 /* if we get here, we've just written the last row, so we need
1841 to flush the compressor */
1842 do
1843 {
1844 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001845 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001846 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001847 if (ret == Z_OK)
1848 {
1849 /* check to see if we need more room */
1850 if (!(png_ptr->zstream.avail_out))
1851 {
1852 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1853 png_ptr->zstream.next_out = png_ptr->zbuf;
1854 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1855 }
1856 }
1857 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001858 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001859 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001860 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001861 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001862 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001863 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001864 } while (ret != Z_STREAM_END);
1865
1866 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001867 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001868 {
1869 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001870 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001871 }
1872
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001873 deflateReset(&png_ptr->zstream);
Guy Schalnat0d580581995-07-20 02:43:20 -05001874}
1875
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001876#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001877/* Pick out the correct pixels for the interlace pass.
1878 * The basic idea here is to go through the row with a source
1879 * pointer and a destination pointer (sp and dp), and copy the
1880 * correct pixels for the pass. As the row gets compacted,
1881 * sp will always be >= dp, so we should never overwrite anything.
1882 * See the default: case for the easiest code to understand.
1883 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001884void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001885png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001886{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001887#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001888 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001889
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001890 /* start of interlace block */
1891 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001892
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001893 /* offset to next interlace block */
1894 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001895#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001896
Andreas Dilger47a0c421997-05-16 02:46:07 -05001897 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001898 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001899#if defined(PNG_USELESS_TESTS_SUPPORTED)
1900 if (row != NULL && row_info != NULL && pass < 6)
1901#else
1902 if (pass < 6)
1903#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001904 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001905 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001906 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001907 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001908 case 1:
1909 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001910 png_bytep sp;
1911 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001912 int shift;
1913 int d;
1914 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001915 png_uint_32 i;
1916 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001917
1918 dp = row;
1919 d = 0;
1920 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001921 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001922 i += png_pass_inc[pass])
1923 {
1924 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001925 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001926 d |= (value << shift);
1927
1928 if (shift == 0)
1929 {
1930 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001931 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001932 d = 0;
1933 }
1934 else
1935 shift--;
1936
1937 }
1938 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001939 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001940 break;
1941 }
1942 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001943 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001944 png_bytep sp;
1945 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001946 int shift;
1947 int d;
1948 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001949 png_uint_32 i;
1950 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001951
1952 dp = row;
1953 shift = 6;
1954 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001955 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001956 i += png_pass_inc[pass])
1957 {
1958 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001959 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05001960 d |= (value << shift);
1961
1962 if (shift == 0)
1963 {
1964 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001965 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001966 d = 0;
1967 }
1968 else
1969 shift -= 2;
1970 }
1971 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001972 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001973 break;
1974 }
1975 case 4:
1976 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001977 png_bytep sp;
1978 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001979 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001980 int d;
1981 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001982 png_uint_32 i;
1983 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001984
1985 dp = row;
1986 shift = 4;
1987 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001988 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001989 i += png_pass_inc[pass])
1990 {
1991 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001992 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05001993 d |= (value << shift);
1994
1995 if (shift == 0)
1996 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001997 shift = 4;
1998 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001999 d = 0;
2000 }
2001 else
2002 shift -= 4;
2003 }
2004 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002005 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002006 break;
2007 }
2008 default:
2009 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002010 png_bytep sp;
2011 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002012 png_uint_32 i;
2013 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002014 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002015
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002016 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002017 dp = row;
2018 /* find out how many bytes each pixel takes up */
2019 pixel_bytes = (row_info->pixel_depth >> 3);
2020 /* loop through the row, only looking at the pixels that
2021 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002022 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002023 i += png_pass_inc[pass])
2024 {
2025 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002026 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002027 /* move the pixel */
2028 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002029 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05002030 /* next pixel */
2031 dp += pixel_bytes;
2032 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002033 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002034 }
2035 }
2036 /* set new row width */
2037 row_info->width = (row_info->width +
2038 png_pass_inc[pass] - 1 -
2039 png_pass_start[pass]) /
2040 png_pass_inc[pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002041 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2042 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002043 }
2044}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002045#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002046
Andreas Dilger47a0c421997-05-16 02:46:07 -05002047/* This filters the row, chooses which filter to use, if it has not already
2048 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002049 * chosen filter.
2050 */
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002051#define PNG_MAXSUM (~((png_uint_32)0) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002052#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002053#define PNG_LOMASK ((png_uint_32)0xffffL)
2054#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002055void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002056png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002057{
Guy Schalnate5a37791996-06-05 15:50:50 -05002058 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002059 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002060 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002061 png_uint_32 row_bytes = row_info->rowbytes;
2062#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2063 int num_p_filters = (int)png_ptr->num_prev_filters;
2064#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002065
Andreas Dilger47a0c421997-05-16 02:46:07 -05002066 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05002067 /* find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002068 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002069
2070 prev_row = png_ptr->prev_row;
2071 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002072 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002073
Andreas Dilger47a0c421997-05-16 02:46:07 -05002074 /* The prediction method we use is to find which method provides the
2075 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002076 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002077 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002078 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002079 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002080 * predictive" method (not implemented yet), which does test compressions
2081 * of lines using different filter methods, and then chooses the
2082 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002083 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002084 *
2085 * GRR 980525: consider also
2086 * (1) minimum sum of absolute differences from running average (i.e.,
2087 * keep running sum of non-absolute differences & count of bytes)
2088 * [track dispersion, too? restart average if dispersion too large?]
2089 * (1b) minimum sum of absolute differences from sliding average, probably
2090 * with window size <= deflate window (usually 32K)
2091 * (2) minimum sum of squared differences from zero or running average
2092 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002093 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002094
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002095
Guy Schalnate5a37791996-06-05 15:50:50 -05002096 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002097 * that has been chosen, as it doesn't actually do anything to the data.
2098 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002099 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002100 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002101 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002102 png_bytep rp;
2103 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002104 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002105 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002106
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002107 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002108 {
2109 v = *rp;
2110 sum += (v < 128) ? v : 256 - v;
2111 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002112
2113#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2114 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2115 {
2116 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002117 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002118 sumlo = sum & PNG_LOMASK;
2119 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2120
2121 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002122 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002123 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002124 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002125 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002126 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002127 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002128 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002129 PNG_WEIGHT_SHIFT;
2130 }
2131 }
2132
2133 /* Factor in the cost of this filter (this is here for completeness,
2134 * but it makes no sense to have a "cost" for the NONE filter, as
2135 * it has the minimum possible computational cost - none).
2136 */
2137 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2138 PNG_COST_SHIFT;
2139 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2140 PNG_COST_SHIFT;
2141
2142 if (sumhi > PNG_HIMASK)
2143 sum = PNG_MAXSUM;
2144 else
2145 sum = (sumhi << PNG_HISHIFT) + sumlo;
2146 }
2147#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002148 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002149 }
2150
Guy Schalnate5a37791996-06-05 15:50:50 -05002151 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002152 if (filter_to_do == PNG_FILTER_SUB)
2153 /* it's the only filter so no testing is needed */
2154 {
2155 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002156 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002157 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2158 i++, rp++, dp++)
2159 {
2160 *dp = *rp;
2161 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002162 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002163 i++, rp++, lp++, dp++)
2164 {
2165 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2166 }
2167 best_row = png_ptr->sub_row;
2168 }
2169
2170 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002171 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002172 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002173 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002174 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002175 int v;
2176
2177#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002178 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002179 * would reduce the sum of this filter, so that we can do the
2180 * early exit comparison without scaling the sum each time.
2181 */
2182 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2183 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002184 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002185 png_uint_32 lmhi, lmlo;
2186 lmlo = lmins & PNG_LOMASK;
2187 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2188
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002189 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002190 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002191 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002192 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002193 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002194 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002195 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002196 PNG_WEIGHT_SHIFT;
2197 }
2198 }
2199
2200 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2201 PNG_COST_SHIFT;
2202 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2203 PNG_COST_SHIFT;
2204
2205 if (lmhi > PNG_HIMASK)
2206 lmins = PNG_MAXSUM;
2207 else
2208 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2209 }
2210#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002211
Guy Schalnate5a37791996-06-05 15:50:50 -05002212 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2213 i++, rp++, dp++)
2214 {
2215 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002216
Guy Schalnate5a37791996-06-05 15:50:50 -05002217 sum += (v < 128) ? v : 256 - v;
2218 }
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002219 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002220 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002221 {
2222 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002223
Guy Schalnate5a37791996-06-05 15:50:50 -05002224 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002225
2226 if (sum > lmins) /* We are already worse, don't continue. */
2227 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002228 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002229
2230#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2231 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2232 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002233 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002234 png_uint_32 sumhi, sumlo;
2235 sumlo = sum & PNG_LOMASK;
2236 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2237
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002238 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002239 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002240 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002241 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002242 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002243 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002244 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002245 PNG_WEIGHT_SHIFT;
2246 }
2247 }
2248
2249 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2250 PNG_COST_SHIFT;
2251 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2252 PNG_COST_SHIFT;
2253
2254 if (sumhi > PNG_HIMASK)
2255 sum = PNG_MAXSUM;
2256 else
2257 sum = (sumhi << PNG_HISHIFT) + sumlo;
2258 }
2259#endif
2260
Guy Schalnate5a37791996-06-05 15:50:50 -05002261 if (sum < mins)
2262 {
2263 mins = sum;
2264 best_row = png_ptr->sub_row;
2265 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002266 }
2267
Guy Schalnate5a37791996-06-05 15:50:50 -05002268 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002269 if (filter_to_do == PNG_FILTER_UP)
2270 {
2271 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002272 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002273
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002274 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002275 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002276 i++, rp++, pp++, dp++)
2277 {
2278 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2279 }
2280 best_row = png_ptr->up_row;
2281 }
2282
2283 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002284 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002285 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002286 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002287 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002288 int v;
2289
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002290
Andreas Dilger47a0c421997-05-16 02:46:07 -05002291#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2292 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2293 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002294 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002295 png_uint_32 lmhi, lmlo;
2296 lmlo = lmins & PNG_LOMASK;
2297 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2298
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002299 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002300 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002301 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002302 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002303 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002304 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002305 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002306 PNG_WEIGHT_SHIFT;
2307 }
2308 }
2309
2310 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2311 PNG_COST_SHIFT;
2312 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2313 PNG_COST_SHIFT;
2314
2315 if (lmhi > PNG_HIMASK)
2316 lmins = PNG_MAXSUM;
2317 else
2318 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2319 }
2320#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002321
2322 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002323 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002324 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002325 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002326
2327 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002328
2329 if (sum > lmins) /* We are already worse, don't continue. */
2330 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002331 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002332
2333#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2334 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2335 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002336 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002337 png_uint_32 sumhi, sumlo;
2338 sumlo = sum & PNG_LOMASK;
2339 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2340
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002341 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002342 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002343 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002344 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002345 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002346 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002347 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002348 PNG_WEIGHT_SHIFT;
2349 }
2350 }
2351
2352 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2353 PNG_COST_SHIFT;
2354 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2355 PNG_COST_SHIFT;
2356
2357 if (sumhi > PNG_HIMASK)
2358 sum = PNG_MAXSUM;
2359 else
2360 sum = (sumhi << PNG_HISHIFT) + sumlo;
2361 }
2362#endif
2363
Guy Schalnate5a37791996-06-05 15:50:50 -05002364 if (sum < mins)
2365 {
2366 mins = sum;
2367 best_row = png_ptr->up_row;
2368 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002369 }
2370
Guy Schalnate5a37791996-06-05 15:50:50 -05002371 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002372 if (filter_to_do == PNG_FILTER_AVG)
2373 {
2374 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002375 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002376 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002377 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002378 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002379 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002380 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002381 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002382 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002383 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2384 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002385 }
2386 best_row = png_ptr->avg_row;
2387 }
2388
2389 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002390 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002391 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002392 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002393 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002394 int v;
2395
2396#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2397 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2398 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002399 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002400 png_uint_32 lmhi, lmlo;
2401 lmlo = lmins & PNG_LOMASK;
2402 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2403
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002404 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002405 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002406 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002407 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002408 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002409 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002410 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002411 PNG_WEIGHT_SHIFT;
2412 }
2413 }
2414
2415 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2416 PNG_COST_SHIFT;
2417 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2418 PNG_COST_SHIFT;
2419
2420 if (lmhi > PNG_HIMASK)
2421 lmins = PNG_MAXSUM;
2422 else
2423 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2424 }
2425#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002426
2427 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002428 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002429 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002430 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002431
2432 sum += (v < 128) ? v : 256 - v;
2433 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002434 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002435 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002436 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002437 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002438
2439 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002440
2441 if (sum > lmins) /* We are already worse, don't continue. */
2442 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002443 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002444
2445#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2446 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2447 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002448 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002449 png_uint_32 sumhi, sumlo;
2450 sumlo = sum & PNG_LOMASK;
2451 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2452
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002453 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002454 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002455 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002456 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002457 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002458 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002459 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002460 PNG_WEIGHT_SHIFT;
2461 }
2462 }
2463
2464 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2465 PNG_COST_SHIFT;
2466 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2467 PNG_COST_SHIFT;
2468
2469 if (sumhi > PNG_HIMASK)
2470 sum = PNG_MAXSUM;
2471 else
2472 sum = (sumhi << PNG_HISHIFT) + sumlo;
2473 }
2474#endif
2475
Guy Schalnate5a37791996-06-05 15:50:50 -05002476 if (sum < mins)
2477 {
2478 mins = sum;
2479 best_row = png_ptr->avg_row;
2480 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002481 }
2482
Andreas Dilger47a0c421997-05-16 02:46:07 -05002483 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002484 if (filter_to_do == PNG_FILTER_PAETH)
2485 {
2486 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002487 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002488 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002489 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002490 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002491 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002492 }
2493
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002494 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002495 {
2496 int a, b, c, pa, pb, pc, p;
2497
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002498 b = *pp++;
2499 c = *cp++;
2500 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002501
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002502 p = b - c;
2503 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002504
2505#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002506 pa = abs(p);
2507 pb = abs(pc);
2508 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002509#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002510 pa = p < 0 ? -p : p;
2511 pb = pc < 0 ? -pc : pc;
2512 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002513#endif
2514
2515 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2516
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002517 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002518 }
2519 best_row = png_ptr->paeth_row;
2520 }
2521
2522 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002523 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002524 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002525 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002526 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002527 int v;
2528
2529#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2530 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2531 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002532 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002533 png_uint_32 lmhi, lmlo;
2534 lmlo = lmins & PNG_LOMASK;
2535 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2536
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002537 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002538 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002539 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002540 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002541 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002542 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002543 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002544 PNG_WEIGHT_SHIFT;
2545 }
2546 }
2547
2548 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2549 PNG_COST_SHIFT;
2550 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2551 PNG_COST_SHIFT;
2552
2553 if (lmhi > PNG_HIMASK)
2554 lmins = PNG_MAXSUM;
2555 else
2556 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2557 }
2558#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002559
2560 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002561 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002562 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002563 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002564
2565 sum += (v < 128) ? v : 256 - v;
2566 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002567
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002568 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002569 {
2570 int a, b, c, pa, pb, pc, p;
2571
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002572 b = *pp++;
2573 c = *cp++;
2574 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002575
2576#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002577 p = b - c;
2578 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002579#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002580 pa = abs(p);
2581 pb = abs(pc);
2582 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002583#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002584 pa = p < 0 ? -p : p;
2585 pb = pc < 0 ? -pc : pc;
2586 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002587#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002588 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2589#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002590 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002591 pa = abs(p - a);
2592 pb = abs(p - b);
2593 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002594 if (pa <= pb && pa <= pc)
2595 p = a;
2596 else if (pb <= pc)
2597 p = b;
2598 else
2599 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002600#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002601
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002602 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002603
2604 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002605
2606 if (sum > lmins) /* We are already worse, don't continue. */
2607 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002608 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002609
2610#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2611 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2612 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002613 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002614 png_uint_32 sumhi, sumlo;
2615 sumlo = sum & PNG_LOMASK;
2616 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2617
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002618 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002619 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002620 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002621 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002622 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002623 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002624 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002625 PNG_WEIGHT_SHIFT;
2626 }
2627 }
2628
2629 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2630 PNG_COST_SHIFT;
2631 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2632 PNG_COST_SHIFT;
2633
2634 if (sumhi > PNG_HIMASK)
2635 sum = PNG_MAXSUM;
2636 else
2637 sum = (sumhi << PNG_HISHIFT) + sumlo;
2638 }
2639#endif
2640
Guy Schalnate5a37791996-06-05 15:50:50 -05002641 if (sum < mins)
2642 {
2643 best_row = png_ptr->paeth_row;
2644 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002645 }
2646
Andreas Dilger47a0c421997-05-16 02:46:07 -05002647 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002648
Guy Schalnate5a37791996-06-05 15:50:50 -05002649 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002650
2651#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2652 /* Save the type of filter we picked this time for future calculations */
2653 if (png_ptr->num_prev_filters > 0)
2654 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002655 int j;
2656 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002657 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002658 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002659 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002660 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002661 }
2662#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002663}
2664
2665
Andreas Dilger47a0c421997-05-16 02:46:07 -05002666/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002667void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002668png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2669{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002670 png_debug(1, "in png_write_filtered_row\n");
2671 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002672 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002673
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002674 png_ptr->zstream.next_in = filtered_row;
2675 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002676 /* repeat until we have compressed all the data */
2677 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002678 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002679 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002680
Guy Schalnate5a37791996-06-05 15:50:50 -05002681 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002682 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002683 /* check for compression errors */
2684 if (ret != Z_OK)
2685 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002686 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002687 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002688 else
2689 png_error(png_ptr, "zlib error");
2690 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002691
Guy Schalnate5a37791996-06-05 15:50:50 -05002692 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002693 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002694 {
2695 /* write the IDAT and reset the zlib output buffer */
2696 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002697 png_ptr->zstream.next_out = png_ptr->zbuf;
2698 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002699 }
2700 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002701 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002702
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002703 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002704 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002705 {
2706 png_bytep tptr;
2707
2708 tptr = png_ptr->prev_row;
2709 png_ptr->prev_row = png_ptr->row_buf;
2710 png_ptr->row_buf = tptr;
2711 }
2712
Guy Schalnate5a37791996-06-05 15:50:50 -05002713 /* finish row - updates counters and flushes zlib if last row */
2714 png_write_finish_row(png_ptr);
2715
2716#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2717 png_ptr->flush_rows++;
2718
2719 if (png_ptr->flush_dist > 0 &&
2720 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002721 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002722 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002723 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002724#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002725}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05002726#endif /* PNG_WRITE_SUPPORTED */