blob: a1ccde355110abe3767cc8060c4b454afac71297 [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-Pehrson9c3ab682006-02-20 22:09:05 -06004 * libpng version 1.2.9beta1 - February 21, 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
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -050028#if defined(PNG_WRITE_pCAL_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -050029/* The png_save_int_32 function assumes integers are stored in two's
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060030 * complement format. If this isn't the case, then this routine needs to
31 * be modified to write data in two's complement format.
32 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060033void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050034png_save_int_32(png_bytep buf, png_int_32 i)
35{
36 buf[0] = (png_byte)((i >> 24) & 0xff);
37 buf[1] = (png_byte)((i >> 16) & 0xff);
38 buf[2] = (png_byte)((i >> 8) & 0xff);
39 buf[3] = (png_byte)(i & 0xff);
40}
41#endif
42
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060043/* Place a 16-bit number into a buffer in PNG byte order.
44 * The parameter is declared unsigned int, not png_uint_16,
45 * just to avoid potential problems on pre-ANSI C compilers.
46 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060047void PNGAPI
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060048png_save_uint_16(png_bytep buf, unsigned int i)
Guy Schalnat0d580581995-07-20 02:43:20 -050049{
50 buf[0] = (png_byte)((i >> 8) & 0xff);
51 buf[1] = (png_byte)(i & 0xff);
52}
53
Andreas Dilger47a0c421997-05-16 02:46:07 -050054/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060055 * representing the chunk name. The array must be at least 4 bytes in
56 * length, and does not need to be null terminated. To be safe, pass the
57 * pre-defined chunk names here, and if you need a new one, define it
58 * where the others are defined. The length is the length of the data.
59 * All the data must be present. If that is not possible, use the
60 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
61 * functions instead.
62 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050063void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060064png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050065 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050066{
Andreas Dilger47a0c421997-05-16 02:46:07 -050067 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060068 png_write_chunk_data(png_ptr, data, length);
69 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050070}
71
Andreas Dilger47a0c421997-05-16 02:46:07 -050072/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060073 * The total_length is the sum of the lengths of all the data you will be
74 * passing in png_write_chunk_data().
75 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050076void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060077png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
78 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -050079{
Andreas Dilger47a0c421997-05-16 02:46:07 -050080 png_byte buf[4];
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -050081 png_debug2(0, "Writing %s chunk (%lu bytes)\n", chunk_name, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -050082
Guy Schalnat0d580581995-07-20 02:43:20 -050083 /* write the length */
Andreas Dilger47a0c421997-05-16 02:46:07 -050084 png_save_uint_32(buf, length);
85 png_write_data(png_ptr, buf, (png_size_t)4);
86
Guy Schalnat0d580581995-07-20 02:43:20 -050087 /* write the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -050088 png_write_data(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050089 /* reset the crc and run it over the chunk name */
90 png_reset_crc(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -050091 png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -050092}
93
Andreas Dilger47a0c421997-05-16 02:46:07 -050094/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060095 * Note that multiple calls to this function are allowed, and that the
96 * sum of the lengths from these calls *must* add up to the total_length
97 * given to png_write_chunk_start().
98 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050099void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500100png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500101{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500102 /* write the data, and run the CRC over it */
103 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500104 {
105 png_calculate_crc(png_ptr, data, length);
Guy Schalnat6d764711995-12-19 03:22:19 -0600106 png_write_data(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500107 }
108}
109
Andreas Dilger47a0c421997-05-16 02:46:07 -0500110/* Finish a chunk started with png_write_chunk_start(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500111void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600112png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500113{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500114 png_byte buf[4];
115
Guy Schalnat0d580581995-07-20 02:43:20 -0500116 /* write the crc */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500117 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500118
119 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500120}
121
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600122/* Simple function to write the signature. If we have already written
123 * the magic bytes of the signature, or more likely, the PNG stream is
124 * being embedded into another stream and doesn't need its own signature,
125 * we should call png_set_sig_bytes() to tell libpng how many of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600126 * bytes have already been written.
127 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500128void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600129png_write_sig(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500130{
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600131 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600132 /* write the rest of the 8 byte signature */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600133 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
Andreas Dilger47a0c421997-05-16 02:46:07 -0500134 (png_size_t)8 - png_ptr->sig_bytes);
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600135 if(png_ptr->sig_bytes < 3)
136 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500137}
138
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600139#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600140/*
141 * This pair of functions encapsulates the operation of (a) compressing a
142 * text string, and (b) issuing it later as a series of chunk data writes.
143 * The compression_state structure is shared context for these functions
144 * set up by the caller in order to make the whole mess thread-safe.
145 */
146
147typedef struct
148{
149 char *input; /* the uncompressed input data */
150 int input_len; /* its length */
151 int num_output_ptr; /* number of output pointers used */
152 int max_output_ptr; /* size of output_ptr */
153 png_charpp output_ptr; /* array of pointers to output */
154} compression_state;
155
156/* compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500157static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600158png_text_compress(png_structp png_ptr,
159 png_charp text, png_size_t text_len, int compression,
160 compression_state *comp)
161{
162 int ret;
163
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600164 comp->num_output_ptr = 0;
165 comp->max_output_ptr = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600166 comp->output_ptr = NULL;
167 comp->input = NULL;
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600168 comp->input_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600169
170 /* we may just want to pass the text right through */
171 if (compression == PNG_TEXT_COMPRESSION_NONE)
172 {
173 comp->input = text;
174 comp->input_len = text_len;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500175 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600176 }
177
178 if (compression >= PNG_TEXT_COMPRESSION_LAST)
179 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500180#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600181 char msg[50];
182 sprintf(msg, "Unknown compression type %d", compression);
183 png_warning(png_ptr, msg);
184#else
185 png_warning(png_ptr, "Unknown compression type");
186#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600187 }
188
189 /* We can't write the chunk until we find out how much data we have,
190 * which means we need to run the compressor first and save the
191 * output. This shouldn't be a problem, as the vast majority of
192 * comments should be reasonable, but we will set up an array of
193 * malloc'd pointers to be sure.
194 *
195 * If we knew the application was well behaved, we could simplify this
196 * greatly by assuming we can always malloc an output buffer large
197 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
198 * and malloc this directly. The only time this would be a bad idea is
199 * if we can't malloc more than 64K and we have 64K of random input
200 * data, or if the input string is incredibly large (although this
201 * wouldn't cause a failure, just a slowdown due to swapping).
202 */
203
204 /* set up the compression buffers */
205 png_ptr->zstream.avail_in = (uInt)text_len;
206 png_ptr->zstream.next_in = (Bytef *)text;
207 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
208 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
209
210 /* this is the same compression loop as in png_write_row() */
211 do
212 {
213 /* compress the data */
214 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
215 if (ret != Z_OK)
216 {
217 /* error */
218 if (png_ptr->zstream.msg != NULL)
219 png_error(png_ptr, png_ptr->zstream.msg);
220 else
221 png_error(png_ptr, "zlib error");
222 }
223 /* check to see if we need more room */
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600224 if (!(png_ptr->zstream.avail_out))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600225 {
226 /* make sure the output array has room */
227 if (comp->num_output_ptr >= comp->max_output_ptr)
228 {
229 int old_max;
230
231 old_max = comp->max_output_ptr;
232 comp->max_output_ptr = comp->num_output_ptr + 4;
233 if (comp->output_ptr != NULL)
234 {
235 png_charpp old_ptr;
236
237 old_ptr = comp->output_ptr;
238 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500239 (png_uint_32)(comp->max_output_ptr *
240 png_sizeof (png_charpp)));
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500241 png_memcpy(comp->output_ptr, old_ptr, old_max
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500242 * png_sizeof (png_charp));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600243 png_free(png_ptr, old_ptr);
244 }
245 else
246 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500247 (png_uint_32)(comp->max_output_ptr *
248 png_sizeof (png_charp)));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600249 }
250
251 /* save the data */
252 comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr,
253 (png_uint_32)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500254 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
255 png_ptr->zbuf_size);
256 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600257
258 /* and reset the buffer */
259 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
260 png_ptr->zstream.next_out = png_ptr->zbuf;
261 }
262 /* continue until we don't have any more to compress */
263 } while (png_ptr->zstream.avail_in);
264
265 /* finish the compression */
266 do
267 {
268 /* tell zlib we are finished */
269 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500270
271 if (ret == Z_OK)
272 {
273 /* check to see if we need more room */
274 if (!(png_ptr->zstream.avail_out))
275 {
276 /* check to make sure our output array has room */
277 if (comp->num_output_ptr >= comp->max_output_ptr)
278 {
279 int old_max;
280
281 old_max = comp->max_output_ptr;
282 comp->max_output_ptr = comp->num_output_ptr + 4;
283 if (comp->output_ptr != NULL)
284 {
285 png_charpp old_ptr;
286
287 old_ptr = comp->output_ptr;
288 /* This could be optimized to realloc() */
289 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500290 (png_uint_32)(comp->max_output_ptr *
291 png_sizeof (png_charpp)));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500292 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500293 old_max * png_sizeof (png_charp));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500294 png_free(png_ptr, old_ptr);
295 }
296 else
297 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500298 (png_uint_32)(comp->max_output_ptr *
299 png_sizeof (png_charp)));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500300 }
301
302 /* save off the data */
303 comp->output_ptr[comp->num_output_ptr] =
304 (png_charp)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500305 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
306 png_ptr->zbuf_size);
307 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500308
309 /* and reset the buffer pointers */
310 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
311 png_ptr->zstream.next_out = png_ptr->zbuf;
312 }
313 }
314 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600315 {
316 /* we got an error */
317 if (png_ptr->zstream.msg != NULL)
318 png_error(png_ptr, png_ptr->zstream.msg);
319 else
320 png_error(png_ptr, "zlib error");
321 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600322 } while (ret != Z_STREAM_END);
323
324 /* text length is number of buffers plus last buffer */
325 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
326 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
327 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
328
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500329 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600330}
331
332/* ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500333static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600334png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
335{
336 int i;
337
338 /* handle the no-compression case */
339 if (comp->input)
340 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500341 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
342 (png_size_t)comp->input_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600343 return;
344 }
345
346 /* write saved output buffers, if any */
347 for (i = 0; i < comp->num_output_ptr; i++)
348 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600349 png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
350 png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600351 png_free(png_ptr, comp->output_ptr[i]);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500352 comp->output_ptr[i]=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600353 }
354 if (comp->max_output_ptr != 0)
355 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500356 comp->output_ptr=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600357 /* write anything left in zbuf */
358 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
359 png_write_chunk_data(png_ptr, png_ptr->zbuf,
360 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
361
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600362 /* reset zlib for another zTXt/iTXt or image data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600363 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600364 png_ptr->zstream.data_type = Z_BINARY;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600365}
366#endif
367
Guy Schalnat0d580581995-07-20 02:43:20 -0500368/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600369 * information. Note that the rest of this code depends upon this
370 * information being correct.
371 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500372void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600373png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500374 int bit_depth, int color_type, int compression_type, int filter_type,
375 int interlace_type)
376{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600377#ifdef PNG_USE_LOCAL_ARRAYS
378 PNG_IHDR;
379#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500380 png_byte buf[13]; /* buffer to store the IHDR info */
381
Andreas Dilger47a0c421997-05-16 02:46:07 -0500382 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500383 /* Check that we have valid input data from the application info */
384 switch (color_type)
385 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500386 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500387 switch (bit_depth)
388 {
389 case 1:
390 case 2:
391 case 4:
392 case 8:
393 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500394 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500395 }
396 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500397 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500398 if (bit_depth != 8 && bit_depth != 16)
399 png_error(png_ptr, "Invalid bit depth for RGB image");
400 png_ptr->channels = 3;
401 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500402 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500403 switch (bit_depth)
404 {
405 case 1:
406 case 2:
407 case 4:
408 case 8: png_ptr->channels = 1; break;
409 default: png_error(png_ptr, "Invalid bit depth for paletted image");
410 }
411 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500412 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500413 if (bit_depth != 8 && bit_depth != 16)
414 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
415 png_ptr->channels = 2;
416 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500417 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500418 if (bit_depth != 8 && bit_depth != 16)
419 png_error(png_ptr, "Invalid bit depth for RGBA image");
420 png_ptr->channels = 4;
421 break;
422 default:
423 png_error(png_ptr, "Invalid image color type specified");
424 }
425
Andreas Dilger47a0c421997-05-16 02:46:07 -0500426 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500427 {
428 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500429 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500430 }
431
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600432 /* Write filter_method 64 (intrapixel differencing) only if
433 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
434 * 2. Libpng did not write a PNG signature (this filter_method is only
435 * used in PNG datastreams that are embedded in MNG datastreams) and
436 * 3. The application called png_permit_mng_features with a mask that
437 * included PNG_FLAG_MNG_FILTER_64 and
438 * 4. The filter_method is 64 and
439 * 5. The color_type is RGB or RGBA
440 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600441 if (
442#if defined(PNG_MNG_FEATURES_SUPPORTED)
443 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600444 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500445 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600446 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600447 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
448#endif
449 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500450 {
451 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500452 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500453 }
454
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600455#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500456 if (interlace_type != PNG_INTERLACE_NONE &&
457 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500458 {
459 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500460 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500461 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600462#else
463 interlace_type=PNG_INTERLACE_NONE;
464#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500465
466 /* save off the relevent information */
467 png_ptr->bit_depth = (png_byte)bit_depth;
468 png_ptr->color_type = (png_byte)color_type;
469 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500470#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600471 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500472#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500473 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500474 png_ptr->width = width;
475 png_ptr->height = height;
476
477 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500478 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
Guy Schalnate5a37791996-06-05 15:50:50 -0500479 /* set the usr info, so any transformations can modify it */
480 png_ptr->usr_width = png_ptr->width;
481 png_ptr->usr_bit_depth = png_ptr->bit_depth;
482 png_ptr->usr_channels = png_ptr->channels;
483
Guy Schalnat0d580581995-07-20 02:43:20 -0500484 /* pack the header information into the buffer */
485 png_save_uint_32(buf, width);
486 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600487 buf[8] = (png_byte)bit_depth;
488 buf[9] = (png_byte)color_type;
489 buf[10] = (png_byte)compression_type;
490 buf[11] = (png_byte)filter_type;
491 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500492
493 /* write the chunk */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600494 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500495
Andreas Dilger47a0c421997-05-16 02:46:07 -0500496 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600497 png_ptr->zstream.zalloc = png_zalloc;
498 png_ptr->zstream.zfree = png_zfree;
499 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500500 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500501 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500502 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
503 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500504 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500505 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500506 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500507 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500508 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500509 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500510 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500511 png_ptr->zlib_strategy = Z_FILTERED;
512 else
513 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
514 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500515 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500516 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500517 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500518 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500519 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500520 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500521 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500522 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600523 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500524 png_ptr->zlib_method, png_ptr->zlib_window_bits,
525 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600526 png_ptr->zstream.next_out = png_ptr->zbuf;
527 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600528 /* libpng is not interested in zstream.data_type */
529 /* set it to a predefined value, to avoid its evaluation inside zlib */
530 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500531
Guy Schalnate5a37791996-06-05 15:50:50 -0500532 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500533}
534
535/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500536 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600537 * structure.
538 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500539void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500540png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500541{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600542#ifdef PNG_USE_LOCAL_ARRAYS
543 PNG_PLTE;
544#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500545 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600546 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500547 png_byte buf[3];
548
Andreas Dilger47a0c421997-05-16 02:46:07 -0500549 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500550 if ((
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500551#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600552 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500553#endif
554 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500555 {
556 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500557 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500558 png_error(png_ptr, "Invalid number of colors in palette");
559 }
560 else
561 {
562 png_warning(png_ptr, "Invalid number of colors in palette");
563 return;
564 }
565 }
566
567 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
568 {
569 png_warning(png_ptr,
570 "Ignoring request to write a PLTE chunk in grayscale PNG");
571 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500572 }
573
Andreas Dilger47a0c421997-05-16 02:46:07 -0500574 png_ptr->num_palette = (png_uint_16)num_pal;
575 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500576
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600577 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500578#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500579 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500580 {
581 buf[0] = pal_ptr->red;
582 buf[1] = pal_ptr->green;
583 buf[2] = pal_ptr->blue;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500584 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500585 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500586#else
587 /* This is a little slower but some buggy compilers need to do this instead */
588 pal_ptr=palette;
589 for (i = 0; i < num_pal; i++)
590 {
591 buf[0] = pal_ptr[i].red;
592 buf[1] = pal_ptr[i].green;
593 buf[2] = pal_ptr[i].blue;
594 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
595 }
596#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500597 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500598 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500599}
600
601/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500602void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500603png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500604{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600605#ifdef PNG_USE_LOCAL_ARRAYS
606 PNG_IDAT;
607#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500608 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500609
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500610 /* Optimize the CMF field in the zlib stream. */
611 /* This hack of the zlib stream is compliant to the stream specification. */
612 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
613 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
614 {
615 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
616 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
617 {
618 /* Avoid memory underflows and multiplication overflows. */
619 /* The conditions below are practically always satisfied;
620 however, they still must be checked. */
621 if (length >= 2 &&
622 png_ptr->height < 16384 && png_ptr->width < 16384)
623 {
624 png_uint_32 uncompressed_idat_size = png_ptr->height *
625 ((png_ptr->width *
626 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
627 unsigned int z_cinfo = z_cmf >> 4;
628 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
629 while (uncompressed_idat_size <= half_z_window_size &&
630 half_z_window_size >= 256)
631 {
632 z_cinfo--;
633 half_z_window_size >>= 1;
634 }
635 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
636 if (data[0] != (png_byte)z_cmf)
637 {
638 data[0] = (png_byte)z_cmf;
639 data[1] &= 0xe0;
640 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
641 }
642 }
643 }
644 else
645 png_error(png_ptr,
646 "Invalid zlib compression method or flags in IDAT");
647 }
648
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600649 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500650 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500651}
652
653/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500654void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600655png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500656{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600657#ifdef PNG_USE_LOCAL_ARRAYS
658 PNG_IEND;
659#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500660 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500661 png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL,
Glenn Randers-Pehrson6c97ddb2001-10-24 22:01:19 -0500662 (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600663 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500664}
665
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500666#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500667/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600668#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500669void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500670png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500671{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600672#ifdef PNG_USE_LOCAL_ARRAYS
673 PNG_gAMA;
674#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500675 png_uint_32 igamma;
676 png_byte buf[4];
677
Andreas Dilger47a0c421997-05-16 02:46:07 -0500678 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600679 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600680 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500681 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600682 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500683}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500684#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600685#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500686void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600687png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600688{
689#ifdef PNG_USE_LOCAL_ARRAYS
690 PNG_gAMA;
691#endif
692 png_byte buf[4];
693
694 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600695 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500696 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600697 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
698}
699#endif
700#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500701
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600702#if defined(PNG_WRITE_sRGB_SUPPORTED)
703/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500704void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600705png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600706{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600707#ifdef PNG_USE_LOCAL_ARRAYS
708 PNG_sRGB;
709#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600710 png_byte buf[1];
711
712 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600713 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600714 png_warning(png_ptr,
715 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600716 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600717 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600718}
719#endif
720
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600721#if defined(PNG_WRITE_iCCP_SUPPORTED)
722/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500723void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600724png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
725 png_charp profile, int profile_len)
726{
727#ifdef PNG_USE_LOCAL_ARRAYS
728 PNG_iCCP;
729#endif
730 png_size_t name_len;
731 png_charp new_name;
732 compression_state comp;
733
734 png_debug(1, "in png_write_iCCP\n");
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600735
736 comp.num_output_ptr = 0;
737 comp.max_output_ptr = 0;
738 comp.output_ptr = NULL;
739 comp.input = NULL;
740 comp.input_len = 0;
741
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600742 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
743 &new_name)) == 0)
744 {
745 png_warning(png_ptr, "Empty keyword in iCCP chunk");
746 return;
747 }
748
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600749 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600750 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600751
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500752 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600753 profile_len = 0;
754
755 if (profile_len)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500756 profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600757 PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600758
759 /* make sure we include the NULL after the name and the compression type */
760 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
761 (png_uint_32)name_len+profile_len+2);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600762 new_name[name_len+1]=0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600763 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
764
765 if (profile_len)
766 png_write_compressed_data_out(png_ptr, &comp);
767
768 png_write_chunk_end(png_ptr);
769 png_free(png_ptr, new_name);
770}
771#endif
772
773#if defined(PNG_WRITE_sPLT_SUPPORTED)
774/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500775void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600776png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600777{
778#ifdef PNG_USE_LOCAL_ARRAYS
779 PNG_sPLT;
780#endif
781 png_size_t name_len;
782 png_charp new_name;
783 png_byte entrybuf[10];
784 int entry_size = (spalette->depth == 8 ? 6 : 10);
785 int palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600786 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500787#ifdef PNG_NO_POINTER_INDEXING
788 int i;
789#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600790
791 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600792 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
793 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600794 {
795 png_warning(png_ptr, "Empty keyword in sPLT chunk");
796 return;
797 }
798
799 /* make sure we include the NULL after the name */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500800 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600801 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600802 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600803 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600804
805 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500806#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600807 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
808 {
809 if (spalette->depth == 8)
810 {
811 entrybuf[0] = (png_byte)ep->red;
812 entrybuf[1] = (png_byte)ep->green;
813 entrybuf[2] = (png_byte)ep->blue;
814 entrybuf[3] = (png_byte)ep->alpha;
815 png_save_uint_16(entrybuf + 4, ep->frequency);
816 }
817 else
818 {
819 png_save_uint_16(entrybuf + 0, ep->red);
820 png_save_uint_16(entrybuf + 2, ep->green);
821 png_save_uint_16(entrybuf + 4, ep->blue);
822 png_save_uint_16(entrybuf + 6, ep->alpha);
823 png_save_uint_16(entrybuf + 8, ep->frequency);
824 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500825 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600826 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500827#else
828 ep=spalette->entries;
829 for (i=0; i>spalette->nentries; i++)
830 {
831 if (spalette->depth == 8)
832 {
833 entrybuf[0] = (png_byte)ep[i].red;
834 entrybuf[1] = (png_byte)ep[i].green;
835 entrybuf[2] = (png_byte)ep[i].blue;
836 entrybuf[3] = (png_byte)ep[i].alpha;
837 png_save_uint_16(entrybuf + 4, ep[i].frequency);
838 }
839 else
840 {
841 png_save_uint_16(entrybuf + 0, ep[i].red);
842 png_save_uint_16(entrybuf + 2, ep[i].green);
843 png_save_uint_16(entrybuf + 4, ep[i].blue);
844 png_save_uint_16(entrybuf + 6, ep[i].alpha);
845 png_save_uint_16(entrybuf + 8, ep[i].frequency);
846 }
847 png_write_chunk_data(png_ptr, entrybuf, entry_size);
848 }
849#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600850
851 png_write_chunk_end(png_ptr);
852 png_free(png_ptr, new_name);
853}
854#endif
855
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500856#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500857/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500858void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600859png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500860{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600861#ifdef PNG_USE_LOCAL_ARRAYS
862 PNG_sBIT;
863#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500864 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500865 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500866
Andreas Dilger47a0c421997-05-16 02:46:07 -0500867 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600868 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500869 if (color_type & PNG_COLOR_MASK_COLOR)
870 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600871 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500872
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500873 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
874 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600875 if (sbit->red == 0 || sbit->red > maxbits ||
876 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500877 sbit->blue == 0 || sbit->blue > maxbits)
878 {
879 png_warning(png_ptr, "Invalid sBIT depth specified");
880 return;
881 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500882 buf[0] = sbit->red;
883 buf[1] = sbit->green;
884 buf[2] = sbit->blue;
885 size = 3;
886 }
887 else
888 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500889 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
890 {
891 png_warning(png_ptr, "Invalid sBIT depth specified");
892 return;
893 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500894 buf[0] = sbit->gray;
895 size = 1;
896 }
897
898 if (color_type & PNG_COLOR_MASK_ALPHA)
899 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500900 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
901 {
902 png_warning(png_ptr, "Invalid sBIT depth specified");
903 return;
904 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500905 buf[size++] = sbit->alpha;
906 }
907
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600908 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500909}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500910#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500911
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500912#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500913/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600914#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500915void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500916png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600917 double red_x, double red_y, double green_x, double green_y,
918 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500919{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600920#ifdef PNG_USE_LOCAL_ARRAYS
921 PNG_cHRM;
922#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500923 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600924 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500925
Andreas Dilger47a0c421997-05-16 02:46:07 -0500926 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600927 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500928 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
929 white_x + white_y > 1.0)
930 {
931 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500932#if !defined(PNG_NO_CONSOLE_IO)
933 fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600934#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500935 return;
936 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600937 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500938 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600939 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500940 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500941
942 if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 ||
943 red_x + red_y > 1.0)
944 {
945 png_warning(png_ptr, "Invalid cHRM red point specified");
946 return;
947 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600948 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500949 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600950 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500951 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500952
953 if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 ||
954 green_x + green_y > 1.0)
955 {
956 png_warning(png_ptr, "Invalid cHRM green point specified");
957 return;
958 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600959 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500960 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600961 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500962 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500963
964 if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 ||
965 blue_x + blue_y > 1.0)
966 {
967 png_warning(png_ptr, "Invalid cHRM blue point specified");
968 return;
969 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600970 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500971 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600972 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500973 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500974
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600975 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -0500976}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500977#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600978#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500979void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600980png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
981 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
982 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
983 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600984{
985#ifdef PNG_USE_LOCAL_ARRAYS
986 PNG_cHRM;
987#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600988 png_byte buf[32];
989
990 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600991 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600992 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
993 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600994 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500995#if !defined(PNG_NO_CONSOLE_IO)
996 fprintf(stderr,"white_x=%ld, white_y=%ld\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600997#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600998 return;
999 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001000 png_save_uint_32(buf, (png_uint_32)white_x);
1001 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001002
1003 if (red_x > 80000L || red_y > 80000L || red_x + red_y > 100000L)
1004 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001005 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001006 return;
1007 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001008 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1009 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001010
1011 if (green_x > 80000L || green_y > 80000L || green_x + green_y > 100000L)
1012 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001013 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001014 return;
1015 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001016 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1017 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001018
1019 if (blue_x > 80000L || blue_y > 80000L || blue_x + blue_y > 100000L)
1020 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001021 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001022 return;
1023 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001024 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1025 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001026
1027 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
1028}
1029#endif
1030#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001031
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001032#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001033/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001034void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001035png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -05001036 int num_trans, int color_type)
1037{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001038#ifdef PNG_USE_LOCAL_ARRAYS
1039 PNG_tRNS;
1040#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001041 png_byte buf[6];
1042
Andreas Dilger47a0c421997-05-16 02:46:07 -05001043 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001044 if (color_type == PNG_COLOR_TYPE_PALETTE)
1045 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001046 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001047 {
1048 png_warning(png_ptr,"Invalid number of transparent colors specified");
1049 return;
1050 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001051 /* write the chunk out as it is */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001052 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001053 }
1054 else if (color_type == PNG_COLOR_TYPE_GRAY)
1055 {
1056 /* one 16 bit value */
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001057 if(tran->gray >= (1 << png_ptr->bit_depth))
1058 {
1059 png_warning(png_ptr,
1060 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1061 return;
1062 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001063 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001064 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001065 }
1066 else if (color_type == PNG_COLOR_TYPE_RGB)
1067 {
1068 /* three 16 bit values */
1069 png_save_uint_16(buf, tran->red);
1070 png_save_uint_16(buf + 2, tran->green);
1071 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001072 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1073 {
1074 png_warning(png_ptr,
1075 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1076 return;
1077 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001078 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001079 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001080 else
1081 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001082 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001083 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001084}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001085#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001086
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001087#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001088/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001089void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001090png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001091{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001092#ifdef PNG_USE_LOCAL_ARRAYS
1093 PNG_bKGD;
1094#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001095 png_byte buf[6];
1096
Andreas Dilger47a0c421997-05-16 02:46:07 -05001097 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001098 if (color_type == PNG_COLOR_TYPE_PALETTE)
1099 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001100 if (
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001101#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001102 (png_ptr->num_palette ||
1103 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001104#endif
1105 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001106 {
1107 png_warning(png_ptr, "Invalid background palette index");
1108 return;
1109 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001110 buf[0] = back->index;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001111 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001112 }
1113 else if (color_type & PNG_COLOR_MASK_COLOR)
1114 {
1115 png_save_uint_16(buf, back->red);
1116 png_save_uint_16(buf + 2, back->green);
1117 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001118 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1119 {
1120 png_warning(png_ptr,
1121 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1122 return;
1123 }
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001124 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001125 }
1126 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001127 {
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001128 if(back->gray >= (1 << png_ptr->bit_depth))
1129 {
1130 png_warning(png_ptr,
1131 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1132 return;
1133 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001134 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001135 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001136 }
1137}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001138#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001139
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001140#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001141/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001142void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001143png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001144{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001145#ifdef PNG_USE_LOCAL_ARRAYS
1146 PNG_hIST;
1147#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001148 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001149 png_byte buf[3];
1150
Andreas Dilger47a0c421997-05-16 02:46:07 -05001151 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001152 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001153 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001154 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1155 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001156 png_warning(png_ptr, "Invalid number of histogram entries specified");
1157 return;
1158 }
1159
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001160 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001161 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001162 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001163 png_save_uint_16(buf, hist[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001164 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001165 }
1166 png_write_chunk_end(png_ptr);
1167}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001168#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001169
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001170#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1171 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001172/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1173 * and if invalid, correct the keyword rather than discarding the entire
1174 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1175 * length, forbids leading or trailing whitespace, multiple internal spaces,
1176 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001177 *
1178 * The new_key is allocated to hold the corrected keyword and must be freed
1179 * by the calling routine. This avoids problems with trying to write to
1180 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001181 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001182png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001183png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001184{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001185 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001186 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001187 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001188 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001189
Andreas Dilger47a0c421997-05-16 02:46:07 -05001190 png_debug(1, "in png_check_keyword\n");
1191 *new_key = NULL;
1192
1193 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001194 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001195 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001196 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001197 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001198
Andreas Dilger47a0c421997-05-16 02:46:07 -05001199 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1200
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001201 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
1202 if (*new_key == NULL)
1203 {
1204 png_warning(png_ptr, "Out of memory while procesing keyword");
1205 return ((png_size_t)0);
1206 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001207
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001208 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001209 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001210 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001211 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001212 {
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001213#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001214 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001215
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001216 sprintf(msg, "invalid keyword character 0x%02X", *kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001217 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001218#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001219 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001220#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001221 *dp = ' ';
1222 }
1223 else
1224 {
1225 *dp = *kp;
1226 }
1227 }
1228 *dp = '\0';
1229
1230 /* Remove any trailing white space. */
1231 kp = *new_key + key_len - 1;
1232 if (*kp == ' ')
1233 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001234 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001235
1236 while (*kp == ' ')
1237 {
1238 *(kp--) = '\0';
1239 key_len--;
1240 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001241 }
1242
1243 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001244 kp = *new_key;
1245 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001246 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001247 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001248
1249 while (*kp == ' ')
1250 {
1251 kp++;
1252 key_len--;
1253 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001254 }
1255
Andreas Dilger47a0c421997-05-16 02:46:07 -05001256 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001257
Andreas Dilger47a0c421997-05-16 02:46:07 -05001258 /* Remove multiple internal spaces. */
1259 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001260 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001261 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001262 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001263 *(dp++) = *kp;
1264 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001265 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001266 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001267 {
1268 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001269 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001270 }
1271 else
1272 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001273 *(dp++) = *kp;
1274 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001275 }
1276 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001277 *dp = '\0';
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001278 if(kwarn)
1279 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001280
1281 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001282 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001283 png_free(png_ptr, *new_key);
1284 *new_key=NULL;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001285 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001286 }
1287
1288 if (key_len > 79)
1289 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001290 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001291 new_key[79] = '\0';
1292 key_len = 79;
1293 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001294
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001295 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001296}
1297#endif
1298
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001299#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001300/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001301void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001302png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001303 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001304{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001305#ifdef PNG_USE_LOCAL_ARRAYS
1306 PNG_tEXt;
1307#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001308 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001309 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001310
Andreas Dilger47a0c421997-05-16 02:46:07 -05001311 png_debug(1, "in png_write_tEXt\n");
1312 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1313 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001314 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001315 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001316 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001317
Andreas Dilger47a0c421997-05-16 02:46:07 -05001318 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001319 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001320 else
1321 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001322
1323 /* make sure we include the 0 after the key */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001324 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 -05001325 /*
1326 * We leave it to the application to meet PNG-1.0 requirements on the
1327 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1328 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001329 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001330 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001331 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001332 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001333 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001334
Guy Schalnat0d580581995-07-20 02:43:20 -05001335 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001336 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001337}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001338#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001339
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001340#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001341/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001342void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001343png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001344 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001345{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001346#ifdef PNG_USE_LOCAL_ARRAYS
1347 PNG_zTXt;
1348#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001349 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001350 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001351 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001352 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001353
Andreas Dilger47a0c421997-05-16 02:46:07 -05001354 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001355
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001356 comp.num_output_ptr = 0;
1357 comp.max_output_ptr = 0;
1358 comp.output_ptr = NULL;
1359 comp.input = NULL;
1360 comp.input_len = 0;
1361
Andreas Dilger47a0c421997-05-16 02:46:07 -05001362 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001363 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001364 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001365 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001366 }
1367
Andreas Dilger47a0c421997-05-16 02:46:07 -05001368 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1369 {
1370 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1371 png_free(png_ptr, new_key);
1372 return;
1373 }
1374
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001375 text_len = png_strlen(text);
1376
Andreas Dilger47a0c421997-05-16 02:46:07 -05001377 png_free(png_ptr, new_key);
1378
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001379 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001380 text_len = png_text_compress(png_ptr, text, text_len, compression,
1381 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001382
1383 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001384 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1385 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001386 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001387 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001388 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001389 /* write compression */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001390 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001391 /* write the compressed data */
1392 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001393
Guy Schalnat0d580581995-07-20 02:43:20 -05001394 /* close the chunk */
1395 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001396}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001397#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001398
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001399#if defined(PNG_WRITE_iTXt_SUPPORTED)
1400/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001401void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001402png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001403 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001404{
1405#ifdef PNG_USE_LOCAL_ARRAYS
1406 PNG_iTXt;
1407#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001408 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001409 png_charp new_lang, new_key;
1410 png_byte cbuf[2];
1411 compression_state comp;
1412
1413 png_debug(1, "in png_write_iTXt\n");
1414
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001415 comp.num_output_ptr = 0;
1416 comp.max_output_ptr = 0;
1417 comp.output_ptr = NULL;
1418 comp.input = NULL;
1419
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001420 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1421 {
1422 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1423 return;
1424 }
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001425 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001426 {
1427 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001428 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001429 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001430 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001431
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001432 if (lang_key == NULL)
1433 lang_key_len = 0;
1434 else
1435 lang_key_len = png_strlen(lang_key);
1436
1437 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001438 text_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001439 else
1440 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001441
1442 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001443 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1444 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001445
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001446
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001447 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001448 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001449
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001450 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1451 (png_uint_32)(
1452 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1453 + key_len
1454 + lang_len
1455 + lang_key_len
1456 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001457
1458 /*
1459 * We leave it to the application to meet PNG-1.0 requirements on the
1460 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1461 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1462 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1463 */
1464 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001465
1466 /* set the compression flag */
1467 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1468 compression == PNG_TEXT_COMPRESSION_NONE)
1469 cbuf[0] = 0;
1470 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1471 cbuf[0] = 1;
1472 /* set the compression method */
1473 cbuf[1] = 0;
1474 png_write_chunk_data(png_ptr, cbuf, 2);
1475
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001476 cbuf[0] = 0;
1477 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf), lang_len + 1);
1478 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 -06001479 png_write_compressed_data_out(png_ptr, &comp);
1480
1481 png_write_chunk_end(png_ptr);
1482 png_free(png_ptr, new_key);
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001483 if (new_lang)
1484 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001485}
1486#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001487
1488#if defined(PNG_WRITE_oFFs_SUPPORTED)
1489/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001490void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001491png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001492 int unit_type)
1493{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001494#ifdef PNG_USE_LOCAL_ARRAYS
1495 PNG_oFFs;
1496#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001497 png_byte buf[9];
1498
1499 png_debug(1, "in png_write_oFFs\n");
1500 if (unit_type >= PNG_OFFSET_LAST)
1501 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1502
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001503 png_save_int_32(buf, x_offset);
1504 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001505 buf[8] = (png_byte)unit_type;
1506
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001507 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001508}
1509#endif
1510
1511#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001512/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001513void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001514png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1515 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1516{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001517#ifdef PNG_USE_LOCAL_ARRAYS
1518 PNG_pCAL;
1519#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001520 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001521 png_uint_32p params_len;
1522 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001523 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001524 int i;
1525
1526 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1527 if (type >= PNG_EQUATION_LAST)
1528 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1529
1530 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001531 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001532 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001533 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001534 total_len = purpose_len + units_len + 10;
1535
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001536 params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001537 *png_sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001538
1539 /* Find the length of each parameter, making sure we don't count the
1540 null terminator for the last parameter. */
1541 for (i = 0; i < nparams; i++)
1542 {
1543 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -05001544 png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001545 total_len += (png_size_t)params_len[i];
1546 }
1547
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001548 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001549 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001550 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001551 png_save_int_32(buf, X0);
1552 png_save_int_32(buf + 4, X1);
1553 buf[8] = (png_byte)type;
1554 buf[9] = (png_byte)nparams;
1555 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1556 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1557
1558 png_free(png_ptr, new_purpose);
1559
1560 for (i = 0; i < nparams; i++)
1561 {
1562 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1563 (png_size_t)params_len[i]);
1564 }
1565
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001566 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001567 png_write_chunk_end(png_ptr);
1568}
1569#endif
1570
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001571#if defined(PNG_WRITE_sCAL_SUPPORTED)
1572/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001573#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001574void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001575png_write_sCAL(png_structp png_ptr, int unit, double width,double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001576{
1577#ifdef PNG_USE_LOCAL_ARRAYS
1578 PNG_sCAL;
1579#endif
1580 png_size_t total_len;
1581 char wbuf[32], hbuf[32];
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001582 png_byte bunit = (png_byte)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001583
1584 png_debug(1, "in png_write_sCAL\n");
1585
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001586#if defined(_WIN32_WCE)
1587/* sprintf() function is not supported on WindowsCE */
1588 {
1589 wchar_t wc_buf[32];
1590 swprintf(wc_buf, TEXT("%12.12e"), width);
1591 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, wbuf, 32, NULL, NULL);
1592 swprintf(wc_buf, TEXT("%12.12e"), height);
1593 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, hbuf, 32, NULL, NULL);
1594 }
1595#else
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001596 sprintf(wbuf, "%12.12e", width);
1597 sprintf(hbuf, "%12.12e", height);
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001598#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001599 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001600
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001601 png_debug1(3, "sCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001602 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001603 png_write_chunk_data(png_ptr, (png_bytep)&bunit, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001604 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1605 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001606
1607 png_write_chunk_end(png_ptr);
1608}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001609#else
1610#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001611void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001612png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001613 png_charp height)
1614{
1615#ifdef PNG_USE_LOCAL_ARRAYS
1616 PNG_sCAL;
1617#endif
1618 png_size_t total_len;
1619 char wbuf[32], hbuf[32];
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001620 png_byte bunit = unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001621
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001622 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001623
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001624 png_strcpy(wbuf,(const char *)width);
1625 png_strcpy(hbuf,(const char *)height);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001626 total_len = 1 + png_strlen(wbuf)+1 + png_strlen(hbuf);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001627
1628 png_debug1(3, "sCAL total length = %d\n", total_len);
1629 png_write_chunk_start(png_ptr, (png_bytep)png_sCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001630 png_write_chunk_data(png_ptr, (png_bytep)&bunit, 1);
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05001631 png_write_chunk_data(png_ptr, (png_bytep)wbuf, png_strlen(wbuf)+1);
1632 png_write_chunk_data(png_ptr, (png_bytep)hbuf, png_strlen(hbuf));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001633
1634 png_write_chunk_end(png_ptr);
1635}
1636#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001637#endif
1638#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001639
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001640#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001641/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001642void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001643png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001644 png_uint_32 y_pixels_per_unit,
1645 int unit_type)
1646{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001647#ifdef PNG_USE_LOCAL_ARRAYS
1648 PNG_pHYs;
1649#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001650 png_byte buf[9];
1651
Andreas Dilger47a0c421997-05-16 02:46:07 -05001652 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001653 if (unit_type >= PNG_RESOLUTION_LAST)
1654 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1655
Guy Schalnat0d580581995-07-20 02:43:20 -05001656 png_save_uint_32(buf, x_pixels_per_unit);
1657 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001658 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001659
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001660 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001661}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001662#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001663
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001664#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001665/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1666 * or png_convert_from_time_t(), or fill in the structure yourself.
1667 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001668void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001669png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001670{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001671#ifdef PNG_USE_LOCAL_ARRAYS
1672 PNG_tIME;
1673#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001674 png_byte buf[7];
1675
Andreas Dilger47a0c421997-05-16 02:46:07 -05001676 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001677 if (mod_time->month > 12 || mod_time->month < 1 ||
1678 mod_time->day > 31 || mod_time->day < 1 ||
1679 mod_time->hour > 23 || mod_time->second > 60)
1680 {
1681 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1682 return;
1683 }
1684
Guy Schalnat0d580581995-07-20 02:43:20 -05001685 png_save_uint_16(buf, mod_time->year);
1686 buf[2] = mod_time->month;
1687 buf[3] = mod_time->day;
1688 buf[4] = mod_time->hour;
1689 buf[5] = mod_time->minute;
1690 buf[6] = mod_time->second;
1691
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001692 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001693}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001694#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001695
1696/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001697void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001698png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001699{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001700#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001701 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001702
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001703 /* start of interlace block */
1704 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001705
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001706 /* offset to next interlace block */
1707 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001708
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001709 /* start of interlace block in the y direction */
1710 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001711
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001712 /* offset to next interlace block in the y direction */
1713 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001714#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001715
Andreas Dilger47a0c421997-05-16 02:46:07 -05001716 png_size_t buf_size;
1717
1718 png_debug(1, "in png_write_start_row\n");
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001719 buf_size = (png_size_t)(PNG_ROWBYTES(
1720 png_ptr->usr_channels*png_ptr->usr_bit_depth,png_ptr->width)+1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001721
Guy Schalnat0d580581995-07-20 02:43:20 -05001722 /* set up row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001723 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001724 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001725
1726 /* set up filtering buffer, if using this filter */
1727 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001728 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001729 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001730 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001731 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001732 }
1733
Andreas Dilger47a0c421997-05-16 02:46:07 -05001734 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001735 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1736 {
1737 /* set up previous row buffer */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001738 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001739 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001740
1741 if (png_ptr->do_filter & PNG_FILTER_UP)
1742 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001743 png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001744 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001745 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001746 }
1747
1748 if (png_ptr->do_filter & PNG_FILTER_AVG)
1749 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001750 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1751 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001752 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001753 }
1754
1755 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1756 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001757 png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001758 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001759 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001760 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001761 }
1762
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001763#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001764 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001765 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001766 {
1767 if (!(png_ptr->transformations & PNG_INTERLACE))
1768 {
1769 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1770 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001771 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1772 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001773 }
1774 else
1775 {
1776 png_ptr->num_rows = png_ptr->height;
1777 png_ptr->usr_width = png_ptr->width;
1778 }
1779 }
1780 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001781#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001782 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001783 png_ptr->num_rows = png_ptr->height;
1784 png_ptr->usr_width = png_ptr->width;
1785 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001786 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1787 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001788}
1789
Andreas Dilger47a0c421997-05-16 02:46:07 -05001790/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001791void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001792png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001793{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001794#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001795 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001796
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001797 /* start of interlace block */
1798 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001799
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001800 /* offset to next interlace block */
1801 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001802
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001803 /* start of interlace block in the y direction */
1804 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001805
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001806 /* offset to next interlace block in the y direction */
1807 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001808#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001809
Guy Schalnat0d580581995-07-20 02:43:20 -05001810 int ret;
1811
Andreas Dilger47a0c421997-05-16 02:46:07 -05001812 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001813 /* next row */
1814 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001815
Guy Schalnat0d580581995-07-20 02:43:20 -05001816 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001817 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001818 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001819
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001820#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001821 /* if interlaced, go to next pass */
1822 if (png_ptr->interlaced)
1823 {
1824 png_ptr->row_number = 0;
1825 if (png_ptr->transformations & PNG_INTERLACE)
1826 {
1827 png_ptr->pass++;
1828 }
1829 else
1830 {
1831 /* loop until we find a non-zero width or height pass */
1832 do
1833 {
1834 png_ptr->pass++;
1835 if (png_ptr->pass >= 7)
1836 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001837 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001838 png_pass_inc[png_ptr->pass] - 1 -
1839 png_pass_start[png_ptr->pass]) /
1840 png_pass_inc[png_ptr->pass];
1841 png_ptr->num_rows = (png_ptr->height +
1842 png_pass_yinc[png_ptr->pass] - 1 -
1843 png_pass_ystart[png_ptr->pass]) /
1844 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001845 if (png_ptr->transformations & PNG_INTERLACE)
1846 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001847 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1848
1849 }
1850
Guy Schalnate5a37791996-06-05 15:50:50 -05001851 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001852 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001853 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001854 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001855 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05001856 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
1857 png_ptr->usr_bit_depth,png_ptr->width))+1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001858 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001859 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001860 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001861#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001862
1863 /* if we get here, we've just written the last row, so we need
1864 to flush the compressor */
1865 do
1866 {
1867 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001868 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001869 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001870 if (ret == Z_OK)
1871 {
1872 /* check to see if we need more room */
1873 if (!(png_ptr->zstream.avail_out))
1874 {
1875 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1876 png_ptr->zstream.next_out = png_ptr->zbuf;
1877 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1878 }
1879 }
1880 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001881 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001882 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001883 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001884 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001885 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001886 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001887 } while (ret != Z_STREAM_END);
1888
1889 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001890 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001891 {
1892 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001893 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001894 }
1895
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001896 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -06001897 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat0d580581995-07-20 02:43:20 -05001898}
1899
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001900#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001901/* Pick out the correct pixels for the interlace pass.
1902 * The basic idea here is to go through the row with a source
1903 * pointer and a destination pointer (sp and dp), and copy the
1904 * correct pixels for the pass. As the row gets compacted,
1905 * sp will always be >= dp, so we should never overwrite anything.
1906 * See the default: case for the easiest code to understand.
1907 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001908void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001909png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001910{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001911#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001912 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001913
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001914 /* start of interlace block */
1915 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001916
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001917 /* offset to next interlace block */
1918 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001919#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001920
Andreas Dilger47a0c421997-05-16 02:46:07 -05001921 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001922 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001923#if defined(PNG_USELESS_TESTS_SUPPORTED)
1924 if (row != NULL && row_info != NULL && pass < 6)
1925#else
1926 if (pass < 6)
1927#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001928 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001929 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001930 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001931 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001932 case 1:
1933 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001934 png_bytep sp;
1935 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001936 int shift;
1937 int d;
1938 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001939 png_uint_32 i;
1940 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001941
1942 dp = row;
1943 d = 0;
1944 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001945 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001946 i += png_pass_inc[pass])
1947 {
1948 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001949 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001950 d |= (value << shift);
1951
1952 if (shift == 0)
1953 {
1954 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001955 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001956 d = 0;
1957 }
1958 else
1959 shift--;
1960
1961 }
1962 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001963 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001964 break;
1965 }
1966 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001967 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001968 png_bytep sp;
1969 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001970 int shift;
1971 int d;
1972 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001973 png_uint_32 i;
1974 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001975
1976 dp = row;
1977 shift = 6;
1978 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001979 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001980 i += png_pass_inc[pass])
1981 {
1982 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001983 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05001984 d |= (value << shift);
1985
1986 if (shift == 0)
1987 {
1988 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001989 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001990 d = 0;
1991 }
1992 else
1993 shift -= 2;
1994 }
1995 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001996 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001997 break;
1998 }
1999 case 4:
2000 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002001 png_bytep sp;
2002 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002003 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05002004 int d;
2005 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002006 png_uint_32 i;
2007 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002008
2009 dp = row;
2010 shift = 4;
2011 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002012 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002013 i += png_pass_inc[pass])
2014 {
2015 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002016 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05002017 d |= (value << shift);
2018
2019 if (shift == 0)
2020 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002021 shift = 4;
2022 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002023 d = 0;
2024 }
2025 else
2026 shift -= 4;
2027 }
2028 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002029 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002030 break;
2031 }
2032 default:
2033 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002034 png_bytep sp;
2035 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002036 png_uint_32 i;
2037 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002038 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002039
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002040 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002041 dp = row;
2042 /* find out how many bytes each pixel takes up */
2043 pixel_bytes = (row_info->pixel_depth >> 3);
2044 /* loop through the row, only looking at the pixels that
2045 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002046 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002047 i += png_pass_inc[pass])
2048 {
2049 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002050 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002051 /* move the pixel */
2052 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002053 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05002054 /* next pixel */
2055 dp += pixel_bytes;
2056 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002057 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002058 }
2059 }
2060 /* set new row width */
2061 row_info->width = (row_info->width +
2062 png_pass_inc[pass] - 1 -
2063 png_pass_start[pass]) /
2064 png_pass_inc[pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002065 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2066 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002067 }
2068}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002069#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002070
Andreas Dilger47a0c421997-05-16 02:46:07 -05002071/* This filters the row, chooses which filter to use, if it has not already
2072 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002073 * chosen filter.
2074 */
Glenn Randers-Pehrson78067772004-11-02 21:49:39 -06002075#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002076#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002077#define PNG_LOMASK ((png_uint_32)0xffffL)
2078#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002079void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002080png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002081{
Guy Schalnate5a37791996-06-05 15:50:50 -05002082 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002083 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002084 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002085 png_uint_32 row_bytes = row_info->rowbytes;
2086#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2087 int num_p_filters = (int)png_ptr->num_prev_filters;
2088#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002089
Andreas Dilger47a0c421997-05-16 02:46:07 -05002090 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05002091 /* find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002092 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002093
2094 prev_row = png_ptr->prev_row;
2095 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002096 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002097
Andreas Dilger47a0c421997-05-16 02:46:07 -05002098 /* The prediction method we use is to find which method provides the
2099 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002100 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002101 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002102 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002103 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002104 * predictive" method (not implemented yet), which does test compressions
2105 * of lines using different filter methods, and then chooses the
2106 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002107 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002108 *
2109 * GRR 980525: consider also
2110 * (1) minimum sum of absolute differences from running average (i.e.,
2111 * keep running sum of non-absolute differences & count of bytes)
2112 * [track dispersion, too? restart average if dispersion too large?]
2113 * (1b) minimum sum of absolute differences from sliding average, probably
2114 * with window size <= deflate window (usually 32K)
2115 * (2) minimum sum of squared differences from zero or running average
2116 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002117 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002118
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002119
Guy Schalnate5a37791996-06-05 15:50:50 -05002120 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002121 * that has been chosen, as it doesn't actually do anything to the data.
2122 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002123 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002124 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002125 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002126 png_bytep rp;
2127 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002128 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002129 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002130
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002131 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002132 {
2133 v = *rp;
2134 sum += (v < 128) ? v : 256 - v;
2135 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002136
2137#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2138 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2139 {
2140 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002141 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002142 sumlo = sum & PNG_LOMASK;
2143 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2144
2145 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002146 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002147 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002148 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002149 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002150 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002151 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002152 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002153 PNG_WEIGHT_SHIFT;
2154 }
2155 }
2156
2157 /* Factor in the cost of this filter (this is here for completeness,
2158 * but it makes no sense to have a "cost" for the NONE filter, as
2159 * it has the minimum possible computational cost - none).
2160 */
2161 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2162 PNG_COST_SHIFT;
2163 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2164 PNG_COST_SHIFT;
2165
2166 if (sumhi > PNG_HIMASK)
2167 sum = PNG_MAXSUM;
2168 else
2169 sum = (sumhi << PNG_HISHIFT) + sumlo;
2170 }
2171#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002172 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002173 }
2174
Guy Schalnate5a37791996-06-05 15:50:50 -05002175 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002176 if (filter_to_do == PNG_FILTER_SUB)
2177 /* it's the only filter so no testing is needed */
2178 {
2179 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002180 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002181 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2182 i++, rp++, dp++)
2183 {
2184 *dp = *rp;
2185 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002186 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002187 i++, rp++, lp++, dp++)
2188 {
2189 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2190 }
2191 best_row = png_ptr->sub_row;
2192 }
2193
2194 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002195 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002196 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002197 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002198 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002199 int v;
2200
2201#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002202 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002203 * would reduce the sum of this filter, so that we can do the
2204 * early exit comparison without scaling the sum each time.
2205 */
2206 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2207 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002208 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002209 png_uint_32 lmhi, lmlo;
2210 lmlo = lmins & PNG_LOMASK;
2211 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2212
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002213 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002214 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002215 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002216 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002217 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002218 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002219 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002220 PNG_WEIGHT_SHIFT;
2221 }
2222 }
2223
2224 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2225 PNG_COST_SHIFT;
2226 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2227 PNG_COST_SHIFT;
2228
2229 if (lmhi > PNG_HIMASK)
2230 lmins = PNG_MAXSUM;
2231 else
2232 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2233 }
2234#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002235
Guy Schalnate5a37791996-06-05 15:50:50 -05002236 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2237 i++, rp++, dp++)
2238 {
2239 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002240
Guy Schalnate5a37791996-06-05 15:50:50 -05002241 sum += (v < 128) ? v : 256 - v;
2242 }
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002243 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002244 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002245 {
2246 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002247
Guy Schalnate5a37791996-06-05 15:50:50 -05002248 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002249
2250 if (sum > lmins) /* We are already worse, don't continue. */
2251 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002252 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002253
2254#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2255 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2256 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002257 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002258 png_uint_32 sumhi, sumlo;
2259 sumlo = sum & PNG_LOMASK;
2260 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2261
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002262 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002263 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002264 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002265 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002266 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002267 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002268 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002269 PNG_WEIGHT_SHIFT;
2270 }
2271 }
2272
2273 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2274 PNG_COST_SHIFT;
2275 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2276 PNG_COST_SHIFT;
2277
2278 if (sumhi > PNG_HIMASK)
2279 sum = PNG_MAXSUM;
2280 else
2281 sum = (sumhi << PNG_HISHIFT) + sumlo;
2282 }
2283#endif
2284
Guy Schalnate5a37791996-06-05 15:50:50 -05002285 if (sum < mins)
2286 {
2287 mins = sum;
2288 best_row = png_ptr->sub_row;
2289 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002290 }
2291
Guy Schalnate5a37791996-06-05 15:50:50 -05002292 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002293 if (filter_to_do == PNG_FILTER_UP)
2294 {
2295 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002296 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002297
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002298 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002299 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002300 i++, rp++, pp++, dp++)
2301 {
2302 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2303 }
2304 best_row = png_ptr->up_row;
2305 }
2306
2307 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002308 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002309 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002310 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002311 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002312 int v;
2313
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002314
Andreas Dilger47a0c421997-05-16 02:46:07 -05002315#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2316 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2317 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002318 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002319 png_uint_32 lmhi, lmlo;
2320 lmlo = lmins & PNG_LOMASK;
2321 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2322
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002323 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002324 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002325 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002326 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002327 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002328 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002329 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002330 PNG_WEIGHT_SHIFT;
2331 }
2332 }
2333
2334 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2335 PNG_COST_SHIFT;
2336 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2337 PNG_COST_SHIFT;
2338
2339 if (lmhi > PNG_HIMASK)
2340 lmins = PNG_MAXSUM;
2341 else
2342 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2343 }
2344#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002345
2346 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002347 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002348 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002349 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002350
2351 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002352
2353 if (sum > lmins) /* We are already worse, don't continue. */
2354 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002355 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002356
2357#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2358 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2359 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002360 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002361 png_uint_32 sumhi, sumlo;
2362 sumlo = sum & PNG_LOMASK;
2363 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2364
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002365 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002366 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002367 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002368 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002369 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002370 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002371 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002372 PNG_WEIGHT_SHIFT;
2373 }
2374 }
2375
2376 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2377 PNG_COST_SHIFT;
2378 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2379 PNG_COST_SHIFT;
2380
2381 if (sumhi > PNG_HIMASK)
2382 sum = PNG_MAXSUM;
2383 else
2384 sum = (sumhi << PNG_HISHIFT) + sumlo;
2385 }
2386#endif
2387
Guy Schalnate5a37791996-06-05 15:50:50 -05002388 if (sum < mins)
2389 {
2390 mins = sum;
2391 best_row = png_ptr->up_row;
2392 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002393 }
2394
Guy Schalnate5a37791996-06-05 15:50:50 -05002395 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002396 if (filter_to_do == PNG_FILTER_AVG)
2397 {
2398 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002399 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002400 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002401 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002402 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002403 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002404 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002405 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002406 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002407 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2408 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002409 }
2410 best_row = png_ptr->avg_row;
2411 }
2412
2413 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002414 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002415 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002416 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002417 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002418 int v;
2419
2420#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2421 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2422 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002423 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002424 png_uint_32 lmhi, lmlo;
2425 lmlo = lmins & PNG_LOMASK;
2426 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2427
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002428 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002429 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002430 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002431 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002432 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002433 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002434 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002435 PNG_WEIGHT_SHIFT;
2436 }
2437 }
2438
2439 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2440 PNG_COST_SHIFT;
2441 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2442 PNG_COST_SHIFT;
2443
2444 if (lmhi > PNG_HIMASK)
2445 lmins = PNG_MAXSUM;
2446 else
2447 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2448 }
2449#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002450
2451 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002452 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002453 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002454 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002455
2456 sum += (v < 128) ? v : 256 - v;
2457 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002458 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002459 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002460 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002461 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002462
2463 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002464
2465 if (sum > lmins) /* We are already worse, don't continue. */
2466 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002467 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002468
2469#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2470 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2471 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002472 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002473 png_uint_32 sumhi, sumlo;
2474 sumlo = sum & PNG_LOMASK;
2475 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2476
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002477 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002478 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002479 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002480 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002481 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002482 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002483 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002484 PNG_WEIGHT_SHIFT;
2485 }
2486 }
2487
2488 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2489 PNG_COST_SHIFT;
2490 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2491 PNG_COST_SHIFT;
2492
2493 if (sumhi > PNG_HIMASK)
2494 sum = PNG_MAXSUM;
2495 else
2496 sum = (sumhi << PNG_HISHIFT) + sumlo;
2497 }
2498#endif
2499
Guy Schalnate5a37791996-06-05 15:50:50 -05002500 if (sum < mins)
2501 {
2502 mins = sum;
2503 best_row = png_ptr->avg_row;
2504 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002505 }
2506
Andreas Dilger47a0c421997-05-16 02:46:07 -05002507 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002508 if (filter_to_do == PNG_FILTER_PAETH)
2509 {
2510 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002511 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002512 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002513 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002514 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002515 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002516 }
2517
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002518 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002519 {
2520 int a, b, c, pa, pb, pc, p;
2521
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002522 b = *pp++;
2523 c = *cp++;
2524 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002525
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002526 p = b - c;
2527 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002528
2529#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002530 pa = abs(p);
2531 pb = abs(pc);
2532 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002533#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002534 pa = p < 0 ? -p : p;
2535 pb = pc < 0 ? -pc : pc;
2536 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002537#endif
2538
2539 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2540
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002541 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002542 }
2543 best_row = png_ptr->paeth_row;
2544 }
2545
2546 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002547 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002548 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002549 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002550 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002551 int v;
2552
2553#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2554 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2555 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002556 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002557 png_uint_32 lmhi, lmlo;
2558 lmlo = lmins & PNG_LOMASK;
2559 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2560
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002561 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002562 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002563 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002564 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002565 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002566 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002567 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002568 PNG_WEIGHT_SHIFT;
2569 }
2570 }
2571
2572 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2573 PNG_COST_SHIFT;
2574 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2575 PNG_COST_SHIFT;
2576
2577 if (lmhi > PNG_HIMASK)
2578 lmins = PNG_MAXSUM;
2579 else
2580 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2581 }
2582#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002583
2584 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002585 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002586 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002587 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002588
2589 sum += (v < 128) ? v : 256 - v;
2590 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002591
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002592 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002593 {
2594 int a, b, c, pa, pb, pc, p;
2595
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002596 b = *pp++;
2597 c = *cp++;
2598 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002599
2600#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002601 p = b - c;
2602 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002603#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002604 pa = abs(p);
2605 pb = abs(pc);
2606 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002607#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002608 pa = p < 0 ? -p : p;
2609 pb = pc < 0 ? -pc : pc;
2610 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002611#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002612 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2613#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002614 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002615 pa = abs(p - a);
2616 pb = abs(p - b);
2617 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002618 if (pa <= pb && pa <= pc)
2619 p = a;
2620 else if (pb <= pc)
2621 p = b;
2622 else
2623 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002624#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002625
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002626 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002627
2628 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002629
2630 if (sum > lmins) /* We are already worse, don't continue. */
2631 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002632 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002633
2634#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2635 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2636 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002637 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002638 png_uint_32 sumhi, sumlo;
2639 sumlo = sum & PNG_LOMASK;
2640 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2641
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002642 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002643 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002644 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002645 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002646 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002647 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002648 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002649 PNG_WEIGHT_SHIFT;
2650 }
2651 }
2652
2653 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2654 PNG_COST_SHIFT;
2655 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2656 PNG_COST_SHIFT;
2657
2658 if (sumhi > PNG_HIMASK)
2659 sum = PNG_MAXSUM;
2660 else
2661 sum = (sumhi << PNG_HISHIFT) + sumlo;
2662 }
2663#endif
2664
Guy Schalnate5a37791996-06-05 15:50:50 -05002665 if (sum < mins)
2666 {
2667 best_row = png_ptr->paeth_row;
2668 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002669 }
2670
Andreas Dilger47a0c421997-05-16 02:46:07 -05002671 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002672
Guy Schalnate5a37791996-06-05 15:50:50 -05002673 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002674
2675#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2676 /* Save the type of filter we picked this time for future calculations */
2677 if (png_ptr->num_prev_filters > 0)
2678 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002679 int j;
2680 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002681 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002682 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002683 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002684 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002685 }
2686#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002687}
2688
2689
Andreas Dilger47a0c421997-05-16 02:46:07 -05002690/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002691void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002692png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2693{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002694 png_debug(1, "in png_write_filtered_row\n");
2695 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002696 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002697
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002698 png_ptr->zstream.next_in = filtered_row;
2699 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002700 /* repeat until we have compressed all the data */
2701 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002702 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002703 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002704
Guy Schalnate5a37791996-06-05 15:50:50 -05002705 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002706 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002707 /* check for compression errors */
2708 if (ret != Z_OK)
2709 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002710 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002711 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002712 else
2713 png_error(png_ptr, "zlib error");
2714 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002715
Guy Schalnate5a37791996-06-05 15:50:50 -05002716 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002717 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002718 {
2719 /* write the IDAT and reset the zlib output buffer */
2720 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002721 png_ptr->zstream.next_out = png_ptr->zbuf;
2722 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002723 }
2724 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002725 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002726
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002727 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002728 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002729 {
2730 png_bytep tptr;
2731
2732 tptr = png_ptr->prev_row;
2733 png_ptr->prev_row = png_ptr->row_buf;
2734 png_ptr->row_buf = tptr;
2735 }
2736
Guy Schalnate5a37791996-06-05 15:50:50 -05002737 /* finish row - updates counters and flushes zlib if last row */
2738 png_write_finish_row(png_ptr);
2739
2740#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2741 png_ptr->flush_rows++;
2742
2743 if (png_ptr->flush_dist > 0 &&
2744 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002745 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002746 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002747 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002748#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002749}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05002750#endif /* PNG_WRITE_SUPPORTED */