blob: a9131b0461fda8e9584fa14e9d552e67438ad5e5 [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-Pehrson5ade7ed2009-09-30 15:11:49 -05004 * Last changed in libpng 1.4.0 [September 30, 2009]
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05005 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -05008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060012 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050013
Guy Schalnat0d580581995-07-20 02:43:20 -050014#include "png.h"
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050015#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -050016#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050017
Andreas Dilger47a0c421997-05-16 02:46:07 -050018/* Place a 32-bit number into a buffer in PNG byte order. We work
19 * with unsigned numbers for convenience, although one supported
20 * ancillary chunk uses signed (two's complement) numbers.
21 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060022void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -060023png_save_uint_32(png_bytep buf, png_uint_32 i)
Guy Schalnat0d580581995-07-20 02:43:20 -050024{
25 buf[0] = (png_byte)((i >> 24) & 0xff);
26 buf[1] = (png_byte)((i >> 16) & 0xff);
27 buf[2] = (png_byte)((i >> 8) & 0xff);
28 buf[3] = (png_byte)(i & 0xff);
29}
30
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050031#ifdef PNG_SAVE_INT_32_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -050032/* The png_save_int_32 function assumes integers are stored in two's
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060033 * complement format. If this isn't the case, then this routine needs to
34 * be modified to write data in two's complement format.
35 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060036void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050037png_save_int_32(png_bytep buf, png_int_32 i)
38{
39 buf[0] = (png_byte)((i >> 24) & 0xff);
40 buf[1] = (png_byte)((i >> 16) & 0xff);
41 buf[2] = (png_byte)((i >> 8) & 0xff);
42 buf[3] = (png_byte)(i & 0xff);
43}
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -050044#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050045
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060046/* Place a 16-bit number into a buffer in PNG byte order.
47 * The parameter is declared unsigned int, not png_uint_16,
48 * just to avoid potential problems on pre-ANSI C compilers.
49 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060050void PNGAPI
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060051png_save_uint_16(png_bytep buf, unsigned int i)
Guy Schalnat0d580581995-07-20 02:43:20 -050052{
53 buf[0] = (png_byte)((i >> 8) & 0xff);
54 buf[1] = (png_byte)(i & 0xff);
55}
56
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050057/* Simple function to write the signature. If we have already written
58 * the magic bytes of the signature, or more likely, the PNG stream is
59 * being embedded into another stream and doesn't need its own signature,
60 * we should call png_set_sig_bytes() to tell libpng how many of the
61 * bytes have already been written.
62 */
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -050063void PNGAPI
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050064png_write_sig(png_structp png_ptr)
65{
66 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050067
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -050068#ifdef PNG_IO_STATE_SUPPORTED
69 /* Inform the I/O callback that the signature is being written */
70 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_SIGNATURE;
71#endif
72
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050073 /* Write the rest of the 8 byte signature */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050074 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050075 (png_size_t)(8 - png_ptr->sig_bytes));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050076 if (png_ptr->sig_bytes < 3)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050077 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
78}
79
Andreas Dilger47a0c421997-05-16 02:46:07 -050080/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060081 * representing the chunk name. The array must be at least 4 bytes in
82 * length, and does not need to be null terminated. To be safe, pass the
83 * pre-defined chunk names here, and if you need a new one, define it
84 * where the others are defined. The length is the length of the data.
85 * All the data must be present. If that is not possible, use the
86 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
87 * functions instead.
88 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050089void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060090png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050091 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050092{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050093 if (png_ptr == NULL)
94 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -050095 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050096 png_write_chunk_data(png_ptr, data, (png_size_t)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060097 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050098}
99
Andreas Dilger47a0c421997-05-16 02:46:07 -0500100/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600101 * The total_length is the sum of the lengths of all the data you will be
102 * passing in png_write_chunk_data().
103 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500104void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600105png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
106 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500107{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500108 png_byte buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500109
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500110 png_debug2(0, "Writing %s chunk, length = %lu", chunk_name,
111 (unsigned long)length);
112
113 if (png_ptr == NULL)
114 return;
115
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500116#ifdef PNG_IO_STATE_SUPPORTED
117 /* Inform the I/O callback that the chunk header is being written.
118 * PNG_IO_CHUNK_HDR requires a single I/O call.
119 */
120 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR;
121#endif
122
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500123 /* Write the length and the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500124 png_save_uint_32(buf, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500125 png_memcpy(buf + 4, chunk_name, 4);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500126 png_write_data(png_ptr, buf, (png_size_t)8);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500127 /* Put the chunk name into png_ptr->chunk_name */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500128 png_memcpy(png_ptr->chunk_name, chunk_name, 4);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500129 /* Reset the crc and run it over the chunk name */
Guy Schalnat0d580581995-07-20 02:43:20 -0500130 png_reset_crc(png_ptr);
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500131 png_calculate_crc(png_ptr, chunk_name, 4);
132
133#ifdef PNG_IO_STATE_SUPPORTED
134 /* Inform the I/O callback that chunk data will (possibly) be written.
135 * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.
136 */
137 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA;
138#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500139}
140
Andreas Dilger47a0c421997-05-16 02:46:07 -0500141/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600142 * Note that multiple calls to this function are allowed, and that the
143 * sum of the lengths from these calls *must* add up to the total_length
144 * given to png_write_chunk_start().
145 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500146void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500147png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500148{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500149 /* Write the data, and run the CRC over it */
150 if (png_ptr == NULL)
151 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500152 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500153 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600154 png_write_data(png_ptr, data, length);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500155 /* Update the CRC after writing the data,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500156 * in case that the user I/O routine alters it.
157 */
158 png_calculate_crc(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500159 }
160}
161
Andreas Dilger47a0c421997-05-16 02:46:07 -0500162/* Finish a chunk started with png_write_chunk_start(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500163void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600164png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500165{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500166 png_byte buf[4];
167
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500168 if (png_ptr == NULL) return;
169
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500170#ifdef PNG_IO_STATE_SUPPORTED
171 /* Inform the I/O callback that the chunk CRC is being written.
172 * PNG_IO_CHUNK_CRC requires a single I/O function call.
173 */
174 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC;
175#endif
176
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500177 /* Write the crc in a single operation */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500178 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500179
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500180 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500181}
182
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600183#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500184/* This pair of functions encapsulates the operation of (a) compressing a
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600185 * text string, and (b) issuing it later as a series of chunk data writes.
186 * The compression_state structure is shared context for these functions
187 * set up by the caller in order to make the whole mess thread-safe.
188 */
189
190typedef struct
191{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500192 char *input; /* The uncompressed input data */
193 int input_len; /* Its length */
194 int num_output_ptr; /* Number of output pointers used */
195 int max_output_ptr; /* Size of output_ptr */
196 png_charpp output_ptr; /* Array of pointers to output */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600197} compression_state;
198
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500199/* Compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500200static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600201png_text_compress(png_structp png_ptr,
202 png_charp text, png_size_t text_len, int compression,
203 compression_state *comp)
204{
205 int ret;
206
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600207 comp->num_output_ptr = 0;
208 comp->max_output_ptr = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600209 comp->output_ptr = NULL;
210 comp->input = NULL;
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600211 comp->input_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600212
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500213 /* We may just want to pass the text right through */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600214 if (compression == PNG_TEXT_COMPRESSION_NONE)
215 {
216 comp->input = text;
217 comp->input_len = text_len;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500218 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600219 }
220
221 if (compression >= PNG_TEXT_COMPRESSION_LAST)
222 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500223#ifdef PNG_STDIO_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600224 char msg[50];
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500225 png_snprintf(msg, 50, "Unknown compression type %d", compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600226 png_warning(png_ptr, msg);
227#else
228 png_warning(png_ptr, "Unknown compression type");
229#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600230 }
231
232 /* We can't write the chunk until we find out how much data we have,
233 * which means we need to run the compressor first and save the
234 * output. This shouldn't be a problem, as the vast majority of
235 * comments should be reasonable, but we will set up an array of
236 * malloc'd pointers to be sure.
237 *
238 * If we knew the application was well behaved, we could simplify this
239 * greatly by assuming we can always malloc an output buffer large
240 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
241 * and malloc this directly. The only time this would be a bad idea is
242 * if we can't malloc more than 64K and we have 64K of random input
243 * data, or if the input string is incredibly large (although this
244 * wouldn't cause a failure, just a slowdown due to swapping).
245 */
246
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500247 /* Set up the compression buffers */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600248 png_ptr->zstream.avail_in = (uInt)text_len;
249 png_ptr->zstream.next_in = (Bytef *)text;
250 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
251 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
252
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500253 /* This is the same compression loop as in png_write_row() */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600254 do
255 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500256 /* Compress the data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600257 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
258 if (ret != Z_OK)
259 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500260 /* Error */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600261 if (png_ptr->zstream.msg != NULL)
262 png_error(png_ptr, png_ptr->zstream.msg);
263 else
264 png_error(png_ptr, "zlib error");
265 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500266 /* Check to see if we need more room */
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600267 if (!(png_ptr->zstream.avail_out))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600268 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500269 /* Make sure the output array has room */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600270 if (comp->num_output_ptr >= comp->max_output_ptr)
271 {
272 int old_max;
273
274 old_max = comp->max_output_ptr;
275 comp->max_output_ptr = comp->num_output_ptr + 4;
276 if (comp->output_ptr != NULL)
277 {
278 png_charpp old_ptr;
279
280 old_ptr = comp->output_ptr;
281 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500282 (png_alloc_size_t)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500283 (comp->max_output_ptr * png_sizeof(png_charpp)));
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500284 png_memcpy(comp->output_ptr, old_ptr, old_max
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500285 * png_sizeof(png_charp));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600286 png_free(png_ptr, old_ptr);
287 }
288 else
289 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500290 (png_alloc_size_t)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500291 (comp->max_output_ptr * png_sizeof(png_charp)));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600292 }
293
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500294 /* Save the data */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500295 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500296 (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500297 (png_alloc_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500298 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
299 png_ptr->zbuf_size);
300 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600301
302 /* and reset the buffer */
303 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
304 png_ptr->zstream.next_out = png_ptr->zbuf;
305 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500306 /* Continue until we don't have any more to compress */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600307 } while (png_ptr->zstream.avail_in);
308
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500309 /* Finish the compression */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600310 do
311 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500312 /* Tell zlib we are finished */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600313 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500314
315 if (ret == Z_OK)
316 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500317 /* Check to see if we need more room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500318 if (!(png_ptr->zstream.avail_out))
319 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500320 /* Check to make sure our output array has room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500321 if (comp->num_output_ptr >= comp->max_output_ptr)
322 {
323 int old_max;
324
325 old_max = comp->max_output_ptr;
326 comp->max_output_ptr = comp->num_output_ptr + 4;
327 if (comp->output_ptr != NULL)
328 {
329 png_charpp old_ptr;
330
331 old_ptr = comp->output_ptr;
332 /* This could be optimized to realloc() */
333 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500334 (png_alloc_size_t)(comp->max_output_ptr *
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500335 png_sizeof(png_charp)));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500336 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500337 old_max * png_sizeof(png_charp));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500338 png_free(png_ptr, old_ptr);
339 }
340 else
341 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500342 (png_alloc_size_t)(comp->max_output_ptr *
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500343 png_sizeof(png_charp)));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500344 }
345
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500346 /* Save the data */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500347 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500348 (png_charp)png_malloc(png_ptr,
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500349 (png_alloc_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500350 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
351 png_ptr->zbuf_size);
352 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500353
354 /* and reset the buffer pointers */
355 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
356 png_ptr->zstream.next_out = png_ptr->zbuf;
357 }
358 }
359 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600360 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500361 /* We got an error */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600362 if (png_ptr->zstream.msg != NULL)
363 png_error(png_ptr, png_ptr->zstream.msg);
364 else
365 png_error(png_ptr, "zlib error");
366 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600367 } while (ret != Z_STREAM_END);
368
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500369 /* Text length is number of buffers plus last buffer */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600370 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
371 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
372 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
373
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500374 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600375}
376
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500377/* Ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500378static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600379png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
380{
381 int i;
382
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500383 /* Handle the no-compression case */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600384 if (comp->input)
385 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500386 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500387 (png_size_t)comp->input_len);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500388 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600389 }
390
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500391 /* Write saved output buffers, if any */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600392 for (i = 0; i < comp->num_output_ptr; i++)
393 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500394 png_write_chunk_data(png_ptr, (png_bytep)comp->output_ptr[i],
395 (png_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600396 png_free(png_ptr, comp->output_ptr[i]);
397 }
398 if (comp->max_output_ptr != 0)
399 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500400 /* Write anything left in zbuf */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600401 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
402 png_write_chunk_data(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500403 (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600404
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500405 /* Reset zlib for another zTXt/iTXt or image data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600406 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600407 png_ptr->zstream.data_type = Z_BINARY;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600408}
409#endif
410
Guy Schalnat0d580581995-07-20 02:43:20 -0500411/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600412 * information. Note that the rest of this code depends upon this
413 * information being correct.
414 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500415void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600416png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500417 int bit_depth, int color_type, int compression_type, int filter_type,
418 int interlace_type)
419{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600420 PNG_IHDR;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500421 int ret;
422
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500423 png_byte buf[13]; /* Buffer to store the IHDR info */
Guy Schalnat0d580581995-07-20 02:43:20 -0500424
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500425 png_debug(1, "in png_write_IHDR");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500426
Guy Schalnate5a37791996-06-05 15:50:50 -0500427 /* Check that we have valid input data from the application info */
428 switch (color_type)
429 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500430 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500431 switch (bit_depth)
432 {
433 case 1:
434 case 2:
435 case 4:
436 case 8:
437 case 16: png_ptr->channels = 1; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500438 default: png_error(png_ptr, "Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500439 }
440 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500441 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500442 if (bit_depth != 8 && bit_depth != 16)
443 png_error(png_ptr, "Invalid bit depth for RGB image");
444 png_ptr->channels = 3;
445 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500446 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500447 switch (bit_depth)
448 {
449 case 1:
450 case 2:
451 case 4:
452 case 8: png_ptr->channels = 1; break;
453 default: png_error(png_ptr, "Invalid bit depth for paletted image");
454 }
455 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500456 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500457 if (bit_depth != 8 && bit_depth != 16)
458 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
459 png_ptr->channels = 2;
460 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500461 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500462 if (bit_depth != 8 && bit_depth != 16)
463 png_error(png_ptr, "Invalid bit depth for RGBA image");
464 png_ptr->channels = 4;
465 break;
466 default:
467 png_error(png_ptr, "Invalid image color type specified");
468 }
469
Andreas Dilger47a0c421997-05-16 02:46:07 -0500470 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500471 {
472 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500473 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500474 }
475
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600476 /* Write filter_method 64 (intrapixel differencing) only if
477 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
478 * 2. Libpng did not write a PNG signature (this filter_method is only
479 * used in PNG datastreams that are embedded in MNG datastreams) and
480 * 3. The application called png_permit_mng_features with a mask that
481 * included PNG_FLAG_MNG_FILTER_64 and
482 * 4. The filter_method is 64 and
483 * 5. The color_type is RGB or RGBA
484 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600485 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500486#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600487 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600488 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500489 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600490 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600491 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
492#endif
493 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500494 {
495 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500496 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500497 }
498
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600499#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500500 if (interlace_type != PNG_INTERLACE_NONE &&
501 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500502 {
503 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500504 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500505 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600506#else
507 interlace_type=PNG_INTERLACE_NONE;
508#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500509
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500510 /* Save the relevent information */
Guy Schalnate5a37791996-06-05 15:50:50 -0500511 png_ptr->bit_depth = (png_byte)bit_depth;
512 png_ptr->color_type = (png_byte)color_type;
513 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500514#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600515 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500516#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500517 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500518 png_ptr->width = width;
519 png_ptr->height = height;
520
521 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500522 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500523 /* Set the usr info, so any transformations can modify it */
Guy Schalnate5a37791996-06-05 15:50:50 -0500524 png_ptr->usr_width = png_ptr->width;
525 png_ptr->usr_bit_depth = png_ptr->bit_depth;
526 png_ptr->usr_channels = png_ptr->channels;
527
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500528 /* Pack the header information into the buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500529 png_save_uint_32(buf, width);
530 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600531 buf[8] = (png_byte)bit_depth;
532 buf[9] = (png_byte)color_type;
533 buf[10] = (png_byte)compression_type;
534 buf[11] = (png_byte)filter_type;
535 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500536
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500537 /* Write the chunk */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500538 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500539
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500540 /* Initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600541 png_ptr->zstream.zalloc = png_zalloc;
542 png_ptr->zstream.zfree = png_zfree;
543 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500544 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500545 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500546 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
547 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500548 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500549 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500550 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500551 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500552 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500553 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500554 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500555 png_ptr->zlib_strategy = Z_FILTERED;
556 else
557 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
558 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500559 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500560 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500561 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500562 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500563 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500564 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500565 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500566 png_ptr->zlib_method = 8;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500567 ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
568 png_ptr->zlib_method, png_ptr->zlib_window_bits,
569 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
570 if (ret != Z_OK)
571 {
572 if (ret == Z_VERSION_ERROR) png_error(png_ptr,
573 "zlib failed to initialize compressor -- version error");
574 if (ret == Z_STREAM_ERROR) png_error(png_ptr,
575 "zlib failed to initialize compressor -- stream error");
576 if (ret == Z_MEM_ERROR) png_error(png_ptr,
577 "zlib failed to initialize compressor -- mem error");
578 png_error(png_ptr, "zlib failed to initialize compressor");
579 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600580 png_ptr->zstream.next_out = png_ptr->zbuf;
581 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600582 /* libpng is not interested in zstream.data_type */
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500583 /* Set it to a predefined value, to avoid its evaluation inside zlib */
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600584 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500585
Guy Schalnate5a37791996-06-05 15:50:50 -0500586 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500587}
588
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500589/* Write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500590 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600591 * structure.
592 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500593void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500594png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500595{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600596 PNG_PLTE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500597 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600598 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500599 png_byte buf[3];
600
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500601 png_debug(1, "in png_write_PLTE");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500602
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500603 if ((
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500604#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600605 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500606#endif
607 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500608 {
609 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500610 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500611 png_error(png_ptr, "Invalid number of colors in palette");
612 }
613 else
614 {
615 png_warning(png_ptr, "Invalid number of colors in palette");
616 return;
617 }
618 }
619
620 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
621 {
622 png_warning(png_ptr,
623 "Ignoring request to write a PLTE chunk in grayscale PNG");
624 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500625 }
626
Andreas Dilger47a0c421997-05-16 02:46:07 -0500627 png_ptr->num_palette = (png_uint_16)num_pal;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500628 png_debug1(3, "num_palette = %d", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500629
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500630 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE,
631 (png_uint_32)(num_pal * 3));
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500632#ifdef PNG_POINTER_INDEXING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500633 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500634 {
635 buf[0] = pal_ptr->red;
636 buf[1] = pal_ptr->green;
637 buf[2] = pal_ptr->blue;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500638 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500639 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500640#else
641 /* This is a little slower but some buggy compilers need to do this instead */
642 pal_ptr=palette;
643 for (i = 0; i < num_pal; i++)
644 {
645 buf[0] = pal_ptr[i].red;
646 buf[1] = pal_ptr[i].green;
647 buf[2] = pal_ptr[i].blue;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500648 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500649 }
650#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500651 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500652 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500653}
654
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500655/* Write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500656void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500657png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500658{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600659 PNG_IDAT;
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500660
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500661 png_debug(1, "in png_write_IDAT");
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500662
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500663 /* Optimize the CMF field in the zlib stream. */
664 /* This hack of the zlib stream is compliant to the stream specification. */
665 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
666 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
667 {
668 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
669 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
670 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500671 /* Avoid memory underflows and multiplication overflows.
672 *
673 * The conditions below are practically always satisfied;
674 * however, they still must be checked.
675 */
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500676 if (length >= 2 &&
677 png_ptr->height < 16384 && png_ptr->width < 16384)
678 {
679 png_uint_32 uncompressed_idat_size = png_ptr->height *
680 ((png_ptr->width *
681 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
682 unsigned int z_cinfo = z_cmf >> 4;
683 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
684 while (uncompressed_idat_size <= half_z_window_size &&
685 half_z_window_size >= 256)
686 {
687 z_cinfo--;
688 half_z_window_size >>= 1;
689 }
690 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
691 if (data[0] != (png_byte)z_cmf)
692 {
693 data[0] = (png_byte)z_cmf;
694 data[1] &= 0xe0;
695 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
696 }
697 }
698 }
699 else
700 png_error(png_ptr,
701 "Invalid zlib compression method or flags in IDAT");
702 }
703
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600704 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500705 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500706}
707
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500708/* Write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500709void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600710png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500711{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600712 PNG_IEND;
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500713
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500714 png_debug(1, "in png_write_IEND");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500715
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500716 png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500717 (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600718 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500719}
720
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500721#ifdef PNG_WRITE_gAMA_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500722/* Write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600723#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500724void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500725png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500726{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600727 PNG_gAMA;
Guy Schalnat0d580581995-07-20 02:43:20 -0500728 png_uint_32 igamma;
729 png_byte buf[4];
730
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500731 png_debug(1, "in png_write_gAMA");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500732
Glenn Randers-Pehrson626afd62009-08-20 22:52:51 -0500733 /* 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{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600743 PNG_gAMA;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600744 png_byte buf[4];
745
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500746 png_debug(1, "in png_write_gAMA");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500747
Glenn Randers-Pehrson626afd62009-08-20 22:52:51 -0500748 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500749 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500750 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600751}
752#endif
753#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500754
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500755#ifdef PNG_WRITE_sRGB_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500756/* Write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500757void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600758png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600759{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600760 PNG_sRGB;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600761 png_byte buf[1];
762
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500763 png_debug(1, "in png_write_sRGB");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500764
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500765 if (srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600766 png_warning(png_ptr,
767 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600768 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500769 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600770}
771#endif
772
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500773#ifdef PNG_WRITE_iCCP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500774/* Write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500775void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600776png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
777 png_charp profile, int profile_len)
778{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600779 PNG_iCCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600780 png_size_t name_len;
781 png_charp new_name;
782 compression_state comp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500783 int embedded_profile_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600784
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500785 png_debug(1, "in png_write_iCCP");
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600786
787 comp.num_output_ptr = 0;
788 comp.max_output_ptr = 0;
789 comp.output_ptr = NULL;
790 comp.input = NULL;
791 comp.input_len = 0;
792
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -0600793 if ((name_len = png_check_keyword(png_ptr, name,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600794 &new_name)) == 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600795 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600796
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600797 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600798 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600799
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500800 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600801 profile_len = 0;
802
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500803 if (profile_len > 3)
Glenn Randers-Pehrson7edd4582006-12-07 19:16:44 -0600804 embedded_profile_len =
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500805 ((*( (png_bytep)profile ))<<24) |
806 ((*( (png_bytep)profile + 1))<<16) |
807 ((*( (png_bytep)profile + 2))<< 8) |
808 ((*( (png_bytep)profile + 3)) );
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500809
Glenn Randers-Pehrson3a054e12009-08-01 08:53:23 -0500810 if (embedded_profile_len < 0)
811 {
812 png_warning(png_ptr,
813 "Embedded profile length in iCCP chunk is negative");
814 png_free(png_ptr, new_name);
815 return;
816 }
817
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500818 if (profile_len < embedded_profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500819 {
820 png_warning(png_ptr,
821 "Embedded profile length too large in iCCP chunk");
Glenn Randers-Pehrsona6cc6272009-04-01 14:18:43 -0500822 png_free(png_ptr, new_name);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500823 return;
824 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500825
826 if (profile_len > embedded_profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500827 {
828 png_warning(png_ptr,
829 "Truncating profile to actual length in iCCP chunk");
830 profile_len = embedded_profile_len;
831 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500832
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600833 if (profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500834 profile_len = png_text_compress(png_ptr, profile,
835 (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600836
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500837 /* Make sure we include the NULL after the name and the compression type */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600838 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500839 (png_uint_32)(name_len + profile_len + 2));
840 new_name[name_len + 1] = 0x00;
841 png_write_chunk_data(png_ptr, (png_bytep)new_name,
842 (png_size_t)(name_len + 2));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600843
844 if (profile_len)
845 png_write_compressed_data_out(png_ptr, &comp);
846
847 png_write_chunk_end(png_ptr);
848 png_free(png_ptr, new_name);
849}
850#endif
851
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500852#ifdef PNG_WRITE_sPLT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500853/* Write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500854void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600855png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600856{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600857 PNG_sPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600858 png_size_t name_len;
859 png_charp new_name;
860 png_byte entrybuf[10];
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500861 png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
862 png_size_t palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600863 png_sPLT_entryp ep;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500864#ifndef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500865 int i;
866#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600867
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500868 png_debug(1, "in png_write_sPLT");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500869
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500870 if ((name_len = png_check_keyword(png_ptr,spalette->name, &new_name))==0)
871 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600872
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500873 /* Make sure we include the NULL after the name */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500874 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500875 (png_uint_32)(name_len + 2 + palette_size));
876 png_write_chunk_data(png_ptr, (png_bytep)new_name,
877 (png_size_t)(name_len + 1));
878 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600879
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500880 /* Loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500881#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500882 for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600883 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500884 if (spalette->depth == 8)
885 {
886 entrybuf[0] = (png_byte)ep->red;
887 entrybuf[1] = (png_byte)ep->green;
888 entrybuf[2] = (png_byte)ep->blue;
889 entrybuf[3] = (png_byte)ep->alpha;
890 png_save_uint_16(entrybuf + 4, ep->frequency);
891 }
892 else
893 {
894 png_save_uint_16(entrybuf + 0, ep->red);
895 png_save_uint_16(entrybuf + 2, ep->green);
896 png_save_uint_16(entrybuf + 4, ep->blue);
897 png_save_uint_16(entrybuf + 6, ep->alpha);
898 png_save_uint_16(entrybuf + 8, ep->frequency);
899 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500900 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600901 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500902#else
903 ep=spalette->entries;
904 for (i=0; i>spalette->nentries; i++)
905 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500906 if (spalette->depth == 8)
907 {
908 entrybuf[0] = (png_byte)ep[i].red;
909 entrybuf[1] = (png_byte)ep[i].green;
910 entrybuf[2] = (png_byte)ep[i].blue;
911 entrybuf[3] = (png_byte)ep[i].alpha;
912 png_save_uint_16(entrybuf + 4, ep[i].frequency);
913 }
914 else
915 {
916 png_save_uint_16(entrybuf + 0, ep[i].red);
917 png_save_uint_16(entrybuf + 2, ep[i].green);
918 png_save_uint_16(entrybuf + 4, ep[i].blue);
919 png_save_uint_16(entrybuf + 6, ep[i].alpha);
920 png_save_uint_16(entrybuf + 8, ep[i].frequency);
921 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500922 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500923 }
924#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600925
926 png_write_chunk_end(png_ptr);
927 png_free(png_ptr, new_name);
928}
929#endif
930
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500931#ifdef PNG_WRITE_sBIT_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500932/* Write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500933void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600934png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500935{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600936 PNG_sBIT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500937 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500938 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500939
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500940 png_debug(1, "in png_write_sBIT");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500941
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500942 /* Make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500943 if (color_type & PNG_COLOR_MASK_COLOR)
944 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600945 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500946
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500947 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
948 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600949 if (sbit->red == 0 || sbit->red > maxbits ||
950 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500951 sbit->blue == 0 || sbit->blue > maxbits)
952 {
953 png_warning(png_ptr, "Invalid sBIT depth specified");
954 return;
955 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500956 buf[0] = sbit->red;
957 buf[1] = sbit->green;
958 buf[2] = sbit->blue;
959 size = 3;
960 }
961 else
962 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500963 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
964 {
965 png_warning(png_ptr, "Invalid sBIT depth specified");
966 return;
967 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500968 buf[0] = sbit->gray;
969 size = 1;
970 }
971
972 if (color_type & PNG_COLOR_MASK_ALPHA)
973 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500974 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
975 {
976 png_warning(png_ptr, "Invalid sBIT depth specified");
977 return;
978 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500979 buf[size++] = sbit->alpha;
980 }
981
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600982 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500983}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500984#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500985
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500986#ifdef PNG_WRITE_cHRM_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500987/* Write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600988#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500989void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500990png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600991 double red_x, double red_y, double green_x, double green_y,
992 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500993{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600994 PNG_cHRM;
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-Pehrsone26c0952009-09-23 11:22:08 -05001011#ifdef PNG_CHECK_cHRM_SUPPORTED
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-Pehrson4bb4d012009-05-20 12:45:29 -05001016 /* Each value is saved in 1/100,000ths */
Glenn Randers-Pehrsond60c8862009-06-15 21:56:14 -05001017
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001018 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{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001041 PNG_cHRM;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001042 png_byte buf[32];
1043
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001044 png_debug(1, "in png_write_cHRM");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001045
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001046 /* Each value is saved in 1/100,000ths */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001047#ifdef PNG_CHECK_cHRM_SUPPORTED
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001048 if (png_check_cHRM_fixed(png_ptr, white_x, white_y, red_x, red_y,
1049 green_x, green_y, blue_x, blue_y))
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001050#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001051 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001052 png_save_uint_32(buf, (png_uint_32)white_x);
1053 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001054
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001055 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1056 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001057
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001058 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1059 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001060
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001061 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1062 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001063
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001064 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001065 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001066}
1067#endif
1068#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001069
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001070#ifdef PNG_WRITE_tRNS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001071/* Write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001072void /* PRIVATE */
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001073png_write_tRNS(png_structp png_ptr, png_bytep trans_alpha, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -05001074 int num_trans, int color_type)
1075{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001076 PNG_tRNS;
Guy Schalnat0d580581995-07-20 02:43:20 -05001077 png_byte buf[6];
1078
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001079 png_debug(1, "in png_write_tRNS");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001080
Guy Schalnat0d580581995-07-20 02:43:20 -05001081 if (color_type == PNG_COLOR_TYPE_PALETTE)
1082 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001083 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001084 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001085 png_warning(png_ptr, "Invalid number of transparent colors specified");
Guy Schalnate5a37791996-06-05 15:50:50 -05001086 return;
1087 }
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001088 /* Write the chunk out as it is */
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001089 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans_alpha,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001090 (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001091 }
1092 else if (color_type == PNG_COLOR_TYPE_GRAY)
1093 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001094 /* One 16 bit value */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001095 if (tran->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001096 {
1097 png_warning(png_ptr,
1098 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1099 return;
1100 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001101 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001102 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001103 }
1104 else if (color_type == PNG_COLOR_TYPE_RGB)
1105 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001106 /* Three 16 bit values */
Guy Schalnat0d580581995-07-20 02:43:20 -05001107 png_save_uint_16(buf, tran->red);
1108 png_save_uint_16(buf + 2, tran->green);
1109 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001110 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001111 {
1112 png_warning(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001113 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001114 return;
1115 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001116 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001117 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001118 else
1119 {
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001120 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001121 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001122}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001123#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001124
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001125#ifdef PNG_WRITE_bKGD_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001126/* Write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001127void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001128png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001129{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001130 PNG_bKGD;
Guy Schalnat0d580581995-07-20 02:43:20 -05001131 png_byte buf[6];
1132
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001133 png_debug(1, "in png_write_bKGD");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001134
Guy Schalnat0d580581995-07-20 02:43:20 -05001135 if (color_type == PNG_COLOR_TYPE_PALETTE)
1136 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001137 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001138#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001139 (png_ptr->num_palette ||
1140 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001141#endif
Glenn Randers-Pehrsond6d80752008-12-02 09:49:43 -06001142 back->index >= png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001143 {
1144 png_warning(png_ptr, "Invalid background palette index");
1145 return;
1146 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001147 buf[0] = back->index;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001148 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001149 }
1150 else if (color_type & PNG_COLOR_MASK_COLOR)
1151 {
1152 png_save_uint_16(buf, back->red);
1153 png_save_uint_16(buf + 2, back->green);
1154 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001155 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001156 {
1157 png_warning(png_ptr,
1158 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1159 return;
1160 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001161 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001162 }
1163 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001164 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001165 if (back->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001166 {
1167 png_warning(png_ptr,
1168 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1169 return;
1170 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001171 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001172 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001173 }
1174}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001175#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001176
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001177#ifdef PNG_WRITE_hIST_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001178/* Write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001179void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001180png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001181{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001182 PNG_hIST;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001183 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001184 png_byte buf[3];
1185
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001186 png_debug(1, "in png_write_hIST");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001187
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001188 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001189 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001190 png_debug2(3, "num_hist = %d, num_palette = %d", num_hist,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001191 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001192 png_warning(png_ptr, "Invalid number of histogram entries specified");
1193 return;
1194 }
1195
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001196 png_write_chunk_start(png_ptr, (png_bytep)png_hIST,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001197 (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001198 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001199 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001200 png_save_uint_16(buf, hist[i]);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001201 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001202 }
1203 png_write_chunk_end(png_ptr);
1204}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001205#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001206
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001207#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1208 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001209/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1210 * and if invalid, correct the keyword rather than discarding the entire
1211 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1212 * length, forbids leading or trailing whitespace, multiple internal spaces,
1213 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001214 *
1215 * The new_key is allocated to hold the corrected keyword and must be freed
1216 * by the calling routine. This avoids problems with trying to write to
1217 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001218 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001219png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001220png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001221{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001222 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001223 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001224 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001225 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001226
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001227 png_debug(1, "in png_check_keyword");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001228
Andreas Dilger47a0c421997-05-16 02:46:07 -05001229 *new_key = NULL;
1230
1231 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001232 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001233 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001234 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001235 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001236
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001237 png_debug1(2, "Keyword to be checked is '%s'", key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001238
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001239 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001240 if (*new_key == NULL)
1241 {
1242 png_warning(png_ptr, "Out of memory while procesing keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001243 return ((png_size_t)0);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001244 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001245
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001246 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001247 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001248 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001249 if ((png_byte)*kp < 0x20 ||
1250 ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001251 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001252#ifdef PNG_STDIO_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001253 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001254
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001255 png_snprintf(msg, 40,
1256 "invalid keyword character 0x%02X", (png_byte)*kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001257 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001258#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001259 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001260#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001261 *dp = ' ';
1262 }
1263 else
1264 {
1265 *dp = *kp;
1266 }
1267 }
1268 *dp = '\0';
1269
1270 /* Remove any trailing white space. */
1271 kp = *new_key + key_len - 1;
1272 if (*kp == ' ')
1273 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001274 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001275
1276 while (*kp == ' ')
1277 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001278 *(kp--) = '\0';
1279 key_len--;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001280 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001281 }
1282
1283 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001284 kp = *new_key;
1285 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001286 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001287 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001288
1289 while (*kp == ' ')
1290 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001291 kp++;
1292 key_len--;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001293 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001294 }
1295
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001296 png_debug1(2, "Checking for multiple internal spaces in '%s'", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001297
Andreas Dilger47a0c421997-05-16 02:46:07 -05001298 /* Remove multiple internal spaces. */
1299 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001300 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001301 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001302 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001303 *(dp++) = *kp;
1304 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001305 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001306 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001307 {
1308 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001309 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001310 }
1311 else
1312 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001313 *(dp++) = *kp;
1314 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001315 }
1316 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001317 *dp = '\0';
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001318 if (kwarn)
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001319 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001320
1321 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001322 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001323 png_free(png_ptr, *new_key);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001324 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001325 }
1326
1327 if (key_len > 79)
1328 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001329 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001330 (*new_key)[79] = '\0';
Andreas Dilger47a0c421997-05-16 02:46:07 -05001331 key_len = 79;
1332 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001333
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001334 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001335}
1336#endif
1337
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001338#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001339/* Write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001340void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001341png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001342 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001343{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001344 PNG_tEXt;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001345 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001346 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001347
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001348 png_debug(1, "in png_write_tEXt");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001349
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001350 if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001351 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001352
Andreas Dilger47a0c421997-05-16 02:46:07 -05001353 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001354 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001355 else
1356 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001357
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001358 /* Make sure we include the 0 after the key */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001359 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001360 (png_uint_32)(key_len + text_len + 1));
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001361 /*
1362 * We leave it to the application to meet PNG-1.0 requirements on the
1363 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1364 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001365 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001366 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001367 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1368 (png_size_t)(key_len + 1));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001369 if (text_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001370 png_write_chunk_data(png_ptr, (png_bytep)text, (png_size_t)text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001371
Guy Schalnat0d580581995-07-20 02:43:20 -05001372 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001373 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001374}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001375#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001376
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001377#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001378/* Write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001379void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001380png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001381 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001382{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001383 PNG_zTXt;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001384 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001385 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001386 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001387 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001388
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001389 png_debug(1, "in png_write_zTXt");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001390
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001391 comp.num_output_ptr = 0;
1392 comp.max_output_ptr = 0;
1393 comp.output_ptr = NULL;
1394 comp.input = NULL;
1395 comp.input_len = 0;
1396
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001397 if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001398 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001399 png_free(png_ptr, new_key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001400 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001401 }
1402
Andreas Dilger47a0c421997-05-16 02:46:07 -05001403 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1404 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001405 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001406 png_free(png_ptr, new_key);
1407 return;
1408 }
1409
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001410 text_len = png_strlen(text);
1411
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001412 /* Compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001413 text_len = png_text_compress(png_ptr, text, text_len, compression,
1414 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001415
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001416 /* Write start of chunk */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001417 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt,
1418 (png_uint_32)(key_len+text_len + 2));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001419 /* Write key */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001420 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1421 (png_size_t)(key_len + 1));
1422 png_free(png_ptr, new_key);
1423
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001424 buf[0] = (png_byte)compression;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001425 /* Write compression */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001426 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001427 /* Write the compressed data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001428 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001429
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001430 /* Close the chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -05001431 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001432}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001433#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001434
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001435#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001436/* Write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001437void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001438png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001439 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001440{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001441 PNG_iTXt;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001442 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001443 png_charp new_lang;
1444 png_charp new_key = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001445 png_byte cbuf[2];
1446 compression_state comp;
1447
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001448 png_debug(1, "in png_write_iTXt");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001449
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001450 comp.num_output_ptr = 0;
1451 comp.max_output_ptr = 0;
1452 comp.output_ptr = NULL;
1453 comp.input = NULL;
1454
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001455 if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001456 return;
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001457
1458 if ((lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001459 {
1460 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001461 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001462 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001463 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001464
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001465 if (lang_key == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001466 lang_key_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001467 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001468 lang_key_len = png_strlen(lang_key);
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001469
1470 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001471 text_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001472 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001473 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001474
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001475 /* Compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001476 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1477 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001478
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001479
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001480 /* Make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001481 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001482
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001483 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1484 (png_uint_32)(
1485 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1486 + key_len
1487 + lang_len
1488 + lang_key_len
1489 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001490
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001491 /* We leave it to the application to meet PNG-1.0 requirements on the
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001492 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1493 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1494 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1495 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001496 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1497 (png_size_t)(key_len + 1));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001498
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001499 /* Set the compression flag */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001500 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1501 compression == PNG_TEXT_COMPRESSION_NONE)
1502 cbuf[0] = 0;
1503 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1504 cbuf[0] = 1;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001505 /* Set the compression method */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001506 cbuf[1] = 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001507 png_write_chunk_data(png_ptr, cbuf, (png_size_t)2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001508
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001509 cbuf[0] = 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001510 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf),
1511 (png_size_t)(lang_len + 1));
1512 png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf),
1513 (png_size_t)(lang_key_len + 1));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001514 png_write_compressed_data_out(png_ptr, &comp);
1515
1516 png_write_chunk_end(png_ptr);
1517 png_free(png_ptr, new_key);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001518 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001519}
1520#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001521
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001522#ifdef PNG_WRITE_oFFs_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001523/* Write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001524void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001525png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001526 int unit_type)
1527{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001528 PNG_oFFs;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001529 png_byte buf[9];
1530
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001531 png_debug(1, "in png_write_oFFs");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001532
Andreas Dilger47a0c421997-05-16 02:46:07 -05001533 if (unit_type >= PNG_OFFSET_LAST)
1534 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1535
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001536 png_save_int_32(buf, x_offset);
1537 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001538 buf[8] = (png_byte)unit_type;
1539
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001540 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001541}
1542#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001543#ifdef PNG_WRITE_pCAL_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001544/* Write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001545void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001546png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1547 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1548{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001549 PNG_pCAL;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001550 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001551 png_uint_32p params_len;
1552 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001553 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001554 int i;
1555
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001556 png_debug1(1, "in png_write_pCAL (%d parameters)", nparams);
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001557
Andreas Dilger47a0c421997-05-16 02:46:07 -05001558 if (type >= PNG_EQUATION_LAST)
1559 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1560
1561 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001562 png_debug1(3, "pCAL purpose length = %d", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001563 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001564 png_debug1(3, "pCAL units length = %d", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001565 total_len = purpose_len + units_len + 10;
1566
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001567 params_len = (png_uint_32p)png_malloc(png_ptr,
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05001568 (png_alloc_size_t)(nparams * png_sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001569
1570 /* Find the length of each parameter, making sure we don't count the
1571 null terminator for the last parameter. */
1572 for (i = 0; i < nparams; i++)
1573 {
1574 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001575 png_debug2(3, "pCAL parameter %d length = %lu", i,
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001576 (unsigned long) params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001577 total_len += (png_size_t)params_len[i];
1578 }
1579
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001580 png_debug1(3, "pCAL total length = %d", (int)total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001581 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001582 png_write_chunk_data(png_ptr, (png_bytep)new_purpose,
1583 (png_size_t)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001584 png_save_int_32(buf, X0);
1585 png_save_int_32(buf + 4, X1);
1586 buf[8] = (png_byte)type;
1587 buf[9] = (png_byte)nparams;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001588 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1589 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001590
1591 png_free(png_ptr, new_purpose);
1592
1593 for (i = 0; i < nparams; i++)
1594 {
1595 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1596 (png_size_t)params_len[i]);
1597 }
1598
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001599 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001600 png_write_chunk_end(png_ptr);
1601}
1602#endif
1603
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001604#ifdef PNG_WRITE_sCAL_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001605/* Write the sCAL chunk */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001606#if defined(PNG_FLOATING_POINT_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001607void /* PRIVATE */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001608png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001609{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001610 PNG_sCAL;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001611 char buf[64];
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001612 png_size_t total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001613
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001614 png_debug(1, "in png_write_sCAL");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001615
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001616 buf[0] = (char)unit;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001617 png_snprintf(buf + 1, 63, "%12.12e", width);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001618 total_len = 1 + png_strlen(buf + 1) + 1;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001619 png_snprintf(buf + total_len, 64-total_len, "%12.12e", height);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001620 total_len += png_strlen(buf + total_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001621
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001622 png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001623 png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001624}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001625#else
1626#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001627void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001628png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001629 png_charp height)
1630{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001631 PNG_sCAL;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001632 png_byte buf[64];
1633 png_size_t wlen, hlen, total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001634
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001635 png_debug(1, "in png_write_sCAL_s");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001636
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001637 wlen = png_strlen(width);
1638 hlen = png_strlen(height);
1639 total_len = wlen + hlen + 2;
1640 if (total_len > 64)
1641 {
1642 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
1643 return;
1644 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001645
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001646 buf[0] = (png_byte)unit;
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001647 png_memcpy(buf + 1, width, wlen + 1); /* Append the '\0' here */
1648 png_memcpy(buf + wlen + 2, height, hlen); /* Do NOT append the '\0' here */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001649
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001650 png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001651 png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001652}
1653#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001654#endif
1655#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001656
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001657#ifdef PNG_WRITE_pHYs_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001658/* Write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001659void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001660png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001661 png_uint_32 y_pixels_per_unit,
1662 int unit_type)
1663{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001664 PNG_pHYs;
Guy Schalnat0d580581995-07-20 02:43:20 -05001665 png_byte buf[9];
1666
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001667 png_debug(1, "in png_write_pHYs");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001668
Guy Schalnate5a37791996-06-05 15:50:50 -05001669 if (unit_type >= PNG_RESOLUTION_LAST)
1670 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1671
Guy Schalnat0d580581995-07-20 02:43:20 -05001672 png_save_uint_32(buf, x_pixels_per_unit);
1673 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001674 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001675
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001676 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001677}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001678#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001679
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001680#ifdef PNG_WRITE_tIME_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001681/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1682 * or png_convert_from_time_t(), or fill in the structure yourself.
1683 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001684void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001685png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001686{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001687 PNG_tIME;
Guy Schalnat0d580581995-07-20 02:43:20 -05001688 png_byte buf[7];
1689
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001690 png_debug(1, "in png_write_tIME");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001691
Guy Schalnate5a37791996-06-05 15:50:50 -05001692 if (mod_time->month > 12 || mod_time->month < 1 ||
1693 mod_time->day > 31 || mod_time->day < 1 ||
1694 mod_time->hour > 23 || mod_time->second > 60)
1695 {
1696 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1697 return;
1698 }
1699
Guy Schalnat0d580581995-07-20 02:43:20 -05001700 png_save_uint_16(buf, mod_time->year);
1701 buf[2] = mod_time->month;
1702 buf[3] = mod_time->day;
1703 buf[4] = mod_time->hour;
1704 buf[5] = mod_time->minute;
1705 buf[6] = mod_time->second;
1706
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001707 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001708}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001709#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001710
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001711/* Initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001712void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001713png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001714{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001715#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001716 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001717
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001718 /* Start of interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001719 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001720
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001721 /* Offset to next interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001722 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001723
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001724 /* Start of interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001725 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001726
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001727 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001728 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001729#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001730
Andreas Dilger47a0c421997-05-16 02:46:07 -05001731 png_size_t buf_size;
1732
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001733 png_debug(1, "in png_write_start_row");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001734
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001735 buf_size = (png_size_t)(PNG_ROWBYTES(
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001736 png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001737
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001738 /* Set up row buffer */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001739 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05001740 (png_alloc_size_t)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001741 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001742
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001743#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001744 /* Set up filtering buffer, if using this filter */
Guy Schalnate5a37791996-06-05 15:50:50 -05001745 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001746 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001747 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05001748 (png_alloc_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001749 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001750 }
1751
Andreas Dilger47a0c421997-05-16 02:46:07 -05001752 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001753 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1754 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001755 /* Set up previous row buffer */
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05001756 png_ptr->prev_row = (png_bytep)png_calloc(png_ptr,
1757 (png_alloc_size_t)buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001758
1759 if (png_ptr->do_filter & PNG_FILTER_UP)
1760 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001761 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05001762 (png_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001763 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001764 }
1765
1766 if (png_ptr->do_filter & PNG_FILTER_AVG)
1767 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001768 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05001769 (png_alloc_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001770 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001771 }
1772
1773 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1774 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001775 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05001776 (png_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001777 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001778 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001779 }
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001780#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05001781
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001782#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001783 /* If interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001784 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001785 {
1786 if (!(png_ptr->transformations & PNG_INTERLACE))
1787 {
1788 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1789 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001790 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1791 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001792 }
1793 else
1794 {
1795 png_ptr->num_rows = png_ptr->height;
1796 png_ptr->usr_width = png_ptr->width;
1797 }
1798 }
1799 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001800#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001801 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001802 png_ptr->num_rows = png_ptr->height;
1803 png_ptr->usr_width = png_ptr->width;
1804 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001805 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1806 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001807}
1808
Andreas Dilger47a0c421997-05-16 02:46:07 -05001809/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001810void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001811png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001812{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001813#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001814 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001815
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001816 /* Start of interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001817 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001818
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001819 /* Offset to next interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001820 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001821
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001822 /* Start of interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001823 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001824
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001825 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001826 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001827#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001828
Guy Schalnat0d580581995-07-20 02:43:20 -05001829 int ret;
1830
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001831 png_debug(1, "in png_write_finish_row");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001832
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001833 /* Next row */
Guy Schalnat0d580581995-07-20 02:43:20 -05001834 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001835
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001836 /* See if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001837 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001838 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001839
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001840#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001841 /* If interlaced, go to next pass */
Guy Schalnat0d580581995-07-20 02:43:20 -05001842 if (png_ptr->interlaced)
1843 {
1844 png_ptr->row_number = 0;
1845 if (png_ptr->transformations & PNG_INTERLACE)
1846 {
1847 png_ptr->pass++;
1848 }
1849 else
1850 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001851 /* Loop until we find a non-zero width or height pass */
Guy Schalnat0d580581995-07-20 02:43:20 -05001852 do
1853 {
1854 png_ptr->pass++;
1855 if (png_ptr->pass >= 7)
1856 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001857 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001858 png_pass_inc[png_ptr->pass] - 1 -
1859 png_pass_start[png_ptr->pass]) /
1860 png_pass_inc[png_ptr->pass];
1861 png_ptr->num_rows = (png_ptr->height +
1862 png_pass_yinc[png_ptr->pass] - 1 -
1863 png_pass_ystart[png_ptr->pass]) /
1864 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001865 if (png_ptr->transformations & PNG_INTERLACE)
1866 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001867 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1868
1869 }
1870
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001871 /* Reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001872 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001873 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001874 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001875 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001876 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001877 png_ptr->usr_bit_depth, png_ptr->width)) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001878 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001879 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001880 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001881#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001882
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001883 /* If we get here, we've just written the last row, so we need
Guy Schalnat0d580581995-07-20 02:43:20 -05001884 to flush the compressor */
1885 do
1886 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001887 /* Tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001888 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001889 /* Check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001890 if (ret == Z_OK)
1891 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001892 /* Check to see if we need more room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001893 if (!(png_ptr->zstream.avail_out))
1894 {
1895 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1896 png_ptr->zstream.next_out = png_ptr->zbuf;
1897 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1898 }
1899 }
1900 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001901 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001902 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001903 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001904 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001905 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001906 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001907 } while (ret != Z_STREAM_END);
1908
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001909 /* Write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001910 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001911 {
1912 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001913 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001914 }
1915
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001916 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -06001917 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat0d580581995-07-20 02:43:20 -05001918}
1919
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001920#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001921/* Pick out the correct pixels for the interlace pass.
1922 * The basic idea here is to go through the row with a source
1923 * pointer and a destination pointer (sp and dp), and copy the
1924 * correct pixels for the pass. As the row gets compacted,
1925 * sp will always be >= dp, so we should never overwrite anything.
1926 * See the default: case for the easiest code to understand.
1927 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001928void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001929png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001930{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001931 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001932
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001933 /* Start of interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001934 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001935
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001936 /* Offset to next interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001937 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001938
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001939 png_debug(1, "in png_do_write_interlace");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001940
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001941 /* We don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001942 if (pass < 6)
Guy Schalnat0d580581995-07-20 02:43:20 -05001943 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001944 /* Each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001945 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001946 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001947 case 1:
1948 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001949 png_bytep sp;
1950 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001951 int shift;
1952 int d;
1953 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001954 png_uint_32 i;
1955 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001956
1957 dp = row;
1958 d = 0;
1959 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001960 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001961 i += png_pass_inc[pass])
1962 {
1963 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001964 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001965 d |= (value << shift);
1966
1967 if (shift == 0)
1968 {
1969 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001970 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001971 d = 0;
1972 }
1973 else
1974 shift--;
1975
1976 }
1977 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001978 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001979 break;
1980 }
1981 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001982 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001983 png_bytep sp;
1984 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001985 int shift;
1986 int d;
1987 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001988 png_uint_32 i;
1989 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001990
1991 dp = row;
1992 shift = 6;
1993 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001994 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001995 i += png_pass_inc[pass])
1996 {
1997 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001998 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05001999 d |= (value << shift);
2000
2001 if (shift == 0)
2002 {
2003 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002004 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002005 d = 0;
2006 }
2007 else
2008 shift -= 2;
2009 }
2010 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002011 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002012 break;
2013 }
2014 case 4:
2015 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002016 png_bytep sp;
2017 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002018 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05002019 int d;
2020 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002021 png_uint_32 i;
2022 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002023
2024 dp = row;
2025 shift = 4;
2026 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002027 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002028 i += png_pass_inc[pass])
2029 {
2030 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002031 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05002032 d |= (value << shift);
2033
2034 if (shift == 0)
2035 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002036 shift = 4;
2037 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002038 d = 0;
2039 }
2040 else
2041 shift -= 4;
2042 }
2043 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002044 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002045 break;
2046 }
2047 default:
2048 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002049 png_bytep sp;
2050 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002051 png_uint_32 i;
2052 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002053 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002054
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002055 /* Start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002056 dp = row;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002057 /* Find out how many bytes each pixel takes up */
Guy Schalnat0d580581995-07-20 02:43:20 -05002058 pixel_bytes = (row_info->pixel_depth >> 3);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002059 /* Loop through the row, only looking at the pixels that
Guy Schalnat0d580581995-07-20 02:43:20 -05002060 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002061 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002062 i += png_pass_inc[pass])
2063 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002064 /* Find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002065 sp = row + (png_size_t)i * pixel_bytes;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002066 /* Move the pixel */
Guy Schalnat0d580581995-07-20 02:43:20 -05002067 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002068 png_memcpy(dp, sp, pixel_bytes);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002069 /* Next pixel */
Guy Schalnat0d580581995-07-20 02:43:20 -05002070 dp += pixel_bytes;
2071 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002072 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002073 }
2074 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002075 /* Set new row width */
Guy Schalnat0d580581995-07-20 02:43:20 -05002076 row_info->width = (row_info->width +
2077 png_pass_inc[pass] - 1 -
2078 png_pass_start[pass]) /
2079 png_pass_inc[pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002080 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2081 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002082 }
2083}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002084#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002085
Andreas Dilger47a0c421997-05-16 02:46:07 -05002086/* This filters the row, chooses which filter to use, if it has not already
2087 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002088 * chosen filter.
2089 */
Glenn Randers-Pehrson78067772004-11-02 21:49:39 -06002090#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002091#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002092#define PNG_LOMASK ((png_uint_32)0xffffL)
2093#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002094void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002095png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002096{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002097 png_bytep best_row;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002098#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002099 png_bytep prev_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002100 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002101 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002102 png_uint_32 row_bytes = row_info->rowbytes;
Glenn Randers-Pehrson9d8b41e2009-07-19 14:45:43 -05002103#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002104 int num_p_filters = (int)png_ptr->num_prev_filters;
Glenn Randers-Pehrson9d8b41e2009-07-19 14:45:43 -05002105#endif
2106
2107 png_debug(1, "in png_write_find_filter");
2108
2109#ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson4ace0e12009-07-19 15:04:35 -05002110 if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS)
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05002111 {
2112 /* These will never be selected so we need not test them. */
2113 filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_PAETH);
2114 }
Glenn Randers-Pehrsonae4af562009-07-18 11:28:33 -05002115#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002116
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002117 /* Find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002118 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002119
2120 prev_row = png_ptr->prev_row;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002121#endif
2122 best_row = png_ptr->row_buf;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002123#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002124 row_buf = best_row;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002125 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002126
Andreas Dilger47a0c421997-05-16 02:46:07 -05002127 /* The prediction method we use is to find which method provides the
2128 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002129 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002130 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002131 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002132 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002133 * predictive" method (not implemented yet), which does test compressions
2134 * of lines using different filter methods, and then chooses the
2135 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002136 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002137 *
2138 * GRR 980525: consider also
2139 * (1) minimum sum of absolute differences from running average (i.e.,
2140 * keep running sum of non-absolute differences & count of bytes)
2141 * [track dispersion, too? restart average if dispersion too large?]
2142 * (1b) minimum sum of absolute differences from sliding average, probably
2143 * with window size <= deflate window (usually 32K)
2144 * (2) minimum sum of squared differences from zero or running average
2145 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002146 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002147
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002148
Guy Schalnate5a37791996-06-05 15:50:50 -05002149 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002150 * that has been chosen, as it doesn't actually do anything to the data.
2151 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002152 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002153 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002154 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002155 png_bytep rp;
2156 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002157 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002158 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002159
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002160 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002161 {
2162 v = *rp;
2163 sum += (v < 128) ? v : 256 - v;
2164 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002165
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002166#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002167 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2168 {
2169 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002170 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002171 sumlo = sum & PNG_LOMASK;
2172 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2173
2174 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002175 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002176 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002177 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002178 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002179 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002180 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002181 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002182 PNG_WEIGHT_SHIFT;
2183 }
2184 }
2185
2186 /* Factor in the cost of this filter (this is here for completeness,
2187 * but it makes no sense to have a "cost" for the NONE filter, as
2188 * it has the minimum possible computational cost - none).
2189 */
2190 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2191 PNG_COST_SHIFT;
2192 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2193 PNG_COST_SHIFT;
2194
2195 if (sumhi > PNG_HIMASK)
2196 sum = PNG_MAXSUM;
2197 else
2198 sum = (sumhi << PNG_HISHIFT) + sumlo;
2199 }
2200#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002201 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002202 }
2203
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002204 /* Sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002205 if (filter_to_do == PNG_FILTER_SUB)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002206 /* It's the only filter so no testing is needed */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002207 {
2208 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002209 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002210 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2211 i++, rp++, dp++)
2212 {
2213 *dp = *rp;
2214 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002215 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002216 i++, rp++, lp++, dp++)
2217 {
2218 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2219 }
2220 best_row = png_ptr->sub_row;
2221 }
2222
2223 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002224 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002225 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002226 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002227 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002228 int v;
2229
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002230#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002231 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002232 * would reduce the sum of this filter, so that we can do the
2233 * early exit comparison without scaling the sum each time.
2234 */
2235 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2236 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002237 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002238 png_uint_32 lmhi, lmlo;
2239 lmlo = lmins & PNG_LOMASK;
2240 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2241
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002242 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002243 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002244 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002245 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002246 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002247 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002248 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002249 PNG_WEIGHT_SHIFT;
2250 }
2251 }
2252
2253 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2254 PNG_COST_SHIFT;
2255 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2256 PNG_COST_SHIFT;
2257
2258 if (lmhi > PNG_HIMASK)
2259 lmins = PNG_MAXSUM;
2260 else
2261 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2262 }
2263#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002264
Guy Schalnate5a37791996-06-05 15:50:50 -05002265 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2266 i++, rp++, dp++)
2267 {
2268 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002269
Guy Schalnate5a37791996-06-05 15:50:50 -05002270 sum += (v < 128) ? v : 256 - v;
2271 }
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002272 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002273 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002274 {
2275 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002276
Guy Schalnate5a37791996-06-05 15:50:50 -05002277 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002278
2279 if (sum > lmins) /* We are already worse, don't continue. */
2280 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002281 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002282
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002283#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002284 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2285 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002286 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002287 png_uint_32 sumhi, sumlo;
2288 sumlo = sum & PNG_LOMASK;
2289 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2290
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002291 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002292 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002293 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002294 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002295 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002296 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002297 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002298 PNG_WEIGHT_SHIFT;
2299 }
2300 }
2301
2302 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2303 PNG_COST_SHIFT;
2304 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2305 PNG_COST_SHIFT;
2306
2307 if (sumhi > PNG_HIMASK)
2308 sum = PNG_MAXSUM;
2309 else
2310 sum = (sumhi << PNG_HISHIFT) + sumlo;
2311 }
2312#endif
2313
Guy Schalnate5a37791996-06-05 15:50:50 -05002314 if (sum < mins)
2315 {
2316 mins = sum;
2317 best_row = png_ptr->sub_row;
2318 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002319 }
2320
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002321 /* Up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002322 if (filter_to_do == PNG_FILTER_UP)
2323 {
2324 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002325 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002326
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002327 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002328 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002329 i++, rp++, pp++, dp++)
2330 {
2331 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2332 }
2333 best_row = png_ptr->up_row;
2334 }
2335
2336 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002337 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002338 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002339 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002340 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002341 int v;
2342
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002343
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002344#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002345 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2346 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002347 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002348 png_uint_32 lmhi, lmlo;
2349 lmlo = lmins & PNG_LOMASK;
2350 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2351
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002352 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002353 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002354 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002355 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002356 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002357 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002358 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002359 PNG_WEIGHT_SHIFT;
2360 }
2361 }
2362
2363 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2364 PNG_COST_SHIFT;
2365 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2366 PNG_COST_SHIFT;
2367
2368 if (lmhi > PNG_HIMASK)
2369 lmins = PNG_MAXSUM;
2370 else
2371 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2372 }
2373#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002374
2375 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002376 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002377 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002378 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002379
2380 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002381
2382 if (sum > lmins) /* We are already worse, don't continue. */
2383 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002384 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002385
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002386#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002387 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2388 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002389 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002390 png_uint_32 sumhi, sumlo;
2391 sumlo = sum & PNG_LOMASK;
2392 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2393
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002394 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002395 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002396 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002397 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002398 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002399 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002400 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002401 PNG_WEIGHT_SHIFT;
2402 }
2403 }
2404
2405 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2406 PNG_COST_SHIFT;
2407 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2408 PNG_COST_SHIFT;
2409
2410 if (sumhi > PNG_HIMASK)
2411 sum = PNG_MAXSUM;
2412 else
2413 sum = (sumhi << PNG_HISHIFT) + sumlo;
2414 }
2415#endif
2416
Guy Schalnate5a37791996-06-05 15:50:50 -05002417 if (sum < mins)
2418 {
2419 mins = sum;
2420 best_row = png_ptr->up_row;
2421 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002422 }
2423
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002424 /* Avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002425 if (filter_to_do == PNG_FILTER_AVG)
2426 {
2427 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002428 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002429 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002430 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002431 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002432 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002433 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002434 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002435 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002436 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2437 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002438 }
2439 best_row = png_ptr->avg_row;
2440 }
2441
2442 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002443 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002444 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002445 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002446 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002447 int v;
2448
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002449#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002450 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2451 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002452 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002453 png_uint_32 lmhi, lmlo;
2454 lmlo = lmins & PNG_LOMASK;
2455 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2456
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002457 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002458 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002459 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002460 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002461 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002462 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002463 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002464 PNG_WEIGHT_SHIFT;
2465 }
2466 }
2467
2468 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2469 PNG_COST_SHIFT;
2470 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2471 PNG_COST_SHIFT;
2472
2473 if (lmhi > PNG_HIMASK)
2474 lmins = PNG_MAXSUM;
2475 else
2476 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2477 }
2478#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002479
2480 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002481 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002482 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002483 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002484
2485 sum += (v < 128) ? v : 256 - v;
2486 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002487 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002488 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002489 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002490 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002491
2492 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002493
2494 if (sum > lmins) /* We are already worse, don't continue. */
2495 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002496 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002497
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002498#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002499 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2500 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002501 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002502 png_uint_32 sumhi, sumlo;
2503 sumlo = sum & PNG_LOMASK;
2504 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2505
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002506 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002507 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002508 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002509 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002510 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002511 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002512 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002513 PNG_WEIGHT_SHIFT;
2514 }
2515 }
2516
2517 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2518 PNG_COST_SHIFT;
2519 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2520 PNG_COST_SHIFT;
2521
2522 if (sumhi > PNG_HIMASK)
2523 sum = PNG_MAXSUM;
2524 else
2525 sum = (sumhi << PNG_HISHIFT) + sumlo;
2526 }
2527#endif
2528
Guy Schalnate5a37791996-06-05 15:50:50 -05002529 if (sum < mins)
2530 {
2531 mins = sum;
2532 best_row = png_ptr->avg_row;
2533 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002534 }
2535
Andreas Dilger47a0c421997-05-16 02:46:07 -05002536 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002537 if (filter_to_do == PNG_FILTER_PAETH)
2538 {
2539 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002540 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002541 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002542 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002543 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002544 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002545 }
2546
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002547 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002548 {
2549 int a, b, c, pa, pb, pc, p;
2550
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002551 b = *pp++;
2552 c = *cp++;
2553 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002554
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002555 p = b - c;
2556 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002557
2558#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002559 pa = abs(p);
2560 pb = abs(pc);
2561 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002562#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002563 pa = p < 0 ? -p : p;
2564 pb = pc < 0 ? -pc : pc;
2565 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002566#endif
2567
2568 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2569
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002570 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002571 }
2572 best_row = png_ptr->paeth_row;
2573 }
2574
2575 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002576 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002577 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002578 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002579 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002580 int v;
2581
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002582#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002583 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2584 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002585 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002586 png_uint_32 lmhi, lmlo;
2587 lmlo = lmins & PNG_LOMASK;
2588 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2589
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002590 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002591 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002592 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002593 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002594 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002595 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002596 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002597 PNG_WEIGHT_SHIFT;
2598 }
2599 }
2600
2601 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2602 PNG_COST_SHIFT;
2603 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2604 PNG_COST_SHIFT;
2605
2606 if (lmhi > PNG_HIMASK)
2607 lmins = PNG_MAXSUM;
2608 else
2609 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2610 }
2611#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002612
2613 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002614 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002615 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002616 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002617
2618 sum += (v < 128) ? v : 256 - v;
2619 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002620
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002621 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002622 {
2623 int a, b, c, pa, pb, pc, p;
2624
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002625 b = *pp++;
2626 c = *cp++;
2627 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002628
2629#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002630 p = b - c;
2631 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002632#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002633 pa = abs(p);
2634 pb = abs(pc);
2635 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002636#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002637 pa = p < 0 ? -p : p;
2638 pb = pc < 0 ? -pc : pc;
2639 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002640#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002641 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2642#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002643 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002644 pa = abs(p - a);
2645 pb = abs(p - b);
2646 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002647 if (pa <= pb && pa <= pc)
2648 p = a;
2649 else if (pb <= pc)
2650 p = b;
2651 else
2652 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002653#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002654
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002655 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002656
2657 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002658
2659 if (sum > lmins) /* We are already worse, don't continue. */
2660 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002661 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002662
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002663#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002664 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2665 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002666 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002667 png_uint_32 sumhi, sumlo;
2668 sumlo = sum & PNG_LOMASK;
2669 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2670
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002671 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002672 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002673 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002674 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002675 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002676 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002677 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002678 PNG_WEIGHT_SHIFT;
2679 }
2680 }
2681
2682 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2683 PNG_COST_SHIFT;
2684 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2685 PNG_COST_SHIFT;
2686
2687 if (sumhi > PNG_HIMASK)
2688 sum = PNG_MAXSUM;
2689 else
2690 sum = (sumhi << PNG_HISHIFT) + sumlo;
2691 }
2692#endif
2693
Guy Schalnate5a37791996-06-05 15:50:50 -05002694 if (sum < mins)
2695 {
2696 best_row = png_ptr->paeth_row;
2697 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002698 }
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002699#endif /* PNG_WRITE_FILTER_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002700 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002701
Guy Schalnate5a37791996-06-05 15:50:50 -05002702 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002703
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002704#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002705#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002706 /* Save the type of filter we picked this time for future calculations */
2707 if (png_ptr->num_prev_filters > 0)
2708 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002709 int j;
2710 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002711 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002712 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002713 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002714 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002715 }
2716#endif
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002717#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -05002718}
2719
2720
Andreas Dilger47a0c421997-05-16 02:46:07 -05002721/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002722void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002723png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2724{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002725 png_debug(1, "in png_write_filtered_row");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05002726
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002727 png_debug1(2, "filter = %d", filtered_row[0]);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002728 /* Set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002729
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002730 png_ptr->zstream.next_in = filtered_row;
2731 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002732 /* Repeat until we have compressed all the data */
Guy Schalnate5a37791996-06-05 15:50:50 -05002733 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002734 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002735 int ret; /* Return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002736
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002737 /* Compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002738 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002739 /* Check for compression errors */
Guy Schalnate5a37791996-06-05 15:50:50 -05002740 if (ret != Z_OK)
2741 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002742 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002743 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002744 else
2745 png_error(png_ptr, "zlib error");
2746 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002747
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002748 /* See if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002749 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002750 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002751 /* Write the IDAT and reset the zlib output buffer */
Guy Schalnate5a37791996-06-05 15:50:50 -05002752 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002753 png_ptr->zstream.next_out = png_ptr->zbuf;
2754 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002755 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002756 /* Repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002757 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002758
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002759 /* Swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002760 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002761 {
2762 png_bytep tptr;
2763
2764 tptr = png_ptr->prev_row;
2765 png_ptr->prev_row = png_ptr->row_buf;
2766 png_ptr->row_buf = tptr;
2767 }
2768
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002769 /* Finish row - updates counters and flushes zlib if last row */
Guy Schalnate5a37791996-06-05 15:50:50 -05002770 png_write_finish_row(png_ptr);
2771
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002772#ifdef PNG_WRITE_FLUSH_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05002773 png_ptr->flush_rows++;
2774
2775 if (png_ptr->flush_dist > 0 &&
2776 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002777 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002778 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002779 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002780#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002781}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05002782#endif /* PNG_WRITE_SUPPORTED */