blob: 7bb2590c3253c392f63e818fbe4b27b269bb3cda [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-Pehrson145f5c82008-07-10 09:13:13 -05004 * Last changed in libpng 1.4.0 [July 10, 2008]
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05006 * Copyright (c) 1998-2008 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#include "png.h"
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050012#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050013#include "pngpriv.h"
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-Pehrson86dc9812006-05-10 07:27:44 -050028#if defined(PNG_SAVE_INT_32_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}
Glenn Randers-Pehrson86dc9812006-05-10 07:27:44 -050041#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050042
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
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050054/* Simple function to write the signature. If we have already written
55 * the magic bytes of the signature, or more likely, the PNG stream is
56 * being embedded into another stream and doesn't need its own signature,
57 * we should call png_set_sig_bytes() to tell libpng how many of the
58 * bytes have already been written.
59 */
60void PNGAPI
61png_write_sig(png_structp png_ptr)
62{
63 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050064
65#ifdef PNG_IO_STATE_SUPPORTED
66 /* inform the I/O callback that the signature is being written */
67 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_SIGNATURE;
68#endif
69
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050070 /* write the rest of the 8 byte signature */
71 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050072 (png_size_t)(8 - png_ptr->sig_bytes));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050073 if (png_ptr->sig_bytes < 3)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050074 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
75}
76
Andreas Dilger47a0c421997-05-16 02:46:07 -050077/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060078 * representing the chunk name. The array must be at least 4 bytes in
79 * length, and does not need to be null terminated. To be safe, pass the
80 * pre-defined chunk names here, and if you need a new one, define it
81 * where the others are defined. The length is the length of the data.
82 * All the data must be present. If that is not possible, use the
83 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
84 * functions instead.
85 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050086void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060087png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050088 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050089{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050090 if (png_ptr == NULL) return;
Andreas Dilger47a0c421997-05-16 02:46:07 -050091 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050092 png_write_chunk_data(png_ptr, data, (png_size_t)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060093 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050094}
95
Andreas Dilger47a0c421997-05-16 02:46:07 -050096/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060097 * The total_length is the sum of the lengths of all the data you will be
98 * passing in png_write_chunk_data().
99 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500100void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600101png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
102 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500103{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500104 png_byte buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500105
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500106#ifdef PNG_IO_STATE_SUPPORTED
107 /* Inform the I/O callback that the chunk header is being written.
108 * PNG_IO_CHUNK_HDR requires a single I/O call.
109 */
110 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR;
111#endif
112
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500113 png_debug2(0, "Writing %s chunk, length = %lu\n", chunk_name,
114 (unsigned long)length);
115 if (png_ptr == NULL) return;
116
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500117 /* write the length and the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500118 png_save_uint_32(buf, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500119 png_memcpy(buf + 4, chunk_name, 4);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500120 png_write_data(png_ptr, buf, (png_size_t)8);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500121 /* put the chunk name into png_ptr->chunk_name */
122 png_memcpy(png_ptr->chunk_name, chunk_name, 4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500123 /* reset the crc and run it over the chunk name */
124 png_reset_crc(png_ptr);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500125 png_calculate_crc(png_ptr, chunk_name, 4);
126
127#ifdef PNG_IO_STATE_SUPPORTED
128 /* Inform the I/O callback that chunk data will (possibly) be written.
129 * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.
130 */
131 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA;
132#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500133}
134
Andreas Dilger47a0c421997-05-16 02:46:07 -0500135/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600136 * Note that multiple calls to this function are allowed, and that the
137 * sum of the lengths from these calls *must* add up to the total_length
138 * given to png_write_chunk_start().
139 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500140void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500141png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500142{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500143 /* write the data, and run the CRC over it */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500144 if (png_ptr == NULL) return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500145 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500146 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600147 png_write_data(png_ptr, data, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500148 /* update the CRC after writing the data,
149 * in case that the user I/O routine alters it.
150 */
151 png_calculate_crc(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500152 }
153}
154
Andreas Dilger47a0c421997-05-16 02:46:07 -0500155/* Finish a chunk started with png_write_chunk_start(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500156void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600157png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500158{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500159 png_byte buf[4];
160
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500161 if (png_ptr == NULL) return;
162
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500163#ifdef PNG_IO_STATE_SUPPORTED
164 /* Inform the I/O callback that the chunk CRC is being written.
165 * PNG_IO_CHUNK_CRC requires a single I/O function call.
166 */
167 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC;
168#endif
169
170 /* write the crc in a single operation */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500171 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500172
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500173 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500174}
175
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600176#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600177/*
178 * This pair of functions encapsulates the operation of (a) compressing a
179 * text string, and (b) issuing it later as a series of chunk data writes.
180 * The compression_state structure is shared context for these functions
181 * set up by the caller in order to make the whole mess thread-safe.
182 */
183
184typedef struct
185{
186 char *input; /* the uncompressed input data */
187 int input_len; /* its length */
188 int num_output_ptr; /* number of output pointers used */
189 int max_output_ptr; /* size of output_ptr */
190 png_charpp output_ptr; /* array of pointers to output */
191} compression_state;
192
193/* compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500194static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600195png_text_compress(png_structp png_ptr,
196 png_charp text, png_size_t text_len, int compression,
197 compression_state *comp)
198{
199 int ret;
200
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600201 comp->num_output_ptr = 0;
202 comp->max_output_ptr = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600203 comp->output_ptr = NULL;
204 comp->input = NULL;
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600205 comp->input_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600206
207 /* we may just want to pass the text right through */
208 if (compression == PNG_TEXT_COMPRESSION_NONE)
209 {
210 comp->input = text;
211 comp->input_len = text_len;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500212 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600213 }
214
215 if (compression >= PNG_TEXT_COMPRESSION_LAST)
216 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500217#if !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600218 char msg[50];
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500219 png_snprintf(msg, 50, "Unknown compression type %d", compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600220 png_warning(png_ptr, msg);
221#else
222 png_warning(png_ptr, "Unknown compression type");
223#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600224 }
225
226 /* We can't write the chunk until we find out how much data we have,
227 * which means we need to run the compressor first and save the
228 * output. This shouldn't be a problem, as the vast majority of
229 * comments should be reasonable, but we will set up an array of
230 * malloc'd pointers to be sure.
231 *
232 * If we knew the application was well behaved, we could simplify this
233 * greatly by assuming we can always malloc an output buffer large
234 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
235 * and malloc this directly. The only time this would be a bad idea is
236 * if we can't malloc more than 64K and we have 64K of random input
237 * data, or if the input string is incredibly large (although this
238 * wouldn't cause a failure, just a slowdown due to swapping).
239 */
240
241 /* set up the compression buffers */
242 png_ptr->zstream.avail_in = (uInt)text_len;
243 png_ptr->zstream.next_in = (Bytef *)text;
244 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
245 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
246
247 /* this is the same compression loop as in png_write_row() */
248 do
249 {
250 /* compress the data */
251 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
252 if (ret != Z_OK)
253 {
254 /* error */
255 if (png_ptr->zstream.msg != NULL)
256 png_error(png_ptr, png_ptr->zstream.msg);
257 else
258 png_error(png_ptr, "zlib error");
259 }
260 /* check to see if we need more room */
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600261 if (!(png_ptr->zstream.avail_out))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600262 {
263 /* make sure the output array has room */
264 if (comp->num_output_ptr >= comp->max_output_ptr)
265 {
266 int old_max;
267
268 old_max = comp->max_output_ptr;
269 comp->max_output_ptr = comp->num_output_ptr + 4;
270 if (comp->output_ptr != NULL)
271 {
272 png_charpp old_ptr;
273
274 old_ptr = comp->output_ptr;
275 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500276 (png_size_t)
277 (comp->max_output_ptr * png_sizeof(png_charpp)));
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500278 png_memcpy(comp->output_ptr, old_ptr, old_max
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500279 * png_sizeof(png_charp));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600280 png_free(png_ptr, old_ptr);
281 }
282 else
283 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500284 (png_size_t)
285 (comp->max_output_ptr * png_sizeof(png_charp)));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600286 }
287
288 /* save the data */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500289 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500290 (png_charp)png_malloc(png_ptr,
291 png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500292 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
293 png_ptr->zbuf_size);
294 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600295
296 /* and reset the buffer */
297 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
298 png_ptr->zstream.next_out = png_ptr->zbuf;
299 }
300 /* continue until we don't have any more to compress */
301 } while (png_ptr->zstream.avail_in);
302
303 /* finish the compression */
304 do
305 {
306 /* tell zlib we are finished */
307 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500308
309 if (ret == Z_OK)
310 {
311 /* check to see if we need more room */
312 if (!(png_ptr->zstream.avail_out))
313 {
314 /* check to make sure our output array has room */
315 if (comp->num_output_ptr >= comp->max_output_ptr)
316 {
317 int old_max;
318
319 old_max = comp->max_output_ptr;
320 comp->max_output_ptr = comp->num_output_ptr + 4;
321 if (comp->output_ptr != NULL)
322 {
323 png_charpp old_ptr;
324
325 old_ptr = comp->output_ptr;
326 /* This could be optimized to realloc() */
327 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500328 (png_size_t)(comp->max_output_ptr *
329 png_sizeof(png_charp)));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500330 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500331 old_max * png_sizeof(png_charp));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500332 png_free(png_ptr, old_ptr);
333 }
334 else
335 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500336 (png_size_t)(comp->max_output_ptr *
337 png_sizeof(png_charp)));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500338 }
339
340 /* save off the data */
341 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500342 (png_charp)png_malloc(png_ptr,
343 (png_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500344 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
345 png_ptr->zbuf_size);
346 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500347
348 /* and reset the buffer pointers */
349 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
350 png_ptr->zstream.next_out = png_ptr->zbuf;
351 }
352 }
353 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600354 {
355 /* we got an error */
356 if (png_ptr->zstream.msg != NULL)
357 png_error(png_ptr, png_ptr->zstream.msg);
358 else
359 png_error(png_ptr, "zlib error");
360 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600361 } while (ret != Z_STREAM_END);
362
363 /* text length is number of buffers plus last buffer */
364 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
365 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
366 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
367
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500368 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600369}
370
371/* ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500372static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600373png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
374{
375 int i;
376
377 /* handle the no-compression case */
378 if (comp->input)
379 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500380 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
381 (png_size_t)comp->input_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600382 return;
383 }
384
385 /* write saved output buffers, if any */
386 for (i = 0; i < comp->num_output_ptr; i++)
387 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500388 png_write_chunk_data(png_ptr, (png_bytep)comp->output_ptr[i],
389 (png_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600390 png_free(png_ptr, comp->output_ptr[i]);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500391 comp->output_ptr[i]=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600392 }
393 if (comp->max_output_ptr != 0)
394 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500395 comp->output_ptr=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600396 /* write anything left in zbuf */
397 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
398 png_write_chunk_data(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500399 (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600400
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600401 /* reset zlib for another zTXt/iTXt or image data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600402 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600403 png_ptr->zstream.data_type = Z_BINARY;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600404}
405#endif
406
Guy Schalnat0d580581995-07-20 02:43:20 -0500407/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600408 * information. Note that the rest of this code depends upon this
409 * information being correct.
410 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500411void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600412png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500413 int bit_depth, int color_type, int compression_type, int filter_type,
414 int interlace_type)
415{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600416#ifdef PNG_USE_LOCAL_ARRAYS
417 PNG_IHDR;
418#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500419 int ret;
420
Guy Schalnat0d580581995-07-20 02:43:20 -0500421 png_byte buf[13]; /* buffer to store the IHDR info */
422
Andreas Dilger47a0c421997-05-16 02:46:07 -0500423 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500424 /* Check that we have valid input data from the application info */
425 switch (color_type)
426 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500427 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500428 switch (bit_depth)
429 {
430 case 1:
431 case 2:
432 case 4:
433 case 8:
434 case 16: png_ptr->channels = 1; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500435 default: png_error(png_ptr, "Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500436 }
437 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500438 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500439 if (bit_depth != 8 && bit_depth != 16)
440 png_error(png_ptr, "Invalid bit depth for RGB image");
441 png_ptr->channels = 3;
442 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500443 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500444 switch (bit_depth)
445 {
446 case 1:
447 case 2:
448 case 4:
449 case 8: png_ptr->channels = 1; break;
450 default: png_error(png_ptr, "Invalid bit depth for paletted image");
451 }
452 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500453 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500454 if (bit_depth != 8 && bit_depth != 16)
455 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
456 png_ptr->channels = 2;
457 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500458 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500459 if (bit_depth != 8 && bit_depth != 16)
460 png_error(png_ptr, "Invalid bit depth for RGBA image");
461 png_ptr->channels = 4;
462 break;
463 default:
464 png_error(png_ptr, "Invalid image color type specified");
465 }
466
Andreas Dilger47a0c421997-05-16 02:46:07 -0500467 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500468 {
469 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500470 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500471 }
472
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600473 /* Write filter_method 64 (intrapixel differencing) only if
474 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
475 * 2. Libpng did not write a PNG signature (this filter_method is only
476 * used in PNG datastreams that are embedded in MNG datastreams) and
477 * 3. The application called png_permit_mng_features with a mask that
478 * included PNG_FLAG_MNG_FILTER_64 and
479 * 4. The filter_method is 64 and
480 * 5. The color_type is RGB or RGBA
481 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600482 if (
483#if defined(PNG_MNG_FEATURES_SUPPORTED)
484 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600485 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500486 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600487 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600488 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
489#endif
490 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500491 {
492 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500493 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500494 }
495
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600496#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500497 if (interlace_type != PNG_INTERLACE_NONE &&
498 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500499 {
500 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500501 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500502 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600503#else
504 interlace_type=PNG_INTERLACE_NONE;
505#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500506
507 /* save off the relevent information */
508 png_ptr->bit_depth = (png_byte)bit_depth;
509 png_ptr->color_type = (png_byte)color_type;
510 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500511#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600512 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500513#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500514 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500515 png_ptr->width = width;
516 png_ptr->height = height;
517
518 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500519 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
Guy Schalnate5a37791996-06-05 15:50:50 -0500520 /* set the usr info, so any transformations can modify it */
521 png_ptr->usr_width = png_ptr->width;
522 png_ptr->usr_bit_depth = png_ptr->bit_depth;
523 png_ptr->usr_channels = png_ptr->channels;
524
Guy Schalnat0d580581995-07-20 02:43:20 -0500525 /* pack the header information into the buffer */
526 png_save_uint_32(buf, width);
527 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600528 buf[8] = (png_byte)bit_depth;
529 buf[9] = (png_byte)color_type;
530 buf[10] = (png_byte)compression_type;
531 buf[11] = (png_byte)filter_type;
532 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500533
534 /* write the chunk */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500535 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500536
Andreas Dilger47a0c421997-05-16 02:46:07 -0500537 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600538 png_ptr->zstream.zalloc = png_zalloc;
539 png_ptr->zstream.zfree = png_zfree;
540 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500541 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500542 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500543 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
544 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500545 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500546 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500547 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500548 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500549 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500550 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500551 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500552 png_ptr->zlib_strategy = Z_FILTERED;
553 else
554 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
555 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500556 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500557 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500558 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500559 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500560 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500561 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500562 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500563 png_ptr->zlib_method = 8;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500564 ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
565 png_ptr->zlib_method, png_ptr->zlib_window_bits,
566 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
567 if (ret != Z_OK)
568 {
569 if (ret == Z_VERSION_ERROR) png_error(png_ptr,
570 "zlib failed to initialize compressor -- version error");
571 if (ret == Z_STREAM_ERROR) png_error(png_ptr,
572 "zlib failed to initialize compressor -- stream error");
573 if (ret == Z_MEM_ERROR) png_error(png_ptr,
574 "zlib failed to initialize compressor -- mem error");
575 png_error(png_ptr, "zlib failed to initialize compressor");
576 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600577 png_ptr->zstream.next_out = png_ptr->zbuf;
578 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600579 /* libpng is not interested in zstream.data_type */
580 /* set it to a predefined value, to avoid its evaluation inside zlib */
581 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500582
Guy Schalnate5a37791996-06-05 15:50:50 -0500583 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500584}
585
586/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500587 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600588 * structure.
589 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500590void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500591png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500592{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600593#ifdef PNG_USE_LOCAL_ARRAYS
594 PNG_PLTE;
595#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500596 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600597 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500598 png_byte buf[3];
599
Andreas Dilger47a0c421997-05-16 02:46:07 -0500600 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500601 if ((
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500602#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600603 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500604#endif
605 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500606 {
607 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500608 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500609 png_error(png_ptr, "Invalid number of colors in palette");
610 }
611 else
612 {
613 png_warning(png_ptr, "Invalid number of colors in palette");
614 return;
615 }
616 }
617
618 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
619 {
620 png_warning(png_ptr,
621 "Ignoring request to write a PLTE chunk in grayscale PNG");
622 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500623 }
624
Andreas Dilger47a0c421997-05-16 02:46:07 -0500625 png_ptr->num_palette = (png_uint_16)num_pal;
626 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500627
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500628 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE,
629 (png_uint_32)(num_pal * 3));
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500630#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500631 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500632 {
633 buf[0] = pal_ptr->red;
634 buf[1] = pal_ptr->green;
635 buf[2] = pal_ptr->blue;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500636 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500637 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500638#else
639 /* This is a little slower but some buggy compilers need to do this instead */
640 pal_ptr=palette;
641 for (i = 0; i < num_pal; i++)
642 {
643 buf[0] = pal_ptr[i].red;
644 buf[1] = pal_ptr[i].green;
645 buf[2] = pal_ptr[i].blue;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500646 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500647 }
648#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500649 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500650 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500651}
652
653/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500654void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500655png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500656{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600657#ifdef PNG_USE_LOCAL_ARRAYS
658 PNG_IDAT;
659#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500660 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500661
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500662 /* Optimize the CMF field in the zlib stream. */
663 /* This hack of the zlib stream is compliant to the stream specification. */
664 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
665 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
666 {
667 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
668 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
669 {
670 /* Avoid memory underflows and multiplication overflows. */
671 /* The conditions below are practically always satisfied;
672 however, they still must be checked. */
673 if (length >= 2 &&
674 png_ptr->height < 16384 && png_ptr->width < 16384)
675 {
676 png_uint_32 uncompressed_idat_size = png_ptr->height *
677 ((png_ptr->width *
678 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
679 unsigned int z_cinfo = z_cmf >> 4;
680 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
681 while (uncompressed_idat_size <= half_z_window_size &&
682 half_z_window_size >= 256)
683 {
684 z_cinfo--;
685 half_z_window_size >>= 1;
686 }
687 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
688 if (data[0] != (png_byte)z_cmf)
689 {
690 data[0] = (png_byte)z_cmf;
691 data[1] &= 0xe0;
692 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
693 }
694 }
695 }
696 else
697 png_error(png_ptr,
698 "Invalid zlib compression method or flags in IDAT");
699 }
700
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600701 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500702 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500703}
704
705/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500706void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600707png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500708{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600709#ifdef PNG_USE_LOCAL_ARRAYS
710 PNG_IEND;
711#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500712 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500713 png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL,
714 (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600715 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500716}
717
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500718#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500719/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600720#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500721void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500722png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500723{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600724#ifdef PNG_USE_LOCAL_ARRAYS
725 PNG_gAMA;
726#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500727 png_uint_32 igamma;
728 png_byte buf[4];
729
Andreas Dilger47a0c421997-05-16 02:46:07 -0500730 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600731 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600732 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500733 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500734 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500735}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500736#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600737#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500738void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600739png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600740{
741#ifdef PNG_USE_LOCAL_ARRAYS
742 PNG_gAMA;
743#endif
744 png_byte buf[4];
745
746 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600747 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500748 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500749 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600750}
751#endif
752#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500753
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600754#if defined(PNG_WRITE_sRGB_SUPPORTED)
755/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500756void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600757png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600758{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600759#ifdef PNG_USE_LOCAL_ARRAYS
760 PNG_sRGB;
761#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600762 png_byte buf[1];
763
764 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500765 if (srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600766 png_warning(png_ptr,
767 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600768 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500769 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600770}
771#endif
772
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600773#if defined(PNG_WRITE_iCCP_SUPPORTED)
774/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500775void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600776png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
777 png_charp profile, int profile_len)
778{
779#ifdef PNG_USE_LOCAL_ARRAYS
780 PNG_iCCP;
781#endif
782 png_size_t name_len;
783 png_charp new_name;
784 compression_state comp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500785 int embedded_profile_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600786
787 png_debug(1, "in png_write_iCCP\n");
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600788
789 comp.num_output_ptr = 0;
790 comp.max_output_ptr = 0;
791 comp.output_ptr = NULL;
792 comp.input = NULL;
793 comp.input_len = 0;
794
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600795 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
796 &new_name)) == 0)
797 {
798 png_warning(png_ptr, "Empty keyword in iCCP chunk");
799 return;
800 }
801
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600802 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600803 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600804
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500805 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600806 profile_len = 0;
807
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500808 if (profile_len > 3)
Glenn Randers-Pehrson7edd4582006-12-07 19:16:44 -0600809 embedded_profile_len =
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500810 ((*( (png_bytep)profile ))<<24) |
811 ((*( (png_bytep)profile + 1))<<16) |
812 ((*( (png_bytep)profile + 2))<< 8) |
813 ((*( (png_bytep)profile + 3)) );
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500814
815 if (profile_len < embedded_profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500816 {
817 png_warning(png_ptr,
818 "Embedded profile length too large in iCCP chunk");
819 return;
820 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500821
822 if (profile_len > embedded_profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500823 {
824 png_warning(png_ptr,
825 "Truncating profile to actual length in iCCP chunk");
826 profile_len = embedded_profile_len;
827 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500828
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600829 if (profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500830 profile_len = png_text_compress(png_ptr, profile,
831 (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600832
833 /* make sure we include the NULL after the name and the compression type */
834 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500835 (png_uint_32)(name_len + profile_len + 2));
836 new_name[name_len + 1] = 0x00;
837 png_write_chunk_data(png_ptr, (png_bytep)new_name,
838 (png_size_t)(name_len + 2));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600839
840 if (profile_len)
841 png_write_compressed_data_out(png_ptr, &comp);
842
843 png_write_chunk_end(png_ptr);
844 png_free(png_ptr, new_name);
845}
846#endif
847
848#if defined(PNG_WRITE_sPLT_SUPPORTED)
849/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500850void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600851png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600852{
853#ifdef PNG_USE_LOCAL_ARRAYS
854 PNG_sPLT;
855#endif
856 png_size_t name_len;
857 png_charp new_name;
858 png_byte entrybuf[10];
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500859 png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
860 png_size_t palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600861 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500862#ifdef PNG_NO_POINTER_INDEXING
863 int i;
864#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600865
866 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600867 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
868 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600869 {
870 png_warning(png_ptr, "Empty keyword in sPLT chunk");
871 return;
872 }
873
874 /* make sure we include the NULL after the name */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500875 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500876 (png_uint_32)(name_len + 2 + palette_size));
877 png_write_chunk_data(png_ptr, (png_bytep)new_name,
878 (png_size_t)(name_len + 1));
879 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600880
881 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500882#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500883 for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600884 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500885 if (spalette->depth == 8)
886 {
887 entrybuf[0] = (png_byte)ep->red;
888 entrybuf[1] = (png_byte)ep->green;
889 entrybuf[2] = (png_byte)ep->blue;
890 entrybuf[3] = (png_byte)ep->alpha;
891 png_save_uint_16(entrybuf + 4, ep->frequency);
892 }
893 else
894 {
895 png_save_uint_16(entrybuf + 0, ep->red);
896 png_save_uint_16(entrybuf + 2, ep->green);
897 png_save_uint_16(entrybuf + 4, ep->blue);
898 png_save_uint_16(entrybuf + 6, ep->alpha);
899 png_save_uint_16(entrybuf + 8, ep->frequency);
900 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500901 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600902 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500903#else
904 ep=spalette->entries;
905 for (i=0; i>spalette->nentries; i++)
906 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500907 if (spalette->depth == 8)
908 {
909 entrybuf[0] = (png_byte)ep[i].red;
910 entrybuf[1] = (png_byte)ep[i].green;
911 entrybuf[2] = (png_byte)ep[i].blue;
912 entrybuf[3] = (png_byte)ep[i].alpha;
913 png_save_uint_16(entrybuf + 4, ep[i].frequency);
914 }
915 else
916 {
917 png_save_uint_16(entrybuf + 0, ep[i].red);
918 png_save_uint_16(entrybuf + 2, ep[i].green);
919 png_save_uint_16(entrybuf + 4, ep[i].blue);
920 png_save_uint_16(entrybuf + 6, ep[i].alpha);
921 png_save_uint_16(entrybuf + 8, ep[i].frequency);
922 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500923 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500924 }
925#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600926
927 png_write_chunk_end(png_ptr);
928 png_free(png_ptr, new_name);
929}
930#endif
931
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500932#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500933/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500934void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600935png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500936{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600937#ifdef PNG_USE_LOCAL_ARRAYS
938 PNG_sBIT;
939#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500940 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500941 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500942
Andreas Dilger47a0c421997-05-16 02:46:07 -0500943 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600944 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500945 if (color_type & PNG_COLOR_MASK_COLOR)
946 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600947 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500948
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500949 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
950 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600951 if (sbit->red == 0 || sbit->red > maxbits ||
952 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500953 sbit->blue == 0 || sbit->blue > maxbits)
954 {
955 png_warning(png_ptr, "Invalid sBIT depth specified");
956 return;
957 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500958 buf[0] = sbit->red;
959 buf[1] = sbit->green;
960 buf[2] = sbit->blue;
961 size = 3;
962 }
963 else
964 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500965 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
966 {
967 png_warning(png_ptr, "Invalid sBIT depth specified");
968 return;
969 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500970 buf[0] = sbit->gray;
971 size = 1;
972 }
973
974 if (color_type & PNG_COLOR_MASK_ALPHA)
975 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500976 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
977 {
978 png_warning(png_ptr, "Invalid sBIT depth specified");
979 return;
980 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500981 buf[size++] = sbit->alpha;
982 }
983
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600984 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500985}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500986#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500987
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500988#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500989/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600990#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500991void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500992png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600993 double red_x, double red_y, double green_x, double green_y,
994 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500995{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600996#ifdef PNG_USE_LOCAL_ARRAYS
997 PNG_cHRM;
998#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500999 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001000 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001001
Andreas Dilger47a0c421997-05-16 02:46:07 -05001002 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001003 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -05001004 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
1005 white_x + white_y > 1.0)
1006 {
1007 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001008#if !defined(PNG_NO_CONSOLE_IO)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001009 fprintf(stderr, "white_x=%f, white_y=%f\n", white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001010#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001011 return;
1012 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001013 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001014 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001015 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001016 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -05001017
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001018 if (red_x < 0 || red_y < 0 || red_x + red_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001019 {
1020 png_warning(png_ptr, "Invalid cHRM red point specified");
1021 return;
1022 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001023 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001024 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001025 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001026 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -05001027
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001028 if (green_x < 0 || green_y < 0 || green_x + green_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001029 {
1030 png_warning(png_ptr, "Invalid cHRM green point specified");
1031 return;
1032 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001033 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001034 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001035 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001036 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -05001037
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001038 if (blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001039 {
1040 png_warning(png_ptr, "Invalid cHRM blue point specified");
1041 return;
1042 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001043 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001044 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001045 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001046 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -05001047
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001048 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -05001049}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001050#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001051#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001052void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001053png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
1054 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
1055 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
1056 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001057{
1058#ifdef PNG_USE_LOCAL_ARRAYS
1059 PNG_cHRM;
1060#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001061 png_byte buf[32];
1062
1063 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001064 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001065 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
1066 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001067 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001068#if !defined(PNG_NO_CONSOLE_IO)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001069 fprintf(stderr, "white_x=%ld, white_y=%ld\n", (unsigned long)white_x,
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001070 (unsigned long)white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001071#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001072 return;
1073 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001074 png_save_uint_32(buf, (png_uint_32)white_x);
1075 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001076
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001077 if (red_x + red_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001078 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001079 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001080 return;
1081 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001082 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1083 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001084
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001085 if (green_x + green_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001086 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001087 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001088 return;
1089 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001090 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1091 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001092
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001093 if (blue_x + blue_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001094 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001095 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001096 return;
1097 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001098 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1099 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001100
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001101 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001102}
1103#endif
1104#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001105
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001106#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001107/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001108void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001109png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -05001110 int num_trans, int color_type)
1111{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001112#ifdef PNG_USE_LOCAL_ARRAYS
1113 PNG_tRNS;
1114#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001115 png_byte buf[6];
1116
Andreas Dilger47a0c421997-05-16 02:46:07 -05001117 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001118 if (color_type == PNG_COLOR_TYPE_PALETTE)
1119 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001120 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001121 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001122 png_warning(png_ptr, "Invalid number of transparent colors specified");
Guy Schalnate5a37791996-06-05 15:50:50 -05001123 return;
1124 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001125 /* write the chunk out as it is */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001126 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans,
1127 (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001128 }
1129 else if (color_type == PNG_COLOR_TYPE_GRAY)
1130 {
1131 /* one 16 bit value */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001132 if (tran->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001133 {
1134 png_warning(png_ptr,
1135 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1136 return;
1137 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001138 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001139 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001140 }
1141 else if (color_type == PNG_COLOR_TYPE_RGB)
1142 {
1143 /* three 16 bit values */
1144 png_save_uint_16(buf, tran->red);
1145 png_save_uint_16(buf + 2, tran->green);
1146 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001147 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001148 {
1149 png_warning(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001150 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001151 return;
1152 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001153 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001154 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001155 else
1156 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001157 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001158 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001159}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001160#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001161
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001162#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001163/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001164void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001165png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001166{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001167#ifdef PNG_USE_LOCAL_ARRAYS
1168 PNG_bKGD;
1169#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001170 png_byte buf[6];
1171
Andreas Dilger47a0c421997-05-16 02:46:07 -05001172 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001173 if (color_type == PNG_COLOR_TYPE_PALETTE)
1174 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001175 if (
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001176#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001177 (png_ptr->num_palette ||
1178 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001179#endif
1180 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001181 {
1182 png_warning(png_ptr, "Invalid background palette index");
1183 return;
1184 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001185 buf[0] = back->index;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001186 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001187 }
1188 else if (color_type & PNG_COLOR_MASK_COLOR)
1189 {
1190 png_save_uint_16(buf, back->red);
1191 png_save_uint_16(buf + 2, back->green);
1192 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001193 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001194 {
1195 png_warning(png_ptr,
1196 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1197 return;
1198 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001199 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001200 }
1201 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001202 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001203 if (back->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001204 {
1205 png_warning(png_ptr,
1206 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1207 return;
1208 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001209 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001210 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001211 }
1212}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001213#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001214
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001215#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001216/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001217void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001218png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001219{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001220#ifdef PNG_USE_LOCAL_ARRAYS
1221 PNG_hIST;
1222#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001223 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001224 png_byte buf[3];
1225
Andreas Dilger47a0c421997-05-16 02:46:07 -05001226 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001227 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001228 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001229 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1230 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001231 png_warning(png_ptr, "Invalid number of histogram entries specified");
1232 return;
1233 }
1234
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001235 png_write_chunk_start(png_ptr, (png_bytep)png_hIST,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001236 (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001237 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001238 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001239 png_save_uint_16(buf, hist[i]);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001240 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001241 }
1242 png_write_chunk_end(png_ptr);
1243}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001244#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001245
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001246#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1247 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001248/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1249 * and if invalid, correct the keyword rather than discarding the entire
1250 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1251 * length, forbids leading or trailing whitespace, multiple internal spaces,
1252 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001253 *
1254 * The new_key is allocated to hold the corrected keyword and must be freed
1255 * by the calling routine. This avoids problems with trying to write to
1256 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001257 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001258png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001259png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001260{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001261 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001262 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001263 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001264 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001265
Andreas Dilger47a0c421997-05-16 02:46:07 -05001266 png_debug(1, "in png_check_keyword\n");
1267 *new_key = NULL;
1268
1269 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001270 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001271 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001272 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001273 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001274
Andreas Dilger47a0c421997-05-16 02:46:07 -05001275 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1276
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001277 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001278 if (*new_key == NULL)
1279 {
1280 png_warning(png_ptr, "Out of memory while procesing keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001281 return ((png_size_t)0);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001282 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001283
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001284 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001285 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001286 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001287 if ((png_byte)*kp < 0x20 ||
1288 ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001289 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001290#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001291 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001292
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001293 png_snprintf(msg, 40,
1294 "invalid keyword character 0x%02X", (png_byte)*kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001295 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001296#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001297 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001298#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001299 *dp = ' ';
1300 }
1301 else
1302 {
1303 *dp = *kp;
1304 }
1305 }
1306 *dp = '\0';
1307
1308 /* Remove any trailing white space. */
1309 kp = *new_key + key_len - 1;
1310 if (*kp == ' ')
1311 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001312 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001313
1314 while (*kp == ' ')
1315 {
1316 *(kp--) = '\0';
1317 key_len--;
1318 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001319 }
1320
1321 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001322 kp = *new_key;
1323 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001324 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001325 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001326
1327 while (*kp == ' ')
1328 {
1329 kp++;
1330 key_len--;
1331 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001332 }
1333
Andreas Dilger47a0c421997-05-16 02:46:07 -05001334 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001335
Andreas Dilger47a0c421997-05-16 02:46:07 -05001336 /* Remove multiple internal spaces. */
1337 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001338 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001339 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001340 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001341 *(dp++) = *kp;
1342 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001343 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001344 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001345 {
1346 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001347 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001348 }
1349 else
1350 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001351 *(dp++) = *kp;
1352 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001353 }
1354 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001355 *dp = '\0';
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001356 if (kwarn)
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001357 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001358
1359 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001360 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001361 png_free(png_ptr, *new_key);
1362 *new_key=NULL;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001363 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001364 }
1365
1366 if (key_len > 79)
1367 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001368 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001369 new_key[79] = '\0';
Andreas Dilger47a0c421997-05-16 02:46:07 -05001370 key_len = 79;
1371 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001372
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001373 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001374}
1375#endif
1376
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001377#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001378/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001379void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001380png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001381 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001382{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001383#ifdef PNG_USE_LOCAL_ARRAYS
1384 PNG_tEXt;
1385#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001386 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001387 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001388
Andreas Dilger47a0c421997-05-16 02:46:07 -05001389 png_debug(1, "in png_write_tEXt\n");
1390 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1391 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001392 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001393 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001394 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001395
Andreas Dilger47a0c421997-05-16 02:46:07 -05001396 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001397 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001398 else
1399 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001400
1401 /* make sure we include the 0 after the key */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001402 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001403 (png_uint_32)(key_len + text_len + 1));
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001404 /*
1405 * We leave it to the application to meet PNG-1.0 requirements on the
1406 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1407 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001408 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001409 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001410 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1411 (png_size_t)(key_len + 1));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001412 if (text_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001413 png_write_chunk_data(png_ptr, (png_bytep)text, (png_size_t)text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001414
Guy Schalnat0d580581995-07-20 02:43:20 -05001415 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001416 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001417}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001418#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001419
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001420#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001421/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001422void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001423png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001424 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001425{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001426#ifdef PNG_USE_LOCAL_ARRAYS
1427 PNG_zTXt;
1428#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001429 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001430 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001431 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001432 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001433
Andreas Dilger47a0c421997-05-16 02:46:07 -05001434 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001435
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001436 comp.num_output_ptr = 0;
1437 comp.max_output_ptr = 0;
1438 comp.output_ptr = NULL;
1439 comp.input = NULL;
1440 comp.input_len = 0;
1441
Andreas Dilger47a0c421997-05-16 02:46:07 -05001442 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001443 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001444 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001445 png_free(png_ptr, new_key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001446 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001447 }
1448
Andreas Dilger47a0c421997-05-16 02:46:07 -05001449 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1450 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001451 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001452 png_free(png_ptr, new_key);
1453 return;
1454 }
1455
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001456 text_len = png_strlen(text);
1457
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001458 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001459 text_len = png_text_compress(png_ptr, text, text_len, compression,
1460 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001461
1462 /* write start of chunk */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001463 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt,
1464 (png_uint_32)(key_len+text_len + 2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001465 /* write key */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001466 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1467 (png_size_t)(key_len + 1));
1468 png_free(png_ptr, new_key);
1469
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001470 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001471 /* write compression */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001472 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001473 /* write the compressed data */
1474 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001475
Guy Schalnat0d580581995-07-20 02:43:20 -05001476 /* close the chunk */
1477 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001478}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001479#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001480
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001481#if defined(PNG_WRITE_iTXt_SUPPORTED)
1482/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001483void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001484png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001485 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001486{
1487#ifdef PNG_USE_LOCAL_ARRAYS
1488 PNG_iTXt;
1489#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001490 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001491 png_charp new_lang, new_key;
1492 png_byte cbuf[2];
1493 compression_state comp;
1494
1495 png_debug(1, "in png_write_iTXt\n");
1496
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001497 comp.num_output_ptr = 0;
1498 comp.max_output_ptr = 0;
1499 comp.output_ptr = NULL;
1500 comp.input = NULL;
1501
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001502 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1503 {
1504 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1505 return;
1506 }
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001507 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001508 {
1509 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001510 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001511 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001512 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001513
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001514 if (lang_key == NULL)
1515 lang_key_len = 0;
1516 else
1517 lang_key_len = png_strlen(lang_key);
1518
1519 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001520 text_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001521 else
1522 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001523
1524 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001525 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1526 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001527
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001528
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001529 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001530 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001531
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001532 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1533 (png_uint_32)(
1534 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1535 + key_len
1536 + lang_len
1537 + lang_key_len
1538 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001539
1540 /*
1541 * We leave it to the application to meet PNG-1.0 requirements on the
1542 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1543 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1544 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1545 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001546 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1547 (png_size_t)(key_len + 1));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001548
1549 /* set the compression flag */
1550 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1551 compression == PNG_TEXT_COMPRESSION_NONE)
1552 cbuf[0] = 0;
1553 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1554 cbuf[0] = 1;
1555 /* set the compression method */
1556 cbuf[1] = 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001557 png_write_chunk_data(png_ptr, cbuf, (png_size_t)2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001558
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001559 cbuf[0] = 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001560 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf),
1561 (png_size_t)(lang_len + 1));
1562 png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf),
1563 (png_size_t)(lang_key_len + 1));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001564 png_write_compressed_data_out(png_ptr, &comp);
1565
1566 png_write_chunk_end(png_ptr);
1567 png_free(png_ptr, new_key);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001568 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001569}
1570#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001571
1572#if defined(PNG_WRITE_oFFs_SUPPORTED)
1573/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001574void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001575png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001576 int unit_type)
1577{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001578#ifdef PNG_USE_LOCAL_ARRAYS
1579 PNG_oFFs;
1580#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001581 png_byte buf[9];
1582
1583 png_debug(1, "in png_write_oFFs\n");
1584 if (unit_type >= PNG_OFFSET_LAST)
1585 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1586
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001587 png_save_int_32(buf, x_offset);
1588 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001589 buf[8] = (png_byte)unit_type;
1590
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001591 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001592}
1593#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001594#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001595/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001596void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001597png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1598 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1599{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001600#ifdef PNG_USE_LOCAL_ARRAYS
1601 PNG_pCAL;
1602#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001603 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001604 png_uint_32p params_len;
1605 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001606 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001607 int i;
1608
1609 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1610 if (type >= PNG_EQUATION_LAST)
1611 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1612
1613 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001614 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001615 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001616 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001617 total_len = purpose_len + units_len + 10;
1618
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001619 params_len = (png_uint_32p)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001620 (png_size_t)(nparams * png_sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001621
1622 /* Find the length of each parameter, making sure we don't count the
1623 null terminator for the last parameter. */
1624 for (i = 0; i < nparams; i++)
1625 {
1626 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001627 png_debug2(3, "pCAL parameter %d length = %lu\n", i,
1628 (unsigned long) params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001629 total_len += (png_size_t)params_len[i];
1630 }
1631
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001632 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001633 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001634 png_write_chunk_data(png_ptr, (png_bytep)new_purpose,
1635 (png_size_t)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001636 png_save_int_32(buf, X0);
1637 png_save_int_32(buf + 4, X1);
1638 buf[8] = (png_byte)type;
1639 buf[9] = (png_byte)nparams;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001640 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1641 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001642
1643 png_free(png_ptr, new_purpose);
1644
1645 for (i = 0; i < nparams; i++)
1646 {
1647 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1648 (png_size_t)params_len[i]);
1649 }
1650
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001651 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001652 png_write_chunk_end(png_ptr);
1653}
1654#endif
1655
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001656#if defined(PNG_WRITE_sCAL_SUPPORTED)
1657/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001658#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001659void /* PRIVATE */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001660png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001661{
1662#ifdef PNG_USE_LOCAL_ARRAYS
1663 PNG_sCAL;
1664#endif
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001665 char buf[64];
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001666 png_size_t total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001667
1668 png_debug(1, "in png_write_sCAL\n");
1669
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001670 buf[0] = (char)unit;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001671 png_snprintf(buf + 1, 63, "%12.12e", width);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001672 total_len = 1 + png_strlen(buf + 1) + 1;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001673 png_snprintf(buf + total_len, 64-total_len, "%12.12e", height);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001674 total_len += png_strlen(buf + total_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001675
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001676 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
1677 png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001678}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001679#else
1680#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001681void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001682png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001683 png_charp height)
1684{
1685#ifdef PNG_USE_LOCAL_ARRAYS
1686 PNG_sCAL;
1687#endif
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001688 png_byte buf[64];
1689 png_size_t wlen, hlen, total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001690
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001691 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001692
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001693 wlen = png_strlen(width);
1694 hlen = png_strlen(height);
1695 total_len = wlen + hlen + 2;
1696 if (total_len > 64)
1697 {
1698 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
1699 return;
1700 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001701
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001702 buf[0] = (png_byte)unit;
1703 png_memcpy(buf + 1, width, wlen + 1); /* append the '\0' here */
1704 png_memcpy(buf + wlen + 2, height, hlen); /* do NOT append the '\0' here */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001705
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001706 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
1707 png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001708}
1709#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001710#endif
1711#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001712
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001713#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001714/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001715void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001716png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001717 png_uint_32 y_pixels_per_unit,
1718 int unit_type)
1719{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001720#ifdef PNG_USE_LOCAL_ARRAYS
1721 PNG_pHYs;
1722#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001723 png_byte buf[9];
1724
Andreas Dilger47a0c421997-05-16 02:46:07 -05001725 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001726 if (unit_type >= PNG_RESOLUTION_LAST)
1727 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1728
Guy Schalnat0d580581995-07-20 02:43:20 -05001729 png_save_uint_32(buf, x_pixels_per_unit);
1730 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001731 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001732
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001733 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001734}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001735#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001736
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001737#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001738/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1739 * or png_convert_from_time_t(), or fill in the structure yourself.
1740 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001741void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001742png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001743{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001744#ifdef PNG_USE_LOCAL_ARRAYS
1745 PNG_tIME;
1746#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001747 png_byte buf[7];
1748
Andreas Dilger47a0c421997-05-16 02:46:07 -05001749 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001750 if (mod_time->month > 12 || mod_time->month < 1 ||
1751 mod_time->day > 31 || mod_time->day < 1 ||
1752 mod_time->hour > 23 || mod_time->second > 60)
1753 {
1754 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1755 return;
1756 }
1757
Guy Schalnat0d580581995-07-20 02:43:20 -05001758 png_save_uint_16(buf, mod_time->year);
1759 buf[2] = mod_time->month;
1760 buf[3] = mod_time->day;
1761 buf[4] = mod_time->hour;
1762 buf[5] = mod_time->minute;
1763 buf[6] = mod_time->second;
1764
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001765 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001766}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001767#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001768
1769/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001770void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001771png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001772{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001773#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001774#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001775 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001776
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001777 /* start of interlace block */
1778 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001779
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001780 /* offset to next interlace block */
1781 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001782
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001783 /* start of interlace block in the y direction */
1784 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001785
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001786 /* offset to next interlace block in the y direction */
1787 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001788#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001789#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001790
Andreas Dilger47a0c421997-05-16 02:46:07 -05001791 png_size_t buf_size;
1792
1793 png_debug(1, "in png_write_start_row\n");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001794 buf_size = (png_size_t)(PNG_ROWBYTES(
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001795 png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001796
Guy Schalnat0d580581995-07-20 02:43:20 -05001797 /* set up row buffer */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001798 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr,
1799 (png_size_t)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001800 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001801
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001802#ifndef PNG_NO_WRITE_FILTER
Guy Schalnate5a37791996-06-05 15:50:50 -05001803 /* set up filtering buffer, if using this filter */
1804 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001805 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001806 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001807 (png_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001808 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001809 }
1810
Andreas Dilger47a0c421997-05-16 02:46:07 -05001811 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001812 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1813 {
1814 /* set up previous row buffer */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001815 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr,
1816 (png_size_t)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001817 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001818
1819 if (png_ptr->do_filter & PNG_FILTER_UP)
1820 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001821 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001822 (png_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001823 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001824 }
1825
1826 if (png_ptr->do_filter & PNG_FILTER_AVG)
1827 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001828 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001829 (png_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001830 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001831 }
1832
1833 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1834 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001835 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001836 (png_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001837 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001838 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001839#endif /* PNG_NO_WRITE_FILTER */
Guy Schalnat0d580581995-07-20 02:43:20 -05001840 }
1841
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001842#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001843 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001844 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001845 {
1846 if (!(png_ptr->transformations & PNG_INTERLACE))
1847 {
1848 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1849 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001850 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1851 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001852 }
1853 else
1854 {
1855 png_ptr->num_rows = png_ptr->height;
1856 png_ptr->usr_width = png_ptr->width;
1857 }
1858 }
1859 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001860#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001861 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001862 png_ptr->num_rows = png_ptr->height;
1863 png_ptr->usr_width = png_ptr->width;
1864 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001865 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1866 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001867}
1868
Andreas Dilger47a0c421997-05-16 02:46:07 -05001869/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001870void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001871png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001872{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001873#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001874#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001875 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001876
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001877 /* start of interlace block */
1878 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001879
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001880 /* offset to next interlace block */
1881 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001882
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001883 /* start of interlace block in the y direction */
1884 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001885
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001886 /* offset to next interlace block in the y direction */
1887 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001888#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001889#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001890
Guy Schalnat0d580581995-07-20 02:43:20 -05001891 int ret;
1892
Andreas Dilger47a0c421997-05-16 02:46:07 -05001893 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001894 /* next row */
1895 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001896
Guy Schalnat0d580581995-07-20 02:43:20 -05001897 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001898 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001899 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001900
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001901#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001902 /* if interlaced, go to next pass */
1903 if (png_ptr->interlaced)
1904 {
1905 png_ptr->row_number = 0;
1906 if (png_ptr->transformations & PNG_INTERLACE)
1907 {
1908 png_ptr->pass++;
1909 }
1910 else
1911 {
1912 /* loop until we find a non-zero width or height pass */
1913 do
1914 {
1915 png_ptr->pass++;
1916 if (png_ptr->pass >= 7)
1917 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001918 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001919 png_pass_inc[png_ptr->pass] - 1 -
1920 png_pass_start[png_ptr->pass]) /
1921 png_pass_inc[png_ptr->pass];
1922 png_ptr->num_rows = (png_ptr->height +
1923 png_pass_yinc[png_ptr->pass] - 1 -
1924 png_pass_ystart[png_ptr->pass]) /
1925 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001926 if (png_ptr->transformations & PNG_INTERLACE)
1927 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001928 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1929
1930 }
1931
Guy Schalnate5a37791996-06-05 15:50:50 -05001932 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001933 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001934 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001935 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001936 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001937 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001938 png_ptr->usr_bit_depth, png_ptr->width)) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001939 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001940 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001941 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001942#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001943
1944 /* if we get here, we've just written the last row, so we need
1945 to flush the compressor */
1946 do
1947 {
1948 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001949 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001950 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001951 if (ret == Z_OK)
1952 {
1953 /* check to see if we need more room */
1954 if (!(png_ptr->zstream.avail_out))
1955 {
1956 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1957 png_ptr->zstream.next_out = png_ptr->zbuf;
1958 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1959 }
1960 }
1961 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001962 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001963 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001964 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001965 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001966 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001967 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001968 } while (ret != Z_STREAM_END);
1969
1970 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001971 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001972 {
1973 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001974 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001975 }
1976
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001977 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -06001978 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat0d580581995-07-20 02:43:20 -05001979}
1980
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001981#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001982/* Pick out the correct pixels for the interlace pass.
1983 * The basic idea here is to go through the row with a source
1984 * pointer and a destination pointer (sp and dp), and copy the
1985 * correct pixels for the pass. As the row gets compacted,
1986 * sp will always be >= dp, so we should never overwrite anything.
1987 * See the default: case for the easiest code to understand.
1988 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001989void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001990png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001991{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001992#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001993 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001994
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001995 /* start of interlace block */
1996 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001997
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001998 /* offset to next interlace block */
1999 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06002000#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002001
Andreas Dilger47a0c421997-05-16 02:46:07 -05002002 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05002003 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002004#if defined(PNG_USELESS_TESTS_SUPPORTED)
2005 if (row != NULL && row_info != NULL && pass < 6)
2006#else
2007 if (pass < 6)
2008#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002009 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002010 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05002011 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002012 {
Guy Schalnat0d580581995-07-20 02:43:20 -05002013 case 1:
2014 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002015 png_bytep sp;
2016 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05002017 int shift;
2018 int d;
2019 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002020 png_uint_32 i;
2021 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002022
2023 dp = row;
2024 d = 0;
2025 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002026 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002027 i += png_pass_inc[pass])
2028 {
2029 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002030 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05002031 d |= (value << shift);
2032
2033 if (shift == 0)
2034 {
2035 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002036 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002037 d = 0;
2038 }
2039 else
2040 shift--;
2041
2042 }
2043 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002044 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002045 break;
2046 }
2047 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002048 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002049 png_bytep sp;
2050 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05002051 int shift;
2052 int d;
2053 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002054 png_uint_32 i;
2055 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002056
2057 dp = row;
2058 shift = 6;
2059 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002060 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002061 i += png_pass_inc[pass])
2062 {
2063 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002064 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05002065 d |= (value << shift);
2066
2067 if (shift == 0)
2068 {
2069 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002070 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002071 d = 0;
2072 }
2073 else
2074 shift -= 2;
2075 }
2076 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002077 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002078 break;
2079 }
2080 case 4:
2081 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002082 png_bytep sp;
2083 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002084 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05002085 int d;
2086 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002087 png_uint_32 i;
2088 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002089
2090 dp = row;
2091 shift = 4;
2092 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002093 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002094 i += png_pass_inc[pass])
2095 {
2096 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002097 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05002098 d |= (value << shift);
2099
2100 if (shift == 0)
2101 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002102 shift = 4;
2103 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002104 d = 0;
2105 }
2106 else
2107 shift -= 4;
2108 }
2109 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002110 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002111 break;
2112 }
2113 default:
2114 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002115 png_bytep sp;
2116 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002117 png_uint_32 i;
2118 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002119 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002120
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002121 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002122 dp = row;
2123 /* find out how many bytes each pixel takes up */
2124 pixel_bytes = (row_info->pixel_depth >> 3);
2125 /* loop through the row, only looking at the pixels that
2126 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002127 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002128 i += png_pass_inc[pass])
2129 {
2130 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002131 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002132 /* move the pixel */
2133 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002134 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05002135 /* next pixel */
2136 dp += pixel_bytes;
2137 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002138 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002139 }
2140 }
2141 /* set new row width */
2142 row_info->width = (row_info->width +
2143 png_pass_inc[pass] - 1 -
2144 png_pass_start[pass]) /
2145 png_pass_inc[pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002146 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2147 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002148 }
2149}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002150#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002151
Andreas Dilger47a0c421997-05-16 02:46:07 -05002152/* This filters the row, chooses which filter to use, if it has not already
2153 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002154 * chosen filter.
2155 */
Glenn Randers-Pehrson78067772004-11-02 21:49:39 -06002156#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002157#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002158#define PNG_LOMASK ((png_uint_32)0xffffL)
2159#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002160void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002161png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002162{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002163 png_bytep best_row;
2164#ifndef PNG_NO_WRITE_FILTER
2165 png_bytep prev_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002166 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002167 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002168 png_uint_32 row_bytes = row_info->rowbytes;
2169#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2170 int num_p_filters = (int)png_ptr->num_prev_filters;
2171#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002172
Andreas Dilger47a0c421997-05-16 02:46:07 -05002173 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05002174 /* find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002175 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002176
2177 prev_row = png_ptr->prev_row;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002178#endif
2179 best_row = png_ptr->row_buf;
2180#ifndef PNG_NO_WRITE_FILTER
2181 row_buf = best_row;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002182 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002183
Andreas Dilger47a0c421997-05-16 02:46:07 -05002184 /* The prediction method we use is to find which method provides the
2185 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002186 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002187 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002188 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002189 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002190 * predictive" method (not implemented yet), which does test compressions
2191 * of lines using different filter methods, and then chooses the
2192 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002193 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002194 *
2195 * GRR 980525: consider also
2196 * (1) minimum sum of absolute differences from running average (i.e.,
2197 * keep running sum of non-absolute differences & count of bytes)
2198 * [track dispersion, too? restart average if dispersion too large?]
2199 * (1b) minimum sum of absolute differences from sliding average, probably
2200 * with window size <= deflate window (usually 32K)
2201 * (2) minimum sum of squared differences from zero or running average
2202 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002203 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002204
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002205
Guy Schalnate5a37791996-06-05 15:50:50 -05002206 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002207 * that has been chosen, as it doesn't actually do anything to the data.
2208 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002209 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002210 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002211 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002212 png_bytep rp;
2213 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002214 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002215 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002216
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002217 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002218 {
2219 v = *rp;
2220 sum += (v < 128) ? v : 256 - v;
2221 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002222
2223#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2224 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2225 {
2226 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002227 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002228 sumlo = sum & PNG_LOMASK;
2229 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2230
2231 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002232 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002233 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002234 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002235 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002236 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002237 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002238 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002239 PNG_WEIGHT_SHIFT;
2240 }
2241 }
2242
2243 /* Factor in the cost of this filter (this is here for completeness,
2244 * but it makes no sense to have a "cost" for the NONE filter, as
2245 * it has the minimum possible computational cost - none).
2246 */
2247 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2248 PNG_COST_SHIFT;
2249 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2250 PNG_COST_SHIFT;
2251
2252 if (sumhi > PNG_HIMASK)
2253 sum = PNG_MAXSUM;
2254 else
2255 sum = (sumhi << PNG_HISHIFT) + sumlo;
2256 }
2257#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002258 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002259 }
2260
Guy Schalnate5a37791996-06-05 15:50:50 -05002261 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002262 if (filter_to_do == PNG_FILTER_SUB)
2263 /* it's the only filter so no testing is needed */
2264 {
2265 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002266 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002267 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2268 i++, rp++, dp++)
2269 {
2270 *dp = *rp;
2271 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002272 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002273 i++, rp++, lp++, dp++)
2274 {
2275 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2276 }
2277 best_row = png_ptr->sub_row;
2278 }
2279
2280 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002281 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002282 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002283 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002284 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002285 int v;
2286
2287#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002288 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002289 * would reduce the sum of this filter, so that we can do the
2290 * early exit comparison without scaling the sum each time.
2291 */
2292 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2293 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002294 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002295 png_uint_32 lmhi, lmlo;
2296 lmlo = lmins & PNG_LOMASK;
2297 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2298
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002299 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002300 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002301 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002302 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002303 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002304 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002305 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002306 PNG_WEIGHT_SHIFT;
2307 }
2308 }
2309
2310 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2311 PNG_COST_SHIFT;
2312 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2313 PNG_COST_SHIFT;
2314
2315 if (lmhi > PNG_HIMASK)
2316 lmins = PNG_MAXSUM;
2317 else
2318 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2319 }
2320#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002321
Guy Schalnate5a37791996-06-05 15:50:50 -05002322 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2323 i++, rp++, dp++)
2324 {
2325 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002326
Guy Schalnate5a37791996-06-05 15:50:50 -05002327 sum += (v < 128) ? v : 256 - v;
2328 }
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002329 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002330 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002331 {
2332 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002333
Guy Schalnate5a37791996-06-05 15:50:50 -05002334 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002335
2336 if (sum > lmins) /* We are already worse, don't continue. */
2337 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002338 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002339
2340#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2341 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2342 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002343 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002344 png_uint_32 sumhi, sumlo;
2345 sumlo = sum & PNG_LOMASK;
2346 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2347
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002348 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002349 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002350 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002351 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002352 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002353 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002354 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002355 PNG_WEIGHT_SHIFT;
2356 }
2357 }
2358
2359 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2360 PNG_COST_SHIFT;
2361 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2362 PNG_COST_SHIFT;
2363
2364 if (sumhi > PNG_HIMASK)
2365 sum = PNG_MAXSUM;
2366 else
2367 sum = (sumhi << PNG_HISHIFT) + sumlo;
2368 }
2369#endif
2370
Guy Schalnate5a37791996-06-05 15:50:50 -05002371 if (sum < mins)
2372 {
2373 mins = sum;
2374 best_row = png_ptr->sub_row;
2375 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002376 }
2377
Guy Schalnate5a37791996-06-05 15:50:50 -05002378 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002379 if (filter_to_do == PNG_FILTER_UP)
2380 {
2381 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002382 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002383
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002384 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002385 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002386 i++, rp++, pp++, dp++)
2387 {
2388 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2389 }
2390 best_row = png_ptr->up_row;
2391 }
2392
2393 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002394 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002395 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002396 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002397 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002398 int v;
2399
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002400
Andreas Dilger47a0c421997-05-16 02:46:07 -05002401#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2402 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2403 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002404 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002405 png_uint_32 lmhi, lmlo;
2406 lmlo = lmins & PNG_LOMASK;
2407 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2408
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002409 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002410 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002411 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002412 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002413 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002414 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002415 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002416 PNG_WEIGHT_SHIFT;
2417 }
2418 }
2419
2420 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2421 PNG_COST_SHIFT;
2422 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2423 PNG_COST_SHIFT;
2424
2425 if (lmhi > PNG_HIMASK)
2426 lmins = PNG_MAXSUM;
2427 else
2428 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2429 }
2430#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002431
2432 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002433 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002434 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002435 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002436
2437 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002438
2439 if (sum > lmins) /* We are already worse, don't continue. */
2440 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002441 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002442
2443#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2444 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2445 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002446 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002447 png_uint_32 sumhi, sumlo;
2448 sumlo = sum & PNG_LOMASK;
2449 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2450
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002451 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002452 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002453 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002454 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002455 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002456 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002457 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002458 PNG_WEIGHT_SHIFT;
2459 }
2460 }
2461
2462 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2463 PNG_COST_SHIFT;
2464 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2465 PNG_COST_SHIFT;
2466
2467 if (sumhi > PNG_HIMASK)
2468 sum = PNG_MAXSUM;
2469 else
2470 sum = (sumhi << PNG_HISHIFT) + sumlo;
2471 }
2472#endif
2473
Guy Schalnate5a37791996-06-05 15:50:50 -05002474 if (sum < mins)
2475 {
2476 mins = sum;
2477 best_row = png_ptr->up_row;
2478 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002479 }
2480
Guy Schalnate5a37791996-06-05 15:50:50 -05002481 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002482 if (filter_to_do == PNG_FILTER_AVG)
2483 {
2484 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002485 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002486 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002487 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002488 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002489 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002490 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002491 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002492 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002493 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2494 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002495 }
2496 best_row = png_ptr->avg_row;
2497 }
2498
2499 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002500 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002501 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002502 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002503 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002504 int v;
2505
2506#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2507 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2508 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002509 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002510 png_uint_32 lmhi, lmlo;
2511 lmlo = lmins & PNG_LOMASK;
2512 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2513
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002514 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002515 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002516 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002517 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002518 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002519 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002520 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002521 PNG_WEIGHT_SHIFT;
2522 }
2523 }
2524
2525 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2526 PNG_COST_SHIFT;
2527 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2528 PNG_COST_SHIFT;
2529
2530 if (lmhi > PNG_HIMASK)
2531 lmins = PNG_MAXSUM;
2532 else
2533 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2534 }
2535#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002536
2537 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002538 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002539 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002540 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002541
2542 sum += (v < 128) ? v : 256 - v;
2543 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002544 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002545 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002546 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002547 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002548
2549 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002550
2551 if (sum > lmins) /* We are already worse, don't continue. */
2552 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002553 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002554
2555#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2556 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2557 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002558 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002559 png_uint_32 sumhi, sumlo;
2560 sumlo = sum & PNG_LOMASK;
2561 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2562
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002563 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002564 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002565 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002566 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002567 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002568 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002569 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002570 PNG_WEIGHT_SHIFT;
2571 }
2572 }
2573
2574 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2575 PNG_COST_SHIFT;
2576 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2577 PNG_COST_SHIFT;
2578
2579 if (sumhi > PNG_HIMASK)
2580 sum = PNG_MAXSUM;
2581 else
2582 sum = (sumhi << PNG_HISHIFT) + sumlo;
2583 }
2584#endif
2585
Guy Schalnate5a37791996-06-05 15:50:50 -05002586 if (sum < mins)
2587 {
2588 mins = sum;
2589 best_row = png_ptr->avg_row;
2590 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002591 }
2592
Andreas Dilger47a0c421997-05-16 02:46:07 -05002593 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002594 if (filter_to_do == PNG_FILTER_PAETH)
2595 {
2596 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002597 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002598 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002599 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002600 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002601 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002602 }
2603
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002604 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002605 {
2606 int a, b, c, pa, pb, pc, p;
2607
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002608 b = *pp++;
2609 c = *cp++;
2610 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002611
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002612 p = b - c;
2613 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002614
2615#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002616 pa = abs(p);
2617 pb = abs(pc);
2618 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002619#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002620 pa = p < 0 ? -p : p;
2621 pb = pc < 0 ? -pc : pc;
2622 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002623#endif
2624
2625 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2626
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002627 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002628 }
2629 best_row = png_ptr->paeth_row;
2630 }
2631
2632 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002633 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002634 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002635 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002636 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002637 int v;
2638
2639#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2640 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2641 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002642 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002643 png_uint_32 lmhi, lmlo;
2644 lmlo = lmins & PNG_LOMASK;
2645 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2646
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002647 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002648 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002649 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002650 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002651 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002652 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002653 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002654 PNG_WEIGHT_SHIFT;
2655 }
2656 }
2657
2658 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2659 PNG_COST_SHIFT;
2660 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2661 PNG_COST_SHIFT;
2662
2663 if (lmhi > PNG_HIMASK)
2664 lmins = PNG_MAXSUM;
2665 else
2666 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2667 }
2668#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002669
2670 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002671 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002672 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002673 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002674
2675 sum += (v < 128) ? v : 256 - v;
2676 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002677
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002678 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002679 {
2680 int a, b, c, pa, pb, pc, p;
2681
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002682 b = *pp++;
2683 c = *cp++;
2684 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002685
2686#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002687 p = b - c;
2688 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002689#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002690 pa = abs(p);
2691 pb = abs(pc);
2692 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002693#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002694 pa = p < 0 ? -p : p;
2695 pb = pc < 0 ? -pc : pc;
2696 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002697#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002698 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2699#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002700 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002701 pa = abs(p - a);
2702 pb = abs(p - b);
2703 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002704 if (pa <= pb && pa <= pc)
2705 p = a;
2706 else if (pb <= pc)
2707 p = b;
2708 else
2709 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002710#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002711
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002712 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002713
2714 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002715
2716 if (sum > lmins) /* We are already worse, don't continue. */
2717 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002718 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002719
2720#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2721 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2722 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002723 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002724 png_uint_32 sumhi, sumlo;
2725 sumlo = sum & PNG_LOMASK;
2726 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2727
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002728 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002729 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002730 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002731 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002732 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002733 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002734 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002735 PNG_WEIGHT_SHIFT;
2736 }
2737 }
2738
2739 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2740 PNG_COST_SHIFT;
2741 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2742 PNG_COST_SHIFT;
2743
2744 if (sumhi > PNG_HIMASK)
2745 sum = PNG_MAXSUM;
2746 else
2747 sum = (sumhi << PNG_HISHIFT) + sumlo;
2748 }
2749#endif
2750
Guy Schalnate5a37791996-06-05 15:50:50 -05002751 if (sum < mins)
2752 {
2753 best_row = png_ptr->paeth_row;
2754 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002755 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002756#endif /* PNG_NO_WRITE_FILTER */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002757 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002758
Guy Schalnate5a37791996-06-05 15:50:50 -05002759 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002760
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002761#ifndef PNG_NO_WRITE_FILTER
Andreas Dilger47a0c421997-05-16 02:46:07 -05002762#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2763 /* Save the type of filter we picked this time for future calculations */
2764 if (png_ptr->num_prev_filters > 0)
2765 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002766 int j;
2767 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002768 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002769 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002770 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002771 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002772 }
2773#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002774#endif /* PNG_NO_WRITE_FILTER */
Guy Schalnate5a37791996-06-05 15:50:50 -05002775}
2776
2777
Andreas Dilger47a0c421997-05-16 02:46:07 -05002778/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002779void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002780png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2781{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002782 png_debug(1, "in png_write_filtered_row\n");
2783 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002784 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002785
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002786 png_ptr->zstream.next_in = filtered_row;
2787 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002788 /* repeat until we have compressed all the data */
2789 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002790 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002791 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002792
Guy Schalnate5a37791996-06-05 15:50:50 -05002793 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002794 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002795 /* check for compression errors */
2796 if (ret != Z_OK)
2797 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002798 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002799 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002800 else
2801 png_error(png_ptr, "zlib error");
2802 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002803
Guy Schalnate5a37791996-06-05 15:50:50 -05002804 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002805 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002806 {
2807 /* write the IDAT and reset the zlib output buffer */
2808 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002809 png_ptr->zstream.next_out = png_ptr->zbuf;
2810 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002811 }
2812 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002813 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002814
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002815 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002816 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002817 {
2818 png_bytep tptr;
2819
2820 tptr = png_ptr->prev_row;
2821 png_ptr->prev_row = png_ptr->row_buf;
2822 png_ptr->row_buf = tptr;
2823 }
2824
Guy Schalnate5a37791996-06-05 15:50:50 -05002825 /* finish row - updates counters and flushes zlib if last row */
2826 png_write_finish_row(png_ptr);
2827
2828#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2829 png_ptr->flush_rows++;
2830
2831 if (png_ptr->flush_dist > 0 &&
2832 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002833 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002834 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002835 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002836#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002837}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05002838#endif /* PNG_WRITE_SUPPORTED */