blob: c47a435c5737f80a6d8af034995076bba7088084 [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-Pehrsonbfbf8652009-06-26 21:46:52 -05004 * Last changed in libpng 1.4.0 [June 27, 2009]
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06005 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -05008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060012 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050013
Guy Schalnat0d580581995-07-20 02:43:20 -050014#include "png.h"
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050015#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050016#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050017
Andreas Dilger47a0c421997-05-16 02:46:07 -050018/* Place a 32-bit number into a buffer in PNG byte order. We work
19 * with unsigned numbers for convenience, although one supported
20 * ancillary chunk uses signed (two's complement) numbers.
21 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060022void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -060023png_save_uint_32(png_bytep buf, png_uint_32 i)
Guy Schalnat0d580581995-07-20 02:43:20 -050024{
25 buf[0] = (png_byte)((i >> 24) & 0xff);
26 buf[1] = (png_byte)((i >> 16) & 0xff);
27 buf[2] = (png_byte)((i >> 8) & 0xff);
28 buf[3] = (png_byte)(i & 0xff);
29}
30
Glenn Randers-Pehrson86dc9812006-05-10 07:27:44 -050031#if defined(PNG_SAVE_INT_32_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -050032/* The png_save_int_32 function assumes integers are stored in two's
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060033 * complement format. If this isn't the case, then this routine needs to
34 * be modified to write data in two's complement format.
35 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060036void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050037png_save_int_32(png_bytep buf, png_int_32 i)
38{
39 buf[0] = (png_byte)((i >> 24) & 0xff);
40 buf[1] = (png_byte)((i >> 16) & 0xff);
41 buf[2] = (png_byte)((i >> 8) & 0xff);
42 buf[3] = (png_byte)(i & 0xff);
43}
Glenn Randers-Pehrson86dc9812006-05-10 07:27:44 -050044#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050045
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060046/* Place a 16-bit number into a buffer in PNG byte order.
47 * The parameter is declared unsigned int, not png_uint_16,
48 * just to avoid potential problems on pre-ANSI C compilers.
49 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060050void PNGAPI
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060051png_save_uint_16(png_bytep buf, unsigned int i)
Guy Schalnat0d580581995-07-20 02:43:20 -050052{
53 buf[0] = (png_byte)((i >> 8) & 0xff);
54 buf[1] = (png_byte)(i & 0xff);
55}
56
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050057/* Simple function to write the signature. If we have already written
58 * the magic bytes of the signature, or more likely, the PNG stream is
59 * being embedded into another stream and doesn't need its own signature,
60 * we should call png_set_sig_bytes() to tell libpng how many of the
61 * bytes have already been written.
62 */
63void PNGAPI
64png_write_sig(png_structp png_ptr)
65{
66 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050067
68#ifdef PNG_IO_STATE_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050069 /* Inform the I/O callback that the signature is being written */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050070 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_SIGNATURE;
71#endif
72
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050073 /* Write the rest of the 8 byte signature */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050074 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050075 (png_size_t)(8 - png_ptr->sig_bytes));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050076 if (png_ptr->sig_bytes < 3)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050077 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
78}
79
Andreas Dilger47a0c421997-05-16 02:46:07 -050080/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060081 * representing the chunk name. The array must be at least 4 bytes in
82 * length, and does not need to be null terminated. To be safe, pass the
83 * pre-defined chunk names here, and if you need a new one, define it
84 * where the others are defined. The length is the length of the data.
85 * All the data must be present. If that is not possible, use the
86 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
87 * functions instead.
88 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050089void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060090png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050091 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050092{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050093 if (png_ptr == NULL)
94 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -050095 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050096 png_write_chunk_data(png_ptr, data, (png_size_t)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060097 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050098}
99
Andreas Dilger47a0c421997-05-16 02:46:07 -0500100/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600101 * The total_length is the sum of the lengths of all the data you will be
102 * passing in png_write_chunk_data().
103 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500104void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600105png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
106 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500107{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500108 png_byte buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500109
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500110#ifdef PNG_IO_STATE_SUPPORTED
111 /* Inform the I/O callback that the chunk header is being written.
112 * PNG_IO_CHUNK_HDR requires a single I/O call.
113 */
114 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR;
115#endif
116
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500117 png_debug2(0, "Writing %s chunk, length = %lu", chunk_name,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500118 (unsigned long)length);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500119 if (png_ptr == NULL)
120 return;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500121
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500122 /* Write the length and the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500123 png_save_uint_32(buf, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500124 png_memcpy(buf + 4, chunk_name, 4);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500125 png_write_data(png_ptr, buf, (png_size_t)8);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500126 /* Put the chunk name into png_ptr->chunk_name */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500127 png_memcpy(png_ptr->chunk_name, chunk_name, 4);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500128 /* Reset the crc and run it over the chunk name */
Guy Schalnat0d580581995-07-20 02:43:20 -0500129 png_reset_crc(png_ptr);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500130 png_calculate_crc(png_ptr, chunk_name, 4);
131
132#ifdef PNG_IO_STATE_SUPPORTED
133 /* Inform the I/O callback that chunk data will (possibly) be written.
134 * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.
135 */
136 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA;
137#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500138}
139
Andreas Dilger47a0c421997-05-16 02:46:07 -0500140/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600141 * Note that multiple calls to this function are allowed, and that the
142 * sum of the lengths from these calls *must* add up to the total_length
143 * given to png_write_chunk_start().
144 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500145void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500146png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500147{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500148 /* Write the data, and run the CRC over it */
149 if (png_ptr == NULL)
150 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500151 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500152 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600153 png_write_data(png_ptr, data, length);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500154 /* Update the CRC after writing the data,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500155 * in case that the user I/O routine alters it.
156 */
157 png_calculate_crc(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500158 }
159}
160
Andreas Dilger47a0c421997-05-16 02:46:07 -0500161/* Finish a chunk started with png_write_chunk_start(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500162void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600163png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500164{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500165 png_byte buf[4];
166
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500167 if (png_ptr == NULL) return;
168
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500169#ifdef PNG_IO_STATE_SUPPORTED
170 /* Inform the I/O callback that the chunk CRC is being written.
171 * PNG_IO_CHUNK_CRC requires a single I/O function call.
172 */
173 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC;
174#endif
175
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500176 /* Write the crc in a single operation */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500177 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500178
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500179 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500180}
181
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600182#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500183/* This pair of functions encapsulates the operation of (a) compressing a
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600184 * text string, and (b) issuing it later as a series of chunk data writes.
185 * The compression_state structure is shared context for these functions
186 * set up by the caller in order to make the whole mess thread-safe.
187 */
188
189typedef struct
190{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500191 char *input; /* The uncompressed input data */
192 int input_len; /* Its length */
193 int num_output_ptr; /* Number of output pointers used */
194 int max_output_ptr; /* Size of output_ptr */
195 png_charpp output_ptr; /* Array of pointers to output */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600196} compression_state;
197
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500198/* Compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500199static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600200png_text_compress(png_structp png_ptr,
201 png_charp text, png_size_t text_len, int compression,
202 compression_state *comp)
203{
204 int ret;
205
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600206 comp->num_output_ptr = 0;
207 comp->max_output_ptr = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600208 comp->output_ptr = NULL;
209 comp->input = NULL;
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600210 comp->input_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600211
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500212 /* We may just want to pass the text right through */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600213 if (compression == PNG_TEXT_COMPRESSION_NONE)
214 {
215 comp->input = text;
216 comp->input_len = text_len;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500217 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600218 }
219
220 if (compression >= PNG_TEXT_COMPRESSION_LAST)
221 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500222#if !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600223 char msg[50];
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500224 png_snprintf(msg, 50, "Unknown compression type %d", compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600225 png_warning(png_ptr, msg);
226#else
227 png_warning(png_ptr, "Unknown compression type");
228#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600229 }
230
231 /* We can't write the chunk until we find out how much data we have,
232 * which means we need to run the compressor first and save the
233 * output. This shouldn't be a problem, as the vast majority of
234 * comments should be reasonable, but we will set up an array of
235 * malloc'd pointers to be sure.
236 *
237 * If we knew the application was well behaved, we could simplify this
238 * greatly by assuming we can always malloc an output buffer large
239 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
240 * and malloc this directly. The only time this would be a bad idea is
241 * if we can't malloc more than 64K and we have 64K of random input
242 * data, or if the input string is incredibly large (although this
243 * wouldn't cause a failure, just a slowdown due to swapping).
244 */
245
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500246 /* Set up the compression buffers */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600247 png_ptr->zstream.avail_in = (uInt)text_len;
248 png_ptr->zstream.next_in = (Bytef *)text;
249 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
250 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
251
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500252 /* This is the same compression loop as in png_write_row() */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600253 do
254 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500255 /* Compress the data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600256 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
257 if (ret != Z_OK)
258 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500259 /* Error */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600260 if (png_ptr->zstream.msg != NULL)
261 png_error(png_ptr, png_ptr->zstream.msg);
262 else
263 png_error(png_ptr, "zlib error");
264 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500265 /* Check to see if we need more room */
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600266 if (!(png_ptr->zstream.avail_out))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600267 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500268 /* Make sure the output array has room */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600269 if (comp->num_output_ptr >= comp->max_output_ptr)
270 {
271 int old_max;
272
273 old_max = comp->max_output_ptr;
274 comp->max_output_ptr = comp->num_output_ptr + 4;
275 if (comp->output_ptr != NULL)
276 {
277 png_charpp old_ptr;
278
279 old_ptr = comp->output_ptr;
280 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600281 (png_alloc_size_t)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500282 (comp->max_output_ptr * png_sizeof(png_charpp)));
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500283 png_memcpy(comp->output_ptr, old_ptr, old_max
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500284 * png_sizeof(png_charp));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600285 png_free(png_ptr, old_ptr);
286 }
287 else
288 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600289 (png_alloc_size_t)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500290 (comp->max_output_ptr * png_sizeof(png_charp)));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600291 }
292
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500293 /* Save the data */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500294 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500295 (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600296 (png_alloc_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500297 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
298 png_ptr->zbuf_size);
299 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600300
301 /* and reset the buffer */
302 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
303 png_ptr->zstream.next_out = png_ptr->zbuf;
304 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500305 /* Continue until we don't have any more to compress */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600306 } while (png_ptr->zstream.avail_in);
307
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500308 /* Finish the compression */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600309 do
310 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500311 /* Tell zlib we are finished */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600312 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500313
314 if (ret == Z_OK)
315 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500316 /* Check to see if we need more room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500317 if (!(png_ptr->zstream.avail_out))
318 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500319 /* Check to make sure our output array has room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500320 if (comp->num_output_ptr >= comp->max_output_ptr)
321 {
322 int old_max;
323
324 old_max = comp->max_output_ptr;
325 comp->max_output_ptr = comp->num_output_ptr + 4;
326 if (comp->output_ptr != NULL)
327 {
328 png_charpp old_ptr;
329
330 old_ptr = comp->output_ptr;
331 /* This could be optimized to realloc() */
332 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600333 (png_alloc_size_t)(comp->max_output_ptr *
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500334 png_sizeof(png_charp)));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500335 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500336 old_max * png_sizeof(png_charp));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500337 png_free(png_ptr, old_ptr);
338 }
339 else
340 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600341 (png_alloc_size_t)(comp->max_output_ptr *
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500342 png_sizeof(png_charp)));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500343 }
344
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500345 /* Save the data */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500346 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500347 (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600348 (png_alloc_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500349 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
350 png_ptr->zbuf_size);
351 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500352
353 /* and reset the buffer pointers */
354 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
355 png_ptr->zstream.next_out = png_ptr->zbuf;
356 }
357 }
358 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600359 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500360 /* We got an error */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600361 if (png_ptr->zstream.msg != NULL)
362 png_error(png_ptr, png_ptr->zstream.msg);
363 else
364 png_error(png_ptr, "zlib error");
365 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600366 } while (ret != Z_STREAM_END);
367
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500368 /* Text length is number of buffers plus last buffer */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600369 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
370 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
371 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
372
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500373 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600374}
375
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500376/* Ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500377static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600378png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
379{
380 int i;
381
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500382 /* Handle the no-compression case */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600383 if (comp->input)
384 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500385 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500386 (png_size_t)comp->input_len);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500387 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600388 }
389
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500390 /* Write saved output buffers, if any */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600391 for (i = 0; i < comp->num_output_ptr; i++)
392 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500393 png_write_chunk_data(png_ptr, (png_bytep)comp->output_ptr[i],
394 (png_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600395 png_free(png_ptr, comp->output_ptr[i]);
396 }
397 if (comp->max_output_ptr != 0)
398 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500399 /* Write anything left in zbuf */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600400 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
401 png_write_chunk_data(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500402 (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600403
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500404 /* Reset zlib for another zTXt/iTXt or image data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600405 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600406 png_ptr->zstream.data_type = Z_BINARY;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600407}
408#endif
409
Guy Schalnat0d580581995-07-20 02:43:20 -0500410/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600411 * information. Note that the rest of this code depends upon this
412 * information being correct.
413 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500414void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600415png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500416 int bit_depth, int color_type, int compression_type, int filter_type,
417 int interlace_type)
418{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600419#ifdef PNG_USE_LOCAL_ARRAYS
420 PNG_IHDR;
421#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500422 int ret;
423
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500424 png_byte buf[13]; /* Buffer to store the IHDR info */
Guy Schalnat0d580581995-07-20 02:43:20 -0500425
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500426 png_debug(1, "in png_write_IHDR");
Guy Schalnate5a37791996-06-05 15:50:50 -0500427 /* Check that we have valid input data from the application info */
428 switch (color_type)
429 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500430 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500431 switch (bit_depth)
432 {
433 case 1:
434 case 2:
435 case 4:
436 case 8:
437 case 16: png_ptr->channels = 1; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500438 default: png_error(png_ptr, "Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500439 }
440 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500441 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500442 if (bit_depth != 8 && bit_depth != 16)
443 png_error(png_ptr, "Invalid bit depth for RGB image");
444 png_ptr->channels = 3;
445 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500446 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500447 switch (bit_depth)
448 {
449 case 1:
450 case 2:
451 case 4:
452 case 8: png_ptr->channels = 1; break;
453 default: png_error(png_ptr, "Invalid bit depth for paletted image");
454 }
455 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500456 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500457 if (bit_depth != 8 && bit_depth != 16)
458 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
459 png_ptr->channels = 2;
460 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500461 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500462 if (bit_depth != 8 && bit_depth != 16)
463 png_error(png_ptr, "Invalid bit depth for RGBA image");
464 png_ptr->channels = 4;
465 break;
466 default:
467 png_error(png_ptr, "Invalid image color type specified");
468 }
469
Andreas Dilger47a0c421997-05-16 02:46:07 -0500470 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500471 {
472 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500473 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500474 }
475
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600476 /* Write filter_method 64 (intrapixel differencing) only if
477 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
478 * 2. Libpng did not write a PNG signature (this filter_method is only
479 * used in PNG datastreams that are embedded in MNG datastreams) and
480 * 3. The application called png_permit_mng_features with a mask that
481 * included PNG_FLAG_MNG_FILTER_64 and
482 * 4. The filter_method is 64 and
483 * 5. The color_type is RGB or RGBA
484 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600485 if (
486#if defined(PNG_MNG_FEATURES_SUPPORTED)
487 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600488 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500489 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600490 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600491 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
492#endif
493 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500494 {
495 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500496 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500497 }
498
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600499#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500500 if (interlace_type != PNG_INTERLACE_NONE &&
501 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500502 {
503 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500504 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500505 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600506#else
507 interlace_type=PNG_INTERLACE_NONE;
508#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500509
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500510 /* Save the relevent information */
Guy Schalnate5a37791996-06-05 15:50:50 -0500511 png_ptr->bit_depth = (png_byte)bit_depth;
512 png_ptr->color_type = (png_byte)color_type;
513 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500514#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600515 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500516#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500517 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500518 png_ptr->width = width;
519 png_ptr->height = height;
520
521 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500522 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500523 /* Set the usr info, so any transformations can modify it */
Guy Schalnate5a37791996-06-05 15:50:50 -0500524 png_ptr->usr_width = png_ptr->width;
525 png_ptr->usr_bit_depth = png_ptr->bit_depth;
526 png_ptr->usr_channels = png_ptr->channels;
527
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500528 /* Pack the header information into the buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500529 png_save_uint_32(buf, width);
530 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600531 buf[8] = (png_byte)bit_depth;
532 buf[9] = (png_byte)color_type;
533 buf[10] = (png_byte)compression_type;
534 buf[11] = (png_byte)filter_type;
535 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500536
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500537 /* Write the chunk */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500538 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500539
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500540 /* Initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600541 png_ptr->zstream.zalloc = png_zalloc;
542 png_ptr->zstream.zfree = png_zfree;
543 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500544 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500545 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500546 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
547 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500548 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500549 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500550 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500551 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500552 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500553 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500554 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500555 png_ptr->zlib_strategy = Z_FILTERED;
556 else
557 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
558 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500559 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500560 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500561 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500562 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500563 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500564 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500565 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500566 png_ptr->zlib_method = 8;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500567 ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
568 png_ptr->zlib_method, png_ptr->zlib_window_bits,
569 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
570 if (ret != Z_OK)
571 {
572 if (ret == Z_VERSION_ERROR) png_error(png_ptr,
573 "zlib failed to initialize compressor -- version error");
574 if (ret == Z_STREAM_ERROR) png_error(png_ptr,
575 "zlib failed to initialize compressor -- stream error");
576 if (ret == Z_MEM_ERROR) png_error(png_ptr,
577 "zlib failed to initialize compressor -- mem error");
578 png_error(png_ptr, "zlib failed to initialize compressor");
579 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600580 png_ptr->zstream.next_out = png_ptr->zbuf;
581 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600582 /* libpng is not interested in zstream.data_type */
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500583 /* Set it to a predefined value, to avoid its evaluation inside zlib */
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600584 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500585
Guy Schalnate5a37791996-06-05 15:50:50 -0500586 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500587}
588
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500589/* Write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500590 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600591 * structure.
592 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500593void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500594png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500595{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600596#ifdef PNG_USE_LOCAL_ARRAYS
597 PNG_PLTE;
598#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500599 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600600 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500601 png_byte buf[3];
602
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500603 png_debug(1, "in png_write_PLTE");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500604 if ((
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500605#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600606 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500607#endif
608 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500609 {
610 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500611 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500612 png_error(png_ptr, "Invalid number of colors in palette");
613 }
614 else
615 {
616 png_warning(png_ptr, "Invalid number of colors in palette");
617 return;
618 }
619 }
620
621 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
622 {
623 png_warning(png_ptr,
624 "Ignoring request to write a PLTE chunk in grayscale PNG");
625 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500626 }
627
Andreas Dilger47a0c421997-05-16 02:46:07 -0500628 png_ptr->num_palette = (png_uint_16)num_pal;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500629 png_debug1(3, "num_palette = %d", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500630
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500631 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE,
632 (png_uint_32)(num_pal * 3));
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500633#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500634 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500635 {
636 buf[0] = pal_ptr->red;
637 buf[1] = pal_ptr->green;
638 buf[2] = pal_ptr->blue;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500639 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500640 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500641#else
642 /* This is a little slower but some buggy compilers need to do this instead */
643 pal_ptr=palette;
644 for (i = 0; i < num_pal; i++)
645 {
646 buf[0] = pal_ptr[i].red;
647 buf[1] = pal_ptr[i].green;
648 buf[2] = pal_ptr[i].blue;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500649 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500650 }
651#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500652 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500653 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500654}
655
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500656/* Write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500657void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500658png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500659{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600660#ifdef PNG_USE_LOCAL_ARRAYS
661 PNG_IDAT;
662#endif
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500663 png_debug(1, "in png_write_IDAT");
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500664
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500665 /* Optimize the CMF field in the zlib stream. */
666 /* This hack of the zlib stream is compliant to the stream specification. */
667 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
668 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
669 {
670 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
671 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
672 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500673 /* Avoid memory underflows and multiplication overflows.
674 *
675 * The conditions below are practically always satisfied;
676 * however, they still must be checked.
677 */
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500678 if (length >= 2 &&
679 png_ptr->height < 16384 && png_ptr->width < 16384)
680 {
681 png_uint_32 uncompressed_idat_size = png_ptr->height *
682 ((png_ptr->width *
683 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
684 unsigned int z_cinfo = z_cmf >> 4;
685 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
686 while (uncompressed_idat_size <= half_z_window_size &&
687 half_z_window_size >= 256)
688 {
689 z_cinfo--;
690 half_z_window_size >>= 1;
691 }
692 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
693 if (data[0] != (png_byte)z_cmf)
694 {
695 data[0] = (png_byte)z_cmf;
696 data[1] &= 0xe0;
697 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
698 }
699 }
700 }
701 else
702 png_error(png_ptr,
703 "Invalid zlib compression method or flags in IDAT");
704 }
705
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600706 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500707 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500708}
709
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500710/* Write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500711void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600712png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500713{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600714#ifdef PNG_USE_LOCAL_ARRAYS
715 PNG_IEND;
716#endif
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500717 png_debug(1, "in png_write_IEND");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500718 png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL,
719 (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600720 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500721}
722
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500723#if defined(PNG_WRITE_gAMA_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500724/* Write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600725#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500726void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500727png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500728{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600729#ifdef PNG_USE_LOCAL_ARRAYS
730 PNG_gAMA;
731#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500732 png_uint_32 igamma;
733 png_byte buf[4];
734
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500735 png_debug(1, "in png_write_gAMA");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600736 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600737 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500738 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500739 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500740}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500741#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600742#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500743void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600744png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600745{
746#ifdef PNG_USE_LOCAL_ARRAYS
747 PNG_gAMA;
748#endif
749 png_byte buf[4];
750
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500751 png_debug(1, "in png_write_gAMA");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600752 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500753 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500754 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600755}
756#endif
757#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500758
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600759#if defined(PNG_WRITE_sRGB_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500760/* Write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500761void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600762png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600763{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600764#ifdef PNG_USE_LOCAL_ARRAYS
765 PNG_sRGB;
766#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600767 png_byte buf[1];
768
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500769 png_debug(1, "in png_write_sRGB");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500770 if (srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600771 png_warning(png_ptr,
772 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600773 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500774 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600775}
776#endif
777
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600778#if defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500779/* Write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500780void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600781png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
782 png_charp profile, int profile_len)
783{
784#ifdef PNG_USE_LOCAL_ARRAYS
785 PNG_iCCP;
786#endif
787 png_size_t name_len;
788 png_charp new_name;
789 compression_state comp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500790 int embedded_profile_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600791
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500792 png_debug(1, "in png_write_iCCP");
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600793
794 comp.num_output_ptr = 0;
795 comp.max_output_ptr = 0;
796 comp.output_ptr = NULL;
797 comp.input = NULL;
798 comp.input_len = 0;
799
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -0600800 if ((name_len = png_check_keyword(png_ptr, name,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600801 &new_name)) == 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600802 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600803
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600804 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600805 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600806
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500807 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600808 profile_len = 0;
809
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500810 if (profile_len > 3)
Glenn Randers-Pehrson7edd4582006-12-07 19:16:44 -0600811 embedded_profile_len =
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500812 ((*( (png_bytep)profile ))<<24) |
813 ((*( (png_bytep)profile + 1))<<16) |
814 ((*( (png_bytep)profile + 2))<< 8) |
815 ((*( (png_bytep)profile + 3)) );
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500816
817 if (profile_len < embedded_profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500818 {
819 png_warning(png_ptr,
820 "Embedded profile length too large in iCCP chunk");
Glenn Randers-Pehrsona6cc6272009-04-01 14:18:43 -0500821 png_free(png_ptr, new_name);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500822 return;
823 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500824
825 if (profile_len > embedded_profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500826 {
827 png_warning(png_ptr,
828 "Truncating profile to actual length in iCCP chunk");
829 profile_len = embedded_profile_len;
830 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500831
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600832 if (profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500833 profile_len = png_text_compress(png_ptr, profile,
834 (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600835
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500836 /* Make sure we include the NULL after the name and the compression type */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600837 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500838 (png_uint_32)(name_len + profile_len + 2));
839 new_name[name_len + 1] = 0x00;
840 png_write_chunk_data(png_ptr, (png_bytep)new_name,
841 (png_size_t)(name_len + 2));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600842
843 if (profile_len)
844 png_write_compressed_data_out(png_ptr, &comp);
845
846 png_write_chunk_end(png_ptr);
847 png_free(png_ptr, new_name);
848}
849#endif
850
851#if defined(PNG_WRITE_sPLT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500852/* Write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500853void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600854png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600855{
856#ifdef PNG_USE_LOCAL_ARRAYS
857 PNG_sPLT;
858#endif
859 png_size_t name_len;
860 png_charp new_name;
861 png_byte entrybuf[10];
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500862 png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
863 png_size_t palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600864 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500865#ifdef PNG_NO_POINTER_INDEXING
866 int i;
867#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600868
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500869 png_debug(1, "in png_write_sPLT");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500870 if ((name_len = png_check_keyword(png_ptr,spalette->name, &new_name))==0)
871 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600872
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500873 /* Make sure we include the NULL after the name */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500874 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500875 (png_uint_32)(name_len + 2 + palette_size));
876 png_write_chunk_data(png_ptr, (png_bytep)new_name,
877 (png_size_t)(name_len + 1));
878 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600879
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500880 /* Loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500881#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500882 for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600883 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500884 if (spalette->depth == 8)
885 {
886 entrybuf[0] = (png_byte)ep->red;
887 entrybuf[1] = (png_byte)ep->green;
888 entrybuf[2] = (png_byte)ep->blue;
889 entrybuf[3] = (png_byte)ep->alpha;
890 png_save_uint_16(entrybuf + 4, ep->frequency);
891 }
892 else
893 {
894 png_save_uint_16(entrybuf + 0, ep->red);
895 png_save_uint_16(entrybuf + 2, ep->green);
896 png_save_uint_16(entrybuf + 4, ep->blue);
897 png_save_uint_16(entrybuf + 6, ep->alpha);
898 png_save_uint_16(entrybuf + 8, ep->frequency);
899 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500900 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600901 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500902#else
903 ep=spalette->entries;
904 for (i=0; i>spalette->nentries; i++)
905 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500906 if (spalette->depth == 8)
907 {
908 entrybuf[0] = (png_byte)ep[i].red;
909 entrybuf[1] = (png_byte)ep[i].green;
910 entrybuf[2] = (png_byte)ep[i].blue;
911 entrybuf[3] = (png_byte)ep[i].alpha;
912 png_save_uint_16(entrybuf + 4, ep[i].frequency);
913 }
914 else
915 {
916 png_save_uint_16(entrybuf + 0, ep[i].red);
917 png_save_uint_16(entrybuf + 2, ep[i].green);
918 png_save_uint_16(entrybuf + 4, ep[i].blue);
919 png_save_uint_16(entrybuf + 6, ep[i].alpha);
920 png_save_uint_16(entrybuf + 8, ep[i].frequency);
921 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500922 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500923 }
924#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600925
926 png_write_chunk_end(png_ptr);
927 png_free(png_ptr, new_name);
928}
929#endif
930
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500931#if defined(PNG_WRITE_sBIT_SUPPORTED)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500932/* Write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500933void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600934png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500935{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600936#ifdef PNG_USE_LOCAL_ARRAYS
937 PNG_sBIT;
938#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500939 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500940 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500941
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500942 png_debug(1, "in png_write_sBIT");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500943 /* Make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500944 if (color_type & PNG_COLOR_MASK_COLOR)
945 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600946 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500947
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500948 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
949 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600950 if (sbit->red == 0 || sbit->red > maxbits ||
951 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500952 sbit->blue == 0 || sbit->blue > maxbits)
953 {
954 png_warning(png_ptr, "Invalid sBIT depth specified");
955 return;
956 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500957 buf[0] = sbit->red;
958 buf[1] = sbit->green;
959 buf[2] = sbit->blue;
960 size = 3;
961 }
962 else
963 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500964 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
965 {
966 png_warning(png_ptr, "Invalid sBIT depth specified");
967 return;
968 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500969 buf[0] = sbit->gray;
970 size = 1;
971 }
972
973 if (color_type & PNG_COLOR_MASK_ALPHA)
974 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500975 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
976 {
977 png_warning(png_ptr, "Invalid sBIT depth specified");
978 return;
979 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500980 buf[size++] = sbit->alpha;
981 }
982
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600983 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500984}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500985#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500986
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500987#if defined(PNG_WRITE_cHRM_SUPPORTED)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500988/* Write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600989#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500990void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500991png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600992 double red_x, double red_y, double green_x, double green_y,
993 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500994{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600995#ifdef PNG_USE_LOCAL_ARRAYS
996 PNG_cHRM;
997#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500998 png_byte buf[32];
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600999
1000 png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y,
1001 int_green_x, int_green_y, int_blue_x, int_blue_y;
Guy Schalnat0d580581995-07-20 02:43:20 -05001002
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001003 png_debug(1, "in png_write_cHRM");
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -06001004
1005 int_white_x = (png_uint_32)(white_x * 100000.0 + 0.5);
1006 int_white_y = (png_uint_32)(white_y * 100000.0 + 0.5);
1007 int_red_x = (png_uint_32)(red_x * 100000.0 + 0.5);
1008 int_red_y = (png_uint_32)(red_y * 100000.0 + 0.5);
1009 int_green_x = (png_uint_32)(green_x * 100000.0 + 0.5);
1010 int_green_y = (png_uint_32)(green_y * 100000.0 + 0.5);
1011 int_blue_x = (png_uint_32)(blue_x * 100000.0 + 0.5);
1012 int_blue_y = (png_uint_32)(blue_y * 100000.0 + 0.5);
1013
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001014#if !defined(PNG_NO_CHECK_cHRM)
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -06001015 if (png_check_cHRM_fixed(png_ptr, int_white_x, int_white_y,
1016 int_red_x, int_red_y, int_green_x, int_green_y, int_blue_x, int_blue_y))
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001017#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001018 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001019 /* Each value is saved in 1/100,000ths */
Glenn Randers-Pehrsond60c8862009-06-15 21:56:14 -05001020
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001021 png_save_uint_32(buf, int_white_x);
1022 png_save_uint_32(buf + 4, int_white_y);
Guy Schalnate5a37791996-06-05 15:50:50 -05001023
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001024 png_save_uint_32(buf + 8, int_red_x);
1025 png_save_uint_32(buf + 12, int_red_y);
Guy Schalnate5a37791996-06-05 15:50:50 -05001026
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001027 png_save_uint_32(buf + 16, int_green_x);
1028 png_save_uint_32(buf + 20, int_green_y);
Guy Schalnate5a37791996-06-05 15:50:50 -05001029
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001030 png_save_uint_32(buf + 24, int_blue_x);
1031 png_save_uint_32(buf + 28, int_blue_y);
Guy Schalnate5a37791996-06-05 15:50:50 -05001032
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001033 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001034 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001035}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001036#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001037#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001038void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001039png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
1040 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
1041 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
1042 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001043{
1044#ifdef PNG_USE_LOCAL_ARRAYS
1045 PNG_cHRM;
1046#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001047 png_byte buf[32];
1048
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001049 png_debug(1, "in png_write_cHRM");
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001050 /* Each value is saved in 1/100,000ths */
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001051#if !defined(PNG_NO_CHECK_cHRM)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001052 if (png_check_cHRM_fixed(png_ptr, white_x, white_y, red_x, red_y,
1053 green_x, green_y, blue_x, blue_y))
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001054#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001055 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001056 png_save_uint_32(buf, (png_uint_32)white_x);
1057 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001058
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001059 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1060 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001061
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001062 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1063 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001064
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001065 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1066 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001067
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001068 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001069 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001070}
1071#endif
1072#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001073
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001074#if defined(PNG_WRITE_tRNS_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001075/* Write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001076void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001077png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -05001078 int num_trans, int color_type)
1079{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001080#ifdef PNG_USE_LOCAL_ARRAYS
1081 PNG_tRNS;
1082#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001083 png_byte buf[6];
1084
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001085 png_debug(1, "in png_write_tRNS");
Guy Schalnat0d580581995-07-20 02:43:20 -05001086 if (color_type == PNG_COLOR_TYPE_PALETTE)
1087 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001088 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001089 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001090 png_warning(png_ptr, "Invalid number of transparent colors specified");
Guy Schalnate5a37791996-06-05 15:50:50 -05001091 return;
1092 }
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001093 /* Write the chunk out as it is */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001094 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans,
1095 (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001096 }
1097 else if (color_type == PNG_COLOR_TYPE_GRAY)
1098 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001099 /* One 16 bit value */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001100 if (tran->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001101 {
1102 png_warning(png_ptr,
1103 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1104 return;
1105 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001106 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001107 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001108 }
1109 else if (color_type == PNG_COLOR_TYPE_RGB)
1110 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001111 /* Three 16 bit values */
Guy Schalnat0d580581995-07-20 02:43:20 -05001112 png_save_uint_16(buf, tran->red);
1113 png_save_uint_16(buf + 2, tran->green);
1114 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001115 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001116 {
1117 png_warning(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001118 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001119 return;
1120 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001121 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001122 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001123 else
1124 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001125 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001126 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001127}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001128#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001129
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001130#if defined(PNG_WRITE_bKGD_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001131/* Write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001132void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001133png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001134{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001135#ifdef PNG_USE_LOCAL_ARRAYS
1136 PNG_bKGD;
1137#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001138 png_byte buf[6];
1139
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001140 png_debug(1, "in png_write_bKGD");
Guy Schalnat0d580581995-07-20 02:43:20 -05001141 if (color_type == PNG_COLOR_TYPE_PALETTE)
1142 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001143 if (
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001144#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001145 (png_ptr->num_palette ||
1146 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001147#endif
Glenn Randers-Pehrsond6d80752008-12-02 09:49:43 -06001148 back->index >= png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001149 {
1150 png_warning(png_ptr, "Invalid background palette index");
1151 return;
1152 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001153 buf[0] = back->index;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001154 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001155 }
1156 else if (color_type & PNG_COLOR_MASK_COLOR)
1157 {
1158 png_save_uint_16(buf, back->red);
1159 png_save_uint_16(buf + 2, back->green);
1160 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001161 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001162 {
1163 png_warning(png_ptr,
1164 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1165 return;
1166 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001167 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001168 }
1169 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001170 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001171 if (back->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001172 {
1173 png_warning(png_ptr,
1174 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1175 return;
1176 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001177 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001178 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001179 }
1180}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001181#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001182
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001183#if defined(PNG_WRITE_hIST_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001184/* Write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001185void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001186png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001187{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001188#ifdef PNG_USE_LOCAL_ARRAYS
1189 PNG_hIST;
1190#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001191 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001192 png_byte buf[3];
1193
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001194 png_debug(1, "in png_write_hIST");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001195 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001196 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001197 png_debug2(3, "num_hist = %d, num_palette = %d", num_hist,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001198 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001199 png_warning(png_ptr, "Invalid number of histogram entries specified");
1200 return;
1201 }
1202
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001203 png_write_chunk_start(png_ptr, (png_bytep)png_hIST,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001204 (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001205 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001206 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001207 png_save_uint_16(buf, hist[i]);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001208 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001209 }
1210 png_write_chunk_end(png_ptr);
1211}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001212#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001213
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001214#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1215 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001216/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1217 * and if invalid, correct the keyword rather than discarding the entire
1218 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1219 * length, forbids leading or trailing whitespace, multiple internal spaces,
1220 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001221 *
1222 * The new_key is allocated to hold the corrected keyword and must be freed
1223 * by the calling routine. This avoids problems with trying to write to
1224 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001225 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001226png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001227png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001228{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001229 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001230 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001231 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001232 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001233
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001234 png_debug(1, "in png_check_keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001235 *new_key = NULL;
1236
1237 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001238 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001239 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001240 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001241 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001242
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001243 png_debug1(2, "Keyword to be checked is '%s'", key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001244
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001245 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001246 if (*new_key == NULL)
1247 {
1248 png_warning(png_ptr, "Out of memory while procesing keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001249 return ((png_size_t)0);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001250 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001251
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001252 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001253 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001254 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001255 if ((png_byte)*kp < 0x20 ||
1256 ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001257 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001258#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001259 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001260
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001261 png_snprintf(msg, 40,
1262 "invalid keyword character 0x%02X", (png_byte)*kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001263 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001264#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001265 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001266#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001267 *dp = ' ';
1268 }
1269 else
1270 {
1271 *dp = *kp;
1272 }
1273 }
1274 *dp = '\0';
1275
1276 /* Remove any trailing white space. */
1277 kp = *new_key + key_len - 1;
1278 if (*kp == ' ')
1279 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001280 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001281
1282 while (*kp == ' ')
1283 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001284 *(kp--) = '\0';
1285 key_len--;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001286 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001287 }
1288
1289 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001290 kp = *new_key;
1291 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001292 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001293 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001294
1295 while (*kp == ' ')
1296 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001297 kp++;
1298 key_len--;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001299 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001300 }
1301
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001302 png_debug1(2, "Checking for multiple internal spaces in '%s'", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001303
Andreas Dilger47a0c421997-05-16 02:46:07 -05001304 /* Remove multiple internal spaces. */
1305 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001306 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001307 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001308 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001309 *(dp++) = *kp;
1310 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001311 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001312 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001313 {
1314 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001315 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001316 }
1317 else
1318 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001319 *(dp++) = *kp;
1320 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001321 }
1322 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001323 *dp = '\0';
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001324 if (kwarn)
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001325 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001326
1327 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001328 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001329 png_free(png_ptr, *new_key);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001330 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001331 }
1332
1333 if (key_len > 79)
1334 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001335 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001336 (*new_key)[79] = '\0';
Andreas Dilger47a0c421997-05-16 02:46:07 -05001337 key_len = 79;
1338 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001339
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001340 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001341}
1342#endif
1343
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001344#if defined(PNG_WRITE_tEXt_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001345/* Write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001346void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001347png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001348 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001349{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001350#ifdef PNG_USE_LOCAL_ARRAYS
1351 PNG_tEXt;
1352#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001353 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001354 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001355
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001356 png_debug(1, "in png_write_tEXt");
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001357 if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001358 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001359
Andreas Dilger47a0c421997-05-16 02:46:07 -05001360 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001361 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001362 else
1363 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001364
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001365 /* Make sure we include the 0 after the key */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001366 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001367 (png_uint_32)(key_len + text_len + 1));
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001368 /*
1369 * We leave it to the application to meet PNG-1.0 requirements on the
1370 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1371 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001372 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001373 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001374 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1375 (png_size_t)(key_len + 1));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001376 if (text_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001377 png_write_chunk_data(png_ptr, (png_bytep)text, (png_size_t)text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001378
Guy Schalnat0d580581995-07-20 02:43:20 -05001379 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001380 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001381}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001382#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001383
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001384#if defined(PNG_WRITE_zTXt_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001385/* Write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001386void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001387png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001388 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001389{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001390#ifdef PNG_USE_LOCAL_ARRAYS
1391 PNG_zTXt;
1392#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001393 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001394 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001395 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001396 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001397
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001398 png_debug(1, "in png_write_zTXt");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001399
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001400 comp.num_output_ptr = 0;
1401 comp.max_output_ptr = 0;
1402 comp.output_ptr = NULL;
1403 comp.input = NULL;
1404 comp.input_len = 0;
1405
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001406 if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001407 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001408 png_free(png_ptr, new_key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001409 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001410 }
1411
Andreas Dilger47a0c421997-05-16 02:46:07 -05001412 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1413 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001414 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001415 png_free(png_ptr, new_key);
1416 return;
1417 }
1418
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001419 text_len = png_strlen(text);
1420
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001421 /* Compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001422 text_len = png_text_compress(png_ptr, text, text_len, compression,
1423 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001424
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001425 /* Write start of chunk */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001426 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt,
1427 (png_uint_32)(key_len+text_len + 2));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001428 /* Write key */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001429 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1430 (png_size_t)(key_len + 1));
1431 png_free(png_ptr, new_key);
1432
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001433 buf[0] = (png_byte)compression;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001434 /* Write compression */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001435 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001436 /* Write the compressed data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001437 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001438
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001439 /* Close the chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -05001440 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001441}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001442#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001443
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001444#if defined(PNG_WRITE_iTXt_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001445/* Write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001446void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001447png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001448 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001449{
1450#ifdef PNG_USE_LOCAL_ARRAYS
1451 PNG_iTXt;
1452#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001453 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001454 png_charp new_lang;
1455 png_charp new_key = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001456 png_byte cbuf[2];
1457 compression_state comp;
1458
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001459 png_debug(1, "in png_write_iTXt");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001460
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001461 comp.num_output_ptr = 0;
1462 comp.max_output_ptr = 0;
1463 comp.output_ptr = NULL;
1464 comp.input = NULL;
1465
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001466 if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001467 return;
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001468
1469 if ((lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001470 {
1471 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001472 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001473 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001474 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001475
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001476 if (lang_key == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001477 lang_key_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001478 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001479 lang_key_len = png_strlen(lang_key);
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001480
1481 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001482 text_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001483 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001484 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001485
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001486 /* Compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001487 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1488 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001489
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001490
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001491 /* Make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001492 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001493
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001494 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1495 (png_uint_32)(
1496 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1497 + key_len
1498 + lang_len
1499 + lang_key_len
1500 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001501
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001502 /* We leave it to the application to meet PNG-1.0 requirements on the
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001503 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1504 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1505 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1506 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001507 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1508 (png_size_t)(key_len + 1));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001509
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001510 /* Set the compression flag */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001511 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1512 compression == PNG_TEXT_COMPRESSION_NONE)
1513 cbuf[0] = 0;
1514 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1515 cbuf[0] = 1;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001516 /* Set the compression method */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001517 cbuf[1] = 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001518 png_write_chunk_data(png_ptr, cbuf, (png_size_t)2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001519
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001520 cbuf[0] = 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001521 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf),
1522 (png_size_t)(lang_len + 1));
1523 png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf),
1524 (png_size_t)(lang_key_len + 1));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001525 png_write_compressed_data_out(png_ptr, &comp);
1526
1527 png_write_chunk_end(png_ptr);
1528 png_free(png_ptr, new_key);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001529 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001530}
1531#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001532
1533#if defined(PNG_WRITE_oFFs_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001534/* Write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001535void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001536png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001537 int unit_type)
1538{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001539#ifdef PNG_USE_LOCAL_ARRAYS
1540 PNG_oFFs;
1541#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001542 png_byte buf[9];
1543
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001544 png_debug(1, "in png_write_oFFs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001545 if (unit_type >= PNG_OFFSET_LAST)
1546 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1547
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001548 png_save_int_32(buf, x_offset);
1549 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001550 buf[8] = (png_byte)unit_type;
1551
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001552 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001553}
1554#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001555#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001556/* Write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001557void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001558png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1559 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1560{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001561#ifdef PNG_USE_LOCAL_ARRAYS
1562 PNG_pCAL;
1563#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001564 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001565 png_uint_32p params_len;
1566 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001567 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001568 int i;
1569
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001570 png_debug1(1, "in png_write_pCAL (%d parameters)", nparams);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001571 if (type >= PNG_EQUATION_LAST)
1572 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1573
1574 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001575 png_debug1(3, "pCAL purpose length = %d", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001576 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001577 png_debug1(3, "pCAL units length = %d", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001578 total_len = purpose_len + units_len + 10;
1579
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001580 params_len = (png_uint_32p)png_malloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06001581 (png_alloc_size_t)(nparams * png_sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001582
1583 /* Find the length of each parameter, making sure we don't count the
1584 null terminator for the last parameter. */
1585 for (i = 0; i < nparams; i++)
1586 {
1587 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001588 png_debug2(3, "pCAL parameter %d length = %lu", i,
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001589 (unsigned long) params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001590 total_len += (png_size_t)params_len[i];
1591 }
1592
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001593 png_debug1(3, "pCAL total length = %d", (int)total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001594 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001595 png_write_chunk_data(png_ptr, (png_bytep)new_purpose,
1596 (png_size_t)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001597 png_save_int_32(buf, X0);
1598 png_save_int_32(buf + 4, X1);
1599 buf[8] = (png_byte)type;
1600 buf[9] = (png_byte)nparams;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001601 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1602 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001603
1604 png_free(png_ptr, new_purpose);
1605
1606 for (i = 0; i < nparams; i++)
1607 {
1608 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1609 (png_size_t)params_len[i]);
1610 }
1611
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001612 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001613 png_write_chunk_end(png_ptr);
1614}
1615#endif
1616
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001617#if defined(PNG_WRITE_sCAL_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001618/* Write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001619#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001620void /* PRIVATE */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001621png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001622{
1623#ifdef PNG_USE_LOCAL_ARRAYS
1624 PNG_sCAL;
1625#endif
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001626 char buf[64];
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001627 png_size_t total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001628
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001629 png_debug(1, "in png_write_sCAL");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001630
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001631 buf[0] = (char)unit;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001632 png_snprintf(buf + 1, 63, "%12.12e", width);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001633 total_len = 1 + png_strlen(buf + 1) + 1;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001634 png_snprintf(buf + total_len, 64-total_len, "%12.12e", height);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001635 total_len += png_strlen(buf + total_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001636
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001637 png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001638 png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001639}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001640#else
1641#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001642void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001643png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001644 png_charp height)
1645{
1646#ifdef PNG_USE_LOCAL_ARRAYS
1647 PNG_sCAL;
1648#endif
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001649 png_byte buf[64];
1650 png_size_t wlen, hlen, total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001651
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001652 png_debug(1, "in png_write_sCAL_s");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001653
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001654 wlen = png_strlen(width);
1655 hlen = png_strlen(height);
1656 total_len = wlen + hlen + 2;
1657 if (total_len > 64)
1658 {
1659 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
1660 return;
1661 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001662
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001663 buf[0] = (png_byte)unit;
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001664 png_memcpy(buf + 1, width, wlen + 1); /* Append the '\0' here */
1665 png_memcpy(buf + wlen + 2, height, hlen); /* Do NOT append the '\0' here */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001666
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001667 png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001668 png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001669}
1670#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001671#endif
1672#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001673
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001674#if defined(PNG_WRITE_pHYs_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001675/* Write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001676void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001677png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001678 png_uint_32 y_pixels_per_unit,
1679 int unit_type)
1680{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001681#ifdef PNG_USE_LOCAL_ARRAYS
1682 PNG_pHYs;
1683#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001684 png_byte buf[9];
1685
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001686 png_debug(1, "in png_write_pHYs");
Guy Schalnate5a37791996-06-05 15:50:50 -05001687 if (unit_type >= PNG_RESOLUTION_LAST)
1688 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1689
Guy Schalnat0d580581995-07-20 02:43:20 -05001690 png_save_uint_32(buf, x_pixels_per_unit);
1691 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001692 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001693
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001694 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001695}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001696#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001697
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001698#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001699/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1700 * or png_convert_from_time_t(), or fill in the structure yourself.
1701 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001702void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001703png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001704{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001705#ifdef PNG_USE_LOCAL_ARRAYS
1706 PNG_tIME;
1707#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001708 png_byte buf[7];
1709
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001710 png_debug(1, "in png_write_tIME");
Guy Schalnate5a37791996-06-05 15:50:50 -05001711 if (mod_time->month > 12 || mod_time->month < 1 ||
1712 mod_time->day > 31 || mod_time->day < 1 ||
1713 mod_time->hour > 23 || mod_time->second > 60)
1714 {
1715 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1716 return;
1717 }
1718
Guy Schalnat0d580581995-07-20 02:43:20 -05001719 png_save_uint_16(buf, mod_time->year);
1720 buf[2] = mod_time->month;
1721 buf[3] = mod_time->day;
1722 buf[4] = mod_time->hour;
1723 buf[5] = mod_time->minute;
1724 buf[6] = mod_time->second;
1725
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001726 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001727}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001728#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001729
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001730/* Initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001731void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001732png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001733{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001734#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001735#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001736 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001737
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001738 /* Start of interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001739 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001740
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001741 /* Offset to next interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001742 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001743
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001744 /* Start of interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001745 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001746
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001747 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001748 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001749#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001750#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001751
Andreas Dilger47a0c421997-05-16 02:46:07 -05001752 png_size_t buf_size;
1753
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001754 png_debug(1, "in png_write_start_row");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001755 buf_size = (png_size_t)(PNG_ROWBYTES(
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001756 png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001757
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001758 /* Set up row buffer */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001759 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06001760 (png_alloc_size_t)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001761 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001762
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001763#ifndef PNG_NO_WRITE_FILTER
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001764 /* Set up filtering buffer, if using this filter */
Guy Schalnate5a37791996-06-05 15:50:50 -05001765 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001766 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001767 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06001768 (png_alloc_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001769 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001770 }
1771
Andreas Dilger47a0c421997-05-16 02:46:07 -05001772 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001773 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1774 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001775 /* Set up previous row buffer */
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001776#ifdef PNG_CALLOC_SUPPORTED
1777 png_ptr->prev_row = (png_bytep)png_calloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06001778 (png_alloc_size_t)buf_size);
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001779#else
1780 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr,
1781 (png_uint_32)buf_size);
1782 png_memset(png_ptr->prev_row, 0, buf_size);
1783#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001784
1785 if (png_ptr->do_filter & PNG_FILTER_UP)
1786 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001787 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001788 (png_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001789 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001790 }
1791
1792 if (png_ptr->do_filter & PNG_FILTER_AVG)
1793 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001794 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06001795 (png_alloc_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001796 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001797 }
1798
1799 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1800 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001801 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001802 (png_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001803 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001804 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001805 }
Glenn Randers-Pehrsoneb580912008-07-30 14:47:09 -05001806#endif /* PNG_NO_WRITE_FILTER */
Guy Schalnat0d580581995-07-20 02:43:20 -05001807
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001808#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001809 /* If interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001810 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001811 {
1812 if (!(png_ptr->transformations & PNG_INTERLACE))
1813 {
1814 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1815 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001816 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1817 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001818 }
1819 else
1820 {
1821 png_ptr->num_rows = png_ptr->height;
1822 png_ptr->usr_width = png_ptr->width;
1823 }
1824 }
1825 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001826#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001827 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001828 png_ptr->num_rows = png_ptr->height;
1829 png_ptr->usr_width = png_ptr->width;
1830 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001831 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1832 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001833}
1834
Andreas Dilger47a0c421997-05-16 02:46:07 -05001835/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001836void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001837png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001838{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001839#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001840#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001841 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001842
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001843 /* Start of interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001844 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001845
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001846 /* Offset to next interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001847 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001848
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001849 /* Start of interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001850 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001851
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001852 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001853 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001854#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001855#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001856
Guy Schalnat0d580581995-07-20 02:43:20 -05001857 int ret;
1858
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001859 png_debug(1, "in png_write_finish_row");
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001860 /* Next row */
Guy Schalnat0d580581995-07-20 02:43:20 -05001861 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001862
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001863 /* See if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001864 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001865 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001866
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001867#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001868 /* If interlaced, go to next pass */
Guy Schalnat0d580581995-07-20 02:43:20 -05001869 if (png_ptr->interlaced)
1870 {
1871 png_ptr->row_number = 0;
1872 if (png_ptr->transformations & PNG_INTERLACE)
1873 {
1874 png_ptr->pass++;
1875 }
1876 else
1877 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001878 /* Loop until we find a non-zero width or height pass */
Guy Schalnat0d580581995-07-20 02:43:20 -05001879 do
1880 {
1881 png_ptr->pass++;
1882 if (png_ptr->pass >= 7)
1883 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001884 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001885 png_pass_inc[png_ptr->pass] - 1 -
1886 png_pass_start[png_ptr->pass]) /
1887 png_pass_inc[png_ptr->pass];
1888 png_ptr->num_rows = (png_ptr->height +
1889 png_pass_yinc[png_ptr->pass] - 1 -
1890 png_pass_ystart[png_ptr->pass]) /
1891 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001892 if (png_ptr->transformations & PNG_INTERLACE)
1893 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001894 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1895
1896 }
1897
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001898 /* Reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001899 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001900 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001901 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001902 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001903 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001904 png_ptr->usr_bit_depth, png_ptr->width)) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001905 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001906 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001907 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001908#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001909
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001910 /* If we get here, we've just written the last row, so we need
Guy Schalnat0d580581995-07-20 02:43:20 -05001911 to flush the compressor */
1912 do
1913 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001914 /* Tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001915 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001916 /* Check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001917 if (ret == Z_OK)
1918 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001919 /* Check to see if we need more room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001920 if (!(png_ptr->zstream.avail_out))
1921 {
1922 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1923 png_ptr->zstream.next_out = png_ptr->zbuf;
1924 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1925 }
1926 }
1927 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001928 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001929 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001930 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001931 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001932 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001933 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001934 } while (ret != Z_STREAM_END);
1935
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001936 /* Write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001937 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001938 {
1939 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001940 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001941 }
1942
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001943 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -06001944 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat0d580581995-07-20 02:43:20 -05001945}
1946
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001947#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001948/* Pick out the correct pixels for the interlace pass.
1949 * The basic idea here is to go through the row with a source
1950 * pointer and a destination pointer (sp and dp), and copy the
1951 * correct pixels for the pass. As the row gets compacted,
1952 * sp will always be >= dp, so we should never overwrite anything.
1953 * See the default: case for the easiest code to understand.
1954 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001955void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001956png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001957{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001958#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001959 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001960
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001961 /* Start of interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001962 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001963
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001964 /* Offset to next interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001965 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001966#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001967
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001968 png_debug(1, "in png_do_write_interlace");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001969 /* We don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001970 if (pass < 6)
Guy Schalnat0d580581995-07-20 02:43:20 -05001971 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001972 /* Each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001973 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001974 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001975 case 1:
1976 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001977 png_bytep sp;
1978 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001979 int shift;
1980 int d;
1981 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001982 png_uint_32 i;
1983 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001984
1985 dp = row;
1986 d = 0;
1987 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001988 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001989 i += png_pass_inc[pass])
1990 {
1991 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001992 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001993 d |= (value << shift);
1994
1995 if (shift == 0)
1996 {
1997 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001998 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001999 d = 0;
2000 }
2001 else
2002 shift--;
2003
2004 }
2005 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002006 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002007 break;
2008 }
2009 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002010 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002011 png_bytep sp;
2012 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05002013 int shift;
2014 int d;
2015 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002016 png_uint_32 i;
2017 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002018
2019 dp = row;
2020 shift = 6;
2021 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002022 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002023 i += png_pass_inc[pass])
2024 {
2025 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002026 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05002027 d |= (value << shift);
2028
2029 if (shift == 0)
2030 {
2031 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002032 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002033 d = 0;
2034 }
2035 else
2036 shift -= 2;
2037 }
2038 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002039 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002040 break;
2041 }
2042 case 4:
2043 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002044 png_bytep sp;
2045 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002046 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05002047 int d;
2048 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002049 png_uint_32 i;
2050 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002051
2052 dp = row;
2053 shift = 4;
2054 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002055 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002056 i += png_pass_inc[pass])
2057 {
2058 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002059 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05002060 d |= (value << shift);
2061
2062 if (shift == 0)
2063 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002064 shift = 4;
2065 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002066 d = 0;
2067 }
2068 else
2069 shift -= 4;
2070 }
2071 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002072 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002073 break;
2074 }
2075 default:
2076 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002077 png_bytep sp;
2078 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002079 png_uint_32 i;
2080 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002081 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002082
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002083 /* Start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002084 dp = row;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002085 /* Find out how many bytes each pixel takes up */
Guy Schalnat0d580581995-07-20 02:43:20 -05002086 pixel_bytes = (row_info->pixel_depth >> 3);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002087 /* Loop through the row, only looking at the pixels that
Guy Schalnat0d580581995-07-20 02:43:20 -05002088 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002089 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002090 i += png_pass_inc[pass])
2091 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002092 /* Find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002093 sp = row + (png_size_t)i * pixel_bytes;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002094 /* Move the pixel */
Guy Schalnat0d580581995-07-20 02:43:20 -05002095 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002096 png_memcpy(dp, sp, pixel_bytes);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002097 /* Next pixel */
Guy Schalnat0d580581995-07-20 02:43:20 -05002098 dp += pixel_bytes;
2099 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002100 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002101 }
2102 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002103 /* Set new row width */
Guy Schalnat0d580581995-07-20 02:43:20 -05002104 row_info->width = (row_info->width +
2105 png_pass_inc[pass] - 1 -
2106 png_pass_start[pass]) /
2107 png_pass_inc[pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002108 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2109 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002110 }
2111}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002112#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002113
Andreas Dilger47a0c421997-05-16 02:46:07 -05002114/* This filters the row, chooses which filter to use, if it has not already
2115 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002116 * chosen filter.
2117 */
Glenn Randers-Pehrson78067772004-11-02 21:49:39 -06002118#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002119#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002120#define PNG_LOMASK ((png_uint_32)0xffffL)
2121#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002122void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002123png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002124{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002125 png_bytep best_row;
2126#ifndef PNG_NO_WRITE_FILTER
2127 png_bytep prev_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002128 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002129 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002130 png_uint_32 row_bytes = row_info->rowbytes;
2131#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2132 int num_p_filters = (int)png_ptr->num_prev_filters;
2133#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002134
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002135 png_debug(1, "in png_write_find_filter");
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002136 /* Find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002137 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002138
2139 prev_row = png_ptr->prev_row;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002140#endif
2141 best_row = png_ptr->row_buf;
2142#ifndef PNG_NO_WRITE_FILTER
2143 row_buf = best_row;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002144 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002145
Andreas Dilger47a0c421997-05-16 02:46:07 -05002146 /* The prediction method we use is to find which method provides the
2147 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002148 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002149 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002150 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002151 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002152 * predictive" method (not implemented yet), which does test compressions
2153 * of lines using different filter methods, and then chooses the
2154 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002155 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002156 *
2157 * GRR 980525: consider also
2158 * (1) minimum sum of absolute differences from running average (i.e.,
2159 * keep running sum of non-absolute differences & count of bytes)
2160 * [track dispersion, too? restart average if dispersion too large?]
2161 * (1b) minimum sum of absolute differences from sliding average, probably
2162 * with window size <= deflate window (usually 32K)
2163 * (2) minimum sum of squared differences from zero or running average
2164 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002165 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002166
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002167
Guy Schalnate5a37791996-06-05 15:50:50 -05002168 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002169 * that has been chosen, as it doesn't actually do anything to the data.
2170 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002171 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002172 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002173 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002174 png_bytep rp;
2175 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002176 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002177 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002178
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002179 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002180 {
2181 v = *rp;
2182 sum += (v < 128) ? v : 256 - v;
2183 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002184
2185#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2186 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2187 {
2188 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002189 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002190 sumlo = sum & PNG_LOMASK;
2191 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2192
2193 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002194 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002195 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002196 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002197 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002198 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002199 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002200 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002201 PNG_WEIGHT_SHIFT;
2202 }
2203 }
2204
2205 /* Factor in the cost of this filter (this is here for completeness,
2206 * but it makes no sense to have a "cost" for the NONE filter, as
2207 * it has the minimum possible computational cost - none).
2208 */
2209 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2210 PNG_COST_SHIFT;
2211 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2212 PNG_COST_SHIFT;
2213
2214 if (sumhi > PNG_HIMASK)
2215 sum = PNG_MAXSUM;
2216 else
2217 sum = (sumhi << PNG_HISHIFT) + sumlo;
2218 }
2219#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002220 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002221 }
2222
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002223 /* Sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002224 if (filter_to_do == PNG_FILTER_SUB)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002225 /* It's the only filter so no testing is needed */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002226 {
2227 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002228 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002229 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2230 i++, rp++, dp++)
2231 {
2232 *dp = *rp;
2233 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002234 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002235 i++, rp++, lp++, dp++)
2236 {
2237 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2238 }
2239 best_row = png_ptr->sub_row;
2240 }
2241
2242 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002243 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002244 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002245 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002246 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002247 int v;
2248
2249#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002250 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002251 * would reduce the sum of this filter, so that we can do the
2252 * early exit comparison without scaling the sum each time.
2253 */
2254 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2255 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002256 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002257 png_uint_32 lmhi, lmlo;
2258 lmlo = lmins & PNG_LOMASK;
2259 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2260
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002261 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002262 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002263 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002264 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002265 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002266 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002267 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002268 PNG_WEIGHT_SHIFT;
2269 }
2270 }
2271
2272 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2273 PNG_COST_SHIFT;
2274 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2275 PNG_COST_SHIFT;
2276
2277 if (lmhi > PNG_HIMASK)
2278 lmins = PNG_MAXSUM;
2279 else
2280 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2281 }
2282#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002283
Guy Schalnate5a37791996-06-05 15:50:50 -05002284 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2285 i++, rp++, dp++)
2286 {
2287 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002288
Guy Schalnate5a37791996-06-05 15:50:50 -05002289 sum += (v < 128) ? v : 256 - v;
2290 }
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002291 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002292 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002293 {
2294 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002295
Guy Schalnate5a37791996-06-05 15:50:50 -05002296 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002297
2298 if (sum > lmins) /* We are already worse, don't continue. */
2299 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002300 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002301
2302#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2303 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2304 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002305 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002306 png_uint_32 sumhi, sumlo;
2307 sumlo = sum & PNG_LOMASK;
2308 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2309
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002310 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002311 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002312 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002313 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002314 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002315 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002316 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002317 PNG_WEIGHT_SHIFT;
2318 }
2319 }
2320
2321 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2322 PNG_COST_SHIFT;
2323 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2324 PNG_COST_SHIFT;
2325
2326 if (sumhi > PNG_HIMASK)
2327 sum = PNG_MAXSUM;
2328 else
2329 sum = (sumhi << PNG_HISHIFT) + sumlo;
2330 }
2331#endif
2332
Guy Schalnate5a37791996-06-05 15:50:50 -05002333 if (sum < mins)
2334 {
2335 mins = sum;
2336 best_row = png_ptr->sub_row;
2337 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002338 }
2339
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002340 /* Up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002341 if (filter_to_do == PNG_FILTER_UP)
2342 {
2343 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002344 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002345
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002346 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002347 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002348 i++, rp++, pp++, dp++)
2349 {
2350 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2351 }
2352 best_row = png_ptr->up_row;
2353 }
2354
2355 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002356 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002357 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002358 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002359 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002360 int v;
2361
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002362
Andreas Dilger47a0c421997-05-16 02:46:07 -05002363#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2364 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2365 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002366 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002367 png_uint_32 lmhi, lmlo;
2368 lmlo = lmins & PNG_LOMASK;
2369 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2370
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002371 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002372 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002373 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002374 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002375 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002376 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002377 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002378 PNG_WEIGHT_SHIFT;
2379 }
2380 }
2381
2382 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2383 PNG_COST_SHIFT;
2384 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2385 PNG_COST_SHIFT;
2386
2387 if (lmhi > PNG_HIMASK)
2388 lmins = PNG_MAXSUM;
2389 else
2390 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2391 }
2392#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002393
2394 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002395 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002396 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002397 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002398
2399 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002400
2401 if (sum > lmins) /* We are already worse, don't continue. */
2402 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002403 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002404
2405#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2406 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2407 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002408 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002409 png_uint_32 sumhi, sumlo;
2410 sumlo = sum & PNG_LOMASK;
2411 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2412
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002413 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002414 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002415 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002416 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002417 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002418 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002419 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002420 PNG_WEIGHT_SHIFT;
2421 }
2422 }
2423
2424 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2425 PNG_COST_SHIFT;
2426 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2427 PNG_COST_SHIFT;
2428
2429 if (sumhi > PNG_HIMASK)
2430 sum = PNG_MAXSUM;
2431 else
2432 sum = (sumhi << PNG_HISHIFT) + sumlo;
2433 }
2434#endif
2435
Guy Schalnate5a37791996-06-05 15:50:50 -05002436 if (sum < mins)
2437 {
2438 mins = sum;
2439 best_row = png_ptr->up_row;
2440 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002441 }
2442
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002443 /* Avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002444 if (filter_to_do == PNG_FILTER_AVG)
2445 {
2446 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002447 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002448 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002449 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002450 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002451 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002452 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002453 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002454 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002455 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2456 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002457 }
2458 best_row = png_ptr->avg_row;
2459 }
2460
2461 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002462 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002463 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002464 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002465 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002466 int v;
2467
2468#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2469 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2470 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002471 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002472 png_uint_32 lmhi, lmlo;
2473 lmlo = lmins & PNG_LOMASK;
2474 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2475
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002476 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002477 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002478 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002479 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002480 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002481 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002482 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002483 PNG_WEIGHT_SHIFT;
2484 }
2485 }
2486
2487 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2488 PNG_COST_SHIFT;
2489 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2490 PNG_COST_SHIFT;
2491
2492 if (lmhi > PNG_HIMASK)
2493 lmins = PNG_MAXSUM;
2494 else
2495 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2496 }
2497#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002498
2499 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002500 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002501 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002502 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002503
2504 sum += (v < 128) ? v : 256 - v;
2505 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002506 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002507 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002508 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002509 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002510
2511 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002512
2513 if (sum > lmins) /* We are already worse, don't continue. */
2514 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002515 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002516
2517#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2518 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2519 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002520 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002521 png_uint_32 sumhi, sumlo;
2522 sumlo = sum & PNG_LOMASK;
2523 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2524
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002525 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002526 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002527 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002528 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002529 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002530 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002531 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002532 PNG_WEIGHT_SHIFT;
2533 }
2534 }
2535
2536 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2537 PNG_COST_SHIFT;
2538 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2539 PNG_COST_SHIFT;
2540
2541 if (sumhi > PNG_HIMASK)
2542 sum = PNG_MAXSUM;
2543 else
2544 sum = (sumhi << PNG_HISHIFT) + sumlo;
2545 }
2546#endif
2547
Guy Schalnate5a37791996-06-05 15:50:50 -05002548 if (sum < mins)
2549 {
2550 mins = sum;
2551 best_row = png_ptr->avg_row;
2552 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002553 }
2554
Andreas Dilger47a0c421997-05-16 02:46:07 -05002555 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002556 if (filter_to_do == PNG_FILTER_PAETH)
2557 {
2558 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002559 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002560 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002561 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002562 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002563 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002564 }
2565
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002566 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002567 {
2568 int a, b, c, pa, pb, pc, p;
2569
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002570 b = *pp++;
2571 c = *cp++;
2572 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002573
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002574 p = b - c;
2575 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002576
2577#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002578 pa = abs(p);
2579 pb = abs(pc);
2580 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002581#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002582 pa = p < 0 ? -p : p;
2583 pb = pc < 0 ? -pc : pc;
2584 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002585#endif
2586
2587 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2588
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002589 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002590 }
2591 best_row = png_ptr->paeth_row;
2592 }
2593
2594 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002595 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002596 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002597 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002598 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002599 int v;
2600
2601#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2602 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2603 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002604 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002605 png_uint_32 lmhi, lmlo;
2606 lmlo = lmins & PNG_LOMASK;
2607 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2608
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002609 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002610 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002611 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002612 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002613 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002614 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002615 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002616 PNG_WEIGHT_SHIFT;
2617 }
2618 }
2619
2620 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2621 PNG_COST_SHIFT;
2622 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2623 PNG_COST_SHIFT;
2624
2625 if (lmhi > PNG_HIMASK)
2626 lmins = PNG_MAXSUM;
2627 else
2628 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2629 }
2630#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002631
2632 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002633 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002634 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002635 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002636
2637 sum += (v < 128) ? v : 256 - v;
2638 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002639
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002640 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002641 {
2642 int a, b, c, pa, pb, pc, p;
2643
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002644 b = *pp++;
2645 c = *cp++;
2646 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002647
2648#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002649 p = b - c;
2650 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002651#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002652 pa = abs(p);
2653 pb = abs(pc);
2654 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002655#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002656 pa = p < 0 ? -p : p;
2657 pb = pc < 0 ? -pc : pc;
2658 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002659#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002660 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2661#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002662 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002663 pa = abs(p - a);
2664 pb = abs(p - b);
2665 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002666 if (pa <= pb && pa <= pc)
2667 p = a;
2668 else if (pb <= pc)
2669 p = b;
2670 else
2671 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002672#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002673
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002674 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002675
2676 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002677
2678 if (sum > lmins) /* We are already worse, don't continue. */
2679 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002680 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002681
2682#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2683 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2684 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002685 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002686 png_uint_32 sumhi, sumlo;
2687 sumlo = sum & PNG_LOMASK;
2688 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2689
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002690 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002691 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002692 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002693 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002694 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002695 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002696 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002697 PNG_WEIGHT_SHIFT;
2698 }
2699 }
2700
2701 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2702 PNG_COST_SHIFT;
2703 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2704 PNG_COST_SHIFT;
2705
2706 if (sumhi > PNG_HIMASK)
2707 sum = PNG_MAXSUM;
2708 else
2709 sum = (sumhi << PNG_HISHIFT) + sumlo;
2710 }
2711#endif
2712
Guy Schalnate5a37791996-06-05 15:50:50 -05002713 if (sum < mins)
2714 {
2715 best_row = png_ptr->paeth_row;
2716 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002717 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002718#endif /* PNG_NO_WRITE_FILTER */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002719 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002720
Guy Schalnate5a37791996-06-05 15:50:50 -05002721 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002722
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002723#ifndef PNG_NO_WRITE_FILTER
Andreas Dilger47a0c421997-05-16 02:46:07 -05002724#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2725 /* Save the type of filter we picked this time for future calculations */
2726 if (png_ptr->num_prev_filters > 0)
2727 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002728 int j;
2729 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002730 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002731 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002732 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002733 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002734 }
2735#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002736#endif /* PNG_NO_WRITE_FILTER */
Guy Schalnate5a37791996-06-05 15:50:50 -05002737}
2738
2739
Andreas Dilger47a0c421997-05-16 02:46:07 -05002740/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002741void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002742png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2743{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002744 png_debug(1, "in png_write_filtered_row");
2745 png_debug1(2, "filter = %d", filtered_row[0]);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002746 /* Set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002747
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002748 png_ptr->zstream.next_in = filtered_row;
2749 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002750 /* Repeat until we have compressed all the data */
Guy Schalnate5a37791996-06-05 15:50:50 -05002751 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002752 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002753 int ret; /* Return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002754
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002755 /* Compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002756 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002757 /* Check for compression errors */
Guy Schalnate5a37791996-06-05 15:50:50 -05002758 if (ret != Z_OK)
2759 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002760 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002761 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002762 else
2763 png_error(png_ptr, "zlib error");
2764 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002765
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002766 /* See if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002767 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002768 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002769 /* Write the IDAT and reset the zlib output buffer */
Guy Schalnate5a37791996-06-05 15:50:50 -05002770 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002771 png_ptr->zstream.next_out = png_ptr->zbuf;
2772 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002773 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002774 /* Repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002775 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002776
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002777 /* Swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002778 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002779 {
2780 png_bytep tptr;
2781
2782 tptr = png_ptr->prev_row;
2783 png_ptr->prev_row = png_ptr->row_buf;
2784 png_ptr->row_buf = tptr;
2785 }
2786
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002787 /* Finish row - updates counters and flushes zlib if last row */
Guy Schalnate5a37791996-06-05 15:50:50 -05002788 png_write_finish_row(png_ptr);
2789
2790#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2791 png_ptr->flush_rows++;
2792
2793 if (png_ptr->flush_dist > 0 &&
2794 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002795 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002796 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002797 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002798#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002799}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05002800#endif /* PNG_WRITE_SUPPORTED */