blob: bf5872ff8c72f9af5136e4f9e83b1e805a0d6a98 [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-Pehrson6b3d50b2011-03-31 20:14:29 -05004 * Last changed in libpng 1.5.3 [(PENDING RELEASE)]
Glenn Randers-Pehrson64b863c2011-01-04 09:57:06 -06005 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -05008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060012 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050013
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -050014#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050015
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060016#ifdef PNG_WRITE_SUPPORTED
17
Glenn Randers-Pehrson34713ce2010-04-28 07:49:28 -050018#ifdef PNG_WRITE_INT_FUNCTIONS_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -050019/* Place a 32-bit number into a buffer in PNG byte order. We work
20 * with unsigned numbers for convenience, although one supported
21 * ancillary chunk uses signed (two's complement) numbers.
22 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060023void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -060024png_save_uint_32(png_bytep buf, png_uint_32 i)
Guy Schalnat0d580581995-07-20 02:43:20 -050025{
26 buf[0] = (png_byte)((i >> 24) & 0xff);
27 buf[1] = (png_byte)((i >> 16) & 0xff);
28 buf[2] = (png_byte)((i >> 8) & 0xff);
29 buf[3] = (png_byte)(i & 0xff);
30}
31
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -050032#ifdef PNG_SAVE_INT_32_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -050033/* The png_save_int_32 function assumes integers are stored in two's
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060034 * complement format. If this isn't the case, then this routine needs to
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050035 * be modified to write data in two's complement format. Note that,
36 * the following works correctly even if png_int_32 has more than 32 bits
37 * (compare the more complex code required on read for sign extention.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060038 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060039void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050040png_save_int_32(png_bytep buf, png_int_32 i)
41{
42 buf[0] = (png_byte)((i >> 24) & 0xff);
43 buf[1] = (png_byte)((i >> 16) & 0xff);
44 buf[2] = (png_byte)((i >> 8) & 0xff);
45 buf[3] = (png_byte)(i & 0xff);
46}
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -050047#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050048
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060049/* Place a 16-bit number into a buffer in PNG byte order.
50 * The parameter is declared unsigned int, not png_uint_16,
51 * just to avoid potential problems on pre-ANSI C compilers.
52 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060053void PNGAPI
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060054png_save_uint_16(png_bytep buf, unsigned int i)
Guy Schalnat0d580581995-07-20 02:43:20 -050055{
56 buf[0] = (png_byte)((i >> 8) & 0xff);
57 buf[1] = (png_byte)(i & 0xff);
58}
Glenn Randers-Pehrson34713ce2010-04-28 07:49:28 -050059#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050060
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050061/* Simple function to write the signature. If we have already written
62 * the magic bytes of the signature, or more likely, the PNG stream is
63 * being embedded into another stream and doesn't need its own signature,
64 * we should call png_set_sig_bytes() to tell libpng how many of the
65 * bytes have already been written.
66 */
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -050067void PNGAPI
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050068png_write_sig(png_structp png_ptr)
69{
70 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050071
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -050072#ifdef PNG_IO_STATE_SUPPORTED
73 /* Inform the I/O callback that the signature is being written */
74 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_SIGNATURE;
75#endif
76
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050077 /* Write the rest of the 8 byte signature */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050078 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050079 (png_size_t)(8 - png_ptr->sig_bytes));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050080
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050081 if (png_ptr->sig_bytes < 3)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050082 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
83}
84
Andreas Dilger47a0c421997-05-16 02:46:07 -050085/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060086 * representing the chunk name. The array must be at least 4 bytes in
87 * length, and does not need to be null terminated. To be safe, pass the
88 * pre-defined chunk names here, and if you need a new one, define it
89 * where the others are defined. The length is the length of the data.
90 * All the data must be present. If that is not possible, use the
91 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
92 * functions instead.
93 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050094void PNGAPI
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -050095png_write_chunk(png_structp png_ptr, png_const_bytep chunk_name,
96 png_const_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050097{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050098 if (png_ptr == NULL)
99 return;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500100
Andreas Dilger47a0c421997-05-16 02:46:07 -0500101 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500102 png_write_chunk_data(png_ptr, data, (png_size_t)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600103 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500104}
105
Andreas Dilger47a0c421997-05-16 02:46:07 -0500106/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600107 * The total_length is the sum of the lengths of all the data you will be
108 * passing in png_write_chunk_data().
109 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500110void PNGAPI
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500111png_write_chunk_start(png_structp png_ptr, png_const_bytep chunk_name,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600112 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500113{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500114 png_byte buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500115
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500116 png_debug2(0, "Writing %s chunk, length = %lu", chunk_name,
117 (unsigned long)length);
118
119 if (png_ptr == NULL)
120 return;
121
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500122#ifdef PNG_IO_STATE_SUPPORTED
123 /* Inform the I/O callback that the chunk header is being written.
124 * PNG_IO_CHUNK_HDR requires a single I/O call.
125 */
126 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR;
127#endif
128
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500129 /* Write the length and the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500130 png_save_uint_32(buf, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500131 png_memcpy(buf + 4, chunk_name, 4);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500132 png_write_data(png_ptr, buf, (png_size_t)8);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500133
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500134 /* Put the chunk name into png_ptr->chunk_name */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500135 png_memcpy(png_ptr->chunk_name, chunk_name, 4);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500136
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500137 /* Reset the crc and run it over the chunk name */
Guy Schalnat0d580581995-07-20 02:43:20 -0500138 png_reset_crc(png_ptr);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500139
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500140 png_calculate_crc(png_ptr, chunk_name, 4);
141
142#ifdef PNG_IO_STATE_SUPPORTED
143 /* Inform the I/O callback that chunk data will (possibly) be written.
144 * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.
145 */
146 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA;
147#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500148}
149
Andreas Dilger47a0c421997-05-16 02:46:07 -0500150/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600151 * Note that multiple calls to this function are allowed, and that the
152 * sum of the lengths from these calls *must* add up to the total_length
153 * given to png_write_chunk_start().
154 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500155void PNGAPI
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500156png_write_chunk_data(png_structp png_ptr, png_const_bytep data,
157 png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500158{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500159 /* Write the data, and run the CRC over it */
160 if (png_ptr == NULL)
161 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500162
Andreas Dilger47a0c421997-05-16 02:46:07 -0500163 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500164 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600165 png_write_data(png_ptr, data, length);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500166
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500167 /* Update the CRC after writing the data,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500168 * in case that the user I/O routine alters it.
169 */
170 png_calculate_crc(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500171 }
172}
173
Andreas Dilger47a0c421997-05-16 02:46:07 -0500174/* Finish a chunk started with png_write_chunk_start(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500175void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600176png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500177{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500178 png_byte buf[4];
179
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500180 if (png_ptr == NULL) return;
181
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500182#ifdef PNG_IO_STATE_SUPPORTED
183 /* Inform the I/O callback that the chunk CRC is being written.
184 * PNG_IO_CHUNK_CRC requires a single I/O function call.
185 */
186 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC;
187#endif
188
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500189 /* Write the crc in a single operation */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500190 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500191
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500192 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500193}
194
Glenn Randers-Pehrson205483d2011-04-01 12:33:42 -0500195#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500196/* This pair of functions encapsulates the operation of (a) compressing a
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600197 * text string, and (b) issuing it later as a series of chunk data writes.
198 * The compression_state structure is shared context for these functions
199 * set up by the caller in order to make the whole mess thread-safe.
200 */
201
202typedef struct
203{
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500204 png_const_bytep input; /* The uncompressed input data */
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500205 png_size_t input_len; /* Its length */
206 int num_output_ptr; /* Number of output pointers used */
207 int max_output_ptr; /* Size of output_ptr */
208 png_bytep *output_ptr; /* Array of pointers to output */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600209} compression_state;
210
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500211/* Compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500212static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600213png_text_compress(png_structp png_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500214 png_const_charp text, png_size_t text_len, int compression,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600215 compression_state *comp)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600216{
217 int ret;
218
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600219 comp->num_output_ptr = 0;
220 comp->max_output_ptr = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600221 comp->output_ptr = NULL;
222 comp->input = NULL;
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600223 comp->input_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600224
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500225 /* We may just want to pass the text right through */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600226 if (compression == PNG_TEXT_COMPRESSION_NONE)
227 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500228 comp->input = (png_const_bytep)text;
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600229 comp->input_len = text_len;
230 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600231 }
232
233 if (compression >= PNG_TEXT_COMPRESSION_LAST)
234 {
Glenn Randers-Pehrson6a9e4802010-02-19 09:47:43 -0600235#ifdef PNG_CONSOLE_IO_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600236 char msg[50];
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500237 png_snprintf(msg, 50, "Unknown compression type %d", compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600238 png_warning(png_ptr, msg);
239#else
240 png_warning(png_ptr, "Unknown compression type");
241#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600242 }
243
244 /* We can't write the chunk until we find out how much data we have,
245 * which means we need to run the compressor first and save the
246 * output. This shouldn't be a problem, as the vast majority of
247 * comments should be reasonable, but we will set up an array of
248 * malloc'd pointers to be sure.
249 *
250 * If we knew the application was well behaved, we could simplify this
251 * greatly by assuming we can always malloc an output buffer large
252 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
253 * and malloc this directly. The only time this would be a bad idea is
254 * if we can't malloc more than 64K and we have 64K of random input
255 * data, or if the input string is incredibly large (although this
256 * wouldn't cause a failure, just a slowdown due to swapping).
257 */
258
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500259 if (!(png_ptr->mode & PNG_ZLIB_READY_FOR_ZTXT))
260 {
Glenn Randers-Pehrsonee1369b2011-03-31 23:44:39 -0500261
262 /* png_warning(png_ptr, "Initialize compressor for ztxt"); */
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500263 /* Free memory from previously opened zstream */
264 deflateEnd(&png_ptr->zstream);
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500265
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500266 /* Initialize the compressor for zTXt compression. */
267 ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_text_level,
268 png_ptr->zlib_text_method, png_ptr->zlib_text_window_bits,
269 png_ptr->zlib_text_mem_level, png_ptr->zlib_text_strategy);
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500270
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500271 if (ret != Z_OK)
272 {
273 if (ret == Z_VERSION_ERROR)
274 png_error(png_ptr,
275 "zlib failed to initialize compressor for text-- version error");
276
277 if (ret == Z_STREAM_ERROR)
278 png_error(png_ptr,
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500279 "zlib failed to initialize compressor for text-- stream error");
280
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500281 if (ret == Z_MEM_ERROR)
282 png_error(png_ptr,
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500283 "zlib failed to initialize compressor for text-- mem error");
284
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500285 png_error(png_ptr, "zlib failed to initialize compressor for text");
286 }
287 png_ptr->mode |= PNG_ZLIB_READY_FOR_ZTXT;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500288 }
289
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500290 /* Set up the compression buffers */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500291
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600292 /* TODO: the following cast hides a potential overflow problem. */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600293 png_ptr->zstream.avail_in = (uInt)text_len;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500294
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600295 /* NOTE: assume zlib doesn't overwrite the input */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600296 png_ptr->zstream.next_in = (Bytef *)text;
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600297 png_ptr->zstream.avail_out = png_ptr->zbuf_size;
298 png_ptr->zstream.next_out = png_ptr->zbuf;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600299
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500300 /* This is the same compression loop as in png_write_row() */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600301 do
302 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500303 /* Compress the data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600304 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500305
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600306 if (ret != Z_OK)
307 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500308 /* Error */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600309 if (png_ptr->zstream.msg != NULL)
310 png_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500311
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600312 else
313 png_error(png_ptr, "zlib error");
314 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500315
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500316 /* Check to see if we need more room */
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600317 if (!(png_ptr->zstream.avail_out))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600318 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500319 /* Make sure the output array has room */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600320 if (comp->num_output_ptr >= comp->max_output_ptr)
321 {
322 int old_max;
323
324 old_max = comp->max_output_ptr;
325 comp->max_output_ptr = comp->num_output_ptr + 4;
326 if (comp->output_ptr != NULL)
327 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500328 png_bytepp old_ptr;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600329
330 old_ptr = comp->output_ptr;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500331
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500332 comp->output_ptr = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600333 (png_alloc_size_t)
334 (comp->max_output_ptr * png_sizeof(png_charpp)));
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500335
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500336 png_memcpy(comp->output_ptr, old_ptr, old_max
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600337 * png_sizeof(png_charp));
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500338
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600339 png_free(png_ptr, old_ptr);
340 }
341 else
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500342 comp->output_ptr = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600343 (png_alloc_size_t)
344 (comp->max_output_ptr * png_sizeof(png_charp)));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600345 }
346
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500347 /* Save the data */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500348 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500349 (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600350 (png_alloc_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500351
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500352 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600353 png_ptr->zbuf_size);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500354
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500355 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600356
357 /* and reset the buffer */
358 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
359 png_ptr->zstream.next_out = png_ptr->zbuf;
360 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500361 /* Continue until we don't have any more to compress */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600362 } while (png_ptr->zstream.avail_in);
363
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500364 /* Finish the compression */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600365 do
366 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500367 /* Tell zlib we are finished */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600368 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500369
370 if (ret == Z_OK)
371 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500372 /* Check to see if we need more room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500373 if (!(png_ptr->zstream.avail_out))
374 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500375 /* Check to make sure our output array has room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500376 if (comp->num_output_ptr >= comp->max_output_ptr)
377 {
378 int old_max;
379
380 old_max = comp->max_output_ptr;
381 comp->max_output_ptr = comp->num_output_ptr + 4;
382 if (comp->output_ptr != NULL)
383 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500384 png_bytepp old_ptr;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500385
386 old_ptr = comp->output_ptr;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500387
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500388 /* This could be optimized to realloc() */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500389 comp->output_ptr = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600390 (png_alloc_size_t)(comp->max_output_ptr *
391 png_sizeof(png_charp)));
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500392
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500393 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600394 old_max * png_sizeof(png_charp));
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500395
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500396 png_free(png_ptr, old_ptr);
397 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500398
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500399 else
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500400 comp->output_ptr = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600401 (png_alloc_size_t)(comp->max_output_ptr *
402 png_sizeof(png_charp)));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500403 }
404
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500405 /* Save the data */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500406 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500407 (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600408 (png_alloc_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500409
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500410 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600411 png_ptr->zbuf_size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500412
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500413 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500414
415 /* and reset the buffer pointers */
416 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
417 png_ptr->zstream.next_out = png_ptr->zbuf;
418 }
419 }
420 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600421 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500422 /* We got an error */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600423 if (png_ptr->zstream.msg != NULL)
424 png_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500425
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600426 else
427 png_error(png_ptr, "zlib error");
428 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600429 } while (ret != Z_STREAM_END);
430
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500431 /* Text length is number of buffers plus last buffer */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600432 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500433
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600434 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
435 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
436
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500437 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600438}
439
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500440/* Ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500441static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600442png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
443{
444 int i;
445
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500446 /* Handle the no-compression case */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600447 if (comp->input)
448 {
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500449 png_write_chunk_data(png_ptr, comp->input, comp->input_len);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500450
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500451 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600452 }
453
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500454 /* Write saved output buffers, if any */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600455 for (i = 0; i < comp->num_output_ptr; i++)
456 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500457 png_write_chunk_data(png_ptr, comp->output_ptr[i],
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600458 (png_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500459
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600460 png_free(png_ptr, comp->output_ptr[i]);
461 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500462
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600463 if (comp->max_output_ptr != 0)
464 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500465
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500466 /* Write anything left in zbuf */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600467 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
468 png_write_chunk_data(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600469 (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600470
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500471 /* Reset zlib for another zTXt/iTXt or image data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600472 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600473}
Glenn Randers-Pehrson205483d2011-04-01 12:33:42 -0500474#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600475
Guy Schalnat0d580581995-07-20 02:43:20 -0500476/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600477 * information. Note that the rest of this code depends upon this
478 * information being correct.
479 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500480void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600481png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600482 int bit_depth, int color_type, int compression_type, int filter_type,
483 int interlace_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500484{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600485 PNG_IHDR;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500486 int ret;
487
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500488 png_byte buf[13]; /* Buffer to store the IHDR info */
Guy Schalnat0d580581995-07-20 02:43:20 -0500489
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500490 png_debug(1, "in png_write_IHDR");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500491
Guy Schalnate5a37791996-06-05 15:50:50 -0500492 /* Check that we have valid input data from the application info */
493 switch (color_type)
494 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500495 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500496 switch (bit_depth)
497 {
498 case 1:
499 case 2:
500 case 4:
501 case 8:
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500502#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600503 case 16:
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500504#endif
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600505 png_ptr->channels = 1; break;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500506
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600507 default:
508 png_error(png_ptr,
509 "Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500510 }
511 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500512
Andreas Dilger47a0c421997-05-16 02:46:07 -0500513 case PNG_COLOR_TYPE_RGB:
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500514#ifdef PNG_WRITE_16BIT_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500515 if (bit_depth != 8 && bit_depth != 16)
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500516#else
517 if (bit_depth != 8)
518#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500519 png_error(png_ptr, "Invalid bit depth for RGB image");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500520
Guy Schalnate5a37791996-06-05 15:50:50 -0500521 png_ptr->channels = 3;
522 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500523
Andreas Dilger47a0c421997-05-16 02:46:07 -0500524 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500525 switch (bit_depth)
526 {
527 case 1:
528 case 2:
529 case 4:
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600530 case 8:
531 png_ptr->channels = 1;
532 break;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500533
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600534 default:
535 png_error(png_ptr, "Invalid bit depth for paletted image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500536 }
537 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500538
Andreas Dilger47a0c421997-05-16 02:46:07 -0500539 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500540 if (bit_depth != 8 && bit_depth != 16)
541 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500542
Guy Schalnate5a37791996-06-05 15:50:50 -0500543 png_ptr->channels = 2;
544 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500545
Andreas Dilger47a0c421997-05-16 02:46:07 -0500546 case PNG_COLOR_TYPE_RGB_ALPHA:
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500547#ifdef PNG_WRITE_16BIT_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500548 if (bit_depth != 8 && bit_depth != 16)
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500549#else
550 if (bit_depth != 8)
551#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500552 png_error(png_ptr, "Invalid bit depth for RGBA image");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500553
Guy Schalnate5a37791996-06-05 15:50:50 -0500554 png_ptr->channels = 4;
555 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500556
Guy Schalnate5a37791996-06-05 15:50:50 -0500557 default:
558 png_error(png_ptr, "Invalid image color type specified");
559 }
560
Andreas Dilger47a0c421997-05-16 02:46:07 -0500561 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500562 {
563 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500564 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500565 }
566
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600567 /* Write filter_method 64 (intrapixel differencing) only if
568 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
569 * 2. Libpng did not write a PNG signature (this filter_method is only
570 * used in PNG datastreams that are embedded in MNG datastreams) and
571 * 3. The application called png_permit_mng_features with a mask that
572 * included PNG_FLAG_MNG_FILTER_64 and
573 * 4. The filter_method is 64 and
574 * 5. The color_type is RGB or RGBA
575 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600576 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500577#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600578 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
579 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
580 (color_type == PNG_COLOR_TYPE_RGB ||
581 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
582 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600583#endif
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600584 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500585 {
586 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500587 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500588 }
589
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600590#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500591 if (interlace_type != PNG_INTERLACE_NONE &&
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500592 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500593 {
594 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500595 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500596 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600597#else
598 interlace_type=PNG_INTERLACE_NONE;
599#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500600
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500601 /* Save the relevent information */
Guy Schalnate5a37791996-06-05 15:50:50 -0500602 png_ptr->bit_depth = (png_byte)bit_depth;
603 png_ptr->color_type = (png_byte)color_type;
604 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500605#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600606 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500607#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500608 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500609 png_ptr->width = width;
610 png_ptr->height = height;
611
612 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500613 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500614 /* Set the usr info, so any transformations can modify it */
Guy Schalnate5a37791996-06-05 15:50:50 -0500615 png_ptr->usr_width = png_ptr->width;
616 png_ptr->usr_bit_depth = png_ptr->bit_depth;
617 png_ptr->usr_channels = png_ptr->channels;
618
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500619 /* Pack the header information into the buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500620 png_save_uint_32(buf, width);
621 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600622 buf[8] = (png_byte)bit_depth;
623 buf[9] = (png_byte)color_type;
624 buf[10] = (png_byte)compression_type;
625 buf[11] = (png_byte)filter_type;
626 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500627
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500628 /* Write the chunk */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500629 png_write_chunk(png_ptr, png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500630
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500631 /* Initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600632 png_ptr->zstream.zalloc = png_zalloc;
633 png_ptr->zstream.zfree = png_zfree;
634 png_ptr->zstream.opaque = (voidpf)png_ptr;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500635
Guy Schalnate5a37791996-06-05 15:50:50 -0500636 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500637 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500638 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600639 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500640 png_ptr->do_filter = PNG_FILTER_NONE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500641
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500642 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500643 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500644 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500645
Guy Schalnate5a37791996-06-05 15:50:50 -0500646 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500647 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500648 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500649 png_ptr->zlib_strategy = Z_FILTERED;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500650
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500651 else
652 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
653 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500654
Guy Schalnate5a37791996-06-05 15:50:50 -0500655 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500656 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500657
Guy Schalnate5a37791996-06-05 15:50:50 -0500658 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500659 png_ptr->zlib_mem_level = 8;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500660
Guy Schalnate5a37791996-06-05 15:50:50 -0500661 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500662 png_ptr->zlib_window_bits = 15;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500663
Guy Schalnate5a37791996-06-05 15:50:50 -0500664 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500665 png_ptr->zlib_method = 8;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500666
Glenn Randers-Pehrson205483d2011-04-01 12:33:42 -0500667#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500668#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION
669 if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_STRATEGY))
670 png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY;
671
672 if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_LEVEL))
Glenn Randers-Pehrsonc6831002011-04-01 15:24:18 -0500673 png_ptr->zlib_text_level = png_ptr->zlib_level;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500674
675 if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_MEM_LEVEL))
Glenn Randers-Pehrsonc6831002011-04-01 15:24:18 -0500676 png_ptr->zlib_text_mem_level = png_ptr->zlib_mem_level;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500677
678 if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrsonc6831002011-04-01 15:24:18 -0500679 png_ptr->zlib_text_window_bits = png_ptr->zlib_window_bits;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500680
681 if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_METHOD))
Glenn Randers-Pehrsonc6831002011-04-01 15:24:18 -0500682 png_ptr->zlib_text_method = png_ptr->zlib_method;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500683#else
684 png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY;
Glenn Randers-Pehrsonc6831002011-04-01 15:24:18 -0500685 png_ptr->zlib_text_level = png_ptr->zlib_level;
686 png_ptr->zlib_text_mem_level = png_ptr->zlib_mem_level;
687 png_ptr->zlib_text_window_bits = png_ptr->zlib_window_bits;
688 png_ptr->zlib_text_method = png_ptr->zlib_method;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500689#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION */
Glenn Randers-Pehrson205483d2011-04-01 12:33:42 -0500690#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500691
692 /* Initialize the zlib compressor */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500693 ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600694 png_ptr->zlib_method, png_ptr->zlib_window_bits,
695 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500696
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500697 png_ptr->mode = PNG_HAVE_IHDR; /* not READY_FOR_ZTXT */
Guy Schalnat0d580581995-07-20 02:43:20 -0500698}
699
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500700/* Write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500701 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600702 * structure.
703 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500704void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500705png_write_PLTE(png_structp png_ptr, png_const_colorp palette,
706 png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500707{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600708 PNG_PLTE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500709 png_uint_32 i;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500710 png_const_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500711 png_byte buf[3];
712
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500713 png_debug(1, "in png_write_PLTE");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500714
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500715 if ((
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500716#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600717 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500718#endif
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600719 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500720 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600721 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
722 {
723 png_error(png_ptr, "Invalid number of colors in palette");
724 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500725
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600726 else
727 {
728 png_warning(png_ptr, "Invalid number of colors in palette");
729 return;
730 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500731 }
732
733 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
734 {
735 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600736 "Ignoring request to write a PLTE chunk in grayscale PNG");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500737
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500738 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500739 }
740
Andreas Dilger47a0c421997-05-16 02:46:07 -0500741 png_ptr->num_palette = (png_uint_16)num_pal;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500742 png_debug1(3, "num_palette = %d", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500743
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500744 png_write_chunk_start(png_ptr, png_PLTE, (png_uint_32)(num_pal * 3));
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500745#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500746
Andreas Dilger47a0c421997-05-16 02:46:07 -0500747 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500748 {
749 buf[0] = pal_ptr->red;
750 buf[1] = pal_ptr->green;
751 buf[2] = pal_ptr->blue;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500752 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500753 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500754
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500755#else
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600756 /* This is a little slower but some buggy compilers need to do this
757 * instead
758 */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500759 pal_ptr=palette;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500760
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500761 for (i = 0; i < num_pal; i++)
762 {
763 buf[0] = pal_ptr[i].red;
764 buf[1] = pal_ptr[i].green;
765 buf[2] = pal_ptr[i].blue;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500766 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500767 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500768
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500769#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500770 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500771 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500772}
773
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500774/* Write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500775void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500776png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500777{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600778 PNG_IDAT;
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500779
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500780 png_debug(1, "in png_write_IDAT");
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500781
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500782 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
783 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
784 {
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500785 unsigned int z_cmf; /* zlib compression method and flags */
786
Glenn Randers-Pehrson205483d2011-04-01 12:33:42 -0500787#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
788 int ret;
Glenn Randers-Pehrsonec8296a2011-04-01 15:09:05 -0500789
790 /* Reinitialize the compressor if necessary */
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500791 if (png_ptr->mode & PNG_ZLIB_READY_FOR_ZTXT)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500792 {
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500793 /* Free memory from previously opened zstream */
794 deflateEnd(&png_ptr->zstream);
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500795
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500796 ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
797 png_ptr->zlib_method, png_ptr->zlib_window_bits,
798 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500799
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500800 if (ret != Z_OK)
801 {
802 if (ret == Z_VERSION_ERROR)
803 png_error(png_ptr,
804 "zlib failed to initialize compressor -- version error");
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500805
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500806 if (ret == Z_STREAM_ERROR)
807 png_error(png_ptr,
808 "zlib failed to initialize compressor -- stream error");
809
810 if (ret == Z_MEM_ERROR)
811 png_error(png_ptr,
812 "zlib failed to initialize compressor -- mem error");
813
814 png_error(png_ptr, "zlib failed to initialize compressor");
815 }
816 png_ptr->mode &= ~PNG_ZLIB_READY_FOR_ZTXT; /* Ready for IDAT */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500817 }
Glenn Randers-Pehrson205483d2011-04-01 12:33:42 -0500818#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500819
820 png_ptr->zstream.next_out = png_ptr->zbuf;
821 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
822 /* libpng is not interested in zstream.data_type, so set it
823 * to a predefined value, to avoid its evaluation inside zlib
824 */
825 png_ptr->zstream.data_type = Z_BINARY;
826
Glenn Randers-Pehrsonec8296a2011-04-01 15:09:05 -0500827 /* Optimize the CMF field in the zlib stream. This hack of the zlib
828 * stream is compliant to the stream specification.
829 */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500830 z_cmf = data[0];
831
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500832 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
833 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500834 /* Avoid memory underflows and multiplication overflows.
835 *
836 * The conditions below are practically always satisfied;
837 * however, they still must be checked.
838 */
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500839 if (length >= 2 &&
840 png_ptr->height < 16384 && png_ptr->width < 16384)
841 {
Glenn Randers-Pehrsonec8296a2011-04-01 15:09:05 -0500842 /* Compute the maximum possible length of the datastream
843 *
844 * To do: verify that this works with interlaced files
845 */
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500846 png_uint_32 uncompressed_idat_size = png_ptr->height *
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600847 ((png_ptr->width *
848 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500849 unsigned int z_cinfo = z_cmf >> 4;
850 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
851 while (uncompressed_idat_size <= half_z_window_size &&
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600852 half_z_window_size >= 256)
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500853 {
854 z_cinfo--;
855 half_z_window_size >>= 1;
856 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500857
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500858 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500859
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500860 if (data[0] != z_cmf)
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500861 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500862 int tmp;
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500863 data[0] = (png_byte)z_cmf;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500864 tmp = data[1] & 0xe0;
865 tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
866 data[1] = (png_byte)tmp;
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500867 }
868 }
869 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500870
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500871 else
872 png_error(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600873 "Invalid zlib compression method or flags in IDAT");
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500874 }
875
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500876 png_write_chunk(png_ptr, png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500877 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500878}
879
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500880/* Write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500881void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600882png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500883{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600884 PNG_IEND;
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500885
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500886 png_debug(1, "in png_write_IEND");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500887
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500888 png_write_chunk(png_ptr, png_IEND, NULL, (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600889 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500890}
891
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500892#ifdef PNG_WRITE_gAMA_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500893/* Write a gAMA chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500894void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600895png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600896{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600897 PNG_gAMA;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600898 png_byte buf[4];
899
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500900 png_debug(1, "in png_write_gAMA");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500901
Glenn Randers-Pehrson626afd62009-08-20 22:52:51 -0500902 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500903 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500904 png_write_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600905}
906#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500907
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500908#ifdef PNG_WRITE_sRGB_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500909/* Write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500910void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600911png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600912{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600913 PNG_sRGB;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600914 png_byte buf[1];
915
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500916 png_debug(1, "in png_write_sRGB");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500917
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500918 if (srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600919 png_warning(png_ptr,
920 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500921
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600922 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500923 png_write_chunk(png_ptr, png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600924}
925#endif
926
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500927#ifdef PNG_WRITE_iCCP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500928/* Write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500929void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500930png_write_iCCP(png_structp png_ptr, png_const_charp name, int compression_type,
931 png_const_charp profile, int profile_len)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600932{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600933 PNG_iCCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600934 png_size_t name_len;
935 png_charp new_name;
936 compression_state comp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500937 int embedded_profile_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600938
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500939 png_debug(1, "in png_write_iCCP");
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600940
941 comp.num_output_ptr = 0;
942 comp.max_output_ptr = 0;
943 comp.output_ptr = NULL;
944 comp.input = NULL;
945 comp.input_len = 0;
946
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500947 if ((name_len = png_check_keyword(png_ptr, name, &new_name)) == 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600948 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600949
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600950 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600951 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600952
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500953 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600954 profile_len = 0;
955
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500956 if (profile_len > 3)
Glenn Randers-Pehrson7edd4582006-12-07 19:16:44 -0600957 embedded_profile_len =
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500958 ((*( (png_const_bytep)profile ))<<24) |
959 ((*( (png_const_bytep)profile + 1))<<16) |
960 ((*( (png_const_bytep)profile + 2))<< 8) |
961 ((*( (png_const_bytep)profile + 3)) );
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500962
Glenn Randers-Pehrson3a054e12009-08-01 08:53:23 -0500963 if (embedded_profile_len < 0)
964 {
965 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600966 "Embedded profile length in iCCP chunk is negative");
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500967
Glenn Randers-Pehrson3a054e12009-08-01 08:53:23 -0500968 png_free(png_ptr, new_name);
969 return;
970 }
971
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500972 if (profile_len < embedded_profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500973 {
974 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600975 "Embedded profile length too large in iCCP chunk");
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500976
Glenn Randers-Pehrsona6cc6272009-04-01 14:18:43 -0500977 png_free(png_ptr, new_name);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500978 return;
979 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500980
981 if (profile_len > embedded_profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500982 {
983 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600984 "Truncating profile to actual length in iCCP chunk");
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500985
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500986 profile_len = embedded_profile_len;
987 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500988
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600989 if (profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500990 profile_len = png_text_compress(png_ptr, profile,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600991 (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600992
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500993 /* Make sure we include the NULL after the name and the compression type */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500994 png_write_chunk_start(png_ptr, png_iCCP,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600995 (png_uint_32)(name_len + profile_len + 2));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500996
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500997 new_name[name_len + 1] = 0x00;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500998
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500999 png_write_chunk_data(png_ptr, (png_bytep)new_name,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001000 (png_size_t)(name_len + 2));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001001
1002 if (profile_len)
1003 png_write_compressed_data_out(png_ptr, &comp);
1004
1005 png_write_chunk_end(png_ptr);
1006 png_free(png_ptr, new_name);
1007}
1008#endif
1009
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001010#ifdef PNG_WRITE_sPLT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001011/* Write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001012void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001013png_write_sPLT(png_structp png_ptr, png_const_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001014{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001015 PNG_sPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001016 png_size_t name_len;
1017 png_charp new_name;
1018 png_byte entrybuf[10];
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05001019 png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
1020 png_size_t palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001021 png_sPLT_entryp ep;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001022#ifndef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001023 int i;
1024#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001025
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001026 png_debug(1, "in png_write_sPLT");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001027
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001028 if ((name_len = png_check_keyword(png_ptr,spalette->name, &new_name))==0)
1029 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001030
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001031 /* Make sure we include the NULL after the name */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001032 png_write_chunk_start(png_ptr, png_sPLT,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001033 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001034
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001035 png_write_chunk_data(png_ptr, (png_bytep)new_name,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001036 (png_size_t)(name_len + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001037
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001038 png_write_chunk_data(png_ptr, &spalette->depth, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001039
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001040 /* Loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001041#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001042 for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001043 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001044 if (spalette->depth == 8)
1045 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001046 entrybuf[0] = (png_byte)ep->red;
1047 entrybuf[1] = (png_byte)ep->green;
1048 entrybuf[2] = (png_byte)ep->blue;
1049 entrybuf[3] = (png_byte)ep->alpha;
1050 png_save_uint_16(entrybuf + 4, ep->frequency);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001051 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001052
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001053 else
1054 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001055 png_save_uint_16(entrybuf + 0, ep->red);
1056 png_save_uint_16(entrybuf + 2, ep->green);
1057 png_save_uint_16(entrybuf + 4, ep->blue);
1058 png_save_uint_16(entrybuf + 6, ep->alpha);
1059 png_save_uint_16(entrybuf + 8, ep->frequency);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001060 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001061
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001062 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001063 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001064#else
1065 ep=spalette->entries;
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001066 for (i = 0; i>spalette->nentries; i++)
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001067 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001068 if (spalette->depth == 8)
1069 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001070 entrybuf[0] = (png_byte)ep[i].red;
1071 entrybuf[1] = (png_byte)ep[i].green;
1072 entrybuf[2] = (png_byte)ep[i].blue;
1073 entrybuf[3] = (png_byte)ep[i].alpha;
1074 png_save_uint_16(entrybuf + 4, ep[i].frequency);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001075 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001076
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001077 else
1078 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001079 png_save_uint_16(entrybuf + 0, ep[i].red);
1080 png_save_uint_16(entrybuf + 2, ep[i].green);
1081 png_save_uint_16(entrybuf + 4, ep[i].blue);
1082 png_save_uint_16(entrybuf + 6, ep[i].alpha);
1083 png_save_uint_16(entrybuf + 8, ep[i].frequency);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001084 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001085
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001086 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001087 }
1088#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001089
1090 png_write_chunk_end(png_ptr);
1091 png_free(png_ptr, new_name);
1092}
1093#endif
1094
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001095#ifdef PNG_WRITE_sBIT_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001096/* Write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001097void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001098png_write_sBIT(png_structp png_ptr, png_const_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001099{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001100 PNG_sBIT;
Guy Schalnat0d580581995-07-20 02:43:20 -05001101 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001102 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -05001103
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001104 png_debug(1, "in png_write_sBIT");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001105
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001106 /* Make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001107 if (color_type & PNG_COLOR_MASK_COLOR)
1108 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001109 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -05001110
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001111 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001112 png_ptr->usr_bit_depth);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001113
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001114 if (sbit->red == 0 || sbit->red > maxbits ||
1115 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -05001116 sbit->blue == 0 || sbit->blue > maxbits)
1117 {
1118 png_warning(png_ptr, "Invalid sBIT depth specified");
1119 return;
1120 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001121
Guy Schalnat0d580581995-07-20 02:43:20 -05001122 buf[0] = sbit->red;
1123 buf[1] = sbit->green;
1124 buf[2] = sbit->blue;
1125 size = 3;
1126 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001127
Guy Schalnat0d580581995-07-20 02:43:20 -05001128 else
1129 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001130 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
1131 {
1132 png_warning(png_ptr, "Invalid sBIT depth specified");
1133 return;
1134 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001135
Guy Schalnat0d580581995-07-20 02:43:20 -05001136 buf[0] = sbit->gray;
1137 size = 1;
1138 }
1139
1140 if (color_type & PNG_COLOR_MASK_ALPHA)
1141 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001142 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
1143 {
1144 png_warning(png_ptr, "Invalid sBIT depth specified");
1145 return;
1146 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001147
Guy Schalnat0d580581995-07-20 02:43:20 -05001148 buf[size++] = sbit->alpha;
1149 }
1150
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001151 png_write_chunk(png_ptr, png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -05001152}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001153#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001154
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001155#ifdef PNG_WRITE_cHRM_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001156/* Write the cHRM chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001157void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001158png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001159 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
1160 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
1161 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001162{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001163 PNG_cHRM;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001164 png_byte buf[32];
1165
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001166 png_debug(1, "in png_write_cHRM");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001167
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001168 /* Each value is saved in 1/100,000ths */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001169#ifdef PNG_CHECK_cHRM_SUPPORTED
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001170 if (png_check_cHRM_fixed(png_ptr, white_x, white_y, red_x, red_y,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001171 green_x, green_y, blue_x, blue_y))
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001172#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001173 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001174 png_save_uint_32(buf, (png_uint_32)white_x);
1175 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001176
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001177 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1178 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001179
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001180 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1181 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001182
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001183 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1184 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001185
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001186 png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001187 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001188}
1189#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001190
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001191#ifdef PNG_WRITE_tRNS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001192/* Write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001193void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001194png_write_tRNS(png_structp png_ptr, png_const_bytep trans_alpha,
1195 png_const_color_16p tran, int num_trans, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001196{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001197 PNG_tRNS;
Guy Schalnat0d580581995-07-20 02:43:20 -05001198 png_byte buf[6];
1199
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001200 png_debug(1, "in png_write_tRNS");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001201
Guy Schalnat0d580581995-07-20 02:43:20 -05001202 if (color_type == PNG_COLOR_TYPE_PALETTE)
1203 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001204 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001205 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001206 png_warning(png_ptr, "Invalid number of transparent colors specified");
Guy Schalnate5a37791996-06-05 15:50:50 -05001207 return;
1208 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001209
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001210 /* Write the chunk out as it is */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001211 png_write_chunk(png_ptr, png_tRNS, trans_alpha, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001212 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001213
Guy Schalnat0d580581995-07-20 02:43:20 -05001214 else if (color_type == PNG_COLOR_TYPE_GRAY)
1215 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001216 /* One 16 bit value */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001217 if (tran->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001218 {
1219 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001220 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001221
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001222 return;
1223 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001224
Guy Schalnat0d580581995-07-20 02:43:20 -05001225 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001226 png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001227 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001228
Guy Schalnat0d580581995-07-20 02:43:20 -05001229 else if (color_type == PNG_COLOR_TYPE_RGB)
1230 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001231 /* Three 16 bit values */
Guy Schalnat0d580581995-07-20 02:43:20 -05001232 png_save_uint_16(buf, tran->red);
1233 png_save_uint_16(buf + 2, tran->green);
1234 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -05001235#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001236 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -05001237#else
1238 if (buf[0] | buf[2] | buf[4])
1239#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001240 {
1241 png_warning(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001242 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001243 return;
1244 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001245
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001246 png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001247 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001248
Guy Schalnate5a37791996-06-05 15:50:50 -05001249 else
1250 {
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001251 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001252 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001253}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001254#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001255
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001256#ifdef PNG_WRITE_bKGD_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001257/* Write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001258void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001259png_write_bKGD(png_structp png_ptr, png_const_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001260{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001261 PNG_bKGD;
Guy Schalnat0d580581995-07-20 02:43:20 -05001262 png_byte buf[6];
1263
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001264 png_debug(1, "in png_write_bKGD");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001265
Guy Schalnat0d580581995-07-20 02:43:20 -05001266 if (color_type == PNG_COLOR_TYPE_PALETTE)
1267 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001268 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001269#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001270 (png_ptr->num_palette ||
1271 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001272#endif
Glenn Randers-Pehrsond6d80752008-12-02 09:49:43 -06001273 back->index >= png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001274 {
1275 png_warning(png_ptr, "Invalid background palette index");
1276 return;
1277 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001278
Guy Schalnat0d580581995-07-20 02:43:20 -05001279 buf[0] = back->index;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001280 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001281 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001282
Guy Schalnat0d580581995-07-20 02:43:20 -05001283 else if (color_type & PNG_COLOR_MASK_COLOR)
1284 {
1285 png_save_uint_16(buf, back->red);
1286 png_save_uint_16(buf + 2, back->green);
1287 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -05001288#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001289 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -05001290#else
1291 if (buf[0] | buf[2] | buf[4])
1292#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001293 {
1294 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001295 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001296
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001297 return;
1298 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001299
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001300 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001301 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001302
Guy Schalnat0d580581995-07-20 02:43:20 -05001303 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001304 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001305 if (back->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001306 {
1307 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001308 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001309
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001310 return;
1311 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001312
Guy Schalnat0d580581995-07-20 02:43:20 -05001313 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001314 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001315 }
1316}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001317#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001318
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001319#ifdef PNG_WRITE_hIST_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001320/* Write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001321void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001322png_write_hIST(png_structp png_ptr, png_const_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001323{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001324 PNG_hIST;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001325 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001326 png_byte buf[3];
1327
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001328 png_debug(1, "in png_write_hIST");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001329
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001330 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001331 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001332 png_debug2(3, "num_hist = %d, num_palette = %d", num_hist,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001333 png_ptr->num_palette);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001334
Guy Schalnate5a37791996-06-05 15:50:50 -05001335 png_warning(png_ptr, "Invalid number of histogram entries specified");
1336 return;
1337 }
1338
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001339 png_write_chunk_start(png_ptr, png_hIST, (png_uint_32)(num_hist * 2));
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001340
Andreas Dilger47a0c421997-05-16 02:46:07 -05001341 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001342 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001343 png_save_uint_16(buf, hist[i]);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001344 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001345 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001346
Guy Schalnat0d580581995-07-20 02:43:20 -05001347 png_write_chunk_end(png_ptr);
1348}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001349#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001350
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001351#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1352 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001353/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1354 * and if invalid, correct the keyword rather than discarding the entire
1355 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1356 * length, forbids leading or trailing whitespace, multiple internal spaces,
1357 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001358 *
1359 * The new_key is allocated to hold the corrected keyword and must be freed
1360 * by the calling routine. This avoids problems with trying to write to
1361 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001362 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001363png_size_t /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001364png_check_keyword(png_structp png_ptr, png_const_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001365{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001366 png_size_t key_len;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001367 png_const_charp ikp;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001368 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001369 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001370 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001371
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001372 png_debug(1, "in png_check_keyword");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001373
Andreas Dilger47a0c421997-05-16 02:46:07 -05001374 *new_key = NULL;
1375
1376 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001377 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001378 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001379 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001380 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001381
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001382 png_debug1(2, "Keyword to be checked is '%s'", key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001383
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001384 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001385
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001386 if (*new_key == NULL)
1387 {
1388 png_warning(png_ptr, "Out of memory while procesing keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001389 return ((png_size_t)0);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001390 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001391
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001392 /* Replace non-printing characters with a blank and print a warning */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001393 for (ikp = key, dp = *new_key; *ikp != '\0'; ikp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001394 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001395 if ((png_byte)*ikp < 0x20 ||
1396 ((png_byte)*ikp > 0x7E && (png_byte)*ikp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001397 {
Glenn Randers-Pehrson6a9e4802010-02-19 09:47:43 -06001398#ifdef PNG_CONSOLE_IO_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001399 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001400
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001401 png_snprintf(msg, 40,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001402 "invalid keyword character 0x%02X", (png_byte)*ikp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001403 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001404#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001405 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001406#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001407 *dp = ' ';
1408 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001409
Andreas Dilger47a0c421997-05-16 02:46:07 -05001410 else
1411 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001412 *dp = *ikp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001413 }
1414 }
1415 *dp = '\0';
1416
1417 /* Remove any trailing white space. */
1418 kp = *new_key + key_len - 1;
1419 if (*kp == ' ')
1420 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001421 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001422
1423 while (*kp == ' ')
1424 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001425 *(kp--) = '\0';
1426 key_len--;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001427 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001428 }
1429
1430 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001431 kp = *new_key;
1432 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001433 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001434 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001435
1436 while (*kp == ' ')
1437 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001438 kp++;
1439 key_len--;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001440 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001441 }
1442
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001443 png_debug1(2, "Checking for multiple internal spaces in '%s'", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001444
Andreas Dilger47a0c421997-05-16 02:46:07 -05001445 /* Remove multiple internal spaces. */
1446 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001447 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001448 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001449 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001450 *(dp++) = *kp;
1451 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001452 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001453
Andreas Dilger47a0c421997-05-16 02:46:07 -05001454 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001455 {
1456 key_len--;
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001457 kwarn = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001458 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001459
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001460 else
1461 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001462 *(dp++) = *kp;
1463 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001464 }
1465 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001466 *dp = '\0';
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001467 if (kwarn)
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001468 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001469
1470 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001471 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001472 png_free(png_ptr, *new_key);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001473 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001474 }
1475
1476 if (key_len > 79)
1477 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001478 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001479 (*new_key)[79] = '\0';
Andreas Dilger47a0c421997-05-16 02:46:07 -05001480 key_len = 79;
1481 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001482
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001483 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001484}
1485#endif
1486
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001487#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001488/* Write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001489void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001490png_write_tEXt(png_structp png_ptr, png_const_charp key, png_const_charp text,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001491 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001492{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001493 PNG_tEXt;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001494 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001495 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001496
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001497 png_debug(1, "in png_write_tEXt");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001498
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001499 if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001500 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001501
Andreas Dilger47a0c421997-05-16 02:46:07 -05001502 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001503 text_len = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001504
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001505 else
1506 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001507
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001508 /* Make sure we include the 0 after the key */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001509 png_write_chunk_start(png_ptr, png_tEXt,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001510 (png_uint_32)(key_len + text_len + 1));
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001511 /*
1512 * We leave it to the application to meet PNG-1.0 requirements on the
1513 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1514 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001515 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001516 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001517 png_write_chunk_data(png_ptr, (png_bytep)new_key,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001518 (png_size_t)(key_len + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001519
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001520 if (text_len)
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001521 png_write_chunk_data(png_ptr, (png_const_bytep)text,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001522 (png_size_t)text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001523
Guy Schalnat0d580581995-07-20 02:43:20 -05001524 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001525 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001526}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001527#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001528
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001529#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001530/* Write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001531void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001532png_write_zTXt(png_structp png_ptr, png_const_charp key, png_const_charp text,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001533 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001534{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001535 PNG_zTXt;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001536 png_size_t key_len;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001537 png_byte buf;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001538 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001539 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001540
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001541 png_debug(1, "in png_write_zTXt");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001542
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001543 comp.num_output_ptr = 0;
1544 comp.max_output_ptr = 0;
1545 comp.output_ptr = NULL;
1546 comp.input = NULL;
1547 comp.input_len = 0;
1548
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001549 if ((key_len = png_check_keyword(png_ptr, key, &new_key)) == 0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001550 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001551 png_free(png_ptr, new_key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001552 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001553 }
1554
Andreas Dilger47a0c421997-05-16 02:46:07 -05001555 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1556 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001557 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001558 png_free(png_ptr, new_key);
1559 return;
1560 }
1561
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001562 text_len = png_strlen(text);
1563
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001564 /* Compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001565 text_len = png_text_compress(png_ptr, text, text_len, compression,
1566 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001567
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001568 /* Write start of chunk */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001569 png_write_chunk_start(png_ptr, png_zTXt,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001570 (png_uint_32)(key_len+text_len + 2));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001571
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001572 /* Write key */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001573 png_write_chunk_data(png_ptr, (png_bytep)new_key,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001574 (png_size_t)(key_len + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001575
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001576 png_free(png_ptr, new_key);
1577
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001578 buf = (png_byte)compression;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001579
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001580 /* Write compression */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001581 png_write_chunk_data(png_ptr, &buf, (png_size_t)1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001582
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001583 /* Write the compressed data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001584 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001585
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001586 /* Close the chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -05001587 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001588}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001589#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001590
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001591#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001592/* Write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001593void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001594png_write_iTXt(png_structp png_ptr, int compression, png_const_charp key,
1595 png_const_charp lang, png_const_charp lang_key, png_const_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001596{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001597 PNG_iTXt;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001598 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001599 png_charp new_lang;
1600 png_charp new_key = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001601 png_byte cbuf[2];
1602 compression_state comp;
1603
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001604 png_debug(1, "in png_write_iTXt");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001605
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001606 comp.num_output_ptr = 0;
1607 comp.max_output_ptr = 0;
1608 comp.output_ptr = NULL;
1609 comp.input = NULL;
1610
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001611 if ((key_len = png_check_keyword(png_ptr, key, &new_key)) == 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001612 return;
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001613
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001614 if ((lang_len = png_check_keyword(png_ptr, lang, &new_lang)) == 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001615 {
1616 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001617 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001618 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001619 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001620
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001621 if (lang_key == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001622 lang_key_len = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001623
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001624 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001625 lang_key_len = png_strlen(lang_key);
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001626
1627 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001628 text_len = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001629
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001630 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001631 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001632
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001633 /* Compute the compressed data; do it now for the length */
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001634 text_len = png_text_compress(png_ptr, text, text_len, compression - 2,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001635 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001636
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001637
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001638 /* Make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001639 * and the NULs after the key, lang, and lang_key parts
1640 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001641
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001642 png_write_chunk_start(png_ptr, png_iTXt, (png_uint_32)(
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001643 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1644 + key_len
1645 + lang_len
1646 + lang_key_len
1647 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001648
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001649 /* We leave it to the application to meet PNG-1.0 requirements on the
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001650 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1651 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1652 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1653 */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001654 png_write_chunk_data(png_ptr, (png_bytep)new_key, (png_size_t)(key_len + 1));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001655
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001656 /* Set the compression flag */
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001657 if (compression == PNG_ITXT_COMPRESSION_NONE ||
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001658 compression == PNG_TEXT_COMPRESSION_NONE)
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001659 cbuf[0] = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001660
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001661 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001662 cbuf[0] = 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001663
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001664 /* Set the compression method */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001665 cbuf[1] = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001666
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001667 png_write_chunk_data(png_ptr, cbuf, (png_size_t)2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001668
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001669 cbuf[0] = 0;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001670 png_write_chunk_data(png_ptr, (new_lang ? (png_const_bytep)new_lang : cbuf),
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001671 (png_size_t)(lang_len + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001672
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001673 png_write_chunk_data(png_ptr, (lang_key ? (png_const_bytep)lang_key : cbuf),
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001674 (png_size_t)(lang_key_len + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001675
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001676 png_write_compressed_data_out(png_ptr, &comp);
1677
1678 png_write_chunk_end(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001679
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001680 png_free(png_ptr, new_key);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001681 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001682}
1683#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001684
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001685#ifdef PNG_WRITE_oFFs_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001686/* Write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001687void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001688png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001689 int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001690{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001691 PNG_oFFs;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001692 png_byte buf[9];
1693
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001694 png_debug(1, "in png_write_oFFs");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001695
Andreas Dilger47a0c421997-05-16 02:46:07 -05001696 if (unit_type >= PNG_OFFSET_LAST)
1697 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1698
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001699 png_save_int_32(buf, x_offset);
1700 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001701 buf[8] = (png_byte)unit_type;
1702
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001703 png_write_chunk(png_ptr, png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001704}
1705#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001706#ifdef PNG_WRITE_pCAL_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001707/* Write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001708void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001709png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001710 png_int_32 X1, int type, int nparams, png_const_charp units,
1711 png_charpp params)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001712{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001713 PNG_pCAL;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001714 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001715 png_uint_32p params_len;
1716 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001717 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001718 int i;
1719
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001720 png_debug1(1, "in png_write_pCAL (%d parameters)", nparams);
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001721
Andreas Dilger47a0c421997-05-16 02:46:07 -05001722 if (type >= PNG_EQUATION_LAST)
1723 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1724
1725 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001726 png_debug1(3, "pCAL purpose length = %d", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001727 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001728 png_debug1(3, "pCAL units length = %d", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001729 total_len = purpose_len + units_len + 10;
1730
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001731 params_len = (png_uint_32p)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001732 (png_alloc_size_t)(nparams * png_sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001733
1734 /* Find the length of each parameter, making sure we don't count the
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001735 * null terminator for the last parameter.
1736 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001737 for (i = 0; i < nparams; i++)
1738 {
1739 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001740 png_debug2(3, "pCAL parameter %d length = %lu", i,
Glenn Randers-Pehrsond2332872010-10-12 19:19:28 -05001741 (unsigned long)params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001742 total_len += (png_size_t)params_len[i];
1743 }
1744
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001745 png_debug1(3, "pCAL total length = %d", (int)total_len);
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001746 png_write_chunk_start(png_ptr, png_pCAL, (png_uint_32)total_len);
1747 png_write_chunk_data(png_ptr, (png_const_bytep)new_purpose,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001748 (png_size_t)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001749 png_save_int_32(buf, X0);
1750 png_save_int_32(buf + 4, X1);
1751 buf[8] = (png_byte)type;
1752 buf[9] = (png_byte)nparams;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001753 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001754 png_write_chunk_data(png_ptr, (png_const_bytep)units, (png_size_t)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001755
1756 png_free(png_ptr, new_purpose);
1757
1758 for (i = 0; i < nparams; i++)
1759 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001760 png_write_chunk_data(png_ptr, (png_const_bytep)params[i],
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001761 (png_size_t)params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001762 }
1763
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001764 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001765 png_write_chunk_end(png_ptr);
1766}
1767#endif
1768
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001769#ifdef PNG_WRITE_sCAL_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001770/* Write the sCAL chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001771void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001772png_write_sCAL_s(png_structp png_ptr, int unit, png_const_charp width,
1773 png_const_charp height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001774{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001775 PNG_sCAL;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001776 png_byte buf[64];
1777 png_size_t wlen, hlen, total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001778
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001779 png_debug(1, "in png_write_sCAL_s");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001780
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001781 wlen = png_strlen(width);
1782 hlen = png_strlen(height);
1783 total_len = wlen + hlen + 2;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001784
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001785 if (total_len > 64)
1786 {
1787 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
1788 return;
1789 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001790
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001791 buf[0] = (png_byte)unit;
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001792 png_memcpy(buf + 1, width, wlen + 1); /* Append the '\0' here */
1793 png_memcpy(buf + wlen + 2, height, hlen); /* Do NOT append the '\0' here */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001794
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001795 png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001796 png_write_chunk(png_ptr, png_sCAL, buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001797}
1798#endif
1799
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001800#ifdef PNG_WRITE_pHYs_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001801/* Write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001802void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001803png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001804 png_uint_32 y_pixels_per_unit,
1805 int unit_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001806{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001807 PNG_pHYs;
Guy Schalnat0d580581995-07-20 02:43:20 -05001808 png_byte buf[9];
1809
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001810 png_debug(1, "in png_write_pHYs");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001811
Guy Schalnate5a37791996-06-05 15:50:50 -05001812 if (unit_type >= PNG_RESOLUTION_LAST)
1813 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1814
Guy Schalnat0d580581995-07-20 02:43:20 -05001815 png_save_uint_32(buf, x_pixels_per_unit);
1816 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001817 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001818
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001819 png_write_chunk(png_ptr, png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001820}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001821#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001822
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001823#ifdef PNG_WRITE_tIME_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001824/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1825 * or png_convert_from_time_t(), or fill in the structure yourself.
1826 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001827void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001828png_write_tIME(png_structp png_ptr, png_const_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001829{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001830 PNG_tIME;
Guy Schalnat0d580581995-07-20 02:43:20 -05001831 png_byte buf[7];
1832
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001833 png_debug(1, "in png_write_tIME");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001834
Guy Schalnate5a37791996-06-05 15:50:50 -05001835 if (mod_time->month > 12 || mod_time->month < 1 ||
1836 mod_time->day > 31 || mod_time->day < 1 ||
1837 mod_time->hour > 23 || mod_time->second > 60)
1838 {
1839 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1840 return;
1841 }
1842
Guy Schalnat0d580581995-07-20 02:43:20 -05001843 png_save_uint_16(buf, mod_time->year);
1844 buf[2] = mod_time->month;
1845 buf[3] = mod_time->day;
1846 buf[4] = mod_time->hour;
1847 buf[5] = mod_time->minute;
1848 buf[6] = mod_time->second;
1849
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001850 png_write_chunk(png_ptr, png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001851}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001852#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001853
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001854/* Initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001855void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001856png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001857{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001858#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001859 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001860
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001861 /* Start of interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001862 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001863
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001864 /* Offset to next interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001865 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001866
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001867 /* Start of interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001868 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001869
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001870 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001871 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001872#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001873
Andreas Dilger47a0c421997-05-16 02:46:07 -05001874 png_size_t buf_size;
1875
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001876 png_debug(1, "in png_write_start_row");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001877
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001878 buf_size = (png_size_t)(PNG_ROWBYTES(
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001879 png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001880
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001881 /* Set up row buffer */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001882 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001883 (png_alloc_size_t)buf_size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001884
Andreas Dilger47a0c421997-05-16 02:46:07 -05001885 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001886
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001887#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001888 /* Set up filtering buffer, if using this filter */
Guy Schalnate5a37791996-06-05 15:50:50 -05001889 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001890 {
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05001891 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, png_ptr->rowbytes + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001892
Andreas Dilger47a0c421997-05-16 02:46:07 -05001893 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001894 }
1895
Andreas Dilger47a0c421997-05-16 02:46:07 -05001896 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001897 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1898 {
Glenn Randers-Pehrsondfa99af2009-10-29 23:33:46 -05001899 /* Set up previous row buffer */
1900 png_ptr->prev_row = (png_bytep)png_calloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001901 (png_alloc_size_t)buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001902
1903 if (png_ptr->do_filter & PNG_FILTER_UP)
1904 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001905 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05001906 png_ptr->rowbytes + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001907
Andreas Dilger47a0c421997-05-16 02:46:07 -05001908 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001909 }
1910
1911 if (png_ptr->do_filter & PNG_FILTER_AVG)
1912 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001913 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05001914 png_ptr->rowbytes + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001915
Andreas Dilger47a0c421997-05-16 02:46:07 -05001916 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001917 }
1918
1919 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1920 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001921 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05001922 png_ptr->rowbytes + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001923
Andreas Dilger47a0c421997-05-16 02:46:07 -05001924 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001925 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001926 }
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001927#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05001928
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001929#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001930 /* If interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001931 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001932 {
1933 if (!(png_ptr->transformations & PNG_INTERLACE))
1934 {
1935 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001936 png_pass_ystart[0]) / png_pass_yinc[0];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001937
Andreas Dilger47a0c421997-05-16 02:46:07 -05001938 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001939 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001940 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001941
Guy Schalnat0d580581995-07-20 02:43:20 -05001942 else
1943 {
1944 png_ptr->num_rows = png_ptr->height;
1945 png_ptr->usr_width = png_ptr->width;
1946 }
1947 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001948
Guy Schalnat0d580581995-07-20 02:43:20 -05001949 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001950#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001951 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001952 png_ptr->num_rows = png_ptr->height;
1953 png_ptr->usr_width = png_ptr->width;
1954 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001955
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001956 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1957 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001958}
1959
Andreas Dilger47a0c421997-05-16 02:46:07 -05001960/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001961void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001962png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001963{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001964#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001965 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001966
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001967 /* Start of interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001968 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001969
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001970 /* Offset to next interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001971 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001972
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001973 /* Start of interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001974 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001975
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001976 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001977 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001978#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001979
Guy Schalnat0d580581995-07-20 02:43:20 -05001980 int ret;
1981
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001982 png_debug(1, "in png_write_finish_row");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001983
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001984 /* Next row */
Guy Schalnat0d580581995-07-20 02:43:20 -05001985 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001986
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001987 /* See if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001988 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001989 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001990
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001991#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001992 /* If interlaced, go to next pass */
Guy Schalnat0d580581995-07-20 02:43:20 -05001993 if (png_ptr->interlaced)
1994 {
1995 png_ptr->row_number = 0;
1996 if (png_ptr->transformations & PNG_INTERLACE)
1997 {
1998 png_ptr->pass++;
1999 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002000
Guy Schalnat0d580581995-07-20 02:43:20 -05002001 else
2002 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002003 /* Loop until we find a non-zero width or height pass */
Guy Schalnat0d580581995-07-20 02:43:20 -05002004 do
2005 {
2006 png_ptr->pass++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002007
Guy Schalnat0d580581995-07-20 02:43:20 -05002008 if (png_ptr->pass >= 7)
2009 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002010
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002011 png_ptr->usr_width = (png_ptr->width +
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002012 png_pass_inc[png_ptr->pass] - 1 -
2013 png_pass_start[png_ptr->pass]) /
2014 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002015
Guy Schalnat0d580581995-07-20 02:43:20 -05002016 png_ptr->num_rows = (png_ptr->height +
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002017 png_pass_yinc[png_ptr->pass] - 1 -
2018 png_pass_ystart[png_ptr->pass]) /
2019 png_pass_yinc[png_ptr->pass];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002020
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002021 if (png_ptr->transformations & PNG_INTERLACE)
2022 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002023
Guy Schalnat0d580581995-07-20 02:43:20 -05002024 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
2025
2026 }
2027
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002028 /* Reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002029 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002030 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002031 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002032 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002033 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
2034 png_ptr->usr_bit_depth, png_ptr->width)) + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002035
Guy Schalnat0d580581995-07-20 02:43:20 -05002036 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002037 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002038 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002039#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002040
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002041 /* If we get here, we've just written the last row, so we need
Guy Schalnat0d580581995-07-20 02:43:20 -05002042 to flush the compressor */
2043 do
2044 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002045 /* Tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002046 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002047
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002048 /* Check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05002049 if (ret == Z_OK)
2050 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002051 /* Check to see if we need more room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05002052 if (!(png_ptr->zstream.avail_out))
2053 {
2054 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
2055 png_ptr->zstream.next_out = png_ptr->zbuf;
2056 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
2057 }
2058 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002059
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05002060 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05002061 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002062 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002063 png_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002064
Guy Schalnat0d580581995-07-20 02:43:20 -05002065 else
Guy Schalnat6d764711995-12-19 03:22:19 -06002066 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05002067 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002068 } while (ret != Z_STREAM_END);
2069
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002070 /* Write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002071 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05002072 {
2073 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002074 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05002075 }
2076
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002077 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -06002078 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat0d580581995-07-20 02:43:20 -05002079}
2080
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002081#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002082/* Pick out the correct pixels for the interlace pass.
2083 * The basic idea here is to go through the row with a source
2084 * pointer and a destination pointer (sp and dp), and copy the
2085 * correct pixels for the pass. As the row gets compacted,
2086 * sp will always be >= dp, so we should never overwrite anything.
2087 * See the default: case for the easiest code to understand.
2088 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002089void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06002090png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05002091{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002092 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002093
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002094 /* Start of interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06002095 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002096
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002097 /* Offset to next interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06002098 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002099
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002100 png_debug(1, "in png_do_write_interlace");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05002101
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002102 /* We don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002103 if (pass < 6)
Guy Schalnat0d580581995-07-20 02:43:20 -05002104 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002105 /* Each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05002106 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002107 {
Guy Schalnat0d580581995-07-20 02:43:20 -05002108 case 1:
2109 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002110 png_bytep sp;
2111 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05002112 int shift;
2113 int d;
2114 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002115 png_uint_32 i;
2116 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002117
2118 dp = row;
2119 d = 0;
2120 shift = 7;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002121
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002122 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002123 i += png_pass_inc[pass])
2124 {
2125 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002126 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05002127 d |= (value << shift);
2128
2129 if (shift == 0)
2130 {
2131 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002132 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002133 d = 0;
2134 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002135
Guy Schalnat0d580581995-07-20 02:43:20 -05002136 else
2137 shift--;
2138
2139 }
2140 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002141 *dp = (png_byte)d;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002142
Guy Schalnat0d580581995-07-20 02:43:20 -05002143 break;
2144 }
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002145
Guy Schalnat0d580581995-07-20 02:43:20 -05002146 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002147 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002148 png_bytep sp;
2149 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05002150 int shift;
2151 int d;
2152 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002153 png_uint_32 i;
2154 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002155
2156 dp = row;
2157 shift = 6;
2158 d = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002159
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002160 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002161 i += png_pass_inc[pass])
2162 {
2163 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002164 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05002165 d |= (value << shift);
2166
2167 if (shift == 0)
2168 {
2169 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002170 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002171 d = 0;
2172 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002173
Guy Schalnat0d580581995-07-20 02:43:20 -05002174 else
2175 shift -= 2;
2176 }
2177 if (shift != 6)
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002178 *dp = (png_byte)d;
2179
Guy Schalnat0d580581995-07-20 02:43:20 -05002180 break;
2181 }
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002182
Guy Schalnat0d580581995-07-20 02:43:20 -05002183 case 4:
2184 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002185 png_bytep sp;
2186 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002187 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05002188 int d;
2189 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002190 png_uint_32 i;
2191 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002192
2193 dp = row;
2194 shift = 4;
2195 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002196 for (i = png_pass_start[pass]; i < row_width;
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002197 i += png_pass_inc[pass])
Guy Schalnat0d580581995-07-20 02:43:20 -05002198 {
2199 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002200 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05002201 d |= (value << shift);
2202
2203 if (shift == 0)
2204 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002205 shift = 4;
2206 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002207 d = 0;
2208 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002209
Guy Schalnat0d580581995-07-20 02:43:20 -05002210 else
2211 shift -= 4;
2212 }
2213 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002214 *dp = (png_byte)d;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002215
Guy Schalnat0d580581995-07-20 02:43:20 -05002216 break;
2217 }
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002218
Guy Schalnat0d580581995-07-20 02:43:20 -05002219 default:
2220 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002221 png_bytep sp;
2222 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002223 png_uint_32 i;
2224 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002225 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002226
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002227 /* Start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002228 dp = row;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002229
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002230 /* Find out how many bytes each pixel takes up */
Guy Schalnat0d580581995-07-20 02:43:20 -05002231 pixel_bytes = (row_info->pixel_depth >> 3);
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002232
2233 /* Loop through the row, only looking at the pixels that matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002234 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002235 i += png_pass_inc[pass])
2236 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002237 /* Find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002238 sp = row + (png_size_t)i * pixel_bytes;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002239
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002240 /* Move the pixel */
Guy Schalnat0d580581995-07-20 02:43:20 -05002241 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002242 png_memcpy(dp, sp, pixel_bytes);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002243
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002244 /* Next pixel */
Guy Schalnat0d580581995-07-20 02:43:20 -05002245 dp += pixel_bytes;
2246 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002247 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002248 }
2249 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002250 /* Set new row width */
Guy Schalnat0d580581995-07-20 02:43:20 -05002251 row_info->width = (row_info->width +
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002252 png_pass_inc[pass] - 1 -
2253 png_pass_start[pass]) /
2254 png_pass_inc[pass];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002255
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002256 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2257 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002258 }
2259}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002260#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002261
Andreas Dilger47a0c421997-05-16 02:46:07 -05002262/* This filters the row, chooses which filter to use, if it has not already
2263 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002264 * chosen filter.
2265 */
Glenn Randers-Pehrson78067772004-11-02 21:49:39 -06002266#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002267#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002268#define PNG_LOMASK ((png_uint_32)0xffffL)
2269#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002270void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002271png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002272{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002273 png_bytep best_row;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002274#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002275 png_bytep prev_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002276 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002277 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002278 png_size_t row_bytes = row_info->rowbytes;
Glenn Randers-Pehrson9d8b41e2009-07-19 14:45:43 -05002279#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002280 int num_p_filters = (int)png_ptr->num_prev_filters;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -05002281#endif
Glenn Randers-Pehrson9d8b41e2009-07-19 14:45:43 -05002282
2283 png_debug(1, "in png_write_find_filter");
2284
2285#ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson4ace0e12009-07-19 15:04:35 -05002286 if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS)
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05002287 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002288 /* These will never be selected so we need not test them. */
2289 filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_PAETH);
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05002290 }
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -05002291#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002292
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002293 /* Find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002294 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002295
2296 prev_row = png_ptr->prev_row;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002297#endif
2298 best_row = png_ptr->row_buf;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002299#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002300 row_buf = best_row;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002301 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002302
Andreas Dilger47a0c421997-05-16 02:46:07 -05002303 /* The prediction method we use is to find which method provides the
2304 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002305 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002306 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002307 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002308 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002309 * predictive" method (not implemented yet), which does test compressions
2310 * of lines using different filter methods, and then chooses the
2311 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002312 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002313 *
2314 * GRR 980525: consider also
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002315 *
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002316 * (1) minimum sum of absolute differences from running average (i.e.,
2317 * keep running sum of non-absolute differences & count of bytes)
2318 * [track dispersion, too? restart average if dispersion too large?]
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002319 *
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002320 * (1b) minimum sum of absolute differences from sliding average, probably
2321 * with window size <= deflate window (usually 32K)
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002322 *
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002323 * (2) minimum sum of squared differences from zero or running average
2324 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002325 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002326
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002327
Guy Schalnate5a37791996-06-05 15:50:50 -05002328 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002329 * that has been chosen, as it doesn't actually do anything to the data.
2330 */
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002331 if ((filter_to_do & PNG_FILTER_NONE) && filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002332 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002333 png_bytep rp;
2334 png_uint_32 sum = 0;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002335 png_size_t i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002336 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002337
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002338 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002339 {
2340 v = *rp;
2341 sum += (v < 128) ? v : 256 - v;
2342 }
Andreas Dilger47a0c421997-05-16 02:46:07 -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 {
2347 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002348 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002349 sumlo = sum & PNG_LOMASK;
2350 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2351
2352 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002353 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002354 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002355 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002356 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002357 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002358 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002359
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002360 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002361 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002362 }
2363 }
2364
2365 /* Factor in the cost of this filter (this is here for completeness,
2366 * but it makes no sense to have a "cost" for the NONE filter, as
2367 * it has the minimum possible computational cost - none).
2368 */
2369 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002370 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002371
Andreas Dilger47a0c421997-05-16 02:46:07 -05002372 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002373 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002374
2375 if (sumhi > PNG_HIMASK)
2376 sum = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002377
Andreas Dilger47a0c421997-05-16 02:46:07 -05002378 else
2379 sum = (sumhi << PNG_HISHIFT) + sumlo;
2380 }
2381#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002382 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002383 }
2384
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002385 /* Sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002386 if (filter_to_do == PNG_FILTER_SUB)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002387 /* It's the only filter so no testing is needed */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002388 {
2389 png_bytep rp, lp, dp;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002390 png_size_t i;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002391
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002392 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2393 i++, rp++, dp++)
2394 {
2395 *dp = *rp;
2396 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002397
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002398 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002399 i++, rp++, lp++, dp++)
2400 {
2401 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2402 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002403
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002404 best_row = png_ptr->sub_row;
2405 }
2406
2407 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002408 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002409 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002410 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002411 png_size_t i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002412 int v;
2413
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002414#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002415 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002416 * would reduce the sum of this filter, so that we can do the
2417 * early exit comparison without scaling the sum each time.
2418 */
2419 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2420 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002421 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002422 png_uint_32 lmhi, lmlo;
2423 lmlo = lmins & PNG_LOMASK;
2424 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2425
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002426 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002427 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002428 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002429 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002430 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002431 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002432
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002433 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002434 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002435 }
2436 }
2437
2438 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002439 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002440
Andreas Dilger47a0c421997-05-16 02:46:07 -05002441 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002442 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002443
2444 if (lmhi > PNG_HIMASK)
2445 lmins = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002446
Andreas Dilger47a0c421997-05-16 02:46:07 -05002447 else
2448 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2449 }
2450#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002451
Guy Schalnate5a37791996-06-05 15:50:50 -05002452 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2453 i++, rp++, dp++)
2454 {
2455 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002456
Guy Schalnate5a37791996-06-05 15:50:50 -05002457 sum += (v < 128) ? v : 256 - v;
2458 }
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002459
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002460 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002461 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002462 {
2463 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002464
Guy Schalnate5a37791996-06-05 15:50:50 -05002465 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002466
2467 if (sum > lmins) /* We are already worse, don't continue. */
2468 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002469 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002470
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002471#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002472 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2473 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002474 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002475 png_uint_32 sumhi, sumlo;
2476 sumlo = sum & PNG_LOMASK;
2477 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2478
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002479 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002480 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002481 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002482 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002483 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002484 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002485
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002486 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002487 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002488 }
2489 }
2490
2491 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002492 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002493
Andreas Dilger47a0c421997-05-16 02:46:07 -05002494 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002495 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002496
2497 if (sumhi > PNG_HIMASK)
2498 sum = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002499
Andreas Dilger47a0c421997-05-16 02:46:07 -05002500 else
2501 sum = (sumhi << PNG_HISHIFT) + sumlo;
2502 }
2503#endif
2504
Guy Schalnate5a37791996-06-05 15:50:50 -05002505 if (sum < mins)
2506 {
2507 mins = sum;
2508 best_row = png_ptr->sub_row;
2509 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002510 }
2511
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002512 /* Up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002513 if (filter_to_do == PNG_FILTER_UP)
2514 {
2515 png_bytep rp, dp, pp;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002516 png_size_t i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002517
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002518 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002519 pp = prev_row + 1; i < row_bytes;
2520 i++, rp++, pp++, dp++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002521 {
2522 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2523 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002524
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002525 best_row = png_ptr->up_row;
2526 }
2527
2528 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002529 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002530 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002531 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002532 png_size_t i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002533 int v;
2534
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002535
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002536#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002537 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2538 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002539 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002540 png_uint_32 lmhi, lmlo;
2541 lmlo = lmins & PNG_LOMASK;
2542 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2543
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002544 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002545 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002546 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002547 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002548 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002549 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002550
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002551 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002552 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002553 }
2554 }
2555
2556 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002557 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002558
Andreas Dilger47a0c421997-05-16 02:46:07 -05002559 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002560 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002561
2562 if (lmhi > PNG_HIMASK)
2563 lmins = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002564
Andreas Dilger47a0c421997-05-16 02:46:07 -05002565 else
2566 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2567 }
2568#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002569
2570 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002571 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002572 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002573 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002574
2575 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002576
2577 if (sum > lmins) /* We are already worse, don't continue. */
2578 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002579 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002580
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002581#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002582 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2583 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002584 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002585 png_uint_32 sumhi, sumlo;
2586 sumlo = sum & PNG_LOMASK;
2587 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2588
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002589 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002590 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002591 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002592 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002593 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002594 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002595
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002596 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002597 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002598 }
2599 }
2600
2601 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002602 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002603
Andreas Dilger47a0c421997-05-16 02:46:07 -05002604 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002605 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002606
2607 if (sumhi > PNG_HIMASK)
2608 sum = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002609
Andreas Dilger47a0c421997-05-16 02:46:07 -05002610 else
2611 sum = (sumhi << PNG_HISHIFT) + sumlo;
2612 }
2613#endif
2614
Guy Schalnate5a37791996-06-05 15:50:50 -05002615 if (sum < mins)
2616 {
2617 mins = sum;
2618 best_row = png_ptr->up_row;
2619 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002620 }
2621
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002622 /* Avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002623 if (filter_to_do == PNG_FILTER_AVG)
2624 {
2625 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002626 png_uint_32 i;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002627
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002628 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002629 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002630 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002631 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002632 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002633
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002634 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002635 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002636 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2637 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002638 }
2639 best_row = png_ptr->avg_row;
2640 }
2641
2642 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002643 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002644 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002645 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002646 png_size_t i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002647 int v;
2648
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002649#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002650 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2651 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002652 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002653 png_uint_32 lmhi, lmlo;
2654 lmlo = lmins & PNG_LOMASK;
2655 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2656
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002657 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002658 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002659 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002660 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002661 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002662 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002663
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002664 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002665 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002666 }
2667 }
2668
2669 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002670 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002671
Andreas Dilger47a0c421997-05-16 02:46:07 -05002672 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002673 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002674
2675 if (lmhi > PNG_HIMASK)
2676 lmins = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002677
Andreas Dilger47a0c421997-05-16 02:46:07 -05002678 else
2679 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2680 }
2681#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002682
2683 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002684 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002685 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002686 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002687
2688 sum += (v < 128) ? v : 256 - v;
2689 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002690
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002691 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002692 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002693 v = *dp++ =
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002694 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002695
2696 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002697
2698 if (sum > lmins) /* We are already worse, don't continue. */
2699 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002700 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002701
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002702#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002703 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2704 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002705 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002706 png_uint_32 sumhi, sumlo;
2707 sumlo = sum & PNG_LOMASK;
2708 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2709
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002710 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002711 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002712 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002713 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002714 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002715 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002716
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002717 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002718 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002719 }
2720 }
2721
2722 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002723 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002724
Andreas Dilger47a0c421997-05-16 02:46:07 -05002725 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002726 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002727
2728 if (sumhi > PNG_HIMASK)
2729 sum = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002730
Andreas Dilger47a0c421997-05-16 02:46:07 -05002731 else
2732 sum = (sumhi << PNG_HISHIFT) + sumlo;
2733 }
2734#endif
2735
Guy Schalnate5a37791996-06-05 15:50:50 -05002736 if (sum < mins)
2737 {
2738 mins = sum;
2739 best_row = png_ptr->avg_row;
2740 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002741 }
2742
Andreas Dilger47a0c421997-05-16 02:46:07 -05002743 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002744 if (filter_to_do == PNG_FILTER_PAETH)
2745 {
2746 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002747 png_size_t i;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002748
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002749 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002750 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002751 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002752 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002753 }
2754
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002755 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002756 {
2757 int a, b, c, pa, pb, pc, p;
2758
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002759 b = *pp++;
2760 c = *cp++;
2761 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002762
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002763 p = b - c;
2764 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002765
2766#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002767 pa = abs(p);
2768 pb = abs(pc);
2769 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002770#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002771 pa = p < 0 ? -p : p;
2772 pb = pc < 0 ? -pc : pc;
2773 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002774#endif
2775
2776 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2777
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002778 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002779 }
2780 best_row = png_ptr->paeth_row;
2781 }
2782
2783 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002784 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002785 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002786 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002787 png_size_t i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002788 int v;
2789
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002790#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002791 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2792 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002793 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002794 png_uint_32 lmhi, lmlo;
2795 lmlo = lmins & PNG_LOMASK;
2796 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2797
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002798 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002799 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002800 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002801 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002802 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002803 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002804
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002805 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002806 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002807 }
2808 }
2809
2810 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002811 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002812
Andreas Dilger47a0c421997-05-16 02:46:07 -05002813 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002814 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002815
2816 if (lmhi > PNG_HIMASK)
2817 lmins = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002818
Andreas Dilger47a0c421997-05-16 02:46:07 -05002819 else
2820 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2821 }
2822#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002823
2824 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002825 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002826 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002827 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002828
2829 sum += (v < 128) ? v : 256 - v;
2830 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002831
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002832 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002833 {
2834 int a, b, c, pa, pb, pc, p;
2835
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002836 b = *pp++;
2837 c = *cp++;
2838 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002839
2840#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002841 p = b - c;
2842 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002843#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002844 pa = abs(p);
2845 pb = abs(pc);
2846 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002847#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002848 pa = p < 0 ? -p : p;
2849 pb = pc < 0 ? -pc : pc;
2850 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002851#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002852 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2853#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002854 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002855 pa = abs(p - a);
2856 pb = abs(p - b);
2857 pc = abs(p - c);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002858
Guy Schalnate5a37791996-06-05 15:50:50 -05002859 if (pa <= pb && pa <= pc)
2860 p = a;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002861
Guy Schalnate5a37791996-06-05 15:50:50 -05002862 else if (pb <= pc)
2863 p = b;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002864
Guy Schalnate5a37791996-06-05 15:50:50 -05002865 else
2866 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002867#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002868
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002869 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002870
2871 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002872
2873 if (sum > lmins) /* We are already worse, don't continue. */
2874 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002875 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002876
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002877#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002878 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2879 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002880 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002881 png_uint_32 sumhi, sumlo;
2882 sumlo = sum & PNG_LOMASK;
2883 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2884
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002885 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002886 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002887 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002888 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002889 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002890 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002891
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002892 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002893 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002894 }
2895 }
2896
2897 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002898 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002899
Andreas Dilger47a0c421997-05-16 02:46:07 -05002900 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002901 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002902
2903 if (sumhi > PNG_HIMASK)
2904 sum = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002905
Andreas Dilger47a0c421997-05-16 02:46:07 -05002906 else
2907 sum = (sumhi << PNG_HISHIFT) + sumlo;
2908 }
2909#endif
2910
Guy Schalnate5a37791996-06-05 15:50:50 -05002911 if (sum < mins)
2912 {
2913 best_row = png_ptr->paeth_row;
2914 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002915 }
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002916#endif /* PNG_WRITE_FILTER_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002917 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002918
Guy Schalnate5a37791996-06-05 15:50:50 -05002919 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002920
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002921#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002922#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002923 /* Save the type of filter we picked this time for future calculations */
2924 if (png_ptr->num_prev_filters > 0)
2925 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002926 int j;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002927
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002928 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002929 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002930 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002931 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002932
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002933 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002934 }
2935#endif
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002936#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -05002937}
2938
2939
Andreas Dilger47a0c421997-05-16 02:46:07 -05002940/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002941void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002942png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2943{
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002944 png_size_t avail;
2945
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002946 png_debug(1, "in png_write_filtered_row");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05002947
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002948 png_debug1(2, "filter = %d", filtered_row[0]);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002949 /* Set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002950
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002951 png_ptr->zstream.next_in = filtered_row;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002952 png_ptr->zstream.avail_in = 0;
2953 avail = png_ptr->row_info.rowbytes + 1;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002954 /* Repeat until we have compressed all the data */
Guy Schalnate5a37791996-06-05 15:50:50 -05002955 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002956 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002957 int ret; /* Return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002958
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002959 /* Record the number of bytes available - zlib supports at least 65535
2960 * bytes at one step, depending on the size of the zlib type 'uInt', the
2961 * maximum size zlib can write at once is ZLIB_IO_MAX (from pngpriv.h).
2962 * Use this because on 16 bit systems 'rowbytes' can be up to 65536 (i.e.
2963 * one more than 16 bits) and, in this case 'rowbytes+1' can overflow a
2964 * uInt. ZLIB_IO_MAX can be safely reduced to cause zlib to be called
2965 * with smaller chunks of data.
2966 */
2967 if (png_ptr->zstream.avail_in == 0)
2968 {
2969 if (avail > ZLIB_IO_MAX)
2970 {
2971 png_ptr->zstream.avail_in = ZLIB_IO_MAX;
2972 avail -= ZLIB_IO_MAX;
2973 }
2974
2975 else
2976 {
2977 /* So this will fit in the available uInt space: */
2978 png_ptr->zstream.avail_in = (uInt)avail;
2979 avail = 0;
2980 }
2981 }
2982
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002983 /* Compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002984 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002985
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002986 /* Check for compression errors */
Guy Schalnate5a37791996-06-05 15:50:50 -05002987 if (ret != Z_OK)
2988 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002989 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002990 png_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002991
Guy Schalnate5a37791996-06-05 15:50:50 -05002992 else
2993 png_error(png_ptr, "zlib error");
2994 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002995
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002996 /* See if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002997 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002998 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002999 /* Write the IDAT and reset the zlib output buffer */
Guy Schalnate5a37791996-06-05 15:50:50 -05003000 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003001 png_ptr->zstream.next_out = png_ptr->zbuf;
3002 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05003003 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003004 /* Repeat until all data has been compressed */
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05003005 } while (avail > 0 || png_ptr->zstream.avail_in > 0);
Guy Schalnate5a37791996-06-05 15:50:50 -05003006
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003007 /* Swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05003008 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05003009 {
3010 png_bytep tptr;
3011
3012 tptr = png_ptr->prev_row;
3013 png_ptr->prev_row = png_ptr->row_buf;
3014 png_ptr->row_buf = tptr;
3015 }
3016
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003017 /* Finish row - updates counters and flushes zlib if last row */
Guy Schalnate5a37791996-06-05 15:50:50 -05003018 png_write_finish_row(png_ptr);
3019
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003020#ifdef PNG_WRITE_FLUSH_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05003021 png_ptr->flush_rows++;
3022
3023 if (png_ptr->flush_dist > 0 &&
3024 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05003025 {
Guy Schalnate5a37791996-06-05 15:50:50 -05003026 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05003027 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003028#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003029}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05003030#endif /* PNG_WRITE_SUPPORTED */