blob: b04b4402c9b8109bfb8f7c5a0e1888ba6fae9de1 [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-Pehrson65a22372010-03-03 05:38:29 -06004 * Last changed in libpng 1.5.0 [March 3, 2010]
Glenn Randers-Pehrsone69b55d2010-01-01 10:29:06 -06005 * Copyright (c) 1998-2010 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
Glenn Randers-Pehrsonf9795312010-02-09 01:16:48 -060014#define PNG_EXPOSE_INTERNAL_STRUCTURES
Glenn Randers-Pehrson03f9b022009-12-04 08:40:41 -060015#define PNG_NO_PEDANTIC_WARNINGS
Guy Schalnat0d580581995-07-20 02:43:20 -050016#include "png.h"
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050017#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -050018#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050019
Andreas Dilger47a0c421997-05-16 02:46:07 -050020/* Place a 32-bit number into a buffer in PNG byte order. We work
21 * with unsigned numbers for convenience, although one supported
22 * ancillary chunk uses signed (two's complement) numbers.
23 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060024void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -060025png_save_uint_32(png_bytep buf, png_uint_32 i)
Guy Schalnat0d580581995-07-20 02:43:20 -050026{
27 buf[0] = (png_byte)((i >> 24) & 0xff);
28 buf[1] = (png_byte)((i >> 16) & 0xff);
29 buf[2] = (png_byte)((i >> 8) & 0xff);
30 buf[3] = (png_byte)(i & 0xff);
31}
32
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050033#ifdef PNG_SAVE_INT_32_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -050034/* The png_save_int_32 function assumes integers are stored in two's
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060035 * complement format. If this isn't the case, then this routine needs to
36 * be modified to write data in two's complement format.
37 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060038void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050039png_save_int_32(png_bytep buf, png_int_32 i)
40{
41 buf[0] = (png_byte)((i >> 24) & 0xff);
42 buf[1] = (png_byte)((i >> 16) & 0xff);
43 buf[2] = (png_byte)((i >> 8) & 0xff);
44 buf[3] = (png_byte)(i & 0xff);
45}
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -050046#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050047
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060048/* Place a 16-bit number into a buffer in PNG byte order.
49 * The parameter is declared unsigned int, not png_uint_16,
50 * just to avoid potential problems on pre-ANSI C compilers.
51 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060052void PNGAPI
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060053png_save_uint_16(png_bytep buf, unsigned int i)
Guy Schalnat0d580581995-07-20 02:43:20 -050054{
55 buf[0] = (png_byte)((i >> 8) & 0xff);
56 buf[1] = (png_byte)(i & 0xff);
57}
58
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050059/* Simple function to write the signature. If we have already written
60 * the magic bytes of the signature, or more likely, the PNG stream is
61 * being embedded into another stream and doesn't need its own signature,
62 * we should call png_set_sig_bytes() to tell libpng how many of the
63 * bytes have already been written.
64 */
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -050065void PNGAPI
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050066png_write_sig(png_structp png_ptr)
67{
68 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050069
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -050070#ifdef PNG_IO_STATE_SUPPORTED
71 /* Inform the I/O callback that the signature is being written */
72 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_SIGNATURE;
73#endif
74
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050075 /* Write the rest of the 8 byte signature */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050076 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050077 (png_size_t)(8 - png_ptr->sig_bytes));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050078 if (png_ptr->sig_bytes < 3)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050079 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
80}
81
Andreas Dilger47a0c421997-05-16 02:46:07 -050082/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060083 * representing the chunk name. The array must be at least 4 bytes in
84 * length, and does not need to be null terminated. To be safe, pass the
85 * pre-defined chunk names here, and if you need a new one, define it
86 * where the others are defined. The length is the length of the data.
87 * All the data must be present. If that is not possible, use the
88 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
89 * functions instead.
90 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050091void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060092png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050093 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050094{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050095 if (png_ptr == NULL)
96 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -050097 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050098 png_write_chunk_data(png_ptr, data, (png_size_t)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060099 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500100}
101
Andreas Dilger47a0c421997-05-16 02:46:07 -0500102/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600103 * The total_length is the sum of the lengths of all the data you will be
104 * passing in png_write_chunk_data().
105 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500106void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600107png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600108 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500109{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500110 png_byte buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500111
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500112 png_debug2(0, "Writing %s chunk, length = %lu", chunk_name,
113 (unsigned long)length);
114
115 if (png_ptr == NULL)
116 return;
117
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500118#ifdef PNG_IO_STATE_SUPPORTED
119 /* Inform the I/O callback that the chunk header is being written.
120 * PNG_IO_CHUNK_HDR requires a single I/O call.
121 */
122 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR;
123#endif
124
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500125 /* Write the length and the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500126 png_save_uint_32(buf, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500127 png_memcpy(buf + 4, chunk_name, 4);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500128 png_write_data(png_ptr, buf, (png_size_t)8);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500129 /* Put the chunk name into png_ptr->chunk_name */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500130 png_memcpy(png_ptr->chunk_name, chunk_name, 4);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500131 /* Reset the crc and run it over the chunk name */
Guy Schalnat0d580581995-07-20 02:43:20 -0500132 png_reset_crc(png_ptr);
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500133 png_calculate_crc(png_ptr, chunk_name, 4);
134
135#ifdef PNG_IO_STATE_SUPPORTED
136 /* Inform the I/O callback that chunk data will (possibly) be written.
137 * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.
138 */
139 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA;
140#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500141}
142
Andreas Dilger47a0c421997-05-16 02:46:07 -0500143/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600144 * Note that multiple calls to this function are allowed, and that the
145 * sum of the lengths from these calls *must* add up to the total_length
146 * given to png_write_chunk_start().
147 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500148void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500149png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500150{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500151 /* Write the data, and run the CRC over it */
152 if (png_ptr == NULL)
153 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500154 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500155 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600156 png_write_data(png_ptr, data, length);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500157 /* Update the CRC after writing the data,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500158 * in case that the user I/O routine alters it.
159 */
160 png_calculate_crc(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500161 }
162}
163
Andreas Dilger47a0c421997-05-16 02:46:07 -0500164/* Finish a chunk started with png_write_chunk_start(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500165void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600166png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500167{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500168 png_byte buf[4];
169
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500170 if (png_ptr == NULL) return;
171
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500172#ifdef PNG_IO_STATE_SUPPORTED
173 /* Inform the I/O callback that the chunk CRC is being written.
174 * PNG_IO_CHUNK_CRC requires a single I/O function call.
175 */
176 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC;
177#endif
178
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500179 /* Write the crc in a single operation */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500180 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500181
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500182 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500183}
184
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600185#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500186/* This pair of functions encapsulates the operation of (a) compressing a
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600187 * text string, and (b) issuing it later as a series of chunk data writes.
188 * The compression_state structure is shared context for these functions
189 * set up by the caller in order to make the whole mess thread-safe.
190 */
191
192typedef struct
193{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500194 char *input; /* The uncompressed input data */
195 int input_len; /* Its length */
196 int num_output_ptr; /* Number of output pointers used */
197 int max_output_ptr; /* Size of output_ptr */
198 png_charpp output_ptr; /* Array of pointers to output */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600199} compression_state;
200
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500201/* Compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500202static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600203png_text_compress(png_structp png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600204 png_charp text, png_size_t text_len, int compression,
205 compression_state *comp)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600206{
207 int ret;
208
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600209 comp->num_output_ptr = 0;
210 comp->max_output_ptr = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600211 comp->output_ptr = NULL;
212 comp->input = NULL;
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600213 comp->input_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600214
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500215 /* We may just want to pass the text right through */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600216 if (compression == PNG_TEXT_COMPRESSION_NONE)
217 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600218 comp->input = text;
219 comp->input_len = text_len;
220 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600221 }
222
223 if (compression >= PNG_TEXT_COMPRESSION_LAST)
224 {
Glenn Randers-Pehrson6a9e4802010-02-19 09:47:43 -0600225#ifdef PNG_CONSOLE_IO_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600226 char msg[50];
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500227 png_snprintf(msg, 50, "Unknown compression type %d", compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600228 png_warning(png_ptr, msg);
229#else
230 png_warning(png_ptr, "Unknown compression type");
231#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600232 }
233
234 /* We can't write the chunk until we find out how much data we have,
235 * which means we need to run the compressor first and save the
236 * output. This shouldn't be a problem, as the vast majority of
237 * comments should be reasonable, but we will set up an array of
238 * malloc'd pointers to be sure.
239 *
240 * If we knew the application was well behaved, we could simplify this
241 * greatly by assuming we can always malloc an output buffer large
242 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
243 * and malloc this directly. The only time this would be a bad idea is
244 * if we can't malloc more than 64K and we have 64K of random input
245 * data, or if the input string is incredibly large (although this
246 * wouldn't cause a failure, just a slowdown due to swapping).
247 */
248
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500249 /* Set up the compression buffers */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600250 png_ptr->zstream.avail_in = (uInt)text_len;
251 png_ptr->zstream.next_in = (Bytef *)text;
252 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
253 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
254
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500255 /* This is the same compression loop as in png_write_row() */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600256 do
257 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500258 /* Compress the data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600259 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
260 if (ret != Z_OK)
261 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500262 /* Error */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600263 if (png_ptr->zstream.msg != NULL)
264 png_error(png_ptr, png_ptr->zstream.msg);
265 else
266 png_error(png_ptr, "zlib error");
267 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500268 /* Check to see if we need more room */
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600269 if (!(png_ptr->zstream.avail_out))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600270 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500271 /* Make sure the output array has room */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600272 if (comp->num_output_ptr >= comp->max_output_ptr)
273 {
274 int old_max;
275
276 old_max = comp->max_output_ptr;
277 comp->max_output_ptr = comp->num_output_ptr + 4;
278 if (comp->output_ptr != NULL)
279 {
280 png_charpp old_ptr;
281
282 old_ptr = comp->output_ptr;
283 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600284 (png_alloc_size_t)
285 (comp->max_output_ptr * png_sizeof(png_charpp)));
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500286 png_memcpy(comp->output_ptr, old_ptr, old_max
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600287 * png_sizeof(png_charp));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600288 png_free(png_ptr, old_ptr);
289 }
290 else
291 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600292 (png_alloc_size_t)
293 (comp->max_output_ptr * png_sizeof(png_charp)));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600294 }
295
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500296 /* Save the data */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500297 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600298 (png_charp)png_malloc(png_ptr,
299 (png_alloc_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500300 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600301 png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500302 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600303
304 /* and reset the buffer */
305 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
306 png_ptr->zstream.next_out = png_ptr->zbuf;
307 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500308 /* Continue until we don't have any more to compress */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600309 } while (png_ptr->zstream.avail_in);
310
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500311 /* Finish the compression */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600312 do
313 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500314 /* Tell zlib we are finished */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600315 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500316
317 if (ret == Z_OK)
318 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500319 /* Check to see if we need more room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500320 if (!(png_ptr->zstream.avail_out))
321 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500322 /* Check to make sure our output array has room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500323 if (comp->num_output_ptr >= comp->max_output_ptr)
324 {
325 int old_max;
326
327 old_max = comp->max_output_ptr;
328 comp->max_output_ptr = comp->num_output_ptr + 4;
329 if (comp->output_ptr != NULL)
330 {
331 png_charpp old_ptr;
332
333 old_ptr = comp->output_ptr;
334 /* This could be optimized to realloc() */
335 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600336 (png_alloc_size_t)(comp->max_output_ptr *
337 png_sizeof(png_charp)));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500338 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600339 old_max * png_sizeof(png_charp));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500340 png_free(png_ptr, old_ptr);
341 }
342 else
343 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600344 (png_alloc_size_t)(comp->max_output_ptr *
345 png_sizeof(png_charp)));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500346 }
347
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500348 /* Save the data */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500349 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600350 (png_charp)png_malloc(png_ptr,
351 (png_alloc_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500352 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600353 png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500354 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500355
356 /* and reset the buffer pointers */
357 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
358 png_ptr->zstream.next_out = png_ptr->zbuf;
359 }
360 }
361 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600362 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500363 /* We got an error */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600364 if (png_ptr->zstream.msg != NULL)
365 png_error(png_ptr, png_ptr->zstream.msg);
366 else
367 png_error(png_ptr, "zlib error");
368 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600369 } while (ret != Z_STREAM_END);
370
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500371 /* Text length is number of buffers plus last buffer */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600372 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
373 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
374 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
375
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500376 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600377}
378
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500379/* Ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500380static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600381png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
382{
383 int i;
384
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500385 /* Handle the no-compression case */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600386 if (comp->input)
387 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500388 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600389 (png_size_t)comp->input_len);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500390 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600391 }
392
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500393 /* Write saved output buffers, if any */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600394 for (i = 0; i < comp->num_output_ptr; i++)
395 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500396 png_write_chunk_data(png_ptr, (png_bytep)comp->output_ptr[i],
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600397 (png_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600398 png_free(png_ptr, comp->output_ptr[i]);
399 }
400 if (comp->max_output_ptr != 0)
401 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500402 /* Write anything left in zbuf */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600403 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
404 png_write_chunk_data(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600405 (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600406
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500407 /* Reset zlib for another zTXt/iTXt or image data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600408 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600409 png_ptr->zstream.data_type = Z_BINARY;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600410}
411#endif
412
Guy Schalnat0d580581995-07-20 02:43:20 -0500413/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600414 * information. Note that the rest of this code depends upon this
415 * information being correct.
416 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500417void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600418png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600419 int bit_depth, int color_type, int compression_type, int filter_type,
420 int interlace_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500421{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600422 PNG_IHDR;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500423 int ret;
424
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500425 png_byte buf[13]; /* Buffer to store the IHDR info */
Guy Schalnat0d580581995-07-20 02:43:20 -0500426
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500427 png_debug(1, "in png_write_IHDR");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500428
Guy Schalnate5a37791996-06-05 15:50:50 -0500429 /* Check that we have valid input data from the application info */
430 switch (color_type)
431 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500432 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500433 switch (bit_depth)
434 {
435 case 1:
436 case 2:
437 case 4:
438 case 8:
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600439 case 16:
440 png_ptr->channels = 1; break;
441 default:
442 png_error(png_ptr,
443 "Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500444 }
445 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500446 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500447 if (bit_depth != 8 && bit_depth != 16)
448 png_error(png_ptr, "Invalid bit depth for RGB image");
449 png_ptr->channels = 3;
450 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500451 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500452 switch (bit_depth)
453 {
454 case 1:
455 case 2:
456 case 4:
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600457 case 8:
458 png_ptr->channels = 1;
459 break;
460 default:
461 png_error(png_ptr, "Invalid bit depth for paletted image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500462 }
463 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500464 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500465 if (bit_depth != 8 && bit_depth != 16)
466 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
467 png_ptr->channels = 2;
468 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500469 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500470 if (bit_depth != 8 && bit_depth != 16)
471 png_error(png_ptr, "Invalid bit depth for RGBA image");
472 png_ptr->channels = 4;
473 break;
474 default:
475 png_error(png_ptr, "Invalid image color type specified");
476 }
477
Andreas Dilger47a0c421997-05-16 02:46:07 -0500478 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500479 {
480 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500481 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500482 }
483
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600484 /* Write filter_method 64 (intrapixel differencing) only if
485 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
486 * 2. Libpng did not write a PNG signature (this filter_method is only
487 * used in PNG datastreams that are embedded in MNG datastreams) and
488 * 3. The application called png_permit_mng_features with a mask that
489 * included PNG_FLAG_MNG_FILTER_64 and
490 * 4. The filter_method is 64 and
491 * 5. The color_type is RGB or RGBA
492 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600493 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500494#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600495 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
496 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
497 (color_type == PNG_COLOR_TYPE_RGB ||
498 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
499 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600500#endif
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600501 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500502 {
503 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500504 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500505 }
506
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600507#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500508 if (interlace_type != PNG_INTERLACE_NONE &&
509 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500510 {
511 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500512 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500513 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600514#else
515 interlace_type=PNG_INTERLACE_NONE;
516#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500517
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500518 /* Save the relevent information */
Guy Schalnate5a37791996-06-05 15:50:50 -0500519 png_ptr->bit_depth = (png_byte)bit_depth;
520 png_ptr->color_type = (png_byte)color_type;
521 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500522#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600523 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500524#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500525 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500526 png_ptr->width = width;
527 png_ptr->height = height;
528
529 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500530 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500531 /* Set the usr info, so any transformations can modify it */
Guy Schalnate5a37791996-06-05 15:50:50 -0500532 png_ptr->usr_width = png_ptr->width;
533 png_ptr->usr_bit_depth = png_ptr->bit_depth;
534 png_ptr->usr_channels = png_ptr->channels;
535
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500536 /* Pack the header information into the buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500537 png_save_uint_32(buf, width);
538 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600539 buf[8] = (png_byte)bit_depth;
540 buf[9] = (png_byte)color_type;
541 buf[10] = (png_byte)compression_type;
542 buf[11] = (png_byte)filter_type;
543 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500544
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500545 /* Write the chunk */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500546 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500547
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500548 /* Initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600549 png_ptr->zstream.zalloc = png_zalloc;
550 png_ptr->zstream.zfree = png_zfree;
551 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500552 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500553 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500554 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600555 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500556 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500557 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500558 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500559 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500560 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500561 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500562 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500563 png_ptr->zlib_strategy = Z_FILTERED;
564 else
565 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
566 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500567 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500568 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500569 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500570 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500571 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500572 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500573 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500574 png_ptr->zlib_method = 8;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500575 ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600576 png_ptr->zlib_method, png_ptr->zlib_window_bits,
577 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500578 if (ret != Z_OK)
579 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600580 if (ret == Z_VERSION_ERROR)
581 png_error(png_ptr,
582 "zlib failed to initialize compressor -- version error");
583 if (ret == Z_STREAM_ERROR)
584 png_error(png_ptr,
585 "zlib failed to initialize compressor -- stream error");
586 if (ret == Z_MEM_ERROR)
587 png_error(png_ptr,
588 "zlib failed to initialize compressor -- mem error");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500589 png_error(png_ptr, "zlib failed to initialize compressor");
590 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600591 png_ptr->zstream.next_out = png_ptr->zbuf;
592 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600593 /* libpng is not interested in zstream.data_type */
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500594 /* Set it to a predefined value, to avoid its evaluation inside zlib */
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600595 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500596
Guy Schalnate5a37791996-06-05 15:50:50 -0500597 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500598}
599
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500600/* Write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500601 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600602 * structure.
603 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500604void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500605png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500606{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600607 PNG_PLTE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500608 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600609 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500610 png_byte buf[3];
611
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500612 png_debug(1, "in png_write_PLTE");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500613
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500614 if ((
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500615#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600616 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500617#endif
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600618 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500619 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600620 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
621 {
622 png_error(png_ptr, "Invalid number of colors in palette");
623 }
624 else
625 {
626 png_warning(png_ptr, "Invalid number of colors in palette");
627 return;
628 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500629 }
630
631 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
632 {
633 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600634 "Ignoring request to write a PLTE chunk in grayscale PNG");
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500635 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500636 }
637
Andreas Dilger47a0c421997-05-16 02:46:07 -0500638 png_ptr->num_palette = (png_uint_16)num_pal;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500639 png_debug1(3, "num_palette = %d", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500640
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500641 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600642 (png_uint_32)(num_pal * 3));
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500643#ifdef PNG_POINTER_INDEXING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500644 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500645 {
646 buf[0] = pal_ptr->red;
647 buf[1] = pal_ptr->green;
648 buf[2] = pal_ptr->blue;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500649 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500650 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500651#else
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600652 /* This is a little slower but some buggy compilers need to do this
653 * instead
654 */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500655 pal_ptr=palette;
656 for (i = 0; i < num_pal; i++)
657 {
658 buf[0] = pal_ptr[i].red;
659 buf[1] = pal_ptr[i].green;
660 buf[2] = pal_ptr[i].blue;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500661 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500662 }
663#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500664 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500665 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500666}
667
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500668/* Write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500669void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500670png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500671{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600672 PNG_IDAT;
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500673
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500674 png_debug(1, "in png_write_IDAT");
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500675
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500676 /* Optimize the CMF field in the zlib stream. */
677 /* This hack of the zlib stream is compliant to the stream specification. */
678 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
679 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
680 {
681 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
682 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
683 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500684 /* Avoid memory underflows and multiplication overflows.
685 *
686 * The conditions below are practically always satisfied;
687 * however, they still must be checked.
688 */
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500689 if (length >= 2 &&
690 png_ptr->height < 16384 && png_ptr->width < 16384)
691 {
692 png_uint_32 uncompressed_idat_size = png_ptr->height *
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600693 ((png_ptr->width *
694 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500695 unsigned int z_cinfo = z_cmf >> 4;
696 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
697 while (uncompressed_idat_size <= half_z_window_size &&
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600698 half_z_window_size >= 256)
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500699 {
700 z_cinfo--;
701 half_z_window_size >>= 1;
702 }
703 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
704 if (data[0] != (png_byte)z_cmf)
705 {
706 data[0] = (png_byte)z_cmf;
707 data[1] &= 0xe0;
708 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
709 }
710 }
711 }
712 else
713 png_error(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600714 "Invalid zlib compression method or flags in IDAT");
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500715 }
716
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600717 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500718 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500719}
720
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500721/* Write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500722void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600723png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500724{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600725 PNG_IEND;
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500726
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500727 png_debug(1, "in png_write_IEND");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500728
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500729 png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600730 (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600731 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500732}
733
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500734#ifdef PNG_WRITE_gAMA_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500735/* Write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600736#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500737void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500738png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500739{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600740 PNG_gAMA;
Guy Schalnat0d580581995-07-20 02:43:20 -0500741 png_uint_32 igamma;
742 png_byte buf[4];
743
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500744 png_debug(1, "in png_write_gAMA");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500745
Glenn Randers-Pehrson626afd62009-08-20 22:52:51 -0500746 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600747 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500748 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500749 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500750}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500751#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600752#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500753void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600754png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600755{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600756 PNG_gAMA;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600757 png_byte buf[4];
758
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500759 png_debug(1, "in png_write_gAMA");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500760
Glenn Randers-Pehrson626afd62009-08-20 22:52:51 -0500761 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500762 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500763 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600764}
765#endif
766#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500767
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500768#ifdef PNG_WRITE_sRGB_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500769/* Write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500770void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600771png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600772{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600773 PNG_sRGB;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600774 png_byte buf[1];
775
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500776 png_debug(1, "in png_write_sRGB");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500777
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500778 if (srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600779 png_warning(png_ptr,
780 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600781 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500782 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600783}
784#endif
785
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500786#ifdef PNG_WRITE_iCCP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500787/* Write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500788void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600789png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600790 png_charp profile, int profile_len)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600791{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600792 PNG_iCCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600793 png_size_t name_len;
794 png_charp new_name;
795 compression_state comp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500796 int embedded_profile_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600797
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500798 png_debug(1, "in png_write_iCCP");
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600799
800 comp.num_output_ptr = 0;
801 comp.max_output_ptr = 0;
802 comp.output_ptr = NULL;
803 comp.input = NULL;
804 comp.input_len = 0;
805
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -0600806 if ((name_len = png_check_keyword(png_ptr, name,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600807 &new_name)) == 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600808 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600809
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600810 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600811 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600812
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500813 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600814 profile_len = 0;
815
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500816 if (profile_len > 3)
Glenn Randers-Pehrson7edd4582006-12-07 19:16:44 -0600817 embedded_profile_len =
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500818 ((*( (png_bytep)profile ))<<24) |
819 ((*( (png_bytep)profile + 1))<<16) |
820 ((*( (png_bytep)profile + 2))<< 8) |
821 ((*( (png_bytep)profile + 3)) );
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500822
Glenn Randers-Pehrson3a054e12009-08-01 08:53:23 -0500823 if (embedded_profile_len < 0)
824 {
825 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600826 "Embedded profile length in iCCP chunk is negative");
Glenn Randers-Pehrson3a054e12009-08-01 08:53:23 -0500827 png_free(png_ptr, new_name);
828 return;
829 }
830
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500831 if (profile_len < embedded_profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500832 {
833 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600834 "Embedded profile length too large in iCCP chunk");
Glenn Randers-Pehrsona6cc6272009-04-01 14:18:43 -0500835 png_free(png_ptr, new_name);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500836 return;
837 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500838
839 if (profile_len > embedded_profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500840 {
841 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600842 "Truncating profile to actual length in iCCP chunk");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500843 profile_len = embedded_profile_len;
844 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500845
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600846 if (profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500847 profile_len = png_text_compress(png_ptr, profile,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600848 (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600849
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500850 /* Make sure we include the NULL after the name and the compression type */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600851 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600852 (png_uint_32)(name_len + profile_len + 2));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500853 new_name[name_len + 1] = 0x00;
854 png_write_chunk_data(png_ptr, (png_bytep)new_name,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600855 (png_size_t)(name_len + 2));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600856
857 if (profile_len)
858 png_write_compressed_data_out(png_ptr, &comp);
859
860 png_write_chunk_end(png_ptr);
861 png_free(png_ptr, new_name);
862}
863#endif
864
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500865#ifdef PNG_WRITE_sPLT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500866/* Write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500867void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600868png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600869{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600870 PNG_sPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600871 png_size_t name_len;
872 png_charp new_name;
873 png_byte entrybuf[10];
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500874 png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
875 png_size_t palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600876 png_sPLT_entryp ep;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500877#ifndef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500878 int i;
879#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600880
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500881 png_debug(1, "in png_write_sPLT");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500882
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500883 if ((name_len = png_check_keyword(png_ptr,spalette->name, &new_name))==0)
884 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600885
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500886 /* Make sure we include the NULL after the name */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500887 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600888 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500889 png_write_chunk_data(png_ptr, (png_bytep)new_name,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600890 (png_size_t)(name_len + 1));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500891 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600892
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500893 /* Loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500894#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500895 for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600896 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500897 if (spalette->depth == 8)
898 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600899 entrybuf[0] = (png_byte)ep->red;
900 entrybuf[1] = (png_byte)ep->green;
901 entrybuf[2] = (png_byte)ep->blue;
902 entrybuf[3] = (png_byte)ep->alpha;
903 png_save_uint_16(entrybuf + 4, ep->frequency);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500904 }
905 else
906 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600907 png_save_uint_16(entrybuf + 0, ep->red);
908 png_save_uint_16(entrybuf + 2, ep->green);
909 png_save_uint_16(entrybuf + 4, ep->blue);
910 png_save_uint_16(entrybuf + 6, ep->alpha);
911 png_save_uint_16(entrybuf + 8, ep->frequency);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500912 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500913 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600914 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500915#else
916 ep=spalette->entries;
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600917 for (i = 0; i>spalette->nentries; i++)
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500918 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500919 if (spalette->depth == 8)
920 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600921 entrybuf[0] = (png_byte)ep[i].red;
922 entrybuf[1] = (png_byte)ep[i].green;
923 entrybuf[2] = (png_byte)ep[i].blue;
924 entrybuf[3] = (png_byte)ep[i].alpha;
925 png_save_uint_16(entrybuf + 4, ep[i].frequency);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500926 }
927 else
928 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600929 png_save_uint_16(entrybuf + 0, ep[i].red);
930 png_save_uint_16(entrybuf + 2, ep[i].green);
931 png_save_uint_16(entrybuf + 4, ep[i].blue);
932 png_save_uint_16(entrybuf + 6, ep[i].alpha);
933 png_save_uint_16(entrybuf + 8, ep[i].frequency);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500934 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500935 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500936 }
937#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600938
939 png_write_chunk_end(png_ptr);
940 png_free(png_ptr, new_name);
941}
942#endif
943
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500944#ifdef PNG_WRITE_sBIT_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500945/* Write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500946void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600947png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500948{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600949 PNG_sBIT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500950 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500951 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500952
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500953 png_debug(1, "in png_write_sBIT");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500954
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500955 /* Make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500956 if (color_type & PNG_COLOR_MASK_COLOR)
957 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600958 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500959
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500960 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600961 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600962 if (sbit->red == 0 || sbit->red > maxbits ||
963 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500964 sbit->blue == 0 || sbit->blue > maxbits)
965 {
966 png_warning(png_ptr, "Invalid sBIT depth specified");
967 return;
968 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500969 buf[0] = sbit->red;
970 buf[1] = sbit->green;
971 buf[2] = sbit->blue;
972 size = 3;
973 }
974 else
975 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500976 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
977 {
978 png_warning(png_ptr, "Invalid sBIT depth specified");
979 return;
980 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500981 buf[0] = sbit->gray;
982 size = 1;
983 }
984
985 if (color_type & PNG_COLOR_MASK_ALPHA)
986 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500987 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
988 {
989 png_warning(png_ptr, "Invalid sBIT depth specified");
990 return;
991 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500992 buf[size++] = sbit->alpha;
993 }
994
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600995 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500996}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500997#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500998
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500999#ifdef PNG_WRITE_cHRM_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001000/* Write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001001#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001002void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001003png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001004 double red_x, double red_y, double green_x, double green_y,
1005 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -05001006{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001007 PNG_cHRM;
Guy Schalnat0d580581995-07-20 02:43:20 -05001008 png_byte buf[32];
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -06001009
1010 png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y,
1011 int_green_x, int_green_y, int_blue_x, int_blue_y;
Guy Schalnat0d580581995-07-20 02:43:20 -05001012
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001013 png_debug(1, "in png_write_cHRM");
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -06001014
1015 int_white_x = (png_uint_32)(white_x * 100000.0 + 0.5);
1016 int_white_y = (png_uint_32)(white_y * 100000.0 + 0.5);
1017 int_red_x = (png_uint_32)(red_x * 100000.0 + 0.5);
1018 int_red_y = (png_uint_32)(red_y * 100000.0 + 0.5);
1019 int_green_x = (png_uint_32)(green_x * 100000.0 + 0.5);
1020 int_green_y = (png_uint_32)(green_y * 100000.0 + 0.5);
1021 int_blue_x = (png_uint_32)(blue_x * 100000.0 + 0.5);
1022 int_blue_y = (png_uint_32)(blue_y * 100000.0 + 0.5);
1023
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001024#ifdef PNG_CHECK_cHRM_SUPPORTED
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -06001025 if (png_check_cHRM_fixed(png_ptr, int_white_x, int_white_y,
1026 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 -06001027#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001028 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001029 /* Each value is saved in 1/100,000ths */
Glenn Randers-Pehrsond60c8862009-06-15 21:56:14 -05001030
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001031 png_save_uint_32(buf, int_white_x);
1032 png_save_uint_32(buf + 4, int_white_y);
Guy Schalnate5a37791996-06-05 15:50:50 -05001033
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001034 png_save_uint_32(buf + 8, int_red_x);
1035 png_save_uint_32(buf + 12, int_red_y);
Guy Schalnate5a37791996-06-05 15:50:50 -05001036
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001037 png_save_uint_32(buf + 16, int_green_x);
1038 png_save_uint_32(buf + 20, int_green_y);
Guy Schalnate5a37791996-06-05 15:50:50 -05001039
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001040 png_save_uint_32(buf + 24, int_blue_x);
1041 png_save_uint_32(buf + 28, int_blue_y);
Guy Schalnate5a37791996-06-05 15:50:50 -05001042
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001043 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001044 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001045}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001046#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001047#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001048void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001049png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001050 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
1051 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
1052 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001053{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001054 PNG_cHRM;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001055 png_byte buf[32];
1056
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001057 png_debug(1, "in png_write_cHRM");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001058
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001059 /* Each value is saved in 1/100,000ths */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001060#ifdef PNG_CHECK_cHRM_SUPPORTED
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001061 if (png_check_cHRM_fixed(png_ptr, white_x, white_y, red_x, red_y,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001062 green_x, green_y, blue_x, blue_y))
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001063#endif
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, (png_uint_32)white_x);
1066 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001067
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001068 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1069 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001070
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001071 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1072 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001073
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001074 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1075 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001076
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001077 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001078 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001079}
1080#endif
1081#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001082
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001083#ifdef PNG_WRITE_tRNS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001084/* Write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001085void /* PRIVATE */
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001086png_write_tRNS(png_structp png_ptr, png_bytep trans_alpha, png_color_16p tran,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001087 int num_trans, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001088{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001089 PNG_tRNS;
Guy Schalnat0d580581995-07-20 02:43:20 -05001090 png_byte buf[6];
1091
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001092 png_debug(1, "in png_write_tRNS");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001093
Guy Schalnat0d580581995-07-20 02:43:20 -05001094 if (color_type == PNG_COLOR_TYPE_PALETTE)
1095 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001096 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001097 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001098 png_warning(png_ptr, "Invalid number of transparent colors specified");
Guy Schalnate5a37791996-06-05 15:50:50 -05001099 return;
1100 }
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001101 /* Write the chunk out as it is */
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001102 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans_alpha,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001103 (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001104 }
1105 else if (color_type == PNG_COLOR_TYPE_GRAY)
1106 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001107 /* One 16 bit value */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001108 if (tran->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001109 {
1110 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001111 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001112 return;
1113 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001114 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001115 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001116 }
1117 else if (color_type == PNG_COLOR_TYPE_RGB)
1118 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001119 /* Three 16 bit values */
Guy Schalnat0d580581995-07-20 02:43:20 -05001120 png_save_uint_16(buf, tran->red);
1121 png_save_uint_16(buf + 2, tran->green);
1122 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001123 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001124 {
1125 png_warning(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001126 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001127 return;
1128 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001129 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001130 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001131 else
1132 {
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001133 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001134 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001135}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001136#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001137
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001138#ifdef PNG_WRITE_bKGD_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001139/* Write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001140void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001141png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001142{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001143 PNG_bKGD;
Guy Schalnat0d580581995-07-20 02:43:20 -05001144 png_byte buf[6];
1145
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001146 png_debug(1, "in png_write_bKGD");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001147
Guy Schalnat0d580581995-07-20 02:43:20 -05001148 if (color_type == PNG_COLOR_TYPE_PALETTE)
1149 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001150 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001151#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001152 (png_ptr->num_palette ||
1153 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001154#endif
Glenn Randers-Pehrsond6d80752008-12-02 09:49:43 -06001155 back->index >= png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001156 {
1157 png_warning(png_ptr, "Invalid background palette index");
1158 return;
1159 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001160 buf[0] = back->index;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001161 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001162 }
1163 else if (color_type & PNG_COLOR_MASK_COLOR)
1164 {
1165 png_save_uint_16(buf, back->red);
1166 png_save_uint_16(buf + 2, back->green);
1167 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001168 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001169 {
1170 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001171 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001172 return;
1173 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001174 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001175 }
1176 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001177 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001178 if (back->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001179 {
1180 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001181 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001182 return;
1183 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001184 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001185 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001186 }
1187}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001188#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001189
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001190#ifdef PNG_WRITE_hIST_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001191/* Write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001192void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001193png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001194{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001195 PNG_hIST;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001196 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001197 png_byte buf[3];
1198
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001199 png_debug(1, "in png_write_hIST");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001200
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001201 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001202 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001203 png_debug2(3, "num_hist = %d, num_palette = %d", num_hist,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001204 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001205 png_warning(png_ptr, "Invalid number of histogram entries specified");
1206 return;
1207 }
1208
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001209 png_write_chunk_start(png_ptr, (png_bytep)png_hIST,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001210 (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001211 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001212 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001213 png_save_uint_16(buf, hist[i]);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001214 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001215 }
1216 png_write_chunk_end(png_ptr);
1217}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001218#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001219
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001220#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1221 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001222/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1223 * and if invalid, correct the keyword rather than discarding the entire
1224 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1225 * length, forbids leading or trailing whitespace, multiple internal spaces,
1226 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001227 *
1228 * The new_key is allocated to hold the corrected keyword and must be freed
1229 * by the calling routine. This avoids problems with trying to write to
1230 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001231 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001232png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001233png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001234{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001235 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001236 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001237 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001238 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001239
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001240 png_debug(1, "in png_check_keyword");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001241
Andreas Dilger47a0c421997-05-16 02:46:07 -05001242 *new_key = NULL;
1243
1244 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001245 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001246 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001247 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001248 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001249
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001250 png_debug1(2, "Keyword to be checked is '%s'", key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001251
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001252 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001253 if (*new_key == NULL)
1254 {
1255 png_warning(png_ptr, "Out of memory while procesing keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001256 return ((png_size_t)0);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001257 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001258
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001259 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001260 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001261 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001262 if ((png_byte)*kp < 0x20 ||
1263 ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001264 {
Glenn Randers-Pehrson6a9e4802010-02-19 09:47:43 -06001265#ifdef PNG_CONSOLE_IO_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001266 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001267
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001268 png_snprintf(msg, 40,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001269 "invalid keyword character 0x%02X", (png_byte)*kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001270 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001271#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001272 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001273#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001274 *dp = ' ';
1275 }
1276 else
1277 {
1278 *dp = *kp;
1279 }
1280 }
1281 *dp = '\0';
1282
1283 /* Remove any trailing white space. */
1284 kp = *new_key + key_len - 1;
1285 if (*kp == ' ')
1286 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001287 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001288
1289 while (*kp == ' ')
1290 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001291 *(kp--) = '\0';
1292 key_len--;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001293 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001294 }
1295
1296 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001297 kp = *new_key;
1298 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001299 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001300 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001301
1302 while (*kp == ' ')
1303 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001304 kp++;
1305 key_len--;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001306 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001307 }
1308
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001309 png_debug1(2, "Checking for multiple internal spaces in '%s'", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001310
Andreas Dilger47a0c421997-05-16 02:46:07 -05001311 /* Remove multiple internal spaces. */
1312 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001313 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001314 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001315 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001316 *(dp++) = *kp;
1317 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001318 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001319 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001320 {
1321 key_len--;
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001322 kwarn = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001323 }
1324 else
1325 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001326 *(dp++) = *kp;
1327 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001328 }
1329 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001330 *dp = '\0';
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001331 if (kwarn)
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001332 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001333
1334 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001335 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001336 png_free(png_ptr, *new_key);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001337 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001338 }
1339
1340 if (key_len > 79)
1341 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001342 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001343 (*new_key)[79] = '\0';
Andreas Dilger47a0c421997-05-16 02:46:07 -05001344 key_len = 79;
1345 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001346
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001347 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001348}
1349#endif
1350
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001351#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001352/* Write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001353void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001354png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001355 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001356{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001357 PNG_tEXt;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001358 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001359 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001360
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001361 png_debug(1, "in png_write_tEXt");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001362
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001363 if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001364 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001365
Andreas Dilger47a0c421997-05-16 02:46:07 -05001366 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001367 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001368 else
1369 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001370
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001371 /* Make sure we include the 0 after the key */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001372 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001373 (png_uint_32)(key_len + text_len + 1));
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001374 /*
1375 * We leave it to the application to meet PNG-1.0 requirements on the
1376 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1377 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001378 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001379 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001380 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1381 (png_size_t)(key_len + 1));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001382 if (text_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001383 png_write_chunk_data(png_ptr, (png_bytep)text, (png_size_t)text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001384
Guy Schalnat0d580581995-07-20 02:43:20 -05001385 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001386 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001387}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001388#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001389
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001390#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001391/* Write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001392void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001393png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001394 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001395{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001396 PNG_zTXt;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001397 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001398 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001399 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001400 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001401
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001402 png_debug(1, "in png_write_zTXt");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001403
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001404 comp.num_output_ptr = 0;
1405 comp.max_output_ptr = 0;
1406 comp.output_ptr = NULL;
1407 comp.input = NULL;
1408 comp.input_len = 0;
1409
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001410 if ((key_len = png_check_keyword(png_ptr, key, &new_key)) == 0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001411 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001412 png_free(png_ptr, new_key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001413 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001414 }
1415
Andreas Dilger47a0c421997-05-16 02:46:07 -05001416 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1417 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001418 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001419 png_free(png_ptr, new_key);
1420 return;
1421 }
1422
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001423 text_len = png_strlen(text);
1424
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001425 /* Compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001426 text_len = png_text_compress(png_ptr, text, text_len, compression,
1427 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001428
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001429 /* Write start of chunk */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001430 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001431 (png_uint_32)(key_len+text_len + 2));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001432 /* Write key */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001433 png_write_chunk_data(png_ptr, (png_bytep)new_key,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001434 (png_size_t)(key_len + 1));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001435 png_free(png_ptr, new_key);
1436
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001437 buf[0] = (png_byte)compression;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001438 /* Write compression */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001439 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001440 /* Write the compressed data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001441 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001442
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001443 /* Close the chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -05001444 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001445}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001446#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001447
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001448#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001449/* Write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001450void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001451png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001452 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001453{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001454 PNG_iTXt;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001455 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001456 png_charp new_lang;
1457 png_charp new_key = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001458 png_byte cbuf[2];
1459 compression_state comp;
1460
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001461 png_debug(1, "in png_write_iTXt");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001462
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001463 comp.num_output_ptr = 0;
1464 comp.max_output_ptr = 0;
1465 comp.output_ptr = NULL;
1466 comp.input = NULL;
1467
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001468 if ((key_len = png_check_keyword(png_ptr, key, &new_key)) == 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001469 return;
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001470
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001471 if ((lang_len = png_check_keyword(png_ptr, lang, &new_lang)) == 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001472 {
1473 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001474 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001475 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001476 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001477
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001478 if (lang_key == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001479 lang_key_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001480 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001481 lang_key_len = png_strlen(lang_key);
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001482
1483 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001484 text_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001485 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001486 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001487
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001488 /* Compute the compressed data; do it now for the length */
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001489 text_len = png_text_compress(png_ptr, text, text_len, compression - 2,
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001490 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001491
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001492
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001493 /* Make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001494 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001495
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001496 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1497 (png_uint_32)(
1498 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1499 + key_len
1500 + lang_len
1501 + lang_key_len
1502 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001503
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001504 /* We leave it to the application to meet PNG-1.0 requirements on the
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001505 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1506 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1507 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1508 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001509 png_write_chunk_data(png_ptr, (png_bytep)new_key,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001510 (png_size_t)(key_len + 1));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001511
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001512 /* Set the compression flag */
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001513 if (compression == PNG_ITXT_COMPRESSION_NONE ||
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001514 compression == PNG_TEXT_COMPRESSION_NONE)
1515 cbuf[0] = 0;
1516 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1517 cbuf[0] = 1;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001518 /* Set the compression method */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001519 cbuf[1] = 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001520 png_write_chunk_data(png_ptr, cbuf, (png_size_t)2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001521
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001522 cbuf[0] = 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001523 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf),
1524 (png_size_t)(lang_len + 1));
1525 png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf),
1526 (png_size_t)(lang_key_len + 1));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001527 png_write_compressed_data_out(png_ptr, &comp);
1528
1529 png_write_chunk_end(png_ptr);
1530 png_free(png_ptr, new_key);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001531 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001532}
1533#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001534
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001535#ifdef PNG_WRITE_oFFs_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001536/* Write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001537void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001538png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001539 int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001540{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001541 PNG_oFFs;
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");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001545
Andreas Dilger47a0c421997-05-16 02:46:07 -05001546 if (unit_type >= PNG_OFFSET_LAST)
1547 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1548
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001549 png_save_int_32(buf, x_offset);
1550 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001551 buf[8] = (png_byte)unit_type;
1552
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001553 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001554}
1555#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001556#ifdef PNG_WRITE_pCAL_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001557/* Write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001558void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001559png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1560 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1561{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001562 PNG_pCAL;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001563 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001564 png_uint_32p params_len;
1565 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001566 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001567 int i;
1568
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001569 png_debug1(1, "in png_write_pCAL (%d parameters)", nparams);
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001570
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-Pehrson16908a12010-03-06 07:34:28 -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
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001584 * null terminator for the last parameter.
1585 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001586 for (i = 0; i < nparams; i++)
1587 {
1588 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001589 png_debug2(3, "pCAL parameter %d length = %lu", i,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001590 (unsigned long) params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001591 total_len += (png_size_t)params_len[i];
1592 }
1593
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001594 png_debug1(3, "pCAL total length = %d", (int)total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001595 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001596 png_write_chunk_data(png_ptr, (png_bytep)new_purpose,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001597 (png_size_t)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001598 png_save_int_32(buf, X0);
1599 png_save_int_32(buf + 4, X1);
1600 buf[8] = (png_byte)type;
1601 buf[9] = (png_byte)nparams;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001602 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1603 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001604
1605 png_free(png_ptr, new_purpose);
1606
1607 for (i = 0; i < nparams; i++)
1608 {
1609 png_write_chunk_data(png_ptr, (png_bytep)params[i],
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001610 (png_size_t)params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001611 }
1612
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001613 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001614 png_write_chunk_end(png_ptr);
1615}
1616#endif
1617
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001618#ifdef PNG_WRITE_sCAL_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001619/* Write the sCAL chunk */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001620#if defined(PNG_FLOATING_POINT_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001621void /* PRIVATE */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001622png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001623{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001624 PNG_sCAL;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001625 char buf[64];
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001626 png_size_t total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001627
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001628 png_debug(1, "in png_write_sCAL");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001629
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001630 buf[0] = (char)unit;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001631 png_snprintf(buf + 1, 63, "%12.12e", width);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001632 total_len = 1 + png_strlen(buf + 1) + 1;
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001633 png_snprintf(buf + total_len, 64 - total_len, "%12.12e", height);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001634 total_len += png_strlen(buf + total_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001635
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001636 png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001637 png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001638}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001639#else
1640#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001641void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001642png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001643 png_charp height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001644{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001645 PNG_sCAL;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001646 png_byte buf[64];
1647 png_size_t wlen, hlen, total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001648
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001649 png_debug(1, "in png_write_sCAL_s");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001650
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001651 wlen = png_strlen(width);
1652 hlen = png_strlen(height);
1653 total_len = wlen + hlen + 2;
1654 if (total_len > 64)
1655 {
1656 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
1657 return;
1658 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001659
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001660 buf[0] = (png_byte)unit;
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001661 png_memcpy(buf + 1, width, wlen + 1); /* Append the '\0' here */
1662 png_memcpy(buf + wlen + 2, height, hlen); /* Do NOT append the '\0' here */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001663
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001664 png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001665 png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001666}
1667#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001668#endif
1669#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001670
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001671#ifdef PNG_WRITE_pHYs_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001672/* Write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001673void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001674png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001675 png_uint_32 y_pixels_per_unit,
1676 int unit_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001677{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001678 PNG_pHYs;
Guy Schalnat0d580581995-07-20 02:43:20 -05001679 png_byte buf[9];
1680
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001681 png_debug(1, "in png_write_pHYs");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001682
Guy Schalnate5a37791996-06-05 15:50:50 -05001683 if (unit_type >= PNG_RESOLUTION_LAST)
1684 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1685
Guy Schalnat0d580581995-07-20 02:43:20 -05001686 png_save_uint_32(buf, x_pixels_per_unit);
1687 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001688 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001689
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001690 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001691}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001692#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001693
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001694#ifdef PNG_WRITE_tIME_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001695/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1696 * or png_convert_from_time_t(), or fill in the structure yourself.
1697 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001698void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001699png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001700{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001701 PNG_tIME;
Guy Schalnat0d580581995-07-20 02:43:20 -05001702 png_byte buf[7];
1703
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001704 png_debug(1, "in png_write_tIME");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001705
Guy Schalnate5a37791996-06-05 15:50:50 -05001706 if (mod_time->month > 12 || mod_time->month < 1 ||
1707 mod_time->day > 31 || mod_time->day < 1 ||
1708 mod_time->hour > 23 || mod_time->second > 60)
1709 {
1710 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1711 return;
1712 }
1713
Guy Schalnat0d580581995-07-20 02:43:20 -05001714 png_save_uint_16(buf, mod_time->year);
1715 buf[2] = mod_time->month;
1716 buf[3] = mod_time->day;
1717 buf[4] = mod_time->hour;
1718 buf[5] = mod_time->minute;
1719 buf[6] = mod_time->second;
1720
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001721 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001722}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001723#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001724
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001725/* Initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001726void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001727png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001728{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001729#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001730 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001731
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001732 /* Start of interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001733 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001734
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001735 /* Offset to next interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001736 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
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 in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001739 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
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 in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001742 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001743#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001744
Andreas Dilger47a0c421997-05-16 02:46:07 -05001745 png_size_t buf_size;
1746
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001747 png_debug(1, "in png_write_start_row");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001748
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001749 buf_size = (png_size_t)(PNG_ROWBYTES(
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001750 png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001751
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001752 /* Set up row buffer */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001753 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001754 (png_alloc_size_t)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001755 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001756
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001757#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001758 /* Set up filtering buffer, if using this filter */
Guy Schalnate5a37791996-06-05 15:50:50 -05001759 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001760 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001761 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001762 (png_alloc_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001763 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001764 }
1765
Andreas Dilger47a0c421997-05-16 02:46:07 -05001766 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001767 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1768 {
Glenn Randers-Pehrsondfa99af2009-10-29 23:33:46 -05001769 /* Set up previous row buffer */
1770 png_ptr->prev_row = (png_bytep)png_calloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001771 (png_alloc_size_t)buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001772
1773 if (png_ptr->do_filter & PNG_FILTER_UP)
1774 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001775 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001776 (png_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001777 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001778 }
1779
1780 if (png_ptr->do_filter & PNG_FILTER_AVG)
1781 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001782 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001783 (png_alloc_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001784 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001785 }
1786
1787 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1788 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001789 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001790 (png_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001791 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001792 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001793 }
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001794#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05001795
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001796#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001797 /* If interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001798 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001799 {
1800 if (!(png_ptr->transformations & PNG_INTERLACE))
1801 {
1802 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001803 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001804 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001805 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001806 }
1807 else
1808 {
1809 png_ptr->num_rows = png_ptr->height;
1810 png_ptr->usr_width = png_ptr->width;
1811 }
1812 }
1813 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001814#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001815 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001816 png_ptr->num_rows = png_ptr->height;
1817 png_ptr->usr_width = png_ptr->width;
1818 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001819 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1820 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001821}
1822
Andreas Dilger47a0c421997-05-16 02:46:07 -05001823/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001824void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001825png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001826{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001827#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001828 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001829
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001830 /* Start of interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001831 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001832
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001833 /* Offset to next interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001834 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001835
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001836 /* Start of interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001837 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001838
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001839 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001840 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001841#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001842
Guy Schalnat0d580581995-07-20 02:43:20 -05001843 int ret;
1844
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001845 png_debug(1, "in png_write_finish_row");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001846
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001847 /* Next row */
Guy Schalnat0d580581995-07-20 02:43:20 -05001848 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001849
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001850 /* See if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001851 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001852 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001853
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001854#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001855 /* If interlaced, go to next pass */
Guy Schalnat0d580581995-07-20 02:43:20 -05001856 if (png_ptr->interlaced)
1857 {
1858 png_ptr->row_number = 0;
1859 if (png_ptr->transformations & PNG_INTERLACE)
1860 {
1861 png_ptr->pass++;
1862 }
1863 else
1864 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001865 /* Loop until we find a non-zero width or height pass */
Guy Schalnat0d580581995-07-20 02:43:20 -05001866 do
1867 {
1868 png_ptr->pass++;
1869 if (png_ptr->pass >= 7)
1870 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001871 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001872 png_pass_inc[png_ptr->pass] - 1 -
1873 png_pass_start[png_ptr->pass]) /
1874 png_pass_inc[png_ptr->pass];
1875 png_ptr->num_rows = (png_ptr->height +
1876 png_pass_yinc[png_ptr->pass] - 1 -
1877 png_pass_ystart[png_ptr->pass]) /
1878 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001879 if (png_ptr->transformations & PNG_INTERLACE)
1880 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001881 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1882
1883 }
1884
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001885 /* Reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001886 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001887 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001888 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001889 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001890 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
1891 png_ptr->usr_bit_depth, png_ptr->width)) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001892 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001893 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001894 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001895#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001896
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001897 /* If we get here, we've just written the last row, so we need
Guy Schalnat0d580581995-07-20 02:43:20 -05001898 to flush the compressor */
1899 do
1900 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001901 /* Tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001902 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001903 /* Check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001904 if (ret == Z_OK)
1905 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001906 /* Check to see if we need more room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001907 if (!(png_ptr->zstream.avail_out))
1908 {
1909 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1910 png_ptr->zstream.next_out = png_ptr->zbuf;
1911 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1912 }
1913 }
1914 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001915 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001916 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001917 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001918 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001919 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001920 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001921 } while (ret != Z_STREAM_END);
1922
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001923 /* Write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001924 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001925 {
1926 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001927 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001928 }
1929
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001930 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -06001931 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat0d580581995-07-20 02:43:20 -05001932}
1933
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001934#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001935/* Pick out the correct pixels for the interlace pass.
1936 * The basic idea here is to go through the row with a source
1937 * pointer and a destination pointer (sp and dp), and copy the
1938 * correct pixels for the pass. As the row gets compacted,
1939 * sp will always be >= dp, so we should never overwrite anything.
1940 * See the default: case for the easiest code to understand.
1941 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001942void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001943png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001944{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001945 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001946
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001947 /* Start of interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001948 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001949
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001950 /* Offset to next interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001951 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001952
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001953 png_debug(1, "in png_do_write_interlace");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001954
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001955 /* We don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001956 if (pass < 6)
Guy Schalnat0d580581995-07-20 02:43:20 -05001957 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001958 /* Each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001959 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001960 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001961 case 1:
1962 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001963 png_bytep sp;
1964 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001965 int shift;
1966 int d;
1967 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001968 png_uint_32 i;
1969 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001970
1971 dp = row;
1972 d = 0;
1973 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001974 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001975 i += png_pass_inc[pass])
1976 {
1977 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001978 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001979 d |= (value << shift);
1980
1981 if (shift == 0)
1982 {
1983 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001984 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001985 d = 0;
1986 }
1987 else
1988 shift--;
1989
1990 }
1991 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001992 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001993 break;
1994 }
1995 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001996 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001997 png_bytep sp;
1998 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001999 int shift;
2000 int d;
2001 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002002 png_uint_32 i;
2003 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002004
2005 dp = row;
2006 shift = 6;
2007 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002008 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002009 i += png_pass_inc[pass])
2010 {
2011 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002012 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05002013 d |= (value << shift);
2014
2015 if (shift == 0)
2016 {
2017 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002018 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002019 d = 0;
2020 }
2021 else
2022 shift -= 2;
2023 }
2024 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002025 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002026 break;
2027 }
2028 case 4:
2029 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002030 png_bytep sp;
2031 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002032 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05002033 int d;
2034 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002035 png_uint_32 i;
2036 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002037
2038 dp = row;
2039 shift = 4;
2040 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002041 for (i = png_pass_start[pass]; i < row_width;
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002042 i += png_pass_inc[pass])
Guy Schalnat0d580581995-07-20 02:43:20 -05002043 {
2044 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002045 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05002046 d |= (value << shift);
2047
2048 if (shift == 0)
2049 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002050 shift = 4;
2051 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002052 d = 0;
2053 }
2054 else
2055 shift -= 4;
2056 }
2057 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002058 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002059 break;
2060 }
2061 default:
2062 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002063 png_bytep sp;
2064 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002065 png_uint_32 i;
2066 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002067 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002068
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002069 /* Start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002070 dp = row;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002071 /* Find out how many bytes each pixel takes up */
Guy Schalnat0d580581995-07-20 02:43:20 -05002072 pixel_bytes = (row_info->pixel_depth >> 3);
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002073
2074 /* Loop through the row, only looking at the pixels that matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002075 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002076 i += png_pass_inc[pass])
2077 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002078 /* Find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002079 sp = row + (png_size_t)i * pixel_bytes;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002080 /* Move the pixel */
Guy Schalnat0d580581995-07-20 02:43:20 -05002081 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002082 png_memcpy(dp, sp, pixel_bytes);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002083 /* Next pixel */
Guy Schalnat0d580581995-07-20 02:43:20 -05002084 dp += pixel_bytes;
2085 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002086 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002087 }
2088 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002089 /* Set new row width */
Guy Schalnat0d580581995-07-20 02:43:20 -05002090 row_info->width = (row_info->width +
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002091 png_pass_inc[pass] - 1 -
2092 png_pass_start[pass]) /
2093 png_pass_inc[pass];
2094 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2095 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002096 }
2097}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002098#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002099
Andreas Dilger47a0c421997-05-16 02:46:07 -05002100/* This filters the row, chooses which filter to use, if it has not already
2101 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002102 * chosen filter.
2103 */
Glenn Randers-Pehrson78067772004-11-02 21:49:39 -06002104#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002105#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002106#define PNG_LOMASK ((png_uint_32)0xffffL)
2107#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002108void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002109png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002110{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002111 png_bytep best_row;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002112#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002113 png_bytep prev_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002114 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002115 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002116 png_uint_32 row_bytes = row_info->rowbytes;
Glenn Randers-Pehrson9d8b41e2009-07-19 14:45:43 -05002117#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002118 int num_p_filters = (int)png_ptr->num_prev_filters;
Glenn Randers-Pehrson9d8b41e2009-07-19 14:45:43 -05002119#endif
2120
2121 png_debug(1, "in png_write_find_filter");
2122
2123#ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson4ace0e12009-07-19 15:04:35 -05002124 if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS)
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05002125 {
2126 /* These will never be selected so we need not test them. */
2127 filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_PAETH);
2128 }
Glenn Randers-Pehrsonae4af562009-07-18 11:28:33 -05002129#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002130
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002131 /* Find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002132 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002133
2134 prev_row = png_ptr->prev_row;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002135#endif
2136 best_row = png_ptr->row_buf;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002137#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002138 row_buf = best_row;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002139 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002140
Andreas Dilger47a0c421997-05-16 02:46:07 -05002141 /* The prediction method we use is to find which method provides the
2142 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002143 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002144 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002145 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002146 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002147 * predictive" method (not implemented yet), which does test compressions
2148 * of lines using different filter methods, and then chooses the
2149 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002150 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002151 *
2152 * GRR 980525: consider also
2153 * (1) minimum sum of absolute differences from running average (i.e.,
2154 * keep running sum of non-absolute differences & count of bytes)
2155 * [track dispersion, too? restart average if dispersion too large?]
2156 * (1b) minimum sum of absolute differences from sliding average, probably
2157 * with window size <= deflate window (usually 32K)
2158 * (2) minimum sum of squared differences from zero or running average
2159 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002160 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002161
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002162
Guy Schalnate5a37791996-06-05 15:50:50 -05002163 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002164 * that has been chosen, as it doesn't actually do anything to the data.
2165 */
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002166 if ((filter_to_do & PNG_FILTER_NONE) && filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002167 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002168 png_bytep rp;
2169 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002170 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002171 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002172
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002173 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002174 {
2175 v = *rp;
2176 sum += (v < 128) ? v : 256 - v;
2177 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002178
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002179#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002180 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2181 {
2182 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002183 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002184 sumlo = sum & PNG_LOMASK;
2185 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2186
2187 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002188 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002189 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002190 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002191 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002192 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002193 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002194 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002195 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002196 }
2197 }
2198
2199 /* Factor in the cost of this filter (this is here for completeness,
2200 * but it makes no sense to have a "cost" for the NONE filter, as
2201 * it has the minimum possible computational cost - none).
2202 */
2203 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002204 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002205 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002206 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002207
2208 if (sumhi > PNG_HIMASK)
2209 sum = PNG_MAXSUM;
2210 else
2211 sum = (sumhi << PNG_HISHIFT) + sumlo;
2212 }
2213#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002214 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002215 }
2216
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002217 /* Sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002218 if (filter_to_do == PNG_FILTER_SUB)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002219 /* It's the only filter so no testing is needed */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002220 {
2221 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002222 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002223 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2224 i++, rp++, dp++)
2225 {
2226 *dp = *rp;
2227 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002228 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002229 i++, rp++, lp++, dp++)
2230 {
2231 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2232 }
2233 best_row = png_ptr->sub_row;
2234 }
2235
2236 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002237 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002238 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002239 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002240 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002241 int v;
2242
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002243#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002244 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002245 * would reduce the sum of this filter, so that we can do the
2246 * early exit comparison without scaling the sum each time.
2247 */
2248 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2249 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002250 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002251 png_uint_32 lmhi, lmlo;
2252 lmlo = lmins & PNG_LOMASK;
2253 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2254
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002255 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002256 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002257 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002258 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002259 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002260 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002261 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002262 PNG_WEIGHT_SHIFT;
2263 }
2264 }
2265
2266 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002267 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002268 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002269 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002270
2271 if (lmhi > PNG_HIMASK)
2272 lmins = PNG_MAXSUM;
2273 else
2274 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2275 }
2276#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002277
Guy Schalnate5a37791996-06-05 15:50:50 -05002278 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2279 i++, rp++, dp++)
2280 {
2281 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002282
Guy Schalnate5a37791996-06-05 15:50:50 -05002283 sum += (v < 128) ? v : 256 - v;
2284 }
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002285 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002286 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002287 {
2288 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002289
Guy Schalnate5a37791996-06-05 15:50:50 -05002290 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002291
2292 if (sum > lmins) /* We are already worse, don't continue. */
2293 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002294 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002295
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002296#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002297 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2298 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002299 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002300 png_uint_32 sumhi, sumlo;
2301 sumlo = sum & PNG_LOMASK;
2302 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2303
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002304 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002305 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002306 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002307 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002308 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002309 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002310 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002311 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002312 }
2313 }
2314
2315 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002316 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002317 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002318 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002319
2320 if (sumhi > PNG_HIMASK)
2321 sum = PNG_MAXSUM;
2322 else
2323 sum = (sumhi << PNG_HISHIFT) + sumlo;
2324 }
2325#endif
2326
Guy Schalnate5a37791996-06-05 15:50:50 -05002327 if (sum < mins)
2328 {
2329 mins = sum;
2330 best_row = png_ptr->sub_row;
2331 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002332 }
2333
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002334 /* Up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002335 if (filter_to_do == PNG_FILTER_UP)
2336 {
2337 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002338 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002339
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002340 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002341 pp = prev_row + 1; i < row_bytes;
2342 i++, rp++, pp++, dp++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002343 {
2344 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2345 }
2346 best_row = png_ptr->up_row;
2347 }
2348
2349 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002350 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002351 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002352 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002353 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002354 int v;
2355
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002356
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002357#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002358 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2359 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002360 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002361 png_uint_32 lmhi, lmlo;
2362 lmlo = lmins & PNG_LOMASK;
2363 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2364
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002365 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002366 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002367 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002368 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002369 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002370 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002371 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002372 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002373 }
2374 }
2375
2376 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002377 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002378 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002379 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002380
2381 if (lmhi > PNG_HIMASK)
2382 lmins = PNG_MAXSUM;
2383 else
2384 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2385 }
2386#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002387
2388 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002389 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002390 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002391 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002392
2393 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002394
2395 if (sum > lmins) /* We are already worse, don't continue. */
2396 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002397 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002398
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002399#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002400 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2401 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002402 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002403 png_uint_32 sumhi, sumlo;
2404 sumlo = sum & PNG_LOMASK;
2405 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2406
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002407 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002408 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002409 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002410 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002411 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002412 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002413 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002414 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002415 }
2416 }
2417
2418 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002419 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002420 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002421 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002422
2423 if (sumhi > PNG_HIMASK)
2424 sum = PNG_MAXSUM;
2425 else
2426 sum = (sumhi << PNG_HISHIFT) + sumlo;
2427 }
2428#endif
2429
Guy Schalnate5a37791996-06-05 15:50:50 -05002430 if (sum < mins)
2431 {
2432 mins = sum;
2433 best_row = png_ptr->up_row;
2434 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002435 }
2436
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002437 /* Avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002438 if (filter_to_do == PNG_FILTER_AVG)
2439 {
2440 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002441 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002442 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002443 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002444 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002445 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002446 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002447 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002448 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002449 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2450 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002451 }
2452 best_row = png_ptr->avg_row;
2453 }
2454
2455 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002456 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002457 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002458 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002459 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002460 int v;
2461
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002462#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002463 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2464 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002465 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002466 png_uint_32 lmhi, lmlo;
2467 lmlo = lmins & PNG_LOMASK;
2468 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2469
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002470 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002471 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002472 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002473 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002474 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002475 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002476 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002477 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002478 }
2479 }
2480
2481 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002482 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002483 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002484 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002485
2486 if (lmhi > PNG_HIMASK)
2487 lmins = PNG_MAXSUM;
2488 else
2489 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2490 }
2491#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002492
2493 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002494 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002495 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002496 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002497
2498 sum += (v < 128) ? v : 256 - v;
2499 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002500 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002501 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002502 v = *dp++ =
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002503 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002504
2505 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002506
2507 if (sum > lmins) /* We are already worse, don't continue. */
2508 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002509 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002510
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002511#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002512 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2513 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002514 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002515 png_uint_32 sumhi, sumlo;
2516 sumlo = sum & PNG_LOMASK;
2517 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2518
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002519 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002520 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002521 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002522 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002523 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002524 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002525 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002526 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002527 }
2528 }
2529
2530 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002531 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002532 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002533 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002534
2535 if (sumhi > PNG_HIMASK)
2536 sum = PNG_MAXSUM;
2537 else
2538 sum = (sumhi << PNG_HISHIFT) + sumlo;
2539 }
2540#endif
2541
Guy Schalnate5a37791996-06-05 15:50:50 -05002542 if (sum < mins)
2543 {
2544 mins = sum;
2545 best_row = png_ptr->avg_row;
2546 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002547 }
2548
Andreas Dilger47a0c421997-05-16 02:46:07 -05002549 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002550 if (filter_to_do == PNG_FILTER_PAETH)
2551 {
2552 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002553 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002554 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002555 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002556 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002557 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002558 }
2559
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002560 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002561 {
2562 int a, b, c, pa, pb, pc, p;
2563
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002564 b = *pp++;
2565 c = *cp++;
2566 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002567
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002568 p = b - c;
2569 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002570
2571#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002572 pa = abs(p);
2573 pb = abs(pc);
2574 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002575#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002576 pa = p < 0 ? -p : p;
2577 pb = pc < 0 ? -pc : pc;
2578 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002579#endif
2580
2581 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2582
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002583 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002584 }
2585 best_row = png_ptr->paeth_row;
2586 }
2587
2588 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002589 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002590 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002591 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002592 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002593 int v;
2594
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002595#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002596 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2597 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002598 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002599 png_uint_32 lmhi, lmlo;
2600 lmlo = lmins & PNG_LOMASK;
2601 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2602
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002603 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002604 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002605 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002606 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002607 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002608 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002609 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002610 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002611 }
2612 }
2613
2614 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002615 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002616 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002617 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002618
2619 if (lmhi > PNG_HIMASK)
2620 lmins = PNG_MAXSUM;
2621 else
2622 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2623 }
2624#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002625
2626 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002627 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002628 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002629 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002630
2631 sum += (v < 128) ? v : 256 - v;
2632 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002633
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002634 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002635 {
2636 int a, b, c, pa, pb, pc, p;
2637
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002638 b = *pp++;
2639 c = *cp++;
2640 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002641
2642#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002643 p = b - c;
2644 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002645#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002646 pa = abs(p);
2647 pb = abs(pc);
2648 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002649#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002650 pa = p < 0 ? -p : p;
2651 pb = pc < 0 ? -pc : pc;
2652 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002653#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002654 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2655#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002656 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002657 pa = abs(p - a);
2658 pb = abs(p - b);
2659 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002660 if (pa <= pb && pa <= pc)
2661 p = a;
2662 else if (pb <= pc)
2663 p = b;
2664 else
2665 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002666#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002667
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002668 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002669
2670 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002671
2672 if (sum > lmins) /* We are already worse, don't continue. */
2673 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002674 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002675
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002676#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002677 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2678 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002679 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002680 png_uint_32 sumhi, sumlo;
2681 sumlo = sum & PNG_LOMASK;
2682 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2683
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002684 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002685 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002686 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002687 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002688 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002689 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002690 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002691 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002692 }
2693 }
2694
2695 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002696 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002697 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002698 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002699
2700 if (sumhi > PNG_HIMASK)
2701 sum = PNG_MAXSUM;
2702 else
2703 sum = (sumhi << PNG_HISHIFT) + sumlo;
2704 }
2705#endif
2706
Guy Schalnate5a37791996-06-05 15:50:50 -05002707 if (sum < mins)
2708 {
2709 best_row = png_ptr->paeth_row;
2710 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002711 }
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002712#endif /* PNG_WRITE_FILTER_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002713 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002714
Guy Schalnate5a37791996-06-05 15:50:50 -05002715 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002716
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002717#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002718#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002719 /* Save the type of filter we picked this time for future calculations */
2720 if (png_ptr->num_prev_filters > 0)
2721 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002722 int j;
2723 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002724 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002725 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002726 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002727 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002728 }
2729#endif
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002730#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -05002731}
2732
2733
Andreas Dilger47a0c421997-05-16 02:46:07 -05002734/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002735void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002736png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2737{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002738 png_debug(1, "in png_write_filtered_row");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05002739
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002740 png_debug1(2, "filter = %d", filtered_row[0]);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002741 /* Set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002742
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002743 png_ptr->zstream.next_in = filtered_row;
2744 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002745 /* Repeat until we have compressed all the data */
Guy Schalnate5a37791996-06-05 15:50:50 -05002746 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002747 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002748 int ret; /* Return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002749
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002750 /* Compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002751 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002752 /* Check for compression errors */
Guy Schalnate5a37791996-06-05 15:50:50 -05002753 if (ret != Z_OK)
2754 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002755 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002756 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002757 else
2758 png_error(png_ptr, "zlib error");
2759 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002760
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002761 /* See if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002762 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002763 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002764 /* Write the IDAT and reset the zlib output buffer */
Guy Schalnate5a37791996-06-05 15:50:50 -05002765 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002766 png_ptr->zstream.next_out = png_ptr->zbuf;
2767 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002768 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002769 /* Repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002770 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002771
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002772 /* Swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002773 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002774 {
2775 png_bytep tptr;
2776
2777 tptr = png_ptr->prev_row;
2778 png_ptr->prev_row = png_ptr->row_buf;
2779 png_ptr->row_buf = tptr;
2780 }
2781
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002782 /* Finish row - updates counters and flushes zlib if last row */
Guy Schalnate5a37791996-06-05 15:50:50 -05002783 png_write_finish_row(png_ptr);
2784
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002785#ifdef PNG_WRITE_FLUSH_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05002786 png_ptr->flush_rows++;
2787
2788 if (png_ptr->flush_dist > 0 &&
2789 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002790 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002791 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002792 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002793#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002794}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05002795#endif /* PNG_WRITE_SUPPORTED */