blob: 76f2bdacf7756ffcf3d36803aeddc91acbc446aa [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-Pehrson glennrp@comcast.net11a3c7b2009-05-15 20:33:24 -05004 * Last changed in libpng 1.4.0 [May 15, 2009]
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06006 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05007 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050010
Guy Schalnat0d580581995-07-20 02:43:20 -050011#include "png.h"
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050012#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050013#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050014
Andreas Dilger47a0c421997-05-16 02:46:07 -050015/* Place a 32-bit number into a buffer in PNG byte order. We work
16 * with unsigned numbers for convenience, although one supported
17 * ancillary chunk uses signed (two's complement) numbers.
18 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060019void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -060020png_save_uint_32(png_bytep buf, png_uint_32 i)
Guy Schalnat0d580581995-07-20 02:43:20 -050021{
22 buf[0] = (png_byte)((i >> 24) & 0xff);
23 buf[1] = (png_byte)((i >> 16) & 0xff);
24 buf[2] = (png_byte)((i >> 8) & 0xff);
25 buf[3] = (png_byte)(i & 0xff);
26}
27
Glenn Randers-Pehrson86dc9812006-05-10 07:27:44 -050028#if defined(PNG_SAVE_INT_32_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -050029/* The png_save_int_32 function assumes integers are stored in two's
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060030 * complement format. If this isn't the case, then this routine needs to
31 * be modified to write data in two's complement format.
32 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060033void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050034png_save_int_32(png_bytep buf, png_int_32 i)
35{
36 buf[0] = (png_byte)((i >> 24) & 0xff);
37 buf[1] = (png_byte)((i >> 16) & 0xff);
38 buf[2] = (png_byte)((i >> 8) & 0xff);
39 buf[3] = (png_byte)(i & 0xff);
40}
Glenn Randers-Pehrson86dc9812006-05-10 07:27:44 -050041#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050042
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060043/* Place a 16-bit number into a buffer in PNG byte order.
44 * The parameter is declared unsigned int, not png_uint_16,
45 * just to avoid potential problems on pre-ANSI C compilers.
46 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060047void PNGAPI
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060048png_save_uint_16(png_bytep buf, unsigned int i)
Guy Schalnat0d580581995-07-20 02:43:20 -050049{
50 buf[0] = (png_byte)((i >> 8) & 0xff);
51 buf[1] = (png_byte)(i & 0xff);
52}
53
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050054/* Simple function to write the signature. If we have already written
55 * the magic bytes of the signature, or more likely, the PNG stream is
56 * being embedded into another stream and doesn't need its own signature,
57 * we should call png_set_sig_bytes() to tell libpng how many of the
58 * bytes have already been written.
59 */
60void PNGAPI
61png_write_sig(png_structp png_ptr)
62{
63 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050064
65#ifdef PNG_IO_STATE_SUPPORTED
66 /* inform the I/O callback that the signature is being written */
67 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_SIGNATURE;
68#endif
69
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050070 /* Write the rest of the 8 byte signature */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050071 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050072 (png_size_t)(8 - png_ptr->sig_bytes));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050073 if (png_ptr->sig_bytes < 3)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050074 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
75}
76
Andreas Dilger47a0c421997-05-16 02:46:07 -050077/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060078 * representing the chunk name. The array must be at least 4 bytes in
79 * length, and does not need to be null terminated. To be safe, pass the
80 * pre-defined chunk names here, and if you need a new one, define it
81 * where the others are defined. The length is the length of the data.
82 * All the data must be present. If that is not possible, use the
83 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
84 * functions instead.
85 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050086void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060087png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050088 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050089{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050090 if (png_ptr == NULL)
91 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -050092 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050093 png_write_chunk_data(png_ptr, data, (png_size_t)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060094 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050095}
96
Andreas Dilger47a0c421997-05-16 02:46:07 -050097/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060098 * The total_length is the sum of the lengths of all the data you will be
99 * passing in png_write_chunk_data().
100 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500101void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600102png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
103 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500104{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500105 png_byte buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500106
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500107#ifdef PNG_IO_STATE_SUPPORTED
108 /* Inform the I/O callback that the chunk header is being written.
109 * PNG_IO_CHUNK_HDR requires a single I/O call.
110 */
111 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR;
112#endif
113
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500114 png_debug2(0, "Writing %s chunk, length = %lu", chunk_name,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500115 (unsigned long)length);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500116 if (png_ptr == NULL)
117 return;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500118
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500119 /* Write the length and the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500120 png_save_uint_32(buf, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500121 png_memcpy(buf + 4, chunk_name, 4);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500122 png_write_data(png_ptr, buf, (png_size_t)8);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500123 /* Put the chunk name into png_ptr->chunk_name */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500124 png_memcpy(png_ptr->chunk_name, chunk_name, 4);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500125 /* Reset the crc and run it over the chunk name */
Guy Schalnat0d580581995-07-20 02:43:20 -0500126 png_reset_crc(png_ptr);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500127 png_calculate_crc(png_ptr, chunk_name, 4);
128
129#ifdef PNG_IO_STATE_SUPPORTED
130 /* Inform the I/O callback that chunk data will (possibly) be written.
131 * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.
132 */
133 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA;
134#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500135}
136
Andreas Dilger47a0c421997-05-16 02:46:07 -0500137/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600138 * Note that multiple calls to this function are allowed, and that the
139 * sum of the lengths from these calls *must* add up to the total_length
140 * given to png_write_chunk_start().
141 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500142void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500143png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500144{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500145 /* Write the data, and run the CRC over it */
146 if (png_ptr == NULL)
147 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500148 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500149 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600150 png_write_data(png_ptr, data, length);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500151 /* Update the CRC after writing the data,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500152 * in case that the user I/O routine alters it.
153 */
154 png_calculate_crc(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500155 }
156}
157
Andreas Dilger47a0c421997-05-16 02:46:07 -0500158/* Finish a chunk started with png_write_chunk_start(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500159void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600160png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500161{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500162 png_byte buf[4];
163
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500164 if (png_ptr == NULL) return;
165
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500166#ifdef PNG_IO_STATE_SUPPORTED
167 /* Inform the I/O callback that the chunk CRC is being written.
168 * PNG_IO_CHUNK_CRC requires a single I/O function call.
169 */
170 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC;
171#endif
172
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500173 /* Write the crc in a single operation */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500174 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500175
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500176 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500177}
178
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600179#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500180/* This pair of functions encapsulates the operation of (a) compressing a
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600181 * text string, and (b) issuing it later as a series of chunk data writes.
182 * The compression_state structure is shared context for these functions
183 * set up by the caller in order to make the whole mess thread-safe.
184 */
185
186typedef struct
187{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500188 char *input; /* the uncompressed input data */
189 int input_len; /* its length */
190 int num_output_ptr; /* number of output pointers used */
191 int max_output_ptr; /* size of output_ptr */
192 png_charpp output_ptr; /* array of pointers to output */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600193} compression_state;
194
195/* compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500196static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600197png_text_compress(png_structp png_ptr,
198 png_charp text, png_size_t text_len, int compression,
199 compression_state *comp)
200{
201 int ret;
202
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600203 comp->num_output_ptr = 0;
204 comp->max_output_ptr = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600205 comp->output_ptr = NULL;
206 comp->input = NULL;
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600207 comp->input_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600208
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500209 /* We may just want to pass the text right through */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600210 if (compression == PNG_TEXT_COMPRESSION_NONE)
211 {
212 comp->input = text;
213 comp->input_len = text_len;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500214 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600215 }
216
217 if (compression >= PNG_TEXT_COMPRESSION_LAST)
218 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500219#if !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600220 char msg[50];
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500221 png_snprintf(msg, 50, "Unknown compression type %d", compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600222 png_warning(png_ptr, msg);
223#else
224 png_warning(png_ptr, "Unknown compression type");
225#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600226 }
227
228 /* We can't write the chunk until we find out how much data we have,
229 * which means we need to run the compressor first and save the
230 * output. This shouldn't be a problem, as the vast majority of
231 * comments should be reasonable, but we will set up an array of
232 * malloc'd pointers to be sure.
233 *
234 * If we knew the application was well behaved, we could simplify this
235 * greatly by assuming we can always malloc an output buffer large
236 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
237 * and malloc this directly. The only time this would be a bad idea is
238 * if we can't malloc more than 64K and we have 64K of random input
239 * data, or if the input string is incredibly large (although this
240 * wouldn't cause a failure, just a slowdown due to swapping).
241 */
242
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500243 /* Set up the compression buffers */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600244 png_ptr->zstream.avail_in = (uInt)text_len;
245 png_ptr->zstream.next_in = (Bytef *)text;
246 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
247 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
248
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500249 /* This is the same compression loop as in png_write_row() */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600250 do
251 {
252 /* compress the data */
253 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
254 if (ret != Z_OK)
255 {
256 /* error */
257 if (png_ptr->zstream.msg != NULL)
258 png_error(png_ptr, png_ptr->zstream.msg);
259 else
260 png_error(png_ptr, "zlib error");
261 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500262 /* Check to see if we need more room */
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600263 if (!(png_ptr->zstream.avail_out))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600264 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500265 /* Make sure the output array has room */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600266 if (comp->num_output_ptr >= comp->max_output_ptr)
267 {
268 int old_max;
269
270 old_max = comp->max_output_ptr;
271 comp->max_output_ptr = comp->num_output_ptr + 4;
272 if (comp->output_ptr != NULL)
273 {
274 png_charpp old_ptr;
275
276 old_ptr = comp->output_ptr;
277 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600278 (png_alloc_size_t)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500279 (comp->max_output_ptr * png_sizeof(png_charpp)));
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500280 png_memcpy(comp->output_ptr, old_ptr, old_max
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500281 * png_sizeof(png_charp));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600282 png_free(png_ptr, old_ptr);
283 }
284 else
285 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600286 (png_alloc_size_t)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500287 (comp->max_output_ptr * png_sizeof(png_charp)));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600288 }
289
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500290 /* Save the data */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500291 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500292 (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600293 (png_alloc_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500294 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
295 png_ptr->zbuf_size);
296 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600297
298 /* and reset the buffer */
299 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
300 png_ptr->zstream.next_out = png_ptr->zbuf;
301 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500302 /* Continue until we don't have any more to compress */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600303 } while (png_ptr->zstream.avail_in);
304
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500305 /* Finish the compression */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600306 do
307 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500308 /* Tell zlib we are finished */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600309 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500310
311 if (ret == Z_OK)
312 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500313 /* Check to see if we need more room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500314 if (!(png_ptr->zstream.avail_out))
315 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500316 /* Check to make sure our output array has room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500317 if (comp->num_output_ptr >= comp->max_output_ptr)
318 {
319 int old_max;
320
321 old_max = comp->max_output_ptr;
322 comp->max_output_ptr = comp->num_output_ptr + 4;
323 if (comp->output_ptr != NULL)
324 {
325 png_charpp old_ptr;
326
327 old_ptr = comp->output_ptr;
328 /* This could be optimized to realloc() */
329 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600330 (png_alloc_size_t)(comp->max_output_ptr *
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500331 png_sizeof(png_charp)));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500332 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500333 old_max * png_sizeof(png_charp));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500334 png_free(png_ptr, old_ptr);
335 }
336 else
337 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600338 (png_alloc_size_t)(comp->max_output_ptr *
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500339 png_sizeof(png_charp)));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500340 }
341
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500342 /* Save the data */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500343 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500344 (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600345 (png_alloc_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500346 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
347 png_ptr->zbuf_size);
348 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500349
350 /* and reset the buffer pointers */
351 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
352 png_ptr->zstream.next_out = png_ptr->zbuf;
353 }
354 }
355 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600356 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500357 /* We got an error */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600358 if (png_ptr->zstream.msg != NULL)
359 png_error(png_ptr, png_ptr->zstream.msg);
360 else
361 png_error(png_ptr, "zlib error");
362 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600363 } while (ret != Z_STREAM_END);
364
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500365 /* Text length is number of buffers plus last buffer */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600366 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
367 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
368 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
369
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500370 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600371}
372
373/* ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500374static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600375png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
376{
377 int i;
378
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500379 /* Handle the no-compression case */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600380 if (comp->input)
381 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500382 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500383 (png_size_t)comp->input_len);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500384 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600385 }
386
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500387 /* Write saved output buffers, if any */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600388 for (i = 0; i < comp->num_output_ptr; i++)
389 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500390 png_write_chunk_data(png_ptr, (png_bytep)comp->output_ptr[i],
391 (png_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600392 png_free(png_ptr, comp->output_ptr[i]);
393 }
394 if (comp->max_output_ptr != 0)
395 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500396 /* Write anything left in zbuf */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600397 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
398 png_write_chunk_data(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500399 (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600400
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500401 /* Reset zlib for another zTXt/iTXt or image data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600402 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600403 png_ptr->zstream.data_type = Z_BINARY;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600404}
405#endif
406
Guy Schalnat0d580581995-07-20 02:43:20 -0500407/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600408 * information. Note that the rest of this code depends upon this
409 * information being correct.
410 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500411void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600412png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500413 int bit_depth, int color_type, int compression_type, int filter_type,
414 int interlace_type)
415{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600416#ifdef PNG_USE_LOCAL_ARRAYS
417 PNG_IHDR;
418#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500419 int ret;
420
Guy Schalnat0d580581995-07-20 02:43:20 -0500421 png_byte buf[13]; /* buffer to store the IHDR info */
422
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500423 png_debug(1, "in png_write_IHDR");
Guy Schalnate5a37791996-06-05 15:50:50 -0500424 /* Check that we have valid input data from the application info */
425 switch (color_type)
426 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500427 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500428 switch (bit_depth)
429 {
430 case 1:
431 case 2:
432 case 4:
433 case 8:
434 case 16: png_ptr->channels = 1; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500435 default: png_error(png_ptr, "Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500436 }
437 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500438 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500439 if (bit_depth != 8 && bit_depth != 16)
440 png_error(png_ptr, "Invalid bit depth for RGB image");
441 png_ptr->channels = 3;
442 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500443 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500444 switch (bit_depth)
445 {
446 case 1:
447 case 2:
448 case 4:
449 case 8: png_ptr->channels = 1; break;
450 default: png_error(png_ptr, "Invalid bit depth for paletted image");
451 }
452 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500453 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500454 if (bit_depth != 8 && bit_depth != 16)
455 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
456 png_ptr->channels = 2;
457 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500458 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500459 if (bit_depth != 8 && bit_depth != 16)
460 png_error(png_ptr, "Invalid bit depth for RGBA image");
461 png_ptr->channels = 4;
462 break;
463 default:
464 png_error(png_ptr, "Invalid image color type specified");
465 }
466
Andreas Dilger47a0c421997-05-16 02:46:07 -0500467 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500468 {
469 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500470 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500471 }
472
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600473 /* Write filter_method 64 (intrapixel differencing) only if
474 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
475 * 2. Libpng did not write a PNG signature (this filter_method is only
476 * used in PNG datastreams that are embedded in MNG datastreams) and
477 * 3. The application called png_permit_mng_features with a mask that
478 * included PNG_FLAG_MNG_FILTER_64 and
479 * 4. The filter_method is 64 and
480 * 5. The color_type is RGB or RGBA
481 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600482 if (
483#if defined(PNG_MNG_FEATURES_SUPPORTED)
484 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600485 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500486 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600487 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600488 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
489#endif
490 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500491 {
492 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500493 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500494 }
495
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600496#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500497 if (interlace_type != PNG_INTERLACE_NONE &&
498 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500499 {
500 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500501 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500502 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600503#else
504 interlace_type=PNG_INTERLACE_NONE;
505#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500506
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500507 /* Save the relevent information */
Guy Schalnate5a37791996-06-05 15:50:50 -0500508 png_ptr->bit_depth = (png_byte)bit_depth;
509 png_ptr->color_type = (png_byte)color_type;
510 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500511#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600512 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500513#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500514 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500515 png_ptr->width = width;
516 png_ptr->height = height;
517
518 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500519 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
Guy Schalnate5a37791996-06-05 15:50:50 -0500520 /* set the usr info, so any transformations can modify it */
521 png_ptr->usr_width = png_ptr->width;
522 png_ptr->usr_bit_depth = png_ptr->bit_depth;
523 png_ptr->usr_channels = png_ptr->channels;
524
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500525 /* Pack the header information into the buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500526 png_save_uint_32(buf, width);
527 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600528 buf[8] = (png_byte)bit_depth;
529 buf[9] = (png_byte)color_type;
530 buf[10] = (png_byte)compression_type;
531 buf[11] = (png_byte)filter_type;
532 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500533
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500534 /* Write the chunk */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500535 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500536
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500537 /* Initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600538 png_ptr->zstream.zalloc = png_zalloc;
539 png_ptr->zstream.zfree = png_zfree;
540 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500541 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500542 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500543 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
544 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500545 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500546 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500547 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500548 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500549 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500550 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500551 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500552 png_ptr->zlib_strategy = Z_FILTERED;
553 else
554 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
555 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500556 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500557 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500558 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500559 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500560 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500561 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500562 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500563 png_ptr->zlib_method = 8;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500564 ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
565 png_ptr->zlib_method, png_ptr->zlib_window_bits,
566 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
567 if (ret != Z_OK)
568 {
569 if (ret == Z_VERSION_ERROR) png_error(png_ptr,
570 "zlib failed to initialize compressor -- version error");
571 if (ret == Z_STREAM_ERROR) png_error(png_ptr,
572 "zlib failed to initialize compressor -- stream error");
573 if (ret == Z_MEM_ERROR) png_error(png_ptr,
574 "zlib failed to initialize compressor -- mem error");
575 png_error(png_ptr, "zlib failed to initialize compressor");
576 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600577 png_ptr->zstream.next_out = png_ptr->zbuf;
578 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600579 /* libpng is not interested in zstream.data_type */
580 /* set it to a predefined value, to avoid its evaluation inside zlib */
581 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500582
Guy Schalnate5a37791996-06-05 15:50:50 -0500583 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500584}
585
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500586/* Write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500587 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600588 * structure.
589 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500590void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500591png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500592{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600593#ifdef PNG_USE_LOCAL_ARRAYS
594 PNG_PLTE;
595#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500596 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600597 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500598 png_byte buf[3];
599
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500600 png_debug(1, "in png_write_PLTE");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500601 if ((
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500602#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600603 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500604#endif
605 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500606 {
607 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500608 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500609 png_error(png_ptr, "Invalid number of colors in palette");
610 }
611 else
612 {
613 png_warning(png_ptr, "Invalid number of colors in palette");
614 return;
615 }
616 }
617
618 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
619 {
620 png_warning(png_ptr,
621 "Ignoring request to write a PLTE chunk in grayscale PNG");
622 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500623 }
624
Andreas Dilger47a0c421997-05-16 02:46:07 -0500625 png_ptr->num_palette = (png_uint_16)num_pal;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500626 png_debug1(3, "num_palette = %d", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500627
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500628 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE,
629 (png_uint_32)(num_pal * 3));
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500630#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500631 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500632 {
633 buf[0] = pal_ptr->red;
634 buf[1] = pal_ptr->green;
635 buf[2] = pal_ptr->blue;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500636 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500637 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500638#else
639 /* This is a little slower but some buggy compilers need to do this instead */
640 pal_ptr=palette;
641 for (i = 0; i < num_pal; i++)
642 {
643 buf[0] = pal_ptr[i].red;
644 buf[1] = pal_ptr[i].green;
645 buf[2] = pal_ptr[i].blue;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500646 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500647 }
648#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500649 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500650 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500651}
652
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500653/* Write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500654void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500655png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500656{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600657#ifdef PNG_USE_LOCAL_ARRAYS
658 PNG_IDAT;
659#endif
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500660 png_debug(1, "in png_write_IDAT");
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500661
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500662 /* Optimize the CMF field in the zlib stream. */
663 /* This hack of the zlib stream is compliant to the stream specification. */
664 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
665 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
666 {
667 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
668 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
669 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500670 /* Avoid memory underflows and multiplication overflows.
671 *
672 * The conditions below are practically always satisfied;
673 * however, they still must be checked.
674 */
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500675 if (length >= 2 &&
676 png_ptr->height < 16384 && png_ptr->width < 16384)
677 {
678 png_uint_32 uncompressed_idat_size = png_ptr->height *
679 ((png_ptr->width *
680 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
681 unsigned int z_cinfo = z_cmf >> 4;
682 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
683 while (uncompressed_idat_size <= half_z_window_size &&
684 half_z_window_size >= 256)
685 {
686 z_cinfo--;
687 half_z_window_size >>= 1;
688 }
689 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
690 if (data[0] != (png_byte)z_cmf)
691 {
692 data[0] = (png_byte)z_cmf;
693 data[1] &= 0xe0;
694 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
695 }
696 }
697 }
698 else
699 png_error(png_ptr,
700 "Invalid zlib compression method or flags in IDAT");
701 }
702
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600703 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500704 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500705}
706
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500707/* Write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500708void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600709png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500710{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600711#ifdef PNG_USE_LOCAL_ARRAYS
712 PNG_IEND;
713#endif
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500714 png_debug(1, "in png_write_IEND");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500715 png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL,
716 (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600717 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500718}
719
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500720#if defined(PNG_WRITE_gAMA_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500721/* Write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600722#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500723void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500724png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500725{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600726#ifdef PNG_USE_LOCAL_ARRAYS
727 PNG_gAMA;
728#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500729 png_uint_32 igamma;
730 png_byte buf[4];
731
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500732 png_debug(1, "in png_write_gAMA");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600733 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600734 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500735 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500736 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500737}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500738#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600739#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500740void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600741png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600742{
743#ifdef PNG_USE_LOCAL_ARRAYS
744 PNG_gAMA;
745#endif
746 png_byte buf[4];
747
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500748 png_debug(1, "in png_write_gAMA");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600749 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500750 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500751 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600752}
753#endif
754#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500755
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600756#if defined(PNG_WRITE_sRGB_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500757/* Write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500758void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600759png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600760{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600761#ifdef PNG_USE_LOCAL_ARRAYS
762 PNG_sRGB;
763#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600764 png_byte buf[1];
765
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500766 png_debug(1, "in png_write_sRGB");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500767 if (srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600768 png_warning(png_ptr,
769 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600770 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500771 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600772}
773#endif
774
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600775#if defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500776/* Write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500777void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600778png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
779 png_charp profile, int profile_len)
780{
781#ifdef PNG_USE_LOCAL_ARRAYS
782 PNG_iCCP;
783#endif
784 png_size_t name_len;
785 png_charp new_name;
786 compression_state comp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500787 int embedded_profile_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600788
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500789 png_debug(1, "in png_write_iCCP");
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600790
791 comp.num_output_ptr = 0;
792 comp.max_output_ptr = 0;
793 comp.output_ptr = NULL;
794 comp.input = NULL;
795 comp.input_len = 0;
796
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -0600797 if ((name_len = png_check_keyword(png_ptr, name,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600798 &new_name)) == 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600799 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600800
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600801 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600802 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600803
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500804 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600805 profile_len = 0;
806
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500807 if (profile_len > 3)
Glenn Randers-Pehrson7edd4582006-12-07 19:16:44 -0600808 embedded_profile_len =
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500809 ((*( (png_bytep)profile ))<<24) |
810 ((*( (png_bytep)profile + 1))<<16) |
811 ((*( (png_bytep)profile + 2))<< 8) |
812 ((*( (png_bytep)profile + 3)) );
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500813
814 if (profile_len < embedded_profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500815 {
816 png_warning(png_ptr,
817 "Embedded profile length too large in iCCP chunk");
Glenn Randers-Pehrsona6cc6272009-04-01 14:18:43 -0500818 png_free(png_ptr, new_name);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500819 return;
820 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500821
822 if (profile_len > embedded_profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500823 {
824 png_warning(png_ptr,
825 "Truncating profile to actual length in iCCP chunk");
826 profile_len = embedded_profile_len;
827 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500828
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600829 if (profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500830 profile_len = png_text_compress(png_ptr, profile,
831 (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600832
833 /* make sure we include the NULL after the name and the compression type */
834 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500835 (png_uint_32)(name_len + profile_len + 2));
836 new_name[name_len + 1] = 0x00;
837 png_write_chunk_data(png_ptr, (png_bytep)new_name,
838 (png_size_t)(name_len + 2));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600839
840 if (profile_len)
841 png_write_compressed_data_out(png_ptr, &comp);
842
843 png_write_chunk_end(png_ptr);
844 png_free(png_ptr, new_name);
845}
846#endif
847
848#if defined(PNG_WRITE_sPLT_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500849/* Write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500850void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600851png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600852{
853#ifdef PNG_USE_LOCAL_ARRAYS
854 PNG_sPLT;
855#endif
856 png_size_t name_len;
857 png_charp new_name;
858 png_byte entrybuf[10];
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500859 png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
860 png_size_t palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600861 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500862#ifdef PNG_NO_POINTER_INDEXING
863 int i;
864#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600865
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500866 png_debug(1, "in png_write_sPLT");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500867 if ((name_len = png_check_keyword(png_ptr,spalette->name, &new_name))==0)
868 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600869
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500870 /* Make sure we include the NULL after the name */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500871 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500872 (png_uint_32)(name_len + 2 + palette_size));
873 png_write_chunk_data(png_ptr, (png_bytep)new_name,
874 (png_size_t)(name_len + 1));
875 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600876
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500877 /* Loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500878#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500879 for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600880 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500881 if (spalette->depth == 8)
882 {
883 entrybuf[0] = (png_byte)ep->red;
884 entrybuf[1] = (png_byte)ep->green;
885 entrybuf[2] = (png_byte)ep->blue;
886 entrybuf[3] = (png_byte)ep->alpha;
887 png_save_uint_16(entrybuf + 4, ep->frequency);
888 }
889 else
890 {
891 png_save_uint_16(entrybuf + 0, ep->red);
892 png_save_uint_16(entrybuf + 2, ep->green);
893 png_save_uint_16(entrybuf + 4, ep->blue);
894 png_save_uint_16(entrybuf + 6, ep->alpha);
895 png_save_uint_16(entrybuf + 8, ep->frequency);
896 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500897 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600898 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500899#else
900 ep=spalette->entries;
901 for (i=0; i>spalette->nentries; i++)
902 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500903 if (spalette->depth == 8)
904 {
905 entrybuf[0] = (png_byte)ep[i].red;
906 entrybuf[1] = (png_byte)ep[i].green;
907 entrybuf[2] = (png_byte)ep[i].blue;
908 entrybuf[3] = (png_byte)ep[i].alpha;
909 png_save_uint_16(entrybuf + 4, ep[i].frequency);
910 }
911 else
912 {
913 png_save_uint_16(entrybuf + 0, ep[i].red);
914 png_save_uint_16(entrybuf + 2, ep[i].green);
915 png_save_uint_16(entrybuf + 4, ep[i].blue);
916 png_save_uint_16(entrybuf + 6, ep[i].alpha);
917 png_save_uint_16(entrybuf + 8, ep[i].frequency);
918 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500919 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500920 }
921#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600922
923 png_write_chunk_end(png_ptr);
924 png_free(png_ptr, new_name);
925}
926#endif
927
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500928#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500929/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500930void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600931png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500932{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600933#ifdef PNG_USE_LOCAL_ARRAYS
934 PNG_sBIT;
935#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500936 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500937 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500938
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500939 png_debug(1, "in png_write_sBIT");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500940 /* Make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500941 if (color_type & PNG_COLOR_MASK_COLOR)
942 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600943 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500944
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500945 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
946 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600947 if (sbit->red == 0 || sbit->red > maxbits ||
948 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500949 sbit->blue == 0 || sbit->blue > maxbits)
950 {
951 png_warning(png_ptr, "Invalid sBIT depth specified");
952 return;
953 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500954 buf[0] = sbit->red;
955 buf[1] = sbit->green;
956 buf[2] = sbit->blue;
957 size = 3;
958 }
959 else
960 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500961 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
962 {
963 png_warning(png_ptr, "Invalid sBIT depth specified");
964 return;
965 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500966 buf[0] = sbit->gray;
967 size = 1;
968 }
969
970 if (color_type & PNG_COLOR_MASK_ALPHA)
971 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500972 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
973 {
974 png_warning(png_ptr, "Invalid sBIT depth specified");
975 return;
976 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500977 buf[size++] = sbit->alpha;
978 }
979
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600980 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500981}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500982#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500983
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500984#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500985/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600986#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500987void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500988png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600989 double red_x, double red_y, double green_x, double green_y,
990 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500991{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600992#ifdef PNG_USE_LOCAL_ARRAYS
993 PNG_cHRM;
994#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500995 png_byte buf[32];
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -0600996
997 png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y,
998 int_green_x, int_green_y, int_blue_x, int_blue_y;
Guy Schalnat0d580581995-07-20 02:43:20 -0500999
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001000 png_debug(1, "in png_write_cHRM");
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -06001001
1002 int_white_x = (png_uint_32)(white_x * 100000.0 + 0.5);
1003 int_white_y = (png_uint_32)(white_y * 100000.0 + 0.5);
1004 int_red_x = (png_uint_32)(red_x * 100000.0 + 0.5);
1005 int_red_y = (png_uint_32)(red_y * 100000.0 + 0.5);
1006 int_green_x = (png_uint_32)(green_x * 100000.0 + 0.5);
1007 int_green_y = (png_uint_32)(green_y * 100000.0 + 0.5);
1008 int_blue_x = (png_uint_32)(blue_x * 100000.0 + 0.5);
1009 int_blue_y = (png_uint_32)(blue_y * 100000.0 + 0.5);
1010
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001011#if !defined(PNG_NO_CHECK_cHRM)
Glenn Randers-Pehrson02a5e332008-11-24 22:10:23 -06001012 if (png_check_cHRM_fixed(png_ptr, int_white_x, int_white_y,
1013 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 -06001014#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001015 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001016 /* each value is saved in 1/100,000ths */
1017
1018 png_save_uint_32(buf, int_white_x);
1019 png_save_uint_32(buf + 4, int_white_y);
Guy Schalnate5a37791996-06-05 15:50:50 -05001020
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001021 png_save_uint_32(buf + 8, int_red_x);
1022 png_save_uint_32(buf + 12, int_red_y);
Guy Schalnate5a37791996-06-05 15:50:50 -05001023
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001024 png_save_uint_32(buf + 16, int_green_x);
1025 png_save_uint_32(buf + 20, int_green_y);
Guy Schalnate5a37791996-06-05 15:50:50 -05001026
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001027 png_save_uint_32(buf + 24, int_blue_x);
1028 png_save_uint_32(buf + 28, int_blue_y);
Guy Schalnate5a37791996-06-05 15:50:50 -05001029
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001030 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001031 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001032}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001033#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001034#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001035void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001036png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
1037 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
1038 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
1039 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001040{
1041#ifdef PNG_USE_LOCAL_ARRAYS
1042 PNG_cHRM;
1043#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001044 png_byte buf[32];
1045
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001046 png_debug(1, "in png_write_cHRM");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001047 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001048#if !defined(PNG_NO_CHECK_cHRM)
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001049 if (png_check_cHRM_fixed(png_ptr, white_x, white_y, red_x, red_y,
1050 green_x, green_y, blue_x, blue_y))
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001051#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001052 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001053 png_save_uint_32(buf, (png_uint_32)white_x);
1054 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001055
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001056 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1057 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001058
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001059 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1060 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001061
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001062 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1063 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001064
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001065 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001066 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001067}
1068#endif
1069#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001070
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001071#if defined(PNG_WRITE_tRNS_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001072/* Write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001073void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001074png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -05001075 int num_trans, int color_type)
1076{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001077#ifdef PNG_USE_LOCAL_ARRAYS
1078 PNG_tRNS;
1079#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001080 png_byte buf[6];
1081
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001082 png_debug(1, "in png_write_tRNS");
Guy Schalnat0d580581995-07-20 02:43:20 -05001083 if (color_type == PNG_COLOR_TYPE_PALETTE)
1084 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001085 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001086 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001087 png_warning(png_ptr, "Invalid number of transparent colors specified");
Guy Schalnate5a37791996-06-05 15:50:50 -05001088 return;
1089 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001090 /* write the chunk out as it is */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001091 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans,
1092 (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001093 }
1094 else if (color_type == PNG_COLOR_TYPE_GRAY)
1095 {
1096 /* one 16 bit value */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001097 if (tran->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001098 {
1099 png_warning(png_ptr,
1100 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1101 return;
1102 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001103 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001104 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001105 }
1106 else if (color_type == PNG_COLOR_TYPE_RGB)
1107 {
1108 /* three 16 bit values */
1109 png_save_uint_16(buf, tran->red);
1110 png_save_uint_16(buf + 2, tran->green);
1111 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001112 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001113 {
1114 png_warning(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001115 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001116 return;
1117 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001118 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001119 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001120 else
1121 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001122 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001123 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001124}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001125#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001126
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001127#if defined(PNG_WRITE_bKGD_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001128/* Write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001129void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001130png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001131{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001132#ifdef PNG_USE_LOCAL_ARRAYS
1133 PNG_bKGD;
1134#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001135 png_byte buf[6];
1136
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001137 png_debug(1, "in png_write_bKGD");
Guy Schalnat0d580581995-07-20 02:43:20 -05001138 if (color_type == PNG_COLOR_TYPE_PALETTE)
1139 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001140 if (
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001141#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001142 (png_ptr->num_palette ||
1143 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001144#endif
Glenn Randers-Pehrsond6d80752008-12-02 09:49:43 -06001145 back->index >= png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001146 {
1147 png_warning(png_ptr, "Invalid background palette index");
1148 return;
1149 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001150 buf[0] = back->index;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001151 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001152 }
1153 else if (color_type & PNG_COLOR_MASK_COLOR)
1154 {
1155 png_save_uint_16(buf, back->red);
1156 png_save_uint_16(buf + 2, back->green);
1157 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001158 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001159 {
1160 png_warning(png_ptr,
1161 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1162 return;
1163 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001164 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001165 }
1166 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001167 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001168 if (back->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001169 {
1170 png_warning(png_ptr,
1171 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1172 return;
1173 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001174 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001175 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001176 }
1177}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001178#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001179
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001180#if defined(PNG_WRITE_hIST_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001181/* Write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001182void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001183png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001184{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001185#ifdef PNG_USE_LOCAL_ARRAYS
1186 PNG_hIST;
1187#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001188 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001189 png_byte buf[3];
1190
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001191 png_debug(1, "in png_write_hIST");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001192 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001193 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001194 png_debug2(3, "num_hist = %d, num_palette = %d", num_hist,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001195 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001196 png_warning(png_ptr, "Invalid number of histogram entries specified");
1197 return;
1198 }
1199
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001200 png_write_chunk_start(png_ptr, (png_bytep)png_hIST,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001201 (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001202 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001203 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001204 png_save_uint_16(buf, hist[i]);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001205 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001206 }
1207 png_write_chunk_end(png_ptr);
1208}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001209#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001210
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001211#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1212 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001213/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1214 * and if invalid, correct the keyword rather than discarding the entire
1215 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1216 * length, forbids leading or trailing whitespace, multiple internal spaces,
1217 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001218 *
1219 * The new_key is allocated to hold the corrected keyword and must be freed
1220 * by the calling routine. This avoids problems with trying to write to
1221 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001222 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001223png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001224png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001225{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001226 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001227 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001228 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001229 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001230
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001231 png_debug(1, "in png_check_keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001232 *new_key = NULL;
1233
1234 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001235 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001236 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001237 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001238 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001239
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001240 png_debug1(2, "Keyword to be checked is '%s'", key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001241
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001242 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001243 if (*new_key == NULL)
1244 {
1245 png_warning(png_ptr, "Out of memory while procesing keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001246 return ((png_size_t)0);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001247 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001248
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001249 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001250 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001251 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001252 if ((png_byte)*kp < 0x20 ||
1253 ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001254 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001255#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001256 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001257
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001258 png_snprintf(msg, 40,
1259 "invalid keyword character 0x%02X", (png_byte)*kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001260 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001261#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001262 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001263#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001264 *dp = ' ';
1265 }
1266 else
1267 {
1268 *dp = *kp;
1269 }
1270 }
1271 *dp = '\0';
1272
1273 /* Remove any trailing white space. */
1274 kp = *new_key + key_len - 1;
1275 if (*kp == ' ')
1276 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001277 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001278
1279 while (*kp == ' ')
1280 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001281 *(kp--) = '\0';
1282 key_len--;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001283 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001284 }
1285
1286 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001287 kp = *new_key;
1288 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001289 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001290 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001291
1292 while (*kp == ' ')
1293 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001294 kp++;
1295 key_len--;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001296 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001297 }
1298
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001299 png_debug1(2, "Checking for multiple internal spaces in '%s'", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001300
Andreas Dilger47a0c421997-05-16 02:46:07 -05001301 /* Remove multiple internal spaces. */
1302 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001303 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001304 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001305 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001306 *(dp++) = *kp;
1307 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001308 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001309 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001310 {
1311 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001312 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001313 }
1314 else
1315 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001316 *(dp++) = *kp;
1317 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001318 }
1319 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001320 *dp = '\0';
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001321 if (kwarn)
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001322 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001323
1324 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001325 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001326 png_free(png_ptr, *new_key);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001327 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001328 }
1329
1330 if (key_len > 79)
1331 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001332 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001333 (*new_key)[79] = '\0';
Andreas Dilger47a0c421997-05-16 02:46:07 -05001334 key_len = 79;
1335 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001336
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001337 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001338}
1339#endif
1340
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001341#if defined(PNG_WRITE_tEXt_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001342/* Write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001343void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001344png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001345 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001346{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001347#ifdef PNG_USE_LOCAL_ARRAYS
1348 PNG_tEXt;
1349#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001350 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001351 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001352
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001353 png_debug(1, "in png_write_tEXt");
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001354 if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001355 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001356
Andreas Dilger47a0c421997-05-16 02:46:07 -05001357 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001358 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001359 else
1360 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001361
1362 /* make sure we include the 0 after the key */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001363 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001364 (png_uint_32)(key_len + text_len + 1));
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001365 /*
1366 * We leave it to the application to meet PNG-1.0 requirements on the
1367 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1368 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001369 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001370 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001371 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1372 (png_size_t)(key_len + 1));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001373 if (text_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001374 png_write_chunk_data(png_ptr, (png_bytep)text, (png_size_t)text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001375
Guy Schalnat0d580581995-07-20 02:43:20 -05001376 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001377 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001378}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001379#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001380
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001381#if defined(PNG_WRITE_zTXt_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001382/* Write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001383void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001384png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001385 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001386{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001387#ifdef PNG_USE_LOCAL_ARRAYS
1388 PNG_zTXt;
1389#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001390 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001391 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001392 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001393 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001394
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001395 png_debug(1, "in png_write_zTXt");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001396
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001397 comp.num_output_ptr = 0;
1398 comp.max_output_ptr = 0;
1399 comp.output_ptr = NULL;
1400 comp.input = NULL;
1401 comp.input_len = 0;
1402
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001403 if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001404 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001405 png_free(png_ptr, new_key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001406 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001407 }
1408
Andreas Dilger47a0c421997-05-16 02:46:07 -05001409 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1410 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001411 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001412 png_free(png_ptr, new_key);
1413 return;
1414 }
1415
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001416 text_len = png_strlen(text);
1417
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001418 /* Compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001419 text_len = png_text_compress(png_ptr, text, text_len, compression,
1420 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001421
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001422 /* Write start of chunk */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001423 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt,
1424 (png_uint_32)(key_len+text_len + 2));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001425 /* Write key */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001426 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1427 (png_size_t)(key_len + 1));
1428 png_free(png_ptr, new_key);
1429
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001430 buf[0] = (png_byte)compression;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001431 /* Write compression */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001432 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001433 /* Write the compressed data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001434 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001435
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001436 /* Close the chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -05001437 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001438}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001439#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001440
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001441#if defined(PNG_WRITE_iTXt_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001442/* Write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001443void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001444png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001445 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001446{
1447#ifdef PNG_USE_LOCAL_ARRAYS
1448 PNG_iTXt;
1449#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001450 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001451 png_charp new_lang;
1452 png_charp new_key = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001453 png_byte cbuf[2];
1454 compression_state comp;
1455
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001456 png_debug(1, "in png_write_iTXt");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001457
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001458 comp.num_output_ptr = 0;
1459 comp.max_output_ptr = 0;
1460 comp.output_ptr = NULL;
1461 comp.input = NULL;
1462
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001463 if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001464 return;
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001465
1466 if ((lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001467 {
1468 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001469 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001470 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001471 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001472
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001473 if (lang_key == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001474 lang_key_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001475 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001476 lang_key_len = png_strlen(lang_key);
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001477
1478 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001479 text_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001480 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001481 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001482
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001483 /* Compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001484 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1485 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001486
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001487
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001488 /* Make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001489 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001490
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001491 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1492 (png_uint_32)(
1493 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1494 + key_len
1495 + lang_len
1496 + lang_key_len
1497 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001498
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001499 /* We leave it to the application to meet PNG-1.0 requirements on the
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001500 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1501 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1502 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1503 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001504 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1505 (png_size_t)(key_len + 1));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001506
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001507 /* Set the compression flag */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001508 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1509 compression == PNG_TEXT_COMPRESSION_NONE)
1510 cbuf[0] = 0;
1511 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1512 cbuf[0] = 1;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001513 /* Set the compression method */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001514 cbuf[1] = 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001515 png_write_chunk_data(png_ptr, cbuf, (png_size_t)2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001516
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001517 cbuf[0] = 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001518 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf),
1519 (png_size_t)(lang_len + 1));
1520 png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf),
1521 (png_size_t)(lang_key_len + 1));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001522 png_write_compressed_data_out(png_ptr, &comp);
1523
1524 png_write_chunk_end(png_ptr);
1525 png_free(png_ptr, new_key);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001526 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001527}
1528#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001529
1530#if defined(PNG_WRITE_oFFs_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001531/* Write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001532void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001533png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001534 int unit_type)
1535{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001536#ifdef PNG_USE_LOCAL_ARRAYS
1537 PNG_oFFs;
1538#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001539 png_byte buf[9];
1540
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001541 png_debug(1, "in png_write_oFFs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001542 if (unit_type >= PNG_OFFSET_LAST)
1543 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1544
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001545 png_save_int_32(buf, x_offset);
1546 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001547 buf[8] = (png_byte)unit_type;
1548
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001549 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001550}
1551#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001552#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001553/* Write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001554void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001555png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1556 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1557{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001558#ifdef PNG_USE_LOCAL_ARRAYS
1559 PNG_pCAL;
1560#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001561 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001562 png_uint_32p params_len;
1563 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001564 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001565 int i;
1566
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001567 png_debug1(1, "in png_write_pCAL (%d parameters)", nparams);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001568 if (type >= PNG_EQUATION_LAST)
1569 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1570
1571 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001572 png_debug1(3, "pCAL purpose length = %d", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001573 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001574 png_debug1(3, "pCAL units length = %d", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001575 total_len = purpose_len + units_len + 10;
1576
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001577 params_len = (png_uint_32p)png_malloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06001578 (png_alloc_size_t)(nparams * png_sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001579
1580 /* Find the length of each parameter, making sure we don't count the
1581 null terminator for the last parameter. */
1582 for (i = 0; i < nparams; i++)
1583 {
1584 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001585 png_debug2(3, "pCAL parameter %d length = %lu", i,
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001586 (unsigned long) params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001587 total_len += (png_size_t)params_len[i];
1588 }
1589
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001590 png_debug1(3, "pCAL total length = %d", (int)total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001591 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001592 png_write_chunk_data(png_ptr, (png_bytep)new_purpose,
1593 (png_size_t)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001594 png_save_int_32(buf, X0);
1595 png_save_int_32(buf + 4, X1);
1596 buf[8] = (png_byte)type;
1597 buf[9] = (png_byte)nparams;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001598 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1599 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001600
1601 png_free(png_ptr, new_purpose);
1602
1603 for (i = 0; i < nparams; i++)
1604 {
1605 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1606 (png_size_t)params_len[i]);
1607 }
1608
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001609 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001610 png_write_chunk_end(png_ptr);
1611}
1612#endif
1613
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001614#if defined(PNG_WRITE_sCAL_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001615/* Write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001616#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001617void /* PRIVATE */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001618png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001619{
1620#ifdef PNG_USE_LOCAL_ARRAYS
1621 PNG_sCAL;
1622#endif
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001623 char buf[64];
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001624 png_size_t total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001625
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001626 png_debug(1, "in png_write_sCAL");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001627
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001628 buf[0] = (char)unit;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001629 png_snprintf(buf + 1, 63, "%12.12e", width);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001630 total_len = 1 + png_strlen(buf + 1) + 1;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001631 png_snprintf(buf + total_len, 64-total_len, "%12.12e", height);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001632 total_len += png_strlen(buf + total_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001633
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001634 png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001635 png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001636}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001637#else
1638#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001639void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001640png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001641 png_charp height)
1642{
1643#ifdef PNG_USE_LOCAL_ARRAYS
1644 PNG_sCAL;
1645#endif
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;
1661 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
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001671#if defined(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,
Guy Schalnat0d580581995-07-20 02:43:20 -05001675 png_uint_32 y_pixels_per_unit,
1676 int unit_type)
1677{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001678#ifdef PNG_USE_LOCAL_ARRAYS
1679 PNG_pHYs;
1680#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001681 png_byte buf[9];
1682
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001683 png_debug(1, "in png_write_pHYs");
Guy Schalnate5a37791996-06-05 15:50:50 -05001684 if (unit_type >= PNG_RESOLUTION_LAST)
1685 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1686
Guy Schalnat0d580581995-07-20 02:43:20 -05001687 png_save_uint_32(buf, x_pixels_per_unit);
1688 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001689 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001690
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001691 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001692}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001693#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001694
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001695#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001696/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1697 * or png_convert_from_time_t(), or fill in the structure yourself.
1698 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001699void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001700png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001701{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001702#ifdef PNG_USE_LOCAL_ARRAYS
1703 PNG_tIME;
1704#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001705 png_byte buf[7];
1706
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001707 png_debug(1, "in png_write_tIME");
Guy Schalnate5a37791996-06-05 15:50:50 -05001708 if (mod_time->month > 12 || mod_time->month < 1 ||
1709 mod_time->day > 31 || mod_time->day < 1 ||
1710 mod_time->hour > 23 || mod_time->second > 60)
1711 {
1712 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1713 return;
1714 }
1715
Guy Schalnat0d580581995-07-20 02:43:20 -05001716 png_save_uint_16(buf, mod_time->year);
1717 buf[2] = mod_time->month;
1718 buf[3] = mod_time->day;
1719 buf[4] = mod_time->hour;
1720 buf[5] = mod_time->minute;
1721 buf[6] = mod_time->second;
1722
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001723 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001724}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001725#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001726
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001727/* Initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001728void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001729png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001730{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001731#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001732#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001733 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001734
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001735 /* Start of interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001736 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001737
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001738 /* Offset to next interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001739 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001740
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001741 /* Start of interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001742 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001743
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001744 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001745 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001746#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001747#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001748
Andreas Dilger47a0c421997-05-16 02:46:07 -05001749 png_size_t buf_size;
1750
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001751 png_debug(1, "in png_write_start_row");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001752 buf_size = (png_size_t)(PNG_ROWBYTES(
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001753 png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001754
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001755 /* Set up row buffer */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001756 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06001757 (png_alloc_size_t)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001758 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001759
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001760#ifndef PNG_NO_WRITE_FILTER
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001761 /* Set up filtering buffer, if using this filter */
Guy Schalnate5a37791996-06-05 15:50:50 -05001762 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001763 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001764 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06001765 (png_alloc_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001766 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001767 }
1768
Andreas Dilger47a0c421997-05-16 02:46:07 -05001769 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001770 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1771 {
1772 /* set up previous row buffer */
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001773#ifdef PNG_CALLOC_SUPPORTED
1774 png_ptr->prev_row = (png_bytep)png_calloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06001775 (png_alloc_size_t)buf_size);
Glenn Randers-Pehrson0ffb71a2009-02-28 06:08:20 -06001776#else
1777 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr,
1778 (png_uint_32)buf_size);
1779 png_memset(png_ptr->prev_row, 0, buf_size);
1780#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001781
1782 if (png_ptr->do_filter & PNG_FILTER_UP)
1783 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001784 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001785 (png_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001786 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001787 }
1788
1789 if (png_ptr->do_filter & PNG_FILTER_AVG)
1790 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001791 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06001792 (png_alloc_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001793 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001794 }
1795
1796 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1797 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001798 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001799 (png_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001800 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001801 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001802 }
Glenn Randers-Pehrsoneb580912008-07-30 14:47:09 -05001803#endif /* PNG_NO_WRITE_FILTER */
Guy Schalnat0d580581995-07-20 02:43:20 -05001804
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001805#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001806 /* If interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001807 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001808 {
1809 if (!(png_ptr->transformations & PNG_INTERLACE))
1810 {
1811 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1812 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001813 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1814 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001815 }
1816 else
1817 {
1818 png_ptr->num_rows = png_ptr->height;
1819 png_ptr->usr_width = png_ptr->width;
1820 }
1821 }
1822 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001823#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001824 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001825 png_ptr->num_rows = png_ptr->height;
1826 png_ptr->usr_width = png_ptr->width;
1827 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001828 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1829 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001830}
1831
Andreas Dilger47a0c421997-05-16 02:46:07 -05001832/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001833void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001834png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001835{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001836#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001837#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001838 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001839
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001840 /* Start of interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001841 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001842
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001843 /* Offset to next interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001844 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001845
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001846 /* Start of interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001847 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001848
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001849 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001850 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001851#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001852#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001853
Guy Schalnat0d580581995-07-20 02:43:20 -05001854 int ret;
1855
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001856 png_debug(1, "in png_write_finish_row");
Guy Schalnat0d580581995-07-20 02:43:20 -05001857 /* next row */
1858 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001859
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001860 /* See if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001861 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001862 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001863
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001864#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001865 /* If interlaced, go to next pass */
Guy Schalnat0d580581995-07-20 02:43:20 -05001866 if (png_ptr->interlaced)
1867 {
1868 png_ptr->row_number = 0;
1869 if (png_ptr->transformations & PNG_INTERLACE)
1870 {
1871 png_ptr->pass++;
1872 }
1873 else
1874 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001875 /* Loop until we find a non-zero width or height pass */
Guy Schalnat0d580581995-07-20 02:43:20 -05001876 do
1877 {
1878 png_ptr->pass++;
1879 if (png_ptr->pass >= 7)
1880 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001881 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001882 png_pass_inc[png_ptr->pass] - 1 -
1883 png_pass_start[png_ptr->pass]) /
1884 png_pass_inc[png_ptr->pass];
1885 png_ptr->num_rows = (png_ptr->height +
1886 png_pass_yinc[png_ptr->pass] - 1 -
1887 png_pass_ystart[png_ptr->pass]) /
1888 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001889 if (png_ptr->transformations & PNG_INTERLACE)
1890 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001891 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1892
1893 }
1894
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001895 /* Reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001896 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001897 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001898 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001899 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001900 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001901 png_ptr->usr_bit_depth, png_ptr->width)) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001902 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001903 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001904 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001905#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001906
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001907 /* If we get here, we've just written the last row, so we need
Guy Schalnat0d580581995-07-20 02:43:20 -05001908 to flush the compressor */
1909 do
1910 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001911 /* Tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001912 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001913 /* Check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001914 if (ret == Z_OK)
1915 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001916 /* Check to see if we need more room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001917 if (!(png_ptr->zstream.avail_out))
1918 {
1919 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1920 png_ptr->zstream.next_out = png_ptr->zbuf;
1921 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1922 }
1923 }
1924 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001925 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001926 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001927 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001928 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001929 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001930 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001931 } while (ret != Z_STREAM_END);
1932
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001933 /* Write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001934 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001935 {
1936 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001937 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001938 }
1939
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001940 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -06001941 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat0d580581995-07-20 02:43:20 -05001942}
1943
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001944#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001945/* Pick out the correct pixels for the interlace pass.
1946 * The basic idea here is to go through the row with a source
1947 * pointer and a destination pointer (sp and dp), and copy the
1948 * correct pixels for the pass. As the row gets compacted,
1949 * sp will always be >= dp, so we should never overwrite anything.
1950 * See the default: case for the easiest code to understand.
1951 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001952void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001953png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001954{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001955#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001956 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001957
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001958 /* Start of interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001959 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001960
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001961 /* Offset to next interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001962 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001963#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001964
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001965 png_debug(1, "in png_do_write_interlace");
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001966 /* We don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001967 if (pass < 6)
Guy Schalnat0d580581995-07-20 02:43:20 -05001968 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001969 /* Each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001970 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001971 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001972 case 1:
1973 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001974 png_bytep sp;
1975 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001976 int shift;
1977 int d;
1978 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001979 png_uint_32 i;
1980 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001981
1982 dp = row;
1983 d = 0;
1984 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001985 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001986 i += png_pass_inc[pass])
1987 {
1988 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001989 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001990 d |= (value << shift);
1991
1992 if (shift == 0)
1993 {
1994 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001995 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001996 d = 0;
1997 }
1998 else
1999 shift--;
2000
2001 }
2002 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002003 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002004 break;
2005 }
2006 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002007 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002008 png_bytep sp;
2009 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05002010 int shift;
2011 int d;
2012 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002013 png_uint_32 i;
2014 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002015
2016 dp = row;
2017 shift = 6;
2018 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002019 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002020 i += png_pass_inc[pass])
2021 {
2022 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002023 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05002024 d |= (value << shift);
2025
2026 if (shift == 0)
2027 {
2028 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002029 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002030 d = 0;
2031 }
2032 else
2033 shift -= 2;
2034 }
2035 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002036 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002037 break;
2038 }
2039 case 4:
2040 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002041 png_bytep sp;
2042 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002043 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05002044 int d;
2045 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002046 png_uint_32 i;
2047 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002048
2049 dp = row;
2050 shift = 4;
2051 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002052 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002053 i += png_pass_inc[pass])
2054 {
2055 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002056 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05002057 d |= (value << shift);
2058
2059 if (shift == 0)
2060 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002061 shift = 4;
2062 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002063 d = 0;
2064 }
2065 else
2066 shift -= 4;
2067 }
2068 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002069 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002070 break;
2071 }
2072 default:
2073 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002074 png_bytep sp;
2075 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002076 png_uint_32 i;
2077 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002078 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002079
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002080 /* Start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002081 dp = row;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002082 /* Find out how many bytes each pixel takes up */
Guy Schalnat0d580581995-07-20 02:43:20 -05002083 pixel_bytes = (row_info->pixel_depth >> 3);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002084 /* Loop through the row, only looking at the pixels that
Guy Schalnat0d580581995-07-20 02:43:20 -05002085 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002086 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002087 i += png_pass_inc[pass])
2088 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002089 /* Find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002090 sp = row + (png_size_t)i * pixel_bytes;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002091 /* Move the pixel */
Guy Schalnat0d580581995-07-20 02:43:20 -05002092 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002093 png_memcpy(dp, sp, pixel_bytes);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002094 /* Next pixel */
Guy Schalnat0d580581995-07-20 02:43:20 -05002095 dp += pixel_bytes;
2096 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002097 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002098 }
2099 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002100 /* Set new row width */
Guy Schalnat0d580581995-07-20 02:43:20 -05002101 row_info->width = (row_info->width +
2102 png_pass_inc[pass] - 1 -
2103 png_pass_start[pass]) /
2104 png_pass_inc[pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002105 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2106 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002107 }
2108}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002109#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002110
Andreas Dilger47a0c421997-05-16 02:46:07 -05002111/* This filters the row, chooses which filter to use, if it has not already
2112 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002113 * chosen filter.
2114 */
Glenn Randers-Pehrson78067772004-11-02 21:49:39 -06002115#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002116#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002117#define PNG_LOMASK ((png_uint_32)0xffffL)
2118#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002119void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002120png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002121{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002122 png_bytep best_row;
2123#ifndef PNG_NO_WRITE_FILTER
2124 png_bytep prev_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002125 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002126 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002127 png_uint_32 row_bytes = row_info->rowbytes;
2128#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2129 int num_p_filters = (int)png_ptr->num_prev_filters;
2130#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002131
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002132 png_debug(1, "in png_write_find_filter");
Guy Schalnat0d580581995-07-20 02:43:20 -05002133 /* find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002134 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002135
2136 prev_row = png_ptr->prev_row;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002137#endif
2138 best_row = png_ptr->row_buf;
2139#ifndef PNG_NO_WRITE_FILTER
2140 row_buf = best_row;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002141 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002142
Andreas Dilger47a0c421997-05-16 02:46:07 -05002143 /* The prediction method we use is to find which method provides the
2144 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002145 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002146 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002147 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002148 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002149 * predictive" method (not implemented yet), which does test compressions
2150 * of lines using different filter methods, and then chooses the
2151 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002152 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002153 *
2154 * GRR 980525: consider also
2155 * (1) minimum sum of absolute differences from running average (i.e.,
2156 * keep running sum of non-absolute differences & count of bytes)
2157 * [track dispersion, too? restart average if dispersion too large?]
2158 * (1b) minimum sum of absolute differences from sliding average, probably
2159 * with window size <= deflate window (usually 32K)
2160 * (2) minimum sum of squared differences from zero or running average
2161 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002162 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002163
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002164
Guy Schalnate5a37791996-06-05 15:50:50 -05002165 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002166 * that has been chosen, as it doesn't actually do anything to the data.
2167 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002168 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002169 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002170 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002171 png_bytep rp;
2172 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002173 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002174 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002175
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002176 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002177 {
2178 v = *rp;
2179 sum += (v < 128) ? v : 256 - v;
2180 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002181
2182#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2183 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2184 {
2185 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002186 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002187 sumlo = sum & PNG_LOMASK;
2188 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2189
2190 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002191 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002192 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002193 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002194 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002195 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002196 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002197 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002198 PNG_WEIGHT_SHIFT;
2199 }
2200 }
2201
2202 /* Factor in the cost of this filter (this is here for completeness,
2203 * but it makes no sense to have a "cost" for the NONE filter, as
2204 * it has the minimum possible computational cost - none).
2205 */
2206 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2207 PNG_COST_SHIFT;
2208 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2209 PNG_COST_SHIFT;
2210
2211 if (sumhi > PNG_HIMASK)
2212 sum = PNG_MAXSUM;
2213 else
2214 sum = (sumhi << PNG_HISHIFT) + sumlo;
2215 }
2216#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002217 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002218 }
2219
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002220 /* Sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002221 if (filter_to_do == PNG_FILTER_SUB)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002222 /* It's the only filter so no testing is needed */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002223 {
2224 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002225 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002226 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2227 i++, rp++, dp++)
2228 {
2229 *dp = *rp;
2230 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002231 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002232 i++, rp++, lp++, dp++)
2233 {
2234 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2235 }
2236 best_row = png_ptr->sub_row;
2237 }
2238
2239 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002240 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002241 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002242 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002243 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002244 int v;
2245
2246#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002247 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002248 * would reduce the sum of this filter, so that we can do the
2249 * early exit comparison without scaling the sum each time.
2250 */
2251 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2252 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002253 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002254 png_uint_32 lmhi, lmlo;
2255 lmlo = lmins & PNG_LOMASK;
2256 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2257
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002258 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002259 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002260 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002261 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002262 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002263 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002264 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002265 PNG_WEIGHT_SHIFT;
2266 }
2267 }
2268
2269 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2270 PNG_COST_SHIFT;
2271 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2272 PNG_COST_SHIFT;
2273
2274 if (lmhi > PNG_HIMASK)
2275 lmins = PNG_MAXSUM;
2276 else
2277 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2278 }
2279#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002280
Guy Schalnate5a37791996-06-05 15:50:50 -05002281 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2282 i++, rp++, dp++)
2283 {
2284 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002285
Guy Schalnate5a37791996-06-05 15:50:50 -05002286 sum += (v < 128) ? v : 256 - v;
2287 }
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002288 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002289 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002290 {
2291 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002292
Guy Schalnate5a37791996-06-05 15:50:50 -05002293 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002294
2295 if (sum > lmins) /* We are already worse, don't continue. */
2296 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002297 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002298
2299#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2300 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2301 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002302 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002303 png_uint_32 sumhi, sumlo;
2304 sumlo = sum & PNG_LOMASK;
2305 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2306
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002307 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002308 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002309 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002310 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002311 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002312 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002313 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002314 PNG_WEIGHT_SHIFT;
2315 }
2316 }
2317
2318 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2319 PNG_COST_SHIFT;
2320 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2321 PNG_COST_SHIFT;
2322
2323 if (sumhi > PNG_HIMASK)
2324 sum = PNG_MAXSUM;
2325 else
2326 sum = (sumhi << PNG_HISHIFT) + sumlo;
2327 }
2328#endif
2329
Guy Schalnate5a37791996-06-05 15:50:50 -05002330 if (sum < mins)
2331 {
2332 mins = sum;
2333 best_row = png_ptr->sub_row;
2334 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002335 }
2336
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002337 /* Up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002338 if (filter_to_do == PNG_FILTER_UP)
2339 {
2340 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002341 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002342
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002343 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002344 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002345 i++, rp++, pp++, dp++)
2346 {
2347 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2348 }
2349 best_row = png_ptr->up_row;
2350 }
2351
2352 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002353 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002354 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002355 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002356 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002357 int v;
2358
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002359
Andreas Dilger47a0c421997-05-16 02:46:07 -05002360#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2361 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2362 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002363 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002364 png_uint_32 lmhi, lmlo;
2365 lmlo = lmins & PNG_LOMASK;
2366 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2367
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002368 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002369 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002370 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002371 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002372 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002373 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002374 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002375 PNG_WEIGHT_SHIFT;
2376 }
2377 }
2378
2379 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2380 PNG_COST_SHIFT;
2381 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2382 PNG_COST_SHIFT;
2383
2384 if (lmhi > PNG_HIMASK)
2385 lmins = PNG_MAXSUM;
2386 else
2387 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2388 }
2389#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002390
2391 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002392 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002393 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002394 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002395
2396 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002397
2398 if (sum > lmins) /* We are already worse, don't continue. */
2399 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002400 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002401
2402#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2403 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2404 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002405 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002406 png_uint_32 sumhi, sumlo;
2407 sumlo = sum & PNG_LOMASK;
2408 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2409
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002410 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002411 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002412 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002413 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002414 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002415 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002416 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002417 PNG_WEIGHT_SHIFT;
2418 }
2419 }
2420
2421 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2422 PNG_COST_SHIFT;
2423 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2424 PNG_COST_SHIFT;
2425
2426 if (sumhi > PNG_HIMASK)
2427 sum = PNG_MAXSUM;
2428 else
2429 sum = (sumhi << PNG_HISHIFT) + sumlo;
2430 }
2431#endif
2432
Guy Schalnate5a37791996-06-05 15:50:50 -05002433 if (sum < mins)
2434 {
2435 mins = sum;
2436 best_row = png_ptr->up_row;
2437 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002438 }
2439
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002440 /* Avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002441 if (filter_to_do == PNG_FILTER_AVG)
2442 {
2443 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002444 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002445 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002446 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002447 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002448 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002449 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002450 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002451 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002452 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2453 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002454 }
2455 best_row = png_ptr->avg_row;
2456 }
2457
2458 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002459 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002460 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002461 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002462 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002463 int v;
2464
2465#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2466 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2467 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002468 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002469 png_uint_32 lmhi, lmlo;
2470 lmlo = lmins & PNG_LOMASK;
2471 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2472
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002473 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002474 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002475 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002476 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002477 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002478 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002479 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002480 PNG_WEIGHT_SHIFT;
2481 }
2482 }
2483
2484 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2485 PNG_COST_SHIFT;
2486 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2487 PNG_COST_SHIFT;
2488
2489 if (lmhi > PNG_HIMASK)
2490 lmins = PNG_MAXSUM;
2491 else
2492 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2493 }
2494#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002495
2496 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002497 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002498 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002499 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002500
2501 sum += (v < 128) ? v : 256 - v;
2502 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002503 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002504 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002505 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002506 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002507
2508 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002509
2510 if (sum > lmins) /* We are already worse, don't continue. */
2511 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002512 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002513
2514#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2515 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2516 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002517 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002518 png_uint_32 sumhi, sumlo;
2519 sumlo = sum & PNG_LOMASK;
2520 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2521
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002522 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002523 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002524 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002525 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002526 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002527 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002528 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002529 PNG_WEIGHT_SHIFT;
2530 }
2531 }
2532
2533 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2534 PNG_COST_SHIFT;
2535 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2536 PNG_COST_SHIFT;
2537
2538 if (sumhi > PNG_HIMASK)
2539 sum = PNG_MAXSUM;
2540 else
2541 sum = (sumhi << PNG_HISHIFT) + sumlo;
2542 }
2543#endif
2544
Guy Schalnate5a37791996-06-05 15:50:50 -05002545 if (sum < mins)
2546 {
2547 mins = sum;
2548 best_row = png_ptr->avg_row;
2549 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002550 }
2551
Andreas Dilger47a0c421997-05-16 02:46:07 -05002552 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002553 if (filter_to_do == PNG_FILTER_PAETH)
2554 {
2555 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002556 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002557 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002558 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002559 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002560 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002561 }
2562
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002563 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002564 {
2565 int a, b, c, pa, pb, pc, p;
2566
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002567 b = *pp++;
2568 c = *cp++;
2569 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002570
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002571 p = b - c;
2572 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002573
2574#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002575 pa = abs(p);
2576 pb = abs(pc);
2577 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002578#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002579 pa = p < 0 ? -p : p;
2580 pb = pc < 0 ? -pc : pc;
2581 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002582#endif
2583
2584 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2585
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002586 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002587 }
2588 best_row = png_ptr->paeth_row;
2589 }
2590
2591 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002592 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002593 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002594 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002595 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002596 int v;
2597
2598#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2599 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2600 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002601 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002602 png_uint_32 lmhi, lmlo;
2603 lmlo = lmins & PNG_LOMASK;
2604 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2605
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002606 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002607 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002608 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002609 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002610 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002611 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002612 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002613 PNG_WEIGHT_SHIFT;
2614 }
2615 }
2616
2617 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2618 PNG_COST_SHIFT;
2619 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2620 PNG_COST_SHIFT;
2621
2622 if (lmhi > PNG_HIMASK)
2623 lmins = PNG_MAXSUM;
2624 else
2625 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2626 }
2627#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002628
2629 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002630 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002631 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002632 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002633
2634 sum += (v < 128) ? v : 256 - v;
2635 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002636
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002637 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002638 {
2639 int a, b, c, pa, pb, pc, p;
2640
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002641 b = *pp++;
2642 c = *cp++;
2643 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002644
2645#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002646 p = b - c;
2647 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002648#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002649 pa = abs(p);
2650 pb = abs(pc);
2651 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002652#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002653 pa = p < 0 ? -p : p;
2654 pb = pc < 0 ? -pc : pc;
2655 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002656#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002657 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2658#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002659 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002660 pa = abs(p - a);
2661 pb = abs(p - b);
2662 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002663 if (pa <= pb && pa <= pc)
2664 p = a;
2665 else if (pb <= pc)
2666 p = b;
2667 else
2668 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002669#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002670
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002671 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002672
2673 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002674
2675 if (sum > lmins) /* We are already worse, don't continue. */
2676 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002677 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002678
2679#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2680 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2681 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002682 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002683 png_uint_32 sumhi, sumlo;
2684 sumlo = sum & PNG_LOMASK;
2685 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2686
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002687 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002688 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002689 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002690 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002691 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002692 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002693 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002694 PNG_WEIGHT_SHIFT;
2695 }
2696 }
2697
2698 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2699 PNG_COST_SHIFT;
2700 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2701 PNG_COST_SHIFT;
2702
2703 if (sumhi > PNG_HIMASK)
2704 sum = PNG_MAXSUM;
2705 else
2706 sum = (sumhi << PNG_HISHIFT) + sumlo;
2707 }
2708#endif
2709
Guy Schalnate5a37791996-06-05 15:50:50 -05002710 if (sum < mins)
2711 {
2712 best_row = png_ptr->paeth_row;
2713 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002714 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002715#endif /* PNG_NO_WRITE_FILTER */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002716 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002717
Guy Schalnate5a37791996-06-05 15:50:50 -05002718 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002719
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002720#ifndef PNG_NO_WRITE_FILTER
Andreas Dilger47a0c421997-05-16 02:46:07 -05002721#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2722 /* Save the type of filter we picked this time for future calculations */
2723 if (png_ptr->num_prev_filters > 0)
2724 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002725 int j;
2726 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002727 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002728 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002729 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002730 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002731 }
2732#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002733#endif /* PNG_NO_WRITE_FILTER */
Guy Schalnate5a37791996-06-05 15:50:50 -05002734}
2735
2736
Andreas Dilger47a0c421997-05-16 02:46:07 -05002737/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002738void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002739png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2740{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002741 png_debug(1, "in png_write_filtered_row");
2742 png_debug1(2, "filter = %d", filtered_row[0]);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002743 /* Set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002744
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002745 png_ptr->zstream.next_in = filtered_row;
2746 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002747 /* Repeat until we have compressed all the data */
Guy Schalnate5a37791996-06-05 15:50:50 -05002748 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002749 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002750 int ret; /* Return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002751
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002752 /* Compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002753 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002754 /* Check for compression errors */
Guy Schalnate5a37791996-06-05 15:50:50 -05002755 if (ret != Z_OK)
2756 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002757 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002758 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002759 else
2760 png_error(png_ptr, "zlib error");
2761 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002762
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002763 /* See if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002764 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002765 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002766 /* Write the IDAT and reset the zlib output buffer */
Guy Schalnate5a37791996-06-05 15:50:50 -05002767 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002768 png_ptr->zstream.next_out = png_ptr->zbuf;
2769 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002770 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002771 /* Repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002772 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002773
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002774 /* Swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002775 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002776 {
2777 png_bytep tptr;
2778
2779 tptr = png_ptr->prev_row;
2780 png_ptr->prev_row = png_ptr->row_buf;
2781 png_ptr->row_buf = tptr;
2782 }
2783
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002784 /* Finish row - updates counters and flushes zlib if last row */
Guy Schalnate5a37791996-06-05 15:50:50 -05002785 png_write_finish_row(png_ptr);
2786
2787#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2788 png_ptr->flush_rows++;
2789
2790 if (png_ptr->flush_dist > 0 &&
2791 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002792 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002793 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002794 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002795#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002796}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05002797#endif /* PNG_WRITE_SUPPORTED */