blob: e0e8c63a723db4509e5ca2e9412aadc00b1b23d5 [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-Pehrsonddfebd32006-02-22 09:19:25 -06004 * libpng version 1.2.9beta2 - February 22, 2006
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06006 * Copyright (c) 1998-2006 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-Pehrson9c3ab682006-02-20 22:09:05 -060019void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -060020png_save_uint_32(png_bytep buf, png_uint_32 i)
Guy Schalnat0d580581995-07-20 02:43:20 -050021{
22 buf[0] = (png_byte)((i >> 24) & 0xff);
23 buf[1] = (png_byte)((i >> 16) & 0xff);
24 buf[2] = (png_byte)((i >> 8) & 0xff);
25 buf[3] = (png_byte)(i & 0xff);
26}
27
Andreas Dilger47a0c421997-05-16 02:46:07 -050028/* The png_save_int_32 function assumes integers are stored in two's
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060029 * complement format. If this isn't the case, then this routine needs to
30 * be modified to write data in two's complement format.
31 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060032void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050033png_save_int_32(png_bytep buf, png_int_32 i)
34{
35 buf[0] = (png_byte)((i >> 24) & 0xff);
36 buf[1] = (png_byte)((i >> 16) & 0xff);
37 buf[2] = (png_byte)((i >> 8) & 0xff);
38 buf[3] = (png_byte)(i & 0xff);
39}
Andreas Dilger47a0c421997-05-16 02:46:07 -050040
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060041/* Place a 16-bit number into a buffer in PNG byte order.
42 * The parameter is declared unsigned int, not png_uint_16,
43 * just to avoid potential problems on pre-ANSI C compilers.
44 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060045void PNGAPI
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060046png_save_uint_16(png_bytep buf, unsigned int i)
Guy Schalnat0d580581995-07-20 02:43:20 -050047{
48 buf[0] = (png_byte)((i >> 8) & 0xff);
49 buf[1] = (png_byte)(i & 0xff);
50}
51
Andreas Dilger47a0c421997-05-16 02:46:07 -050052/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060053 * representing the chunk name. The array must be at least 4 bytes in
54 * length, and does not need to be null terminated. To be safe, pass the
55 * pre-defined chunk names here, and if you need a new one, define it
56 * where the others are defined. The length is the length of the data.
57 * All the data must be present. If that is not possible, use the
58 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
59 * functions instead.
60 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050061void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060062png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050063 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050064{
Andreas Dilger47a0c421997-05-16 02:46:07 -050065 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060066 png_write_chunk_data(png_ptr, data, length);
67 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050068}
69
Andreas Dilger47a0c421997-05-16 02:46:07 -050070/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060071 * The total_length is the sum of the lengths of all the data you will be
72 * passing in png_write_chunk_data().
73 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050074void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060075png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
76 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -050077{
Andreas Dilger47a0c421997-05-16 02:46:07 -050078 png_byte buf[4];
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -050079 png_debug2(0, "Writing %s chunk (%lu bytes)\n", chunk_name, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -050080
Guy Schalnat0d580581995-07-20 02:43:20 -050081 /* write the length */
Andreas Dilger47a0c421997-05-16 02:46:07 -050082 png_save_uint_32(buf, length);
83 png_write_data(png_ptr, buf, (png_size_t)4);
84
Guy Schalnat0d580581995-07-20 02:43:20 -050085 /* write the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -050086 png_write_data(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050087 /* reset the crc and run it over the chunk name */
88 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -050089 png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050090}
91
Andreas Dilger47a0c421997-05-16 02:46:07 -050092/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060093 * Note that multiple calls to this function are allowed, and that the
94 * sum of the lengths from these calls *must* add up to the total_length
95 * given to png_write_chunk_start().
96 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050097void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050098png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050099{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500100 /* write the data, and run the CRC over it */
101 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500102 {
103 png_calculate_crc(png_ptr, data, length);
Guy Schalnat6d764711995-12-19 03:22:19 -0600104 png_write_data(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500105 }
106}
107
Andreas Dilger47a0c421997-05-16 02:46:07 -0500108/* Finish a chunk started with png_write_chunk_start(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500109void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600110png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500111{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500112 png_byte buf[4];
113
Guy Schalnat0d580581995-07-20 02:43:20 -0500114 /* write the crc */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500115 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500116
117 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500118}
119
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600120/* Simple function to write the signature. If we have already written
121 * the magic bytes of the signature, or more likely, the PNG stream is
122 * being embedded into another stream and doesn't need its own signature,
123 * we should call png_set_sig_bytes() to tell libpng how many of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600124 * bytes have already been written.
125 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500126void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600127png_write_sig(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500128{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600129 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600130 /* write the rest of the 8 byte signature */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600131 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
Andreas Dilger47a0c421997-05-16 02:46:07 -0500132 (png_size_t)8 - png_ptr->sig_bytes);
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600133 if(png_ptr->sig_bytes < 3)
134 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500135}
136
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600137#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600138/*
139 * This pair of functions encapsulates the operation of (a) compressing a
140 * text string, and (b) issuing it later as a series of chunk data writes.
141 * The compression_state structure is shared context for these functions
142 * set up by the caller in order to make the whole mess thread-safe.
143 */
144
145typedef struct
146{
147 char *input; /* the uncompressed input data */
148 int input_len; /* its length */
149 int num_output_ptr; /* number of output pointers used */
150 int max_output_ptr; /* size of output_ptr */
151 png_charpp output_ptr; /* array of pointers to output */
152} compression_state;
153
154/* compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500155static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600156png_text_compress(png_structp png_ptr,
157 png_charp text, png_size_t text_len, int compression,
158 compression_state *comp)
159{
160 int ret;
161
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600162 comp->num_output_ptr = 0;
163 comp->max_output_ptr = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600164 comp->output_ptr = NULL;
165 comp->input = NULL;
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600166 comp->input_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600167
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");
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600733
734 comp.num_output_ptr = 0;
735 comp.max_output_ptr = 0;
736 comp.output_ptr = NULL;
737 comp.input = NULL;
738 comp.input_len = 0;
739
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600740 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
741 &new_name)) == 0)
742 {
743 png_warning(png_ptr, "Empty keyword in iCCP chunk");
744 return;
745 }
746
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600747 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600748 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600749
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500750 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600751 profile_len = 0;
752
753 if (profile_len)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500754 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600755 PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600756
757 /* make sure we include the NULL after the name and the compression type */
758 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
759 (png_uint_32)name_len+profile_len+2);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600760 new_name[name_len+1]=0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600761 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
762
763 if (profile_len)
764 png_write_compressed_data_out(png_ptr, &comp);
765
766 png_write_chunk_end(png_ptr);
767 png_free(png_ptr, new_name);
768}
769#endif
770
771#if defined(PNG_WRITE_sPLT_SUPPORTED)
772/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500773void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600774png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600775{
776#ifdef PNG_USE_LOCAL_ARRAYS
777 PNG_sPLT;
778#endif
779 png_size_t name_len;
780 png_charp new_name;
781 png_byte entrybuf[10];
782 int entry_size = (spalette->depth == 8 ? 6 : 10);
783 int palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600784 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500785#ifdef PNG_NO_POINTER_INDEXING
786 int i;
787#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600788
789 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600790 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
791 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600792 {
793 png_warning(png_ptr, "Empty keyword in sPLT chunk");
794 return;
795 }
796
797 /* make sure we include the NULL after the name */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500798 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600799 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600800 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600801 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600802
803 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500804#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600805 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
806 {
807 if (spalette->depth == 8)
808 {
809 entrybuf[0] = (png_byte)ep->red;
810 entrybuf[1] = (png_byte)ep->green;
811 entrybuf[2] = (png_byte)ep->blue;
812 entrybuf[3] = (png_byte)ep->alpha;
813 png_save_uint_16(entrybuf + 4, ep->frequency);
814 }
815 else
816 {
817 png_save_uint_16(entrybuf + 0, ep->red);
818 png_save_uint_16(entrybuf + 2, ep->green);
819 png_save_uint_16(entrybuf + 4, ep->blue);
820 png_save_uint_16(entrybuf + 6, ep->alpha);
821 png_save_uint_16(entrybuf + 8, ep->frequency);
822 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500823 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600824 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500825#else
826 ep=spalette->entries;
827 for (i=0; i>spalette->nentries; i++)
828 {
829 if (spalette->depth == 8)
830 {
831 entrybuf[0] = (png_byte)ep[i].red;
832 entrybuf[1] = (png_byte)ep[i].green;
833 entrybuf[2] = (png_byte)ep[i].blue;
834 entrybuf[3] = (png_byte)ep[i].alpha;
835 png_save_uint_16(entrybuf + 4, ep[i].frequency);
836 }
837 else
838 {
839 png_save_uint_16(entrybuf + 0, ep[i].red);
840 png_save_uint_16(entrybuf + 2, ep[i].green);
841 png_save_uint_16(entrybuf + 4, ep[i].blue);
842 png_save_uint_16(entrybuf + 6, ep[i].alpha);
843 png_save_uint_16(entrybuf + 8, ep[i].frequency);
844 }
845 png_write_chunk_data(png_ptr, entrybuf, entry_size);
846 }
847#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600848
849 png_write_chunk_end(png_ptr);
850 png_free(png_ptr, new_name);
851}
852#endif
853
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500854#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500855/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500856void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600857png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500858{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600859#ifdef PNG_USE_LOCAL_ARRAYS
860 PNG_sBIT;
861#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500862 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500863 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500864
Andreas Dilger47a0c421997-05-16 02:46:07 -0500865 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600866 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500867 if (color_type & PNG_COLOR_MASK_COLOR)
868 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600869 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500870
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500871 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
872 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600873 if (sbit->red == 0 || sbit->red > maxbits ||
874 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500875 sbit->blue == 0 || sbit->blue > maxbits)
876 {
877 png_warning(png_ptr, "Invalid sBIT depth specified");
878 return;
879 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500880 buf[0] = sbit->red;
881 buf[1] = sbit->green;
882 buf[2] = sbit->blue;
883 size = 3;
884 }
885 else
886 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500887 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
888 {
889 png_warning(png_ptr, "Invalid sBIT depth specified");
890 return;
891 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500892 buf[0] = sbit->gray;
893 size = 1;
894 }
895
896 if (color_type & PNG_COLOR_MASK_ALPHA)
897 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500898 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
899 {
900 png_warning(png_ptr, "Invalid sBIT depth specified");
901 return;
902 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500903 buf[size++] = sbit->alpha;
904 }
905
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600906 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500907}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500908#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500909
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500910#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500911/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600912#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500913void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500914png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600915 double red_x, double red_y, double green_x, double green_y,
916 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500917{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600918#ifdef PNG_USE_LOCAL_ARRAYS
919 PNG_cHRM;
920#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500921 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600922 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500923
Andreas Dilger47a0c421997-05-16 02:46:07 -0500924 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600925 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500926 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
927 white_x + white_y > 1.0)
928 {
929 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500930#if !defined(PNG_NO_CONSOLE_IO)
931 fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600932#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500933 return;
934 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600935 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500936 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600937 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500938 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500939
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -0600940 if (red_x < 0 || red_y < 0 || red_x + red_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -0500941 {
942 png_warning(png_ptr, "Invalid cHRM red point specified");
943 return;
944 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600945 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500946 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600947 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500948 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500949
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -0600950 if (green_x < 0 || green_y < 0 || green_x + green_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -0500951 {
952 png_warning(png_ptr, "Invalid cHRM green point specified");
953 return;
954 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600955 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500956 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600957 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500958 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500959
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -0600960 if (blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -0500961 {
962 png_warning(png_ptr, "Invalid cHRM blue point specified");
963 return;
964 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600965 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500966 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600967 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500968 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500969
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600970 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500971}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500972#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600973#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500974void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600975png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
976 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
977 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
978 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600979{
980#ifdef PNG_USE_LOCAL_ARRAYS
981 PNG_cHRM;
982#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600983 png_byte buf[32];
984
985 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600986 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600987 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
988 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600989 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500990#if !defined(PNG_NO_CONSOLE_IO)
991 fprintf(stderr,"white_x=%ld, white_y=%ld\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600992#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600993 return;
994 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500995 png_save_uint_32(buf, (png_uint_32)white_x);
996 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600997
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -0600998 if (red_x + red_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600999 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001000 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001001 return;
1002 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001003 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1004 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001005
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001006 if (green_x + green_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001007 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001008 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001009 return;
1010 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001011 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1012 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001013
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001014 if (blue_x + blue_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001015 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001016 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001017 return;
1018 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001019 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1020 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001021
1022 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
1023}
1024#endif
1025#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001026
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001027#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001028/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001029void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001030png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -05001031 int num_trans, int color_type)
1032{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001033#ifdef PNG_USE_LOCAL_ARRAYS
1034 PNG_tRNS;
1035#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001036 png_byte buf[6];
1037
Andreas Dilger47a0c421997-05-16 02:46:07 -05001038 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001039 if (color_type == PNG_COLOR_TYPE_PALETTE)
1040 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001041 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001042 {
1043 png_warning(png_ptr,"Invalid number of transparent colors specified");
1044 return;
1045 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001046 /* write the chunk out as it is */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001047 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001048 }
1049 else if (color_type == PNG_COLOR_TYPE_GRAY)
1050 {
1051 /* one 16 bit value */
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001052 if(tran->gray >= (1 << png_ptr->bit_depth))
1053 {
1054 png_warning(png_ptr,
1055 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1056 return;
1057 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001058 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001059 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001060 }
1061 else if (color_type == PNG_COLOR_TYPE_RGB)
1062 {
1063 /* three 16 bit values */
1064 png_save_uint_16(buf, tran->red);
1065 png_save_uint_16(buf + 2, tran->green);
1066 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001067 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1068 {
1069 png_warning(png_ptr,
1070 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1071 return;
1072 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001073 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001074 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001075 else
1076 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001077 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001078 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001079}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001080#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001081
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001082#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001083/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001084void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001085png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001086{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001087#ifdef PNG_USE_LOCAL_ARRAYS
1088 PNG_bKGD;
1089#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001090 png_byte buf[6];
1091
Andreas Dilger47a0c421997-05-16 02:46:07 -05001092 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001093 if (color_type == PNG_COLOR_TYPE_PALETTE)
1094 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001095 if (
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001096#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001097 (png_ptr->num_palette ||
1098 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001099#endif
1100 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001101 {
1102 png_warning(png_ptr, "Invalid background palette index");
1103 return;
1104 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001105 buf[0] = back->index;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001106 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001107 }
1108 else if (color_type & PNG_COLOR_MASK_COLOR)
1109 {
1110 png_save_uint_16(buf, back->red);
1111 png_save_uint_16(buf + 2, back->green);
1112 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001113 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1114 {
1115 png_warning(png_ptr,
1116 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1117 return;
1118 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001119 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001120 }
1121 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001122 {
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001123 if(back->gray >= (1 << png_ptr->bit_depth))
1124 {
1125 png_warning(png_ptr,
1126 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1127 return;
1128 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001129 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001130 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001131 }
1132}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001133#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001134
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001135#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001136/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001137void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001138png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001139{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001140#ifdef PNG_USE_LOCAL_ARRAYS
1141 PNG_hIST;
1142#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001143 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001144 png_byte buf[3];
1145
Andreas Dilger47a0c421997-05-16 02:46:07 -05001146 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001147 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001148 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001149 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1150 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001151 png_warning(png_ptr, "Invalid number of histogram entries specified");
1152 return;
1153 }
1154
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001155 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001156 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001157 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001158 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001159 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001160 }
1161 png_write_chunk_end(png_ptr);
1162}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001163#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001164
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001165#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1166 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001167/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1168 * and if invalid, correct the keyword rather than discarding the entire
1169 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1170 * length, forbids leading or trailing whitespace, multiple internal spaces,
1171 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001172 *
1173 * The new_key is allocated to hold the corrected keyword and must be freed
1174 * by the calling routine. This avoids problems with trying to write to
1175 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001176 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001177png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001178png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001179{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001180 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001181 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001182 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001183 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001184
Andreas Dilger47a0c421997-05-16 02:46:07 -05001185 png_debug(1, "in png_check_keyword\n");
1186 *new_key = NULL;
1187
1188 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001189 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001190 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001191 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001192 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001193
Andreas Dilger47a0c421997-05-16 02:46:07 -05001194 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1195
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001196 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
1197 if (*new_key == NULL)
1198 {
1199 png_warning(png_ptr, "Out of memory while procesing keyword");
1200 return ((png_size_t)0);
1201 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001202
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001203 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001204 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001205 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001206 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001207 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001208#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001209 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001210
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001211 sprintf(msg, "invalid keyword character 0x%02X", *kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001212 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001213#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001214 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001215#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001216 *dp = ' ';
1217 }
1218 else
1219 {
1220 *dp = *kp;
1221 }
1222 }
1223 *dp = '\0';
1224
1225 /* Remove any trailing white space. */
1226 kp = *new_key + key_len - 1;
1227 if (*kp == ' ')
1228 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001229 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001230
1231 while (*kp == ' ')
1232 {
1233 *(kp--) = '\0';
1234 key_len--;
1235 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001236 }
1237
1238 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001239 kp = *new_key;
1240 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001241 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001242 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001243
1244 while (*kp == ' ')
1245 {
1246 kp++;
1247 key_len--;
1248 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001249 }
1250
Andreas Dilger47a0c421997-05-16 02:46:07 -05001251 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001252
Andreas Dilger47a0c421997-05-16 02:46:07 -05001253 /* Remove multiple internal spaces. */
1254 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001255 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001256 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001257 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001258 *(dp++) = *kp;
1259 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001260 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001261 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001262 {
1263 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001264 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001265 }
1266 else
1267 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001268 *(dp++) = *kp;
1269 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001270 }
1271 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001272 *dp = '\0';
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001273 if(kwarn)
1274 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001275
1276 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001277 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001278 png_free(png_ptr, *new_key);
1279 *new_key=NULL;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001280 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001281 }
1282
1283 if (key_len > 79)
1284 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001285 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001286 new_key[79] = '\0';
1287 key_len = 79;
1288 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001289
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001290 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001291}
1292#endif
1293
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001294#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001295/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001296void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001297png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001298 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001299{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001300#ifdef PNG_USE_LOCAL_ARRAYS
1301 PNG_tEXt;
1302#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001303 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001304 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001305
Andreas Dilger47a0c421997-05-16 02:46:07 -05001306 png_debug(1, "in png_write_tEXt\n");
1307 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1308 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001309 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001310 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001311 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001312
Andreas Dilger47a0c421997-05-16 02:46:07 -05001313 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001314 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001315 else
1316 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001317
1318 /* make sure we include the 0 after the key */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001319 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 -05001320 /*
1321 * We leave it to the application to meet PNG-1.0 requirements on the
1322 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1323 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001324 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001325 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001326 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001327 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001328 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001329
Guy Schalnat0d580581995-07-20 02:43:20 -05001330 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001331 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001332}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001333#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001334
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001335#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001336/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001337void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001338png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001339 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001340{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001341#ifdef PNG_USE_LOCAL_ARRAYS
1342 PNG_zTXt;
1343#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001344 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001345 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001346 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001347 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001348
Andreas Dilger47a0c421997-05-16 02:46:07 -05001349 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001350
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001351 comp.num_output_ptr = 0;
1352 comp.max_output_ptr = 0;
1353 comp.output_ptr = NULL;
1354 comp.input = NULL;
1355 comp.input_len = 0;
1356
Andreas Dilger47a0c421997-05-16 02:46:07 -05001357 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001358 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001359 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001360 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001361 }
1362
Andreas Dilger47a0c421997-05-16 02:46:07 -05001363 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1364 {
1365 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1366 png_free(png_ptr, new_key);
1367 return;
1368 }
1369
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001370 text_len = png_strlen(text);
1371
Andreas Dilger47a0c421997-05-16 02:46:07 -05001372 png_free(png_ptr, new_key);
1373
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001374 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001375 text_len = png_text_compress(png_ptr, text, text_len, compression,
1376 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001377
1378 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001379 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1380 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001381 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001382 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001383 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001384 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001385 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001386 /* write the compressed data */
1387 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001388
Guy Schalnat0d580581995-07-20 02:43:20 -05001389 /* close the chunk */
1390 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001391}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001392#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001393
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001394#if defined(PNG_WRITE_iTXt_SUPPORTED)
1395/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001396void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001397png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001398 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001399{
1400#ifdef PNG_USE_LOCAL_ARRAYS
1401 PNG_iTXt;
1402#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001403 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001404 png_charp new_lang, new_key;
1405 png_byte cbuf[2];
1406 compression_state comp;
1407
1408 png_debug(1, "in png_write_iTXt\n");
1409
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001410 comp.num_output_ptr = 0;
1411 comp.max_output_ptr = 0;
1412 comp.output_ptr = NULL;
1413 comp.input = NULL;
1414
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001415 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1416 {
1417 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1418 return;
1419 }
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001420 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001421 {
1422 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001423 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001424 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001425 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001426
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001427 if (lang_key == NULL)
1428 lang_key_len = 0;
1429 else
1430 lang_key_len = png_strlen(lang_key);
1431
1432 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001433 text_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001434 else
1435 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001436
1437 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001438 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1439 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001440
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001441
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001442 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001443 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001444
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001445 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1446 (png_uint_32)(
1447 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1448 + key_len
1449 + lang_len
1450 + lang_key_len
1451 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001452
1453 /*
1454 * We leave it to the application to meet PNG-1.0 requirements on the
1455 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1456 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1457 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1458 */
1459 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001460
1461 /* set the compression flag */
1462 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1463 compression == PNG_TEXT_COMPRESSION_NONE)
1464 cbuf[0] = 0;
1465 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1466 cbuf[0] = 1;
1467 /* set the compression method */
1468 cbuf[1] = 0;
1469 png_write_chunk_data(png_ptr, cbuf, 2);
1470
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001471 cbuf[0] = 0;
1472 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf), lang_len + 1);
1473 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 -06001474 png_write_compressed_data_out(png_ptr, &comp);
1475
1476 png_write_chunk_end(png_ptr);
1477 png_free(png_ptr, new_key);
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001478 if (new_lang)
1479 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001480}
1481#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001482
1483#if defined(PNG_WRITE_oFFs_SUPPORTED)
1484/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001485void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001486png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001487 int unit_type)
1488{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001489#ifdef PNG_USE_LOCAL_ARRAYS
1490 PNG_oFFs;
1491#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001492 png_byte buf[9];
1493
1494 png_debug(1, "in png_write_oFFs\n");
1495 if (unit_type >= PNG_OFFSET_LAST)
1496 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1497
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001498 png_save_int_32(buf, x_offset);
1499 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001500 buf[8] = (png_byte)unit_type;
1501
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001502 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001503}
1504#endif
1505
1506#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001507/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001508void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001509png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1510 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1511{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001512#ifdef PNG_USE_LOCAL_ARRAYS
1513 PNG_pCAL;
1514#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001515 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001516 png_uint_32p params_len;
1517 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001518 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001519 int i;
1520
1521 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1522 if (type >= PNG_EQUATION_LAST)
1523 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1524
1525 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001526 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001527 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001528 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001529 total_len = purpose_len + units_len + 10;
1530
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001531 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001532 *png_sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001533
1534 /* Find the length of each parameter, making sure we don't count the
1535 null terminator for the last parameter. */
1536 for (i = 0; i < nparams; i++)
1537 {
1538 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001539 png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001540 total_len += (png_size_t)params_len[i];
1541 }
1542
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001543 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001544 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001545 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001546 png_save_int_32(buf, X0);
1547 png_save_int_32(buf + 4, X1);
1548 buf[8] = (png_byte)type;
1549 buf[9] = (png_byte)nparams;
1550 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1551 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1552
1553 png_free(png_ptr, new_purpose);
1554
1555 for (i = 0; i < nparams; i++)
1556 {
1557 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1558 (png_size_t)params_len[i]);
1559 }
1560
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001561 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001562 png_write_chunk_end(png_ptr);
1563}
1564#endif
1565
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001566#if defined(PNG_WRITE_sCAL_SUPPORTED)
1567/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001568#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001569void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001570png_write_sCAL(png_structp png_ptr, int unit, double width,double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001571{
1572#ifdef PNG_USE_LOCAL_ARRAYS
1573 PNG_sCAL;
1574#endif
1575 png_size_t total_len;
1576 char wbuf[32], hbuf[32];
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001577 png_byte bunit = (png_byte)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001578
1579 png_debug(1, "in png_write_sCAL\n");
1580
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001581#if defined(_WIN32_WCE)
1582/* sprintf() function is not supported on WindowsCE */
1583 {
1584 wchar_t wc_buf[32];
1585 swprintf(wc_buf, TEXT("%12.12e"), width);
1586 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, wbuf, 32, NULL, NULL);
1587 swprintf(wc_buf, TEXT("%12.12e"), height);
1588 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, hbuf, 32, NULL, NULL);
1589 }
1590#else
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001591 sprintf(wbuf, "%12.12e", width);
1592 sprintf(hbuf, "%12.12e", height);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001593#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001594 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001595
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001596 png_debug1(3, "sCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001597 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001598 png_write_chunk_data(png_ptr, (png_bytep)&bunit, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001599 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1600 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001601
1602 png_write_chunk_end(png_ptr);
1603}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001604#else
1605#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001606void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001607png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001608 png_charp height)
1609{
1610#ifdef PNG_USE_LOCAL_ARRAYS
1611 PNG_sCAL;
1612#endif
1613 png_size_t total_len;
1614 char wbuf[32], hbuf[32];
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001615 png_byte bunit = unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001616
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001617 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001618
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001619 png_strcpy(wbuf,(const char *)width);
1620 png_strcpy(hbuf,(const char *)height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001621 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001622
1623 png_debug1(3, "sCAL total length = %d\n", total_len);
1624 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001625 png_write_chunk_data(png_ptr, (png_bytep)&bunit, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001626 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1627 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001628
1629 png_write_chunk_end(png_ptr);
1630}
1631#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001632#endif
1633#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001634
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001635#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001636/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001637void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001638png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001639 png_uint_32 y_pixels_per_unit,
1640 int unit_type)
1641{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001642#ifdef PNG_USE_LOCAL_ARRAYS
1643 PNG_pHYs;
1644#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001645 png_byte buf[9];
1646
Andreas Dilger47a0c421997-05-16 02:46:07 -05001647 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001648 if (unit_type >= PNG_RESOLUTION_LAST)
1649 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1650
Guy Schalnat0d580581995-07-20 02:43:20 -05001651 png_save_uint_32(buf, x_pixels_per_unit);
1652 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001653 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001654
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001655 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001656}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001657#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001658
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001659#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001660/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1661 * or png_convert_from_time_t(), or fill in the structure yourself.
1662 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001663void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001664png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001665{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001666#ifdef PNG_USE_LOCAL_ARRAYS
1667 PNG_tIME;
1668#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001669 png_byte buf[7];
1670
Andreas Dilger47a0c421997-05-16 02:46:07 -05001671 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001672 if (mod_time->month > 12 || mod_time->month < 1 ||
1673 mod_time->day > 31 || mod_time->day < 1 ||
1674 mod_time->hour > 23 || mod_time->second > 60)
1675 {
1676 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1677 return;
1678 }
1679
Guy Schalnat0d580581995-07-20 02:43:20 -05001680 png_save_uint_16(buf, mod_time->year);
1681 buf[2] = mod_time->month;
1682 buf[3] = mod_time->day;
1683 buf[4] = mod_time->hour;
1684 buf[5] = mod_time->minute;
1685 buf[6] = mod_time->second;
1686
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001687 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001688}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001689#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001690
1691/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001692void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001693png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001694{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001695#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001696 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001697
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001698 /* start of interlace block */
1699 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001700
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001701 /* offset to next interlace block */
1702 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001703
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001704 /* start of interlace block in the y direction */
1705 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001706
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001707 /* offset to next interlace block in the y direction */
1708 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001709#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001710
Andreas Dilger47a0c421997-05-16 02:46:07 -05001711 png_size_t buf_size;
1712
1713 png_debug(1, "in png_write_start_row\n");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001714 buf_size = (png_size_t)(PNG_ROWBYTES(
1715 png_ptr->usr_channels*png_ptr->usr_bit_depth,png_ptr->width)+1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001716
Guy Schalnat0d580581995-07-20 02:43:20 -05001717 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001718 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001719 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001720
1721 /* set up filtering buffer, if using this filter */
1722 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001723 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001724 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001725 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001726 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001727 }
1728
Andreas Dilger47a0c421997-05-16 02:46:07 -05001729 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001730 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1731 {
1732 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001733 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001734 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001735
1736 if (png_ptr->do_filter & PNG_FILTER_UP)
1737 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001738 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001739 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001740 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001741 }
1742
1743 if (png_ptr->do_filter & PNG_FILTER_AVG)
1744 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001745 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1746 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001747 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001748 }
1749
1750 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1751 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001752 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001753 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001754 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001755 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001756 }
1757
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001758#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001759 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001760 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001761 {
1762 if (!(png_ptr->transformations & PNG_INTERLACE))
1763 {
1764 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1765 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001766 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1767 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001768 }
1769 else
1770 {
1771 png_ptr->num_rows = png_ptr->height;
1772 png_ptr->usr_width = png_ptr->width;
1773 }
1774 }
1775 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001776#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001777 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001778 png_ptr->num_rows = png_ptr->height;
1779 png_ptr->usr_width = png_ptr->width;
1780 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001781 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1782 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001783}
1784
Andreas Dilger47a0c421997-05-16 02:46:07 -05001785/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001786void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001787png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001788{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001789#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001790 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001791
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001792 /* start of interlace block */
1793 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001794
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001795 /* offset to next interlace block */
1796 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001797
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001798 /* start of interlace block in the y direction */
1799 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001800
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001801 /* offset to next interlace block in the y direction */
1802 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001803#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001804
Guy Schalnat0d580581995-07-20 02:43:20 -05001805 int ret;
1806
Andreas Dilger47a0c421997-05-16 02:46:07 -05001807 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001808 /* next row */
1809 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001810
Guy Schalnat0d580581995-07-20 02:43:20 -05001811 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001812 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001813 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001814
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001815#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001816 /* if interlaced, go to next pass */
1817 if (png_ptr->interlaced)
1818 {
1819 png_ptr->row_number = 0;
1820 if (png_ptr->transformations & PNG_INTERLACE)
1821 {
1822 png_ptr->pass++;
1823 }
1824 else
1825 {
1826 /* loop until we find a non-zero width or height pass */
1827 do
1828 {
1829 png_ptr->pass++;
1830 if (png_ptr->pass >= 7)
1831 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001832 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001833 png_pass_inc[png_ptr->pass] - 1 -
1834 png_pass_start[png_ptr->pass]) /
1835 png_pass_inc[png_ptr->pass];
1836 png_ptr->num_rows = (png_ptr->height +
1837 png_pass_yinc[png_ptr->pass] - 1 -
1838 png_pass_ystart[png_ptr->pass]) /
1839 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001840 if (png_ptr->transformations & PNG_INTERLACE)
1841 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001842 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1843
1844 }
1845
Guy Schalnate5a37791996-06-05 15:50:50 -05001846 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001847 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001848 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001849 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001850 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001851 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
1852 png_ptr->usr_bit_depth,png_ptr->width))+1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001853 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001854 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001855 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001856#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001857
1858 /* if we get here, we've just written the last row, so we need
1859 to flush the compressor */
1860 do
1861 {
1862 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001863 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001864 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001865 if (ret == Z_OK)
1866 {
1867 /* check to see if we need more room */
1868 if (!(png_ptr->zstream.avail_out))
1869 {
1870 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1871 png_ptr->zstream.next_out = png_ptr->zbuf;
1872 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1873 }
1874 }
1875 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001876 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001877 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001878 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001879 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001880 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001881 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001882 } while (ret != Z_STREAM_END);
1883
1884 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001885 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001886 {
1887 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001888 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001889 }
1890
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001891 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -06001892 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat0d580581995-07-20 02:43:20 -05001893}
1894
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001895#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001896/* Pick out the correct pixels for the interlace pass.
1897 * The basic idea here is to go through the row with a source
1898 * pointer and a destination pointer (sp and dp), and copy the
1899 * correct pixels for the pass. As the row gets compacted,
1900 * sp will always be >= dp, so we should never overwrite anything.
1901 * See the default: case for the easiest code to understand.
1902 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001903void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001904png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001905{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001906#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001907 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001908
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001909 /* start of interlace block */
1910 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001911
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001912 /* offset to next interlace block */
1913 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001914#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001915
Andreas Dilger47a0c421997-05-16 02:46:07 -05001916 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001917 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001918#if defined(PNG_USELESS_TESTS_SUPPORTED)
1919 if (row != NULL && row_info != NULL && pass < 6)
1920#else
1921 if (pass < 6)
1922#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001923 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001924 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001925 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001926 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001927 case 1:
1928 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001929 png_bytep sp;
1930 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001931 int shift;
1932 int d;
1933 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001934 png_uint_32 i;
1935 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001936
1937 dp = row;
1938 d = 0;
1939 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001940 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001941 i += png_pass_inc[pass])
1942 {
1943 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001944 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001945 d |= (value << shift);
1946
1947 if (shift == 0)
1948 {
1949 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001950 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001951 d = 0;
1952 }
1953 else
1954 shift--;
1955
1956 }
1957 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001958 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001959 break;
1960 }
1961 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001962 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001963 png_bytep sp;
1964 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001965 int shift;
1966 int d;
1967 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001968 png_uint_32 i;
1969 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001970
1971 dp = row;
1972 shift = 6;
1973 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001974 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001975 i += png_pass_inc[pass])
1976 {
1977 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001978 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05001979 d |= (value << shift);
1980
1981 if (shift == 0)
1982 {
1983 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001984 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001985 d = 0;
1986 }
1987 else
1988 shift -= 2;
1989 }
1990 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001991 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001992 break;
1993 }
1994 case 4:
1995 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001996 png_bytep sp;
1997 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001998 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05001999 int d;
2000 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002001 png_uint_32 i;
2002 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002003
2004 dp = row;
2005 shift = 4;
2006 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002007 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002008 i += png_pass_inc[pass])
2009 {
2010 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002011 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05002012 d |= (value << shift);
2013
2014 if (shift == 0)
2015 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002016 shift = 4;
2017 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002018 d = 0;
2019 }
2020 else
2021 shift -= 4;
2022 }
2023 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002024 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002025 break;
2026 }
2027 default:
2028 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002029 png_bytep sp;
2030 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002031 png_uint_32 i;
2032 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002033 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002034
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002035 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002036 dp = row;
2037 /* find out how many bytes each pixel takes up */
2038 pixel_bytes = (row_info->pixel_depth >> 3);
2039 /* loop through the row, only looking at the pixels that
2040 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002041 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002042 i += png_pass_inc[pass])
2043 {
2044 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002045 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002046 /* move the pixel */
2047 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002048 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05002049 /* next pixel */
2050 dp += pixel_bytes;
2051 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002052 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002053 }
2054 }
2055 /* set new row width */
2056 row_info->width = (row_info->width +
2057 png_pass_inc[pass] - 1 -
2058 png_pass_start[pass]) /
2059 png_pass_inc[pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002060 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2061 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002062 }
2063}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002064#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002065
Andreas Dilger47a0c421997-05-16 02:46:07 -05002066/* This filters the row, chooses which filter to use, if it has not already
2067 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002068 * chosen filter.
2069 */
Glenn Randers-Pehrson78067772004-11-02 21:49:39 -06002070#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002071#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002072#define PNG_LOMASK ((png_uint_32)0xffffL)
2073#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002074void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002075png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002076{
Guy Schalnate5a37791996-06-05 15:50:50 -05002077 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002078 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002079 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002080 png_uint_32 row_bytes = row_info->rowbytes;
2081#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2082 int num_p_filters = (int)png_ptr->num_prev_filters;
2083#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002084
Andreas Dilger47a0c421997-05-16 02:46:07 -05002085 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05002086 /* find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002087 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002088
2089 prev_row = png_ptr->prev_row;
2090 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002091 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002092
Andreas Dilger47a0c421997-05-16 02:46:07 -05002093 /* The prediction method we use is to find which method provides the
2094 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002095 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002096 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002097 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002098 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002099 * predictive" method (not implemented yet), which does test compressions
2100 * of lines using different filter methods, and then chooses the
2101 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002102 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002103 *
2104 * GRR 980525: consider also
2105 * (1) minimum sum of absolute differences from running average (i.e.,
2106 * keep running sum of non-absolute differences & count of bytes)
2107 * [track dispersion, too? restart average if dispersion too large?]
2108 * (1b) minimum sum of absolute differences from sliding average, probably
2109 * with window size <= deflate window (usually 32K)
2110 * (2) minimum sum of squared differences from zero or running average
2111 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002112 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002113
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002114
Guy Schalnate5a37791996-06-05 15:50:50 -05002115 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002116 * that has been chosen, as it doesn't actually do anything to the data.
2117 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002118 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002119 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002120 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002121 png_bytep rp;
2122 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002123 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002124 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002125
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002126 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002127 {
2128 v = *rp;
2129 sum += (v < 128) ? v : 256 - v;
2130 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002131
2132#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2133 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2134 {
2135 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002136 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002137 sumlo = sum & PNG_LOMASK;
2138 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2139
2140 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002141 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002142 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002143 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002144 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002145 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002146 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002147 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002148 PNG_WEIGHT_SHIFT;
2149 }
2150 }
2151
2152 /* Factor in the cost of this filter (this is here for completeness,
2153 * but it makes no sense to have a "cost" for the NONE filter, as
2154 * it has the minimum possible computational cost - none).
2155 */
2156 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2157 PNG_COST_SHIFT;
2158 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2159 PNG_COST_SHIFT;
2160
2161 if (sumhi > PNG_HIMASK)
2162 sum = PNG_MAXSUM;
2163 else
2164 sum = (sumhi << PNG_HISHIFT) + sumlo;
2165 }
2166#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002167 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002168 }
2169
Guy Schalnate5a37791996-06-05 15:50:50 -05002170 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002171 if (filter_to_do == PNG_FILTER_SUB)
2172 /* it's the only filter so no testing is needed */
2173 {
2174 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002175 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002176 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2177 i++, rp++, dp++)
2178 {
2179 *dp = *rp;
2180 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002181 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002182 i++, rp++, lp++, dp++)
2183 {
2184 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2185 }
2186 best_row = png_ptr->sub_row;
2187 }
2188
2189 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002190 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002191 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002192 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002193 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002194 int v;
2195
2196#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002197 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002198 * would reduce the sum of this filter, so that we can do the
2199 * early exit comparison without scaling the sum each time.
2200 */
2201 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2202 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002203 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002204 png_uint_32 lmhi, lmlo;
2205 lmlo = lmins & PNG_LOMASK;
2206 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2207
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002208 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002209 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002210 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002211 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002212 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002213 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002214 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002215 PNG_WEIGHT_SHIFT;
2216 }
2217 }
2218
2219 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2220 PNG_COST_SHIFT;
2221 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2222 PNG_COST_SHIFT;
2223
2224 if (lmhi > PNG_HIMASK)
2225 lmins = PNG_MAXSUM;
2226 else
2227 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2228 }
2229#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002230
Guy Schalnate5a37791996-06-05 15:50:50 -05002231 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2232 i++, rp++, dp++)
2233 {
2234 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002235
Guy Schalnate5a37791996-06-05 15:50:50 -05002236 sum += (v < 128) ? v : 256 - v;
2237 }
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002238 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002239 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002240 {
2241 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002242
Guy Schalnate5a37791996-06-05 15:50:50 -05002243 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002244
2245 if (sum > lmins) /* We are already worse, don't continue. */
2246 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002247 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002248
2249#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2250 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2251 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002252 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002253 png_uint_32 sumhi, sumlo;
2254 sumlo = sum & PNG_LOMASK;
2255 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2256
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002257 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002258 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002259 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002260 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002261 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002262 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002263 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002264 PNG_WEIGHT_SHIFT;
2265 }
2266 }
2267
2268 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2269 PNG_COST_SHIFT;
2270 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2271 PNG_COST_SHIFT;
2272
2273 if (sumhi > PNG_HIMASK)
2274 sum = PNG_MAXSUM;
2275 else
2276 sum = (sumhi << PNG_HISHIFT) + sumlo;
2277 }
2278#endif
2279
Guy Schalnate5a37791996-06-05 15:50:50 -05002280 if (sum < mins)
2281 {
2282 mins = sum;
2283 best_row = png_ptr->sub_row;
2284 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002285 }
2286
Guy Schalnate5a37791996-06-05 15:50:50 -05002287 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002288 if (filter_to_do == PNG_FILTER_UP)
2289 {
2290 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002291 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002292
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002293 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002294 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002295 i++, rp++, pp++, dp++)
2296 {
2297 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2298 }
2299 best_row = png_ptr->up_row;
2300 }
2301
2302 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002303 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002304 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002305 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002306 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002307 int v;
2308
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002309
Andreas Dilger47a0c421997-05-16 02:46:07 -05002310#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2311 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2312 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002313 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002314 png_uint_32 lmhi, lmlo;
2315 lmlo = lmins & PNG_LOMASK;
2316 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2317
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002318 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002319 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002320 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002321 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002322 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002323 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002324 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002325 PNG_WEIGHT_SHIFT;
2326 }
2327 }
2328
2329 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2330 PNG_COST_SHIFT;
2331 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2332 PNG_COST_SHIFT;
2333
2334 if (lmhi > PNG_HIMASK)
2335 lmins = PNG_MAXSUM;
2336 else
2337 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2338 }
2339#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002340
2341 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002342 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002343 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002344 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002345
2346 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002347
2348 if (sum > lmins) /* We are already worse, don't continue. */
2349 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002350 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002351
2352#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2353 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2354 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002355 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002356 png_uint_32 sumhi, sumlo;
2357 sumlo = sum & PNG_LOMASK;
2358 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2359
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002360 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002361 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002362 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002363 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002364 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002365 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002366 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002367 PNG_WEIGHT_SHIFT;
2368 }
2369 }
2370
2371 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2372 PNG_COST_SHIFT;
2373 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2374 PNG_COST_SHIFT;
2375
2376 if (sumhi > PNG_HIMASK)
2377 sum = PNG_MAXSUM;
2378 else
2379 sum = (sumhi << PNG_HISHIFT) + sumlo;
2380 }
2381#endif
2382
Guy Schalnate5a37791996-06-05 15:50:50 -05002383 if (sum < mins)
2384 {
2385 mins = sum;
2386 best_row = png_ptr->up_row;
2387 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002388 }
2389
Guy Schalnate5a37791996-06-05 15:50:50 -05002390 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002391 if (filter_to_do == PNG_FILTER_AVG)
2392 {
2393 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002394 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002395 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002396 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002397 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002398 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002399 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002400 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002401 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002402 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2403 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002404 }
2405 best_row = png_ptr->avg_row;
2406 }
2407
2408 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002409 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002410 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002411 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002412 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002413 int v;
2414
2415#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2416 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2417 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002418 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002419 png_uint_32 lmhi, lmlo;
2420 lmlo = lmins & PNG_LOMASK;
2421 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2422
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002423 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002424 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002425 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002426 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002427 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002428 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002429 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002430 PNG_WEIGHT_SHIFT;
2431 }
2432 }
2433
2434 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2435 PNG_COST_SHIFT;
2436 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2437 PNG_COST_SHIFT;
2438
2439 if (lmhi > PNG_HIMASK)
2440 lmins = PNG_MAXSUM;
2441 else
2442 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2443 }
2444#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002445
2446 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002447 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002448 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002449 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002450
2451 sum += (v < 128) ? v : 256 - v;
2452 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002453 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002454 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002455 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002456 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002457
2458 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002459
2460 if (sum > lmins) /* We are already worse, don't continue. */
2461 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002462 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002463
2464#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2465 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2466 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002467 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002468 png_uint_32 sumhi, sumlo;
2469 sumlo = sum & PNG_LOMASK;
2470 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2471
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002472 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002473 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002474 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002475 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002476 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002477 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002478 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002479 PNG_WEIGHT_SHIFT;
2480 }
2481 }
2482
2483 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2484 PNG_COST_SHIFT;
2485 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2486 PNG_COST_SHIFT;
2487
2488 if (sumhi > PNG_HIMASK)
2489 sum = PNG_MAXSUM;
2490 else
2491 sum = (sumhi << PNG_HISHIFT) + sumlo;
2492 }
2493#endif
2494
Guy Schalnate5a37791996-06-05 15:50:50 -05002495 if (sum < mins)
2496 {
2497 mins = sum;
2498 best_row = png_ptr->avg_row;
2499 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002500 }
2501
Andreas Dilger47a0c421997-05-16 02:46:07 -05002502 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002503 if (filter_to_do == PNG_FILTER_PAETH)
2504 {
2505 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002506 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002507 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002508 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002509 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002510 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002511 }
2512
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002513 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002514 {
2515 int a, b, c, pa, pb, pc, p;
2516
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002517 b = *pp++;
2518 c = *cp++;
2519 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002520
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002521 p = b - c;
2522 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002523
2524#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002525 pa = abs(p);
2526 pb = abs(pc);
2527 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002528#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002529 pa = p < 0 ? -p : p;
2530 pb = pc < 0 ? -pc : pc;
2531 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002532#endif
2533
2534 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2535
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002536 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002537 }
2538 best_row = png_ptr->paeth_row;
2539 }
2540
2541 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002542 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002543 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002544 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002545 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002546 int v;
2547
2548#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2549 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2550 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002551 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002552 png_uint_32 lmhi, lmlo;
2553 lmlo = lmins & PNG_LOMASK;
2554 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2555
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002556 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002557 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002558 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002559 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002560 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002561 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002562 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002563 PNG_WEIGHT_SHIFT;
2564 }
2565 }
2566
2567 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2568 PNG_COST_SHIFT;
2569 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2570 PNG_COST_SHIFT;
2571
2572 if (lmhi > PNG_HIMASK)
2573 lmins = PNG_MAXSUM;
2574 else
2575 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2576 }
2577#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002578
2579 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002580 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002581 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002582 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002583
2584 sum += (v < 128) ? v : 256 - v;
2585 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002586
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002587 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002588 {
2589 int a, b, c, pa, pb, pc, p;
2590
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002591 b = *pp++;
2592 c = *cp++;
2593 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002594
2595#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002596 p = b - c;
2597 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002598#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002599 pa = abs(p);
2600 pb = abs(pc);
2601 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002602#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002603 pa = p < 0 ? -p : p;
2604 pb = pc < 0 ? -pc : pc;
2605 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002606#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002607 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2608#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002609 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002610 pa = abs(p - a);
2611 pb = abs(p - b);
2612 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002613 if (pa <= pb && pa <= pc)
2614 p = a;
2615 else if (pb <= pc)
2616 p = b;
2617 else
2618 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002619#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002620
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002621 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002622
2623 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002624
2625 if (sum > lmins) /* We are already worse, don't continue. */
2626 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002627 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002628
2629#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2630 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2631 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002632 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002633 png_uint_32 sumhi, sumlo;
2634 sumlo = sum & PNG_LOMASK;
2635 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2636
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002637 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002638 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002639 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002640 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002641 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002642 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002643 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002644 PNG_WEIGHT_SHIFT;
2645 }
2646 }
2647
2648 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2649 PNG_COST_SHIFT;
2650 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2651 PNG_COST_SHIFT;
2652
2653 if (sumhi > PNG_HIMASK)
2654 sum = PNG_MAXSUM;
2655 else
2656 sum = (sumhi << PNG_HISHIFT) + sumlo;
2657 }
2658#endif
2659
Guy Schalnate5a37791996-06-05 15:50:50 -05002660 if (sum < mins)
2661 {
2662 best_row = png_ptr->paeth_row;
2663 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002664 }
2665
Andreas Dilger47a0c421997-05-16 02:46:07 -05002666 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002667
Guy Schalnate5a37791996-06-05 15:50:50 -05002668 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002669
2670#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2671 /* Save the type of filter we picked this time for future calculations */
2672 if (png_ptr->num_prev_filters > 0)
2673 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002674 int j;
2675 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002676 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002677 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002678 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002679 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002680 }
2681#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002682}
2683
2684
Andreas Dilger47a0c421997-05-16 02:46:07 -05002685/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002686void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002687png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2688{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002689 png_debug(1, "in png_write_filtered_row\n");
2690 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002691 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002692
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002693 png_ptr->zstream.next_in = filtered_row;
2694 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002695 /* repeat until we have compressed all the data */
2696 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002697 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002698 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002699
Guy Schalnate5a37791996-06-05 15:50:50 -05002700 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002701 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002702 /* check for compression errors */
2703 if (ret != Z_OK)
2704 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002705 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002706 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002707 else
2708 png_error(png_ptr, "zlib error");
2709 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002710
Guy Schalnate5a37791996-06-05 15:50:50 -05002711 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002712 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002713 {
2714 /* write the IDAT and reset the zlib output buffer */
2715 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002716 png_ptr->zstream.next_out = png_ptr->zbuf;
2717 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002718 }
2719 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002720 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002721
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002722 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002723 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002724 {
2725 png_bytep tptr;
2726
2727 tptr = png_ptr->prev_row;
2728 png_ptr->prev_row = png_ptr->row_buf;
2729 png_ptr->row_buf = tptr;
2730 }
2731
Guy Schalnate5a37791996-06-05 15:50:50 -05002732 /* finish row - updates counters and flushes zlib if last row */
2733 png_write_finish_row(png_ptr);
2734
2735#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2736 png_ptr->flush_rows++;
2737
2738 if (png_ptr->flush_dist > 0 &&
2739 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002740 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002741 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002742 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002743#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002744}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05002745#endif /* PNG_WRITE_SUPPORTED */