blob: dd7b150b75f8da6690e524307ddef115951de881 [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-Pehrson917648e2004-12-02 18:14:51 -06004 * libpng version 1.2.8 - December 3, 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
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600360 /* reset zlib for another zTXt/iTXt or image data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600361 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600362 png_ptr->zstream.data_type = Z_BINARY;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600363}
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;
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600526 /* libpng is not interested in zstream.data_type */
527 /* set it to a predefined value, to avoid its evaluation inside zlib */
528 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500529
Guy Schalnate5a37791996-06-05 15:50:50 -0500530 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500531}
532
533/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500534 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600535 * structure.
536 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500537void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500538png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500539{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600540#ifdef PNG_USE_LOCAL_ARRAYS
541 PNG_PLTE;
542#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500543 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600544 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500545 png_byte buf[3];
546
Andreas Dilger47a0c421997-05-16 02:46:07 -0500547 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500548 if ((
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500549#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600550 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500551#endif
552 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500553 {
554 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500555 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500556 png_error(png_ptr, "Invalid number of colors in palette");
557 }
558 else
559 {
560 png_warning(png_ptr, "Invalid number of colors in palette");
561 return;
562 }
563 }
564
565 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
566 {
567 png_warning(png_ptr,
568 "Ignoring request to write a PLTE chunk in grayscale PNG");
569 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500570 }
571
Andreas Dilger47a0c421997-05-16 02:46:07 -0500572 png_ptr->num_palette = (png_uint_16)num_pal;
573 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500574
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600575 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500576#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500577 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500578 {
579 buf[0] = pal_ptr->red;
580 buf[1] = pal_ptr->green;
581 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500582 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500583 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500584#else
585 /* This is a little slower but some buggy compilers need to do this instead */
586 pal_ptr=palette;
587 for (i = 0; i < num_pal; i++)
588 {
589 buf[0] = pal_ptr[i].red;
590 buf[1] = pal_ptr[i].green;
591 buf[2] = pal_ptr[i].blue;
592 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
593 }
594#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500595 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500596 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500597}
598
599/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500600void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500601png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500602{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600603#ifdef PNG_USE_LOCAL_ARRAYS
604 PNG_IDAT;
605#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500606 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500607
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500608 /* Optimize the CMF field in the zlib stream. */
609 /* This hack of the zlib stream is compliant to the stream specification. */
610 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
611 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
612 {
613 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
614 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
615 {
616 /* Avoid memory underflows and multiplication overflows. */
617 /* The conditions below are practically always satisfied;
618 however, they still must be checked. */
619 if (length >= 2 &&
620 png_ptr->height < 16384 && png_ptr->width < 16384)
621 {
622 png_uint_32 uncompressed_idat_size = png_ptr->height *
623 ((png_ptr->width *
624 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
625 unsigned int z_cinfo = z_cmf >> 4;
626 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
627 while (uncompressed_idat_size <= half_z_window_size &&
628 half_z_window_size >= 256)
629 {
630 z_cinfo--;
631 half_z_window_size >>= 1;
632 }
633 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
634 if (data[0] != (png_byte)z_cmf)
635 {
636 data[0] = (png_byte)z_cmf;
637 data[1] &= 0xe0;
638 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
639 }
640 }
641 }
642 else
643 png_error(png_ptr,
644 "Invalid zlib compression method or flags in IDAT");
645 }
646
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600647 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500648 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500649}
650
651/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500652void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600653png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500654{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600655#ifdef PNG_USE_LOCAL_ARRAYS
656 PNG_IEND;
657#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500658 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500659 png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL,
Glenn Randers-Pehrson6c97ddb2001-10-24 22:01:19 -0500660 (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600661 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500662}
663
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500664#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500665/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600666#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500667void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500668png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500669{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600670#ifdef PNG_USE_LOCAL_ARRAYS
671 PNG_gAMA;
672#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500673 png_uint_32 igamma;
674 png_byte buf[4];
675
Andreas Dilger47a0c421997-05-16 02:46:07 -0500676 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600677 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600678 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500679 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600680 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500681}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500682#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600683#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500684void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600685png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600686{
687#ifdef PNG_USE_LOCAL_ARRAYS
688 PNG_gAMA;
689#endif
690 png_byte buf[4];
691
692 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600693 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500694 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600695 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
696}
697#endif
698#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500699
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600700#if defined(PNG_WRITE_sRGB_SUPPORTED)
701/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500702void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600703png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600704{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600705#ifdef PNG_USE_LOCAL_ARRAYS
706 PNG_sRGB;
707#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600708 png_byte buf[1];
709
710 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600711 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600712 png_warning(png_ptr,
713 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600714 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600715 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600716}
717#endif
718
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600719#if defined(PNG_WRITE_iCCP_SUPPORTED)
720/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500721void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600722png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
723 png_charp profile, int profile_len)
724{
725#ifdef PNG_USE_LOCAL_ARRAYS
726 PNG_iCCP;
727#endif
728 png_size_t name_len;
729 png_charp new_name;
730 compression_state comp;
731
732 png_debug(1, "in png_write_iCCP\n");
733 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
734 &new_name)) == 0)
735 {
736 png_warning(png_ptr, "Empty keyword in iCCP chunk");
737 return;
738 }
739
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600740 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600741 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600742
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500743 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600744 profile_len = 0;
745
746 if (profile_len)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500747 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600748 PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600749
750 /* make sure we include the NULL after the name and the compression type */
751 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
752 (png_uint_32)name_len+profile_len+2);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600753 new_name[name_len+1]=0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600754 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
755
756 if (profile_len)
757 png_write_compressed_data_out(png_ptr, &comp);
758
759 png_write_chunk_end(png_ptr);
760 png_free(png_ptr, new_name);
761}
762#endif
763
764#if defined(PNG_WRITE_sPLT_SUPPORTED)
765/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500766void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600767png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600768{
769#ifdef PNG_USE_LOCAL_ARRAYS
770 PNG_sPLT;
771#endif
772 png_size_t name_len;
773 png_charp new_name;
774 png_byte entrybuf[10];
775 int entry_size = (spalette->depth == 8 ? 6 : 10);
776 int palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600777 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500778#ifdef PNG_NO_POINTER_INDEXING
779 int i;
780#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600781
782 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600783 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
784 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600785 {
786 png_warning(png_ptr, "Empty keyword in sPLT chunk");
787 return;
788 }
789
790 /* make sure we include the NULL after the name */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500791 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600792 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600793 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600794 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600795
796 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500797#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600798 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
799 {
800 if (spalette->depth == 8)
801 {
802 entrybuf[0] = (png_byte)ep->red;
803 entrybuf[1] = (png_byte)ep->green;
804 entrybuf[2] = (png_byte)ep->blue;
805 entrybuf[3] = (png_byte)ep->alpha;
806 png_save_uint_16(entrybuf + 4, ep->frequency);
807 }
808 else
809 {
810 png_save_uint_16(entrybuf + 0, ep->red);
811 png_save_uint_16(entrybuf + 2, ep->green);
812 png_save_uint_16(entrybuf + 4, ep->blue);
813 png_save_uint_16(entrybuf + 6, ep->alpha);
814 png_save_uint_16(entrybuf + 8, ep->frequency);
815 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500816 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600817 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500818#else
819 ep=spalette->entries;
820 for (i=0; i>spalette->nentries; i++)
821 {
822 if (spalette->depth == 8)
823 {
824 entrybuf[0] = (png_byte)ep[i].red;
825 entrybuf[1] = (png_byte)ep[i].green;
826 entrybuf[2] = (png_byte)ep[i].blue;
827 entrybuf[3] = (png_byte)ep[i].alpha;
828 png_save_uint_16(entrybuf + 4, ep[i].frequency);
829 }
830 else
831 {
832 png_save_uint_16(entrybuf + 0, ep[i].red);
833 png_save_uint_16(entrybuf + 2, ep[i].green);
834 png_save_uint_16(entrybuf + 4, ep[i].blue);
835 png_save_uint_16(entrybuf + 6, ep[i].alpha);
836 png_save_uint_16(entrybuf + 8, ep[i].frequency);
837 }
838 png_write_chunk_data(png_ptr, entrybuf, entry_size);
839 }
840#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600841
842 png_write_chunk_end(png_ptr);
843 png_free(png_ptr, new_name);
844}
845#endif
846
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500847#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500848/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500849void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600850png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500851{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600852#ifdef PNG_USE_LOCAL_ARRAYS
853 PNG_sBIT;
854#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500855 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500856 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500857
Andreas Dilger47a0c421997-05-16 02:46:07 -0500858 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600859 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500860 if (color_type & PNG_COLOR_MASK_COLOR)
861 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600862 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500863
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500864 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
865 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600866 if (sbit->red == 0 || sbit->red > maxbits ||
867 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500868 sbit->blue == 0 || sbit->blue > maxbits)
869 {
870 png_warning(png_ptr, "Invalid sBIT depth specified");
871 return;
872 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500873 buf[0] = sbit->red;
874 buf[1] = sbit->green;
875 buf[2] = sbit->blue;
876 size = 3;
877 }
878 else
879 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500880 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
881 {
882 png_warning(png_ptr, "Invalid sBIT depth specified");
883 return;
884 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500885 buf[0] = sbit->gray;
886 size = 1;
887 }
888
889 if (color_type & PNG_COLOR_MASK_ALPHA)
890 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500891 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
892 {
893 png_warning(png_ptr, "Invalid sBIT depth specified");
894 return;
895 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500896 buf[size++] = sbit->alpha;
897 }
898
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600899 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500900}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500901#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500902
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500903#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500904/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600905#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500906void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500907png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600908 double red_x, double red_y, double green_x, double green_y,
909 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500910{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600911#ifdef PNG_USE_LOCAL_ARRAYS
912 PNG_cHRM;
913#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500914 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600915 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500916
Andreas Dilger47a0c421997-05-16 02:46:07 -0500917 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600918 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500919 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
920 white_x + white_y > 1.0)
921 {
922 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500923#if !defined(PNG_NO_CONSOLE_IO)
924 fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600925#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500926 return;
927 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600928 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500929 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600930 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500931 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500932
933 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
934 red_x + red_y > 1.0)
935 {
936 png_warning(png_ptr, "Invalid cHRM red point specified");
937 return;
938 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600939 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500940 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600941 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500942 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500943
944 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
945 green_x + green_y > 1.0)
946 {
947 png_warning(png_ptr, "Invalid cHRM green point specified");
948 return;
949 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600950 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500951 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600952 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500953 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500954
955 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
956 blue_x + blue_y > 1.0)
957 {
958 png_warning(png_ptr, "Invalid cHRM blue point specified");
959 return;
960 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600961 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500962 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600963 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500964 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500965
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600966 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500967}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500968#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600969#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500970void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600971png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
972 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
973 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
974 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600975{
976#ifdef PNG_USE_LOCAL_ARRAYS
977 PNG_cHRM;
978#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600979 png_byte buf[32];
980
981 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600982 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600983 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
984 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600985 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500986#if !defined(PNG_NO_CONSOLE_IO)
987 fprintf(stderr,"white_x=%ld, white_y=%ld\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600988#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600989 return;
990 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500991 png_save_uint_32(buf, (png_uint_32)white_x);
992 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600993
994 if (red_x > 80000L || red_y > 80000L || red_x + red_y > 100000L)
995 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600996 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600997 return;
998 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500999 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1000 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001001
1002 if (green_x > 80000L || green_y > 80000L || green_x + green_y > 100000L)
1003 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001004 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001005 return;
1006 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001007 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1008 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001009
1010 if (blue_x > 80000L || blue_y > 80000L || blue_x + blue_y > 100000L)
1011 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001012 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001013 return;
1014 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001015 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1016 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001017
1018 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
1019}
1020#endif
1021#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001022
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001023#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001024/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001025void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001026png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -05001027 int num_trans, int color_type)
1028{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001029#ifdef PNG_USE_LOCAL_ARRAYS
1030 PNG_tRNS;
1031#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001032 png_byte buf[6];
1033
Andreas Dilger47a0c421997-05-16 02:46:07 -05001034 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001035 if (color_type == PNG_COLOR_TYPE_PALETTE)
1036 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001037 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001038 {
1039 png_warning(png_ptr,"Invalid number of transparent colors specified");
1040 return;
1041 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001042 /* write the chunk out as it is */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001043 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001044 }
1045 else if (color_type == PNG_COLOR_TYPE_GRAY)
1046 {
1047 /* one 16 bit value */
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001048 if(tran->gray >= (1 << png_ptr->bit_depth))
1049 {
1050 png_warning(png_ptr,
1051 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1052 return;
1053 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001054 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001055 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001056 }
1057 else if (color_type == PNG_COLOR_TYPE_RGB)
1058 {
1059 /* three 16 bit values */
1060 png_save_uint_16(buf, tran->red);
1061 png_save_uint_16(buf + 2, tran->green);
1062 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001063 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1064 {
1065 png_warning(png_ptr,
1066 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1067 return;
1068 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001069 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001070 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001071 else
1072 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001073 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001074 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001075}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001076#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001077
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001078#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001079/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001080void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001081png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001082{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001083#ifdef PNG_USE_LOCAL_ARRAYS
1084 PNG_bKGD;
1085#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001086 png_byte buf[6];
1087
Andreas Dilger47a0c421997-05-16 02:46:07 -05001088 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001089 if (color_type == PNG_COLOR_TYPE_PALETTE)
1090 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001091 if (
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001092#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001093 (png_ptr->num_palette ||
1094 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001095#endif
1096 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001097 {
1098 png_warning(png_ptr, "Invalid background palette index");
1099 return;
1100 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001101 buf[0] = back->index;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001102 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001103 }
1104 else if (color_type & PNG_COLOR_MASK_COLOR)
1105 {
1106 png_save_uint_16(buf, back->red);
1107 png_save_uint_16(buf + 2, back->green);
1108 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001109 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1110 {
1111 png_warning(png_ptr,
1112 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1113 return;
1114 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001115 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001116 }
1117 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001118 {
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001119 if(back->gray >= (1 << png_ptr->bit_depth))
1120 {
1121 png_warning(png_ptr,
1122 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1123 return;
1124 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001125 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001126 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001127 }
1128}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001129#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001130
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001131#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001132/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001133void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001134png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001135{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001136#ifdef PNG_USE_LOCAL_ARRAYS
1137 PNG_hIST;
1138#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001139 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001140 png_byte buf[3];
1141
Andreas Dilger47a0c421997-05-16 02:46:07 -05001142 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001143 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001144 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001145 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1146 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001147 png_warning(png_ptr, "Invalid number of histogram entries specified");
1148 return;
1149 }
1150
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001151 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001152 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001153 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001154 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001155 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001156 }
1157 png_write_chunk_end(png_ptr);
1158}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001159#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001160
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001161#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1162 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001163/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1164 * and if invalid, correct the keyword rather than discarding the entire
1165 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1166 * length, forbids leading or trailing whitespace, multiple internal spaces,
1167 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001168 *
1169 * The new_key is allocated to hold the corrected keyword and must be freed
1170 * by the calling routine. This avoids problems with trying to write to
1171 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001172 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001173png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001174png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001175{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001176 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001177 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001178 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001179 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001180
Andreas Dilger47a0c421997-05-16 02:46:07 -05001181 png_debug(1, "in png_check_keyword\n");
1182 *new_key = NULL;
1183
1184 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001185 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001186 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001187 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001188 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001189
Andreas Dilger47a0c421997-05-16 02:46:07 -05001190 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1191
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001192 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
1193 if (*new_key == NULL)
1194 {
1195 png_warning(png_ptr, "Out of memory while procesing keyword");
1196 return ((png_size_t)0);
1197 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001198
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001199 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001200 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001201 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001202 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001203 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001204#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001205 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001206
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001207 sprintf(msg, "invalid keyword character 0x%02X", *kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001208 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001209#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001210 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001211#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001212 *dp = ' ';
1213 }
1214 else
1215 {
1216 *dp = *kp;
1217 }
1218 }
1219 *dp = '\0';
1220
1221 /* Remove any trailing white space. */
1222 kp = *new_key + key_len - 1;
1223 if (*kp == ' ')
1224 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001225 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001226
1227 while (*kp == ' ')
1228 {
1229 *(kp--) = '\0';
1230 key_len--;
1231 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001232 }
1233
1234 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001235 kp = *new_key;
1236 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001237 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001238 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001239
1240 while (*kp == ' ')
1241 {
1242 kp++;
1243 key_len--;
1244 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001245 }
1246
Andreas Dilger47a0c421997-05-16 02:46:07 -05001247 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001248
Andreas Dilger47a0c421997-05-16 02:46:07 -05001249 /* Remove multiple internal spaces. */
1250 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001251 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001252 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001253 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001254 *(dp++) = *kp;
1255 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001256 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001257 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001258 {
1259 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001260 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001261 }
1262 else
1263 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001264 *(dp++) = *kp;
1265 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001266 }
1267 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001268 *dp = '\0';
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001269 if(kwarn)
1270 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001271
1272 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001273 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001274 png_free(png_ptr, *new_key);
1275 *new_key=NULL;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001276 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001277 }
1278
1279 if (key_len > 79)
1280 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001281 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001282 new_key[79] = '\0';
1283 key_len = 79;
1284 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001285
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001286 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001287}
1288#endif
1289
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001290#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001291/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001292void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001293png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001294 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001295{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001296#ifdef PNG_USE_LOCAL_ARRAYS
1297 PNG_tEXt;
1298#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001299 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001300 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001301
Andreas Dilger47a0c421997-05-16 02:46:07 -05001302 png_debug(1, "in png_write_tEXt\n");
1303 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1304 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001305 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001306 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001307 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001308
Andreas Dilger47a0c421997-05-16 02:46:07 -05001309 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001310 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001311 else
1312 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001313
1314 /* make sure we include the 0 after the key */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001315 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 -05001316 /*
1317 * We leave it to the application to meet PNG-1.0 requirements on the
1318 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1319 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001320 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001321 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001322 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001323 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001324 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001325
Guy Schalnat0d580581995-07-20 02:43:20 -05001326 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001327 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001328}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001329#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001330
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001331#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001332/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001333void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001334png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001335 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001336{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001337#ifdef PNG_USE_LOCAL_ARRAYS
1338 PNG_zTXt;
1339#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001340 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001341 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001342 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001343 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001344
Andreas Dilger47a0c421997-05-16 02:46:07 -05001345 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001346
Andreas Dilger47a0c421997-05-16 02:46:07 -05001347 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001348 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001349 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001350 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001351 }
1352
Andreas Dilger47a0c421997-05-16 02:46:07 -05001353 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1354 {
1355 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1356 png_free(png_ptr, new_key);
1357 return;
1358 }
1359
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001360 text_len = png_strlen(text);
1361
Andreas Dilger47a0c421997-05-16 02:46:07 -05001362 png_free(png_ptr, new_key);
1363
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001364 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001365 text_len = png_text_compress(png_ptr, text, text_len, compression,
1366 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001367
1368 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001369 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1370 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001371 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001372 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001373 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001374 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001375 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001376 /* write the compressed data */
1377 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001378
Guy Schalnat0d580581995-07-20 02:43:20 -05001379 /* close the chunk */
1380 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001381}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001382#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001383
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001384#if defined(PNG_WRITE_iTXt_SUPPORTED)
1385/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001386void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001387png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001388 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001389{
1390#ifdef PNG_USE_LOCAL_ARRAYS
1391 PNG_iTXt;
1392#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001393 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001394 png_charp new_lang, new_key;
1395 png_byte cbuf[2];
1396 compression_state comp;
1397
1398 png_debug(1, "in png_write_iTXt\n");
1399
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001400 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1401 {
1402 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1403 return;
1404 }
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001405 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001406 {
1407 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001408 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001409 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001410 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001411
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001412 if (lang_key == NULL)
1413 lang_key_len = 0;
1414 else
1415 lang_key_len = png_strlen(lang_key);
1416
1417 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001418 text_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001419 else
1420 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001421
1422 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001423 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1424 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001425
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001426
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001427 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001428 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001429
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001430 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1431 (png_uint_32)(
1432 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1433 + key_len
1434 + lang_len
1435 + lang_key_len
1436 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001437
1438 /*
1439 * We leave it to the application to meet PNG-1.0 requirements on the
1440 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1441 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1442 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1443 */
1444 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001445
1446 /* set the compression flag */
1447 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1448 compression == PNG_TEXT_COMPRESSION_NONE)
1449 cbuf[0] = 0;
1450 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1451 cbuf[0] = 1;
1452 /* set the compression method */
1453 cbuf[1] = 0;
1454 png_write_chunk_data(png_ptr, cbuf, 2);
1455
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001456 cbuf[0] = 0;
1457 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf), lang_len + 1);
1458 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 -06001459 png_write_compressed_data_out(png_ptr, &comp);
1460
1461 png_write_chunk_end(png_ptr);
1462 png_free(png_ptr, new_key);
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001463 if (new_lang)
1464 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001465}
1466#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001467
1468#if defined(PNG_WRITE_oFFs_SUPPORTED)
1469/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001470void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001471png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001472 int unit_type)
1473{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001474#ifdef PNG_USE_LOCAL_ARRAYS
1475 PNG_oFFs;
1476#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001477 png_byte buf[9];
1478
1479 png_debug(1, "in png_write_oFFs\n");
1480 if (unit_type >= PNG_OFFSET_LAST)
1481 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1482
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001483 png_save_int_32(buf, x_offset);
1484 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001485 buf[8] = (png_byte)unit_type;
1486
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001487 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001488}
1489#endif
1490
1491#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001492/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001493void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001494png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1495 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1496{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001497#ifdef PNG_USE_LOCAL_ARRAYS
1498 PNG_pCAL;
1499#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001500 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001501 png_uint_32p params_len;
1502 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001503 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001504 int i;
1505
1506 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1507 if (type >= PNG_EQUATION_LAST)
1508 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1509
1510 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001511 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001512 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001513 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001514 total_len = purpose_len + units_len + 10;
1515
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001516 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001517 *png_sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001518
1519 /* Find the length of each parameter, making sure we don't count the
1520 null terminator for the last parameter. */
1521 for (i = 0; i < nparams; i++)
1522 {
1523 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001524 png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001525 total_len += (png_size_t)params_len[i];
1526 }
1527
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001528 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001529 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001530 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001531 png_save_int_32(buf, X0);
1532 png_save_int_32(buf + 4, X1);
1533 buf[8] = (png_byte)type;
1534 buf[9] = (png_byte)nparams;
1535 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1536 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1537
1538 png_free(png_ptr, new_purpose);
1539
1540 for (i = 0; i < nparams; i++)
1541 {
1542 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1543 (png_size_t)params_len[i]);
1544 }
1545
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001546 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001547 png_write_chunk_end(png_ptr);
1548}
1549#endif
1550
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001551#if defined(PNG_WRITE_sCAL_SUPPORTED)
1552/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001553#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001554void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001555png_write_sCAL(png_structp png_ptr, int unit, double width,double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001556{
1557#ifdef PNG_USE_LOCAL_ARRAYS
1558 PNG_sCAL;
1559#endif
1560 png_size_t total_len;
1561 char wbuf[32], hbuf[32];
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001562 png_byte bunit = unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001563
1564 png_debug(1, "in png_write_sCAL\n");
1565
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001566#if defined(_WIN32_WCE)
1567/* sprintf() function is not supported on WindowsCE */
1568 {
1569 wchar_t wc_buf[32];
1570 swprintf(wc_buf, TEXT("%12.12e"), width);
1571 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, wbuf, 32, NULL, NULL);
1572 swprintf(wc_buf, TEXT("%12.12e"), height);
1573 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, hbuf, 32, NULL, NULL);
1574 }
1575#else
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001576 sprintf(wbuf, "%12.12e", width);
1577 sprintf(hbuf, "%12.12e", height);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001578#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001579 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001580
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001581 png_debug1(3, "sCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001582 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001583 png_write_chunk_data(png_ptr, (png_bytep)&bunit, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001584 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1585 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001586
1587 png_write_chunk_end(png_ptr);
1588}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001589#else
1590#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001591void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001592png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001593 png_charp height)
1594{
1595#ifdef PNG_USE_LOCAL_ARRAYS
1596 PNG_sCAL;
1597#endif
1598 png_size_t total_len;
1599 char wbuf[32], hbuf[32];
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001600 png_byte bunit = unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001601
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001602 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001603
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001604 png_strcpy(wbuf,(const char *)width);
1605 png_strcpy(hbuf,(const char *)height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001606 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001607
1608 png_debug1(3, "sCAL total length = %d\n", total_len);
1609 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001610 png_write_chunk_data(png_ptr, (png_bytep)&bunit, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001611 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1612 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001613
1614 png_write_chunk_end(png_ptr);
1615}
1616#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001617#endif
1618#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001619
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001620#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001621/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001622void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001623png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001624 png_uint_32 y_pixels_per_unit,
1625 int unit_type)
1626{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001627#ifdef PNG_USE_LOCAL_ARRAYS
1628 PNG_pHYs;
1629#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001630 png_byte buf[9];
1631
Andreas Dilger47a0c421997-05-16 02:46:07 -05001632 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001633 if (unit_type >= PNG_RESOLUTION_LAST)
1634 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1635
Guy Schalnat0d580581995-07-20 02:43:20 -05001636 png_save_uint_32(buf, x_pixels_per_unit);
1637 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001638 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001639
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001640 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001641}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001642#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001643
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001644#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001645/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1646 * or png_convert_from_time_t(), or fill in the structure yourself.
1647 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001648void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001649png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001650{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001651#ifdef PNG_USE_LOCAL_ARRAYS
1652 PNG_tIME;
1653#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001654 png_byte buf[7];
1655
Andreas Dilger47a0c421997-05-16 02:46:07 -05001656 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001657 if (mod_time->month > 12 || mod_time->month < 1 ||
1658 mod_time->day > 31 || mod_time->day < 1 ||
1659 mod_time->hour > 23 || mod_time->second > 60)
1660 {
1661 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1662 return;
1663 }
1664
Guy Schalnat0d580581995-07-20 02:43:20 -05001665 png_save_uint_16(buf, mod_time->year);
1666 buf[2] = mod_time->month;
1667 buf[3] = mod_time->day;
1668 buf[4] = mod_time->hour;
1669 buf[5] = mod_time->minute;
1670 buf[6] = mod_time->second;
1671
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001672 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001673}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001674#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001675
1676/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001677void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001678png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001679{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001680#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001681 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001682
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001683 /* start of interlace block */
1684 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001685
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001686 /* offset to next interlace block */
1687 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001688
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001689 /* start of interlace block in the y direction */
1690 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001691
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001692 /* offset to next interlace block in the y direction */
1693 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001694#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001695
Andreas Dilger47a0c421997-05-16 02:46:07 -05001696 png_size_t buf_size;
1697
1698 png_debug(1, "in png_write_start_row\n");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001699 buf_size = (png_size_t)(PNG_ROWBYTES(
1700 png_ptr->usr_channels*png_ptr->usr_bit_depth,png_ptr->width)+1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001701
Guy Schalnat0d580581995-07-20 02:43:20 -05001702 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001703 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001704 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001705
1706 /* set up filtering buffer, if using this filter */
1707 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001708 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001709 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001710 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001711 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001712 }
1713
Andreas Dilger47a0c421997-05-16 02:46:07 -05001714 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001715 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1716 {
1717 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001718 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001719 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001720
1721 if (png_ptr->do_filter & PNG_FILTER_UP)
1722 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001723 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001724 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001725 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001726 }
1727
1728 if (png_ptr->do_filter & PNG_FILTER_AVG)
1729 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001730 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1731 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001732 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001733 }
1734
1735 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1736 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001737 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001738 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001739 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001740 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001741 }
1742
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001743#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001744 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001745 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001746 {
1747 if (!(png_ptr->transformations & PNG_INTERLACE))
1748 {
1749 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1750 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001751 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1752 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001753 }
1754 else
1755 {
1756 png_ptr->num_rows = png_ptr->height;
1757 png_ptr->usr_width = png_ptr->width;
1758 }
1759 }
1760 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001761#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001762 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001763 png_ptr->num_rows = png_ptr->height;
1764 png_ptr->usr_width = png_ptr->width;
1765 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001766 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1767 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001768}
1769
Andreas Dilger47a0c421997-05-16 02:46:07 -05001770/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001771void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001772png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001773{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001774#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001775 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001776
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001777 /* start of interlace block */
1778 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001779
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001780 /* offset to next interlace block */
1781 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001782
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001783 /* start of interlace block in the y direction */
1784 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001785
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001786 /* offset to next interlace block in the y direction */
1787 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001788#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001789
Guy Schalnat0d580581995-07-20 02:43:20 -05001790 int ret;
1791
Andreas Dilger47a0c421997-05-16 02:46:07 -05001792 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001793 /* next row */
1794 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001795
Guy Schalnat0d580581995-07-20 02:43:20 -05001796 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001797 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001798 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001799
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001800#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001801 /* if interlaced, go to next pass */
1802 if (png_ptr->interlaced)
1803 {
1804 png_ptr->row_number = 0;
1805 if (png_ptr->transformations & PNG_INTERLACE)
1806 {
1807 png_ptr->pass++;
1808 }
1809 else
1810 {
1811 /* loop until we find a non-zero width or height pass */
1812 do
1813 {
1814 png_ptr->pass++;
1815 if (png_ptr->pass >= 7)
1816 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001817 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001818 png_pass_inc[png_ptr->pass] - 1 -
1819 png_pass_start[png_ptr->pass]) /
1820 png_pass_inc[png_ptr->pass];
1821 png_ptr->num_rows = (png_ptr->height +
1822 png_pass_yinc[png_ptr->pass] - 1 -
1823 png_pass_ystart[png_ptr->pass]) /
1824 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001825 if (png_ptr->transformations & PNG_INTERLACE)
1826 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001827 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1828
1829 }
1830
Guy Schalnate5a37791996-06-05 15:50:50 -05001831 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001832 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001833 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001834 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001835 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001836 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
1837 png_ptr->usr_bit_depth,png_ptr->width))+1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001838 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001839 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001840 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001841#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001842
1843 /* if we get here, we've just written the last row, so we need
1844 to flush the compressor */
1845 do
1846 {
1847 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001848 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001849 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001850 if (ret == Z_OK)
1851 {
1852 /* check to see if we need more room */
1853 if (!(png_ptr->zstream.avail_out))
1854 {
1855 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1856 png_ptr->zstream.next_out = png_ptr->zbuf;
1857 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1858 }
1859 }
1860 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001861 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001862 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001863 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001864 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001865 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001866 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001867 } while (ret != Z_STREAM_END);
1868
1869 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001870 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001871 {
1872 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001873 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001874 }
1875
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001876 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -06001877 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat0d580581995-07-20 02:43:20 -05001878}
1879
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001880#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001881/* Pick out the correct pixels for the interlace pass.
1882 * The basic idea here is to go through the row with a source
1883 * pointer and a destination pointer (sp and dp), and copy the
1884 * correct pixels for the pass. As the row gets compacted,
1885 * sp will always be >= dp, so we should never overwrite anything.
1886 * See the default: case for the easiest code to understand.
1887 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001888void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001889png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001890{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001891#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001892 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001893
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001894 /* start of interlace block */
1895 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001896
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001897 /* offset to next interlace block */
1898 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001899#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001900
Andreas Dilger47a0c421997-05-16 02:46:07 -05001901 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001902 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001903#if defined(PNG_USELESS_TESTS_SUPPORTED)
1904 if (row != NULL && row_info != NULL && pass < 6)
1905#else
1906 if (pass < 6)
1907#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001908 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001909 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001910 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001911 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001912 case 1:
1913 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001914 png_bytep sp;
1915 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001916 int shift;
1917 int d;
1918 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001919 png_uint_32 i;
1920 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001921
1922 dp = row;
1923 d = 0;
1924 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001925 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001926 i += png_pass_inc[pass])
1927 {
1928 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001929 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001930 d |= (value << shift);
1931
1932 if (shift == 0)
1933 {
1934 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001935 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001936 d = 0;
1937 }
1938 else
1939 shift--;
1940
1941 }
1942 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001943 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001944 break;
1945 }
1946 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001947 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001948 png_bytep sp;
1949 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001950 int shift;
1951 int d;
1952 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001953 png_uint_32 i;
1954 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001955
1956 dp = row;
1957 shift = 6;
1958 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001959 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001960 i += png_pass_inc[pass])
1961 {
1962 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001963 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05001964 d |= (value << shift);
1965
1966 if (shift == 0)
1967 {
1968 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001969 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001970 d = 0;
1971 }
1972 else
1973 shift -= 2;
1974 }
1975 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001976 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001977 break;
1978 }
1979 case 4:
1980 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001981 png_bytep sp;
1982 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001983 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001984 int d;
1985 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001986 png_uint_32 i;
1987 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001988
1989 dp = row;
1990 shift = 4;
1991 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001992 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001993 i += png_pass_inc[pass])
1994 {
1995 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001996 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05001997 d |= (value << shift);
1998
1999 if (shift == 0)
2000 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002001 shift = 4;
2002 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002003 d = 0;
2004 }
2005 else
2006 shift -= 4;
2007 }
2008 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002009 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002010 break;
2011 }
2012 default:
2013 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002014 png_bytep sp;
2015 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002016 png_uint_32 i;
2017 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002018 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002019
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002020 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002021 dp = row;
2022 /* find out how many bytes each pixel takes up */
2023 pixel_bytes = (row_info->pixel_depth >> 3);
2024 /* loop through the row, only looking at the pixels that
2025 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002026 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002027 i += png_pass_inc[pass])
2028 {
2029 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002030 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002031 /* move the pixel */
2032 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002033 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05002034 /* next pixel */
2035 dp += pixel_bytes;
2036 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002037 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002038 }
2039 }
2040 /* set new row width */
2041 row_info->width = (row_info->width +
2042 png_pass_inc[pass] - 1 -
2043 png_pass_start[pass]) /
2044 png_pass_inc[pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002045 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2046 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002047 }
2048}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002049#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002050
Andreas Dilger47a0c421997-05-16 02:46:07 -05002051/* This filters the row, chooses which filter to use, if it has not already
2052 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002053 * chosen filter.
2054 */
Glenn Randers-Pehrson78067772004-11-02 21:49:39 -06002055#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002056#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002057#define PNG_LOMASK ((png_uint_32)0xffffL)
2058#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002059void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002060png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002061{
Guy Schalnate5a37791996-06-05 15:50:50 -05002062 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002063 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002064 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002065 png_uint_32 row_bytes = row_info->rowbytes;
2066#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2067 int num_p_filters = (int)png_ptr->num_prev_filters;
2068#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002069
Andreas Dilger47a0c421997-05-16 02:46:07 -05002070 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05002071 /* find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002072 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002073
2074 prev_row = png_ptr->prev_row;
2075 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002076 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002077
Andreas Dilger47a0c421997-05-16 02:46:07 -05002078 /* The prediction method we use is to find which method provides the
2079 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002080 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002081 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002082 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002083 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002084 * predictive" method (not implemented yet), which does test compressions
2085 * of lines using different filter methods, and then chooses the
2086 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002087 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002088 *
2089 * GRR 980525: consider also
2090 * (1) minimum sum of absolute differences from running average (i.e.,
2091 * keep running sum of non-absolute differences & count of bytes)
2092 * [track dispersion, too? restart average if dispersion too large?]
2093 * (1b) minimum sum of absolute differences from sliding average, probably
2094 * with window size <= deflate window (usually 32K)
2095 * (2) minimum sum of squared differences from zero or running average
2096 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002097 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002098
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002099
Guy Schalnate5a37791996-06-05 15:50:50 -05002100 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002101 * that has been chosen, as it doesn't actually do anything to the data.
2102 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002103 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002104 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002105 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002106 png_bytep rp;
2107 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002108 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002109 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002110
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002111 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002112 {
2113 v = *rp;
2114 sum += (v < 128) ? v : 256 - v;
2115 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002116
2117#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2118 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2119 {
2120 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002121 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002122 sumlo = sum & PNG_LOMASK;
2123 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2124
2125 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002126 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002127 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002128 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002129 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002130 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002131 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002132 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002133 PNG_WEIGHT_SHIFT;
2134 }
2135 }
2136
2137 /* Factor in the cost of this filter (this is here for completeness,
2138 * but it makes no sense to have a "cost" for the NONE filter, as
2139 * it has the minimum possible computational cost - none).
2140 */
2141 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2142 PNG_COST_SHIFT;
2143 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2144 PNG_COST_SHIFT;
2145
2146 if (sumhi > PNG_HIMASK)
2147 sum = PNG_MAXSUM;
2148 else
2149 sum = (sumhi << PNG_HISHIFT) + sumlo;
2150 }
2151#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002152 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002153 }
2154
Guy Schalnate5a37791996-06-05 15:50:50 -05002155 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002156 if (filter_to_do == PNG_FILTER_SUB)
2157 /* it's the only filter so no testing is needed */
2158 {
2159 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002160 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002161 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2162 i++, rp++, dp++)
2163 {
2164 *dp = *rp;
2165 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002166 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002167 i++, rp++, lp++, dp++)
2168 {
2169 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2170 }
2171 best_row = png_ptr->sub_row;
2172 }
2173
2174 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002175 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002176 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002177 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002178 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002179 int v;
2180
2181#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002182 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002183 * would reduce the sum of this filter, so that we can do the
2184 * early exit comparison without scaling the sum each time.
2185 */
2186 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2187 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002188 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002189 png_uint_32 lmhi, lmlo;
2190 lmlo = lmins & PNG_LOMASK;
2191 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2192
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002193 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002194 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002195 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002196 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002197 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002198 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002199 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002200 PNG_WEIGHT_SHIFT;
2201 }
2202 }
2203
2204 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2205 PNG_COST_SHIFT;
2206 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2207 PNG_COST_SHIFT;
2208
2209 if (lmhi > PNG_HIMASK)
2210 lmins = PNG_MAXSUM;
2211 else
2212 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2213 }
2214#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002215
Guy Schalnate5a37791996-06-05 15:50:50 -05002216 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2217 i++, rp++, dp++)
2218 {
2219 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002220
Guy Schalnate5a37791996-06-05 15:50:50 -05002221 sum += (v < 128) ? v : 256 - v;
2222 }
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002223 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002224 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002225 {
2226 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002227
Guy Schalnate5a37791996-06-05 15:50:50 -05002228 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002229
2230 if (sum > lmins) /* We are already worse, don't continue. */
2231 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002232 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002233
2234#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2235 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2236 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002237 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002238 png_uint_32 sumhi, sumlo;
2239 sumlo = sum & PNG_LOMASK;
2240 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2241
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002242 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002243 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002244 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002245 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002246 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002247 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002248 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002249 PNG_WEIGHT_SHIFT;
2250 }
2251 }
2252
2253 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2254 PNG_COST_SHIFT;
2255 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2256 PNG_COST_SHIFT;
2257
2258 if (sumhi > PNG_HIMASK)
2259 sum = PNG_MAXSUM;
2260 else
2261 sum = (sumhi << PNG_HISHIFT) + sumlo;
2262 }
2263#endif
2264
Guy Schalnate5a37791996-06-05 15:50:50 -05002265 if (sum < mins)
2266 {
2267 mins = sum;
2268 best_row = png_ptr->sub_row;
2269 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002270 }
2271
Guy Schalnate5a37791996-06-05 15:50:50 -05002272 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002273 if (filter_to_do == PNG_FILTER_UP)
2274 {
2275 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002276 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002277
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002278 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002279 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002280 i++, rp++, pp++, dp++)
2281 {
2282 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2283 }
2284 best_row = png_ptr->up_row;
2285 }
2286
2287 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002288 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002289 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002290 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002291 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002292 int v;
2293
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002294
Andreas Dilger47a0c421997-05-16 02:46:07 -05002295#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2296 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2297 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002298 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002299 png_uint_32 lmhi, lmlo;
2300 lmlo = lmins & PNG_LOMASK;
2301 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2302
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002303 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002304 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002305 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002306 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002307 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002308 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002309 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002310 PNG_WEIGHT_SHIFT;
2311 }
2312 }
2313
2314 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2315 PNG_COST_SHIFT;
2316 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2317 PNG_COST_SHIFT;
2318
2319 if (lmhi > PNG_HIMASK)
2320 lmins = PNG_MAXSUM;
2321 else
2322 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2323 }
2324#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002325
2326 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002327 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002328 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002329 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002330
2331 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002332
2333 if (sum > lmins) /* We are already worse, don't continue. */
2334 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002335 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002336
2337#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2338 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2339 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002340 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002341 png_uint_32 sumhi, sumlo;
2342 sumlo = sum & PNG_LOMASK;
2343 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2344
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002345 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002346 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002347 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002348 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002349 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002350 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002351 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002352 PNG_WEIGHT_SHIFT;
2353 }
2354 }
2355
2356 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2357 PNG_COST_SHIFT;
2358 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2359 PNG_COST_SHIFT;
2360
2361 if (sumhi > PNG_HIMASK)
2362 sum = PNG_MAXSUM;
2363 else
2364 sum = (sumhi << PNG_HISHIFT) + sumlo;
2365 }
2366#endif
2367
Guy Schalnate5a37791996-06-05 15:50:50 -05002368 if (sum < mins)
2369 {
2370 mins = sum;
2371 best_row = png_ptr->up_row;
2372 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002373 }
2374
Guy Schalnate5a37791996-06-05 15:50:50 -05002375 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002376 if (filter_to_do == PNG_FILTER_AVG)
2377 {
2378 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002379 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002380 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002381 pp = prev_row + 1; i < bpp; 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++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002384 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002385 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002386 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002387 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2388 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002389 }
2390 best_row = png_ptr->avg_row;
2391 }
2392
2393 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002394 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002395 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002396 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002397 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002398 int v;
2399
2400#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2401 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2402 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002403 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002404 png_uint_32 lmhi, lmlo;
2405 lmlo = lmins & PNG_LOMASK;
2406 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2407
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002408 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002409 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002410 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002411 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002412 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002413 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002414 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002415 PNG_WEIGHT_SHIFT;
2416 }
2417 }
2418
2419 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2420 PNG_COST_SHIFT;
2421 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2422 PNG_COST_SHIFT;
2423
2424 if (lmhi > PNG_HIMASK)
2425 lmins = PNG_MAXSUM;
2426 else
2427 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2428 }
2429#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002430
2431 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002432 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002433 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002434 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002435
2436 sum += (v < 128) ? v : 256 - v;
2437 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002438 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002439 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002440 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002441 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002442
2443 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002444
2445 if (sum > lmins) /* We are already worse, don't continue. */
2446 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002447 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002448
2449#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2450 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2451 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002452 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002453 png_uint_32 sumhi, sumlo;
2454 sumlo = sum & PNG_LOMASK;
2455 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2456
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002457 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002458 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002459 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002460 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002461 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002462 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002463 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002464 PNG_WEIGHT_SHIFT;
2465 }
2466 }
2467
2468 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2469 PNG_COST_SHIFT;
2470 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2471 PNG_COST_SHIFT;
2472
2473 if (sumhi > PNG_HIMASK)
2474 sum = PNG_MAXSUM;
2475 else
2476 sum = (sumhi << PNG_HISHIFT) + sumlo;
2477 }
2478#endif
2479
Guy Schalnate5a37791996-06-05 15:50:50 -05002480 if (sum < mins)
2481 {
2482 mins = sum;
2483 best_row = png_ptr->avg_row;
2484 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002485 }
2486
Andreas Dilger47a0c421997-05-16 02:46:07 -05002487 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002488 if (filter_to_do == PNG_FILTER_PAETH)
2489 {
2490 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002491 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002492 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002493 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002494 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002495 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002496 }
2497
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002498 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002499 {
2500 int a, b, c, pa, pb, pc, p;
2501
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002502 b = *pp++;
2503 c = *cp++;
2504 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002505
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002506 p = b - c;
2507 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002508
2509#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002510 pa = abs(p);
2511 pb = abs(pc);
2512 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002513#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002514 pa = p < 0 ? -p : p;
2515 pb = pc < 0 ? -pc : pc;
2516 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002517#endif
2518
2519 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2520
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002521 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002522 }
2523 best_row = png_ptr->paeth_row;
2524 }
2525
2526 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002527 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002528 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002529 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002530 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002531 int v;
2532
2533#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2534 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2535 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002536 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002537 png_uint_32 lmhi, lmlo;
2538 lmlo = lmins & PNG_LOMASK;
2539 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2540
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002541 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002542 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002543 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002544 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002545 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002546 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002547 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002548 PNG_WEIGHT_SHIFT;
2549 }
2550 }
2551
2552 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2553 PNG_COST_SHIFT;
2554 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2555 PNG_COST_SHIFT;
2556
2557 if (lmhi > PNG_HIMASK)
2558 lmins = PNG_MAXSUM;
2559 else
2560 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2561 }
2562#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002563
2564 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002565 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002566 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002567 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002568
2569 sum += (v < 128) ? v : 256 - v;
2570 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002571
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002572 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002573 {
2574 int a, b, c, pa, pb, pc, p;
2575
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002576 b = *pp++;
2577 c = *cp++;
2578 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002579
2580#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002581 p = b - c;
2582 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002583#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002584 pa = abs(p);
2585 pb = abs(pc);
2586 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002587#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002588 pa = p < 0 ? -p : p;
2589 pb = pc < 0 ? -pc : pc;
2590 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002591#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002592 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2593#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002594 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002595 pa = abs(p - a);
2596 pb = abs(p - b);
2597 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002598 if (pa <= pb && pa <= pc)
2599 p = a;
2600 else if (pb <= pc)
2601 p = b;
2602 else
2603 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002604#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002605
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002606 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002607
2608 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002609
2610 if (sum > lmins) /* We are already worse, don't continue. */
2611 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002612 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002613
2614#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2615 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2616 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002617 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002618 png_uint_32 sumhi, sumlo;
2619 sumlo = sum & PNG_LOMASK;
2620 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2621
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002622 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002623 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002624 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002625 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002626 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002627 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002628 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002629 PNG_WEIGHT_SHIFT;
2630 }
2631 }
2632
2633 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2634 PNG_COST_SHIFT;
2635 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2636 PNG_COST_SHIFT;
2637
2638 if (sumhi > PNG_HIMASK)
2639 sum = PNG_MAXSUM;
2640 else
2641 sum = (sumhi << PNG_HISHIFT) + sumlo;
2642 }
2643#endif
2644
Guy Schalnate5a37791996-06-05 15:50:50 -05002645 if (sum < mins)
2646 {
2647 best_row = png_ptr->paeth_row;
2648 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002649 }
2650
Andreas Dilger47a0c421997-05-16 02:46:07 -05002651 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002652
Guy Schalnate5a37791996-06-05 15:50:50 -05002653 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002654
2655#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2656 /* Save the type of filter we picked this time for future calculations */
2657 if (png_ptr->num_prev_filters > 0)
2658 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002659 int j;
2660 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002661 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002662 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002663 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002664 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002665 }
2666#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002667}
2668
2669
Andreas Dilger47a0c421997-05-16 02:46:07 -05002670/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002671void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002672png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2673{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002674 png_debug(1, "in png_write_filtered_row\n");
2675 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002676 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002677
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002678 png_ptr->zstream.next_in = filtered_row;
2679 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002680 /* repeat until we have compressed all the data */
2681 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002682 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002683 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002684
Guy Schalnate5a37791996-06-05 15:50:50 -05002685 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002686 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002687 /* check for compression errors */
2688 if (ret != Z_OK)
2689 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002690 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002691 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002692 else
2693 png_error(png_ptr, "zlib error");
2694 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002695
Guy Schalnate5a37791996-06-05 15:50:50 -05002696 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002697 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002698 {
2699 /* write the IDAT and reset the zlib output buffer */
2700 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002701 png_ptr->zstream.next_out = png_ptr->zbuf;
2702 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002703 }
2704 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002705 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002706
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002707 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002708 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002709 {
2710 png_bytep tptr;
2711
2712 tptr = png_ptr->prev_row;
2713 png_ptr->prev_row = png_ptr->row_buf;
2714 png_ptr->row_buf = tptr;
2715 }
2716
Guy Schalnate5a37791996-06-05 15:50:50 -05002717 /* finish row - updates counters and flushes zlib if last row */
2718 png_write_finish_row(png_ptr);
2719
2720#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2721 png_ptr->flush_rows++;
2722
2723 if (png_ptr->flush_dist > 0 &&
2724 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002725 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002726 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002727 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002728#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002729}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05002730#endif /* PNG_WRITE_SUPPORTED */