blob: 93845a187505fb751a29e3e55f8add009652f996 [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-Pehrson67858562011-04-02 08:26:42 -0500223 comp->input_len = text_len;
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 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600230 }
231
232 if (compression >= PNG_TEXT_COMPRESSION_LAST)
233 {
Glenn Randers-Pehrson6a9e4802010-02-19 09:47:43 -0600234#ifdef PNG_CONSOLE_IO_SUPPORTED
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600235 char msg[50];
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500236 png_snprintf(msg, 50, "Unknown compression type %d", compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600237 png_warning(png_ptr, msg);
238#else
239 png_warning(png_ptr, "Unknown compression type");
240#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600241 }
242
243 /* We can't write the chunk until we find out how much data we have,
244 * which means we need to run the compressor first and save the
245 * output. This shouldn't be a problem, as the vast majority of
246 * comments should be reasonable, but we will set up an array of
247 * malloc'd pointers to be sure.
248 *
249 * If we knew the application was well behaved, we could simplify this
250 * greatly by assuming we can always malloc an output buffer large
251 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
252 * and malloc this directly. The only time this would be a bad idea is
253 * if we can't malloc more than 64K and we have 64K of random input
254 * data, or if the input string is incredibly large (although this
255 * wouldn't cause a failure, just a slowdown due to swapping).
256 */
257
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500258 if (!(png_ptr->mode & PNG_ZLIB_READY_FOR_ZTXT))
259 {
Glenn Randers-Pehrsonee1369b2011-03-31 23:44:39 -0500260
261 /* png_warning(png_ptr, "Initialize compressor for ztxt"); */
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500262 /* Free memory from previously opened zstream */
263 deflateEnd(&png_ptr->zstream);
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500264
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500265 /* Initialize the compressor for zTXt compression. */
266 ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_text_level,
267 png_ptr->zlib_text_method, png_ptr->zlib_text_window_bits,
268 png_ptr->zlib_text_mem_level, png_ptr->zlib_text_strategy);
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500269
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500270 if (ret != Z_OK)
271 {
272 if (ret == Z_VERSION_ERROR)
273 png_error(png_ptr,
274 "zlib failed to initialize compressor for text-- version error");
275
276 if (ret == Z_STREAM_ERROR)
277 png_error(png_ptr,
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500278 "zlib failed to initialize compressor for text-- stream error");
279
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500280 if (ret == Z_MEM_ERROR)
281 png_error(png_ptr,
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500282 "zlib failed to initialize compressor for text-- mem error");
283
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500284 png_error(png_ptr, "zlib failed to initialize compressor for text");
285 }
286 png_ptr->mode |= PNG_ZLIB_READY_FOR_ZTXT;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500287 }
288
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500289 /* Set up the compression buffers */
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600290 /* TODO: the following cast hides a potential overflow problem. */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600291 png_ptr->zstream.avail_in = (uInt)text_len;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500292
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600293 /* NOTE: assume zlib doesn't overwrite the input */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600294 png_ptr->zstream.next_in = (Bytef *)text;
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600295 png_ptr->zstream.avail_out = png_ptr->zbuf_size;
296 png_ptr->zstream.next_out = png_ptr->zbuf;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600297
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500298 /* This is the same compression loop as in png_write_row() */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600299 do
300 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500301 /* Compress the data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600302 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500303
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600304 if (ret != Z_OK)
305 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500306 /* Error */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600307 if (png_ptr->zstream.msg != NULL)
308 png_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500309
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600310 else
311 png_error(png_ptr, "zlib error");
312 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500313
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500314 /* Check to see if we need more room */
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600315 if (!(png_ptr->zstream.avail_out))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600316 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500317 /* Make sure the output array has room */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600318 if (comp->num_output_ptr >= comp->max_output_ptr)
319 {
320 int old_max;
321
322 old_max = comp->max_output_ptr;
323 comp->max_output_ptr = comp->num_output_ptr + 4;
324 if (comp->output_ptr != NULL)
325 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500326 png_bytepp old_ptr;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600327
328 old_ptr = comp->output_ptr;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500329
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500330 comp->output_ptr = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600331 (png_alloc_size_t)
332 (comp->max_output_ptr * png_sizeof(png_charpp)));
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500333
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500334 png_memcpy(comp->output_ptr, old_ptr, old_max
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600335 * png_sizeof(png_charp));
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500336
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600337 png_free(png_ptr, old_ptr);
338 }
339 else
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500340 comp->output_ptr = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600341 (png_alloc_size_t)
342 (comp->max_output_ptr * png_sizeof(png_charp)));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600343 }
344
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500345 /* Save the data */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500346 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500347 (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600348 (png_alloc_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500349
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500350 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600351 png_ptr->zbuf_size);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500352
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500353 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600354
355 /* and reset the buffer */
356 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
357 png_ptr->zstream.next_out = png_ptr->zbuf;
358 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500359 /* Continue until we don't have any more to compress */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600360 } while (png_ptr->zstream.avail_in);
361
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500362 /* Finish the compression */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600363 do
364 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500365 /* Tell zlib we are finished */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600366 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500367
368 if (ret == Z_OK)
369 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500370 /* Check to see if we need more room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500371 if (!(png_ptr->zstream.avail_out))
372 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500373 /* Check to make sure our output array has room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500374 if (comp->num_output_ptr >= comp->max_output_ptr)
375 {
376 int old_max;
377
378 old_max = comp->max_output_ptr;
379 comp->max_output_ptr = comp->num_output_ptr + 4;
380 if (comp->output_ptr != NULL)
381 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500382 png_bytepp old_ptr;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500383
384 old_ptr = comp->output_ptr;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500385
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500386 /* This could be optimized to realloc() */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500387 comp->output_ptr = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600388 (png_alloc_size_t)(comp->max_output_ptr *
389 png_sizeof(png_charp)));
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500390
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500391 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600392 old_max * png_sizeof(png_charp));
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500393
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500394 png_free(png_ptr, old_ptr);
395 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500396
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500397 else
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500398 comp->output_ptr = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600399 (png_alloc_size_t)(comp->max_output_ptr *
400 png_sizeof(png_charp)));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500401 }
402
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500403 /* Save the data */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500404 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500405 (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600406 (png_alloc_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500407
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500408 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600409 png_ptr->zbuf_size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500410
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500411 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500412
413 /* and reset the buffer pointers */
414 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
415 png_ptr->zstream.next_out = png_ptr->zbuf;
416 }
417 }
418 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600419 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500420 /* We got an error */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600421 if (png_ptr->zstream.msg != NULL)
422 png_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500423
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600424 else
425 png_error(png_ptr, "zlib error");
426 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600427 } while (ret != Z_STREAM_END);
428
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500429 /* Text length is number of buffers plus last buffer */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600430 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500431
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600432 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
433 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
434
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500435 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600436}
437
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500438/* Ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500439static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600440png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
441{
442 int i;
443
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500444 /* Handle the no-compression case */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600445 if (comp->input)
446 {
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500447 png_write_chunk_data(png_ptr, comp->input, comp->input_len);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500448
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500449 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600450 }
451
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -0500452#if 1 /* For testing */
453
454 if (comp->input_len >= 2 && comp->input_len < 16384)
455 {
456 unsigned int z_cmf; /* zlib compression method and flags */
457
458 /* Optimize the CMF field in the zlib stream. This hack of the zlib
459 * stream is compliant to the stream specification.
460 */
461
462 if (comp->num_output_ptr)
463 z_cmf = comp->output_ptr[0][0];
464 else
465 z_cmf = png_ptr->zbuf[0];
466
467 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
468 {
469 unsigned int z_cinfo;
470 unsigned int half_z_window_size;
471 png_uint_32 uncompressed_idat_size = comp->input_len;
472
473 z_cinfo = z_cmf >> 4;
474 half_z_window_size = 1 << (z_cinfo + 7);
475
476 while (uncompressed_idat_size <= half_z_window_size &&
477 half_z_window_size >= 256)
478 {
479 z_cinfo--;
480 half_z_window_size >>= 1;
481 }
482
483 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
484
485 if (comp->num_output_ptr)
486 {
487
488 if (comp->output_ptr[0][0] != z_cmf)
489 {
490 int tmp;
491
492 comp->output_ptr[0][0] = (png_byte)z_cmf;
493 tmp = comp->output_ptr[0][1] & 0xe0;
494 tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
495 comp->output_ptr[0][1] = (png_byte)tmp;
496 }
497 }
498 else
499 {
500 int tmp;
501
502 png_ptr->zbuf[0] = (png_byte)z_cmf;
503 tmp = png_ptr->zbuf[1] & 0xe0;
504 tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
505 png_ptr->zbuf[1] = (png_byte)tmp;
506 }
507 }
508
509 else
510 png_error(png_ptr,
511 "Invalid zlib compression method or flags in non-IDAT chunk");
512 }
513#endif
514
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500515 /* Write saved output buffers, if any */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600516 for (i = 0; i < comp->num_output_ptr; i++)
517 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500518 png_write_chunk_data(png_ptr, comp->output_ptr[i],
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600519 (png_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500520
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600521 png_free(png_ptr, comp->output_ptr[i]);
522 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500523
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600524 if (comp->max_output_ptr != 0)
525 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500526
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500527 /* Write anything left in zbuf */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600528 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
529 png_write_chunk_data(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600530 (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600531
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500532 /* Reset zlib for another zTXt/iTXt or image data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600533 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600534}
Glenn Randers-Pehrson205483d2011-04-01 12:33:42 -0500535#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600536
Guy Schalnat0d580581995-07-20 02:43:20 -0500537/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600538 * information. Note that the rest of this code depends upon this
539 * information being correct.
540 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500541void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600542png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600543 int bit_depth, int color_type, int compression_type, int filter_type,
544 int interlace_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500545{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600546 PNG_IHDR;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500547 int ret;
548
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500549 png_byte buf[13]; /* Buffer to store the IHDR info */
Guy Schalnat0d580581995-07-20 02:43:20 -0500550
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500551 png_debug(1, "in png_write_IHDR");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500552
Guy Schalnate5a37791996-06-05 15:50:50 -0500553 /* Check that we have valid input data from the application info */
554 switch (color_type)
555 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500556 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500557 switch (bit_depth)
558 {
559 case 1:
560 case 2:
561 case 4:
562 case 8:
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500563#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600564 case 16:
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500565#endif
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600566 png_ptr->channels = 1; break;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500567
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600568 default:
569 png_error(png_ptr,
570 "Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500571 }
572 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500573
Andreas Dilger47a0c421997-05-16 02:46:07 -0500574 case PNG_COLOR_TYPE_RGB:
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500575#ifdef PNG_WRITE_16BIT_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500576 if (bit_depth != 8 && bit_depth != 16)
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500577#else
578 if (bit_depth != 8)
579#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500580 png_error(png_ptr, "Invalid bit depth for RGB image");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500581
Guy Schalnate5a37791996-06-05 15:50:50 -0500582 png_ptr->channels = 3;
583 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500584
Andreas Dilger47a0c421997-05-16 02:46:07 -0500585 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500586 switch (bit_depth)
587 {
588 case 1:
589 case 2:
590 case 4:
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600591 case 8:
592 png_ptr->channels = 1;
593 break;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500594
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600595 default:
596 png_error(png_ptr, "Invalid bit depth for paletted image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500597 }
598 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500599
Andreas Dilger47a0c421997-05-16 02:46:07 -0500600 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500601 if (bit_depth != 8 && bit_depth != 16)
602 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500603
Guy Schalnate5a37791996-06-05 15:50:50 -0500604 png_ptr->channels = 2;
605 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500606
Andreas Dilger47a0c421997-05-16 02:46:07 -0500607 case PNG_COLOR_TYPE_RGB_ALPHA:
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500608#ifdef PNG_WRITE_16BIT_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500609 if (bit_depth != 8 && bit_depth != 16)
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500610#else
611 if (bit_depth != 8)
612#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500613 png_error(png_ptr, "Invalid bit depth for RGBA image");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500614
Guy Schalnate5a37791996-06-05 15:50:50 -0500615 png_ptr->channels = 4;
616 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500617
Guy Schalnate5a37791996-06-05 15:50:50 -0500618 default:
619 png_error(png_ptr, "Invalid image color type specified");
620 }
621
Andreas Dilger47a0c421997-05-16 02:46:07 -0500622 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500623 {
624 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500625 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500626 }
627
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600628 /* Write filter_method 64 (intrapixel differencing) only if
629 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
630 * 2. Libpng did not write a PNG signature (this filter_method is only
631 * used in PNG datastreams that are embedded in MNG datastreams) and
632 * 3. The application called png_permit_mng_features with a mask that
633 * included PNG_FLAG_MNG_FILTER_64 and
634 * 4. The filter_method is 64 and
635 * 5. The color_type is RGB or RGBA
636 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600637 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500638#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600639 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
640 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
641 (color_type == PNG_COLOR_TYPE_RGB ||
642 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
643 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600644#endif
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600645 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500646 {
647 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500648 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500649 }
650
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600651#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500652 if (interlace_type != PNG_INTERLACE_NONE &&
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500653 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500654 {
655 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500656 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500657 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600658#else
659 interlace_type=PNG_INTERLACE_NONE;
660#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500661
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500662 /* Save the relevent information */
Guy Schalnate5a37791996-06-05 15:50:50 -0500663 png_ptr->bit_depth = (png_byte)bit_depth;
664 png_ptr->color_type = (png_byte)color_type;
665 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500666#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600667 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500668#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500669 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500670 png_ptr->width = width;
671 png_ptr->height = height;
672
673 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500674 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500675 /* Set the usr info, so any transformations can modify it */
Guy Schalnate5a37791996-06-05 15:50:50 -0500676 png_ptr->usr_width = png_ptr->width;
677 png_ptr->usr_bit_depth = png_ptr->bit_depth;
678 png_ptr->usr_channels = png_ptr->channels;
679
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500680 /* Pack the header information into the buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500681 png_save_uint_32(buf, width);
682 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600683 buf[8] = (png_byte)bit_depth;
684 buf[9] = (png_byte)color_type;
685 buf[10] = (png_byte)compression_type;
686 buf[11] = (png_byte)filter_type;
687 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500688
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500689 /* Write the chunk */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500690 png_write_chunk(png_ptr, png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500691
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500692 /* Initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600693 png_ptr->zstream.zalloc = png_zalloc;
694 png_ptr->zstream.zfree = png_zfree;
695 png_ptr->zstream.opaque = (voidpf)png_ptr;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500696
Guy Schalnate5a37791996-06-05 15:50:50 -0500697 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500698 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500699 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600700 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500701 png_ptr->do_filter = PNG_FILTER_NONE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500702
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500703 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500704 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500705 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500706
Guy Schalnate5a37791996-06-05 15:50:50 -0500707 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500708 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500709 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500710 png_ptr->zlib_strategy = Z_FILTERED;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500711
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500712 else
713 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
714 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500715
Guy Schalnate5a37791996-06-05 15:50:50 -0500716 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500717 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500718
Guy Schalnate5a37791996-06-05 15:50:50 -0500719 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500720 png_ptr->zlib_mem_level = 8;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500721
Guy Schalnate5a37791996-06-05 15:50:50 -0500722 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500723 png_ptr->zlib_window_bits = 15;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500724
Guy Schalnate5a37791996-06-05 15:50:50 -0500725 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500726 png_ptr->zlib_method = 8;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500727
Glenn Randers-Pehrson205483d2011-04-01 12:33:42 -0500728#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500729#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION
730 if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_STRATEGY))
731 png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY;
732
733 if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_LEVEL))
Glenn Randers-Pehrsonc6831002011-04-01 15:24:18 -0500734 png_ptr->zlib_text_level = png_ptr->zlib_level;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500735
736 if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_MEM_LEVEL))
Glenn Randers-Pehrsonc6831002011-04-01 15:24:18 -0500737 png_ptr->zlib_text_mem_level = png_ptr->zlib_mem_level;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500738
739 if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrsonc6831002011-04-01 15:24:18 -0500740 png_ptr->zlib_text_window_bits = png_ptr->zlib_window_bits;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500741
742 if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_METHOD))
Glenn Randers-Pehrsonc6831002011-04-01 15:24:18 -0500743 png_ptr->zlib_text_method = png_ptr->zlib_method;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500744#else
745 png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY;
Glenn Randers-Pehrsonc6831002011-04-01 15:24:18 -0500746 png_ptr->zlib_text_level = png_ptr->zlib_level;
747 png_ptr->zlib_text_mem_level = png_ptr->zlib_mem_level;
748 png_ptr->zlib_text_window_bits = png_ptr->zlib_window_bits;
749 png_ptr->zlib_text_method = png_ptr->zlib_method;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500750#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION */
Glenn Randers-Pehrson205483d2011-04-01 12:33:42 -0500751#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500752
753 /* Initialize the zlib compressor */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500754 ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600755 png_ptr->zlib_method, png_ptr->zlib_window_bits,
756 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500757
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500758 png_ptr->mode = PNG_HAVE_IHDR; /* not READY_FOR_ZTXT */
Guy Schalnat0d580581995-07-20 02:43:20 -0500759}
760
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500761/* Write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500762 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600763 * structure.
764 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500765void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500766png_write_PLTE(png_structp png_ptr, png_const_colorp palette,
767 png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500768{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600769 PNG_PLTE;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500770 png_uint_32 i;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500771 png_const_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500772 png_byte buf[3];
773
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500774 png_debug(1, "in png_write_PLTE");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500775
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500776 if ((
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500777#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600778 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500779#endif
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600780 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500781 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600782 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
783 {
784 png_error(png_ptr, "Invalid number of colors in palette");
785 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500786
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600787 else
788 {
789 png_warning(png_ptr, "Invalid number of colors in palette");
790 return;
791 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500792 }
793
794 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
795 {
796 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600797 "Ignoring request to write a PLTE chunk in grayscale PNG");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500798
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500799 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500800 }
801
Andreas Dilger47a0c421997-05-16 02:46:07 -0500802 png_ptr->num_palette = (png_uint_16)num_pal;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500803 png_debug1(3, "num_palette = %d", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500804
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500805 png_write_chunk_start(png_ptr, png_PLTE, (png_uint_32)(num_pal * 3));
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500806#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500807
Andreas Dilger47a0c421997-05-16 02:46:07 -0500808 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500809 {
810 buf[0] = pal_ptr->red;
811 buf[1] = pal_ptr->green;
812 buf[2] = pal_ptr->blue;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500813 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500814 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500815
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500816#else
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600817 /* This is a little slower but some buggy compilers need to do this
818 * instead
819 */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500820 pal_ptr=palette;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500821
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500822 for (i = 0; i < num_pal; i++)
823 {
824 buf[0] = pal_ptr[i].red;
825 buf[1] = pal_ptr[i].green;
826 buf[2] = pal_ptr[i].blue;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500827 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500828 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500829
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500830#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500831 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500832 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500833}
834
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500835/* Write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500836void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500837png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500838{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600839 PNG_IDAT;
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500840
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500841 png_debug(1, "in png_write_IDAT");
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500842
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500843 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
844 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
845 {
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500846 unsigned int z_cmf; /* zlib compression method and flags */
847
Glenn Randers-Pehrson205483d2011-04-01 12:33:42 -0500848#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
849 int ret;
Glenn Randers-Pehrsonec8296a2011-04-01 15:09:05 -0500850
851 /* Reinitialize the compressor if necessary */
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500852 if (png_ptr->mode & PNG_ZLIB_READY_FOR_ZTXT)
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500853 {
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500854 /* Free memory from previously opened zstream */
855 deflateEnd(&png_ptr->zstream);
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500856
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500857 ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
858 png_ptr->zlib_method, png_ptr->zlib_window_bits,
859 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500860
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500861 if (ret != Z_OK)
862 {
863 if (ret == Z_VERSION_ERROR)
864 png_error(png_ptr,
865 "zlib failed to initialize compressor -- version error");
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500866
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500867 if (ret == Z_STREAM_ERROR)
868 png_error(png_ptr,
869 "zlib failed to initialize compressor -- stream error");
870
871 if (ret == Z_MEM_ERROR)
872 png_error(png_ptr,
873 "zlib failed to initialize compressor -- mem error");
874
875 png_error(png_ptr, "zlib failed to initialize compressor");
876 }
877 png_ptr->mode &= ~PNG_ZLIB_READY_FOR_ZTXT; /* Ready for IDAT */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500878 }
Glenn Randers-Pehrson205483d2011-04-01 12:33:42 -0500879#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500880
881 png_ptr->zstream.next_out = png_ptr->zbuf;
882 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
883 /* libpng is not interested in zstream.data_type, so set it
884 * to a predefined value, to avoid its evaluation inside zlib
885 */
886 png_ptr->zstream.data_type = Z_BINARY;
887
Glenn Randers-Pehrsonec8296a2011-04-01 15:09:05 -0500888 /* Optimize the CMF field in the zlib stream. This hack of the zlib
889 * stream is compliant to the stream specification.
890 */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500891 z_cmf = data[0];
892
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500893 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
894 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500895 /* Avoid memory underflows and multiplication overflows.
896 *
897 * The conditions below are practically always satisfied;
898 * however, they still must be checked.
899 */
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500900 if (length >= 2 &&
901 png_ptr->height < 16384 && png_ptr->width < 16384)
902 {
Glenn Randers-Pehrsonc3b32402011-04-01 22:10:41 -0500903 /* Compute the maximum possible length of the datastream */
904
905 /* Number of pixels, plus for each row a filter byte and possible
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -0500906 * and possibly a padding byte, so increase the maximum
907 * size to account for these.
Glenn Randers-Pehrsonec8296a2011-04-01 15:09:05 -0500908 */
Glenn Randers-Pehrsone99107b2011-04-02 05:49:03 -0500909 unsigned int z_cinfo;
910 unsigned int half_z_window_size;
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500911 png_uint_32 uncompressed_idat_size = png_ptr->height *
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600912 ((png_ptr->width *
913 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
Glenn Randers-Pehrsonc3b32402011-04-01 22:10:41 -0500914
915 /* If it's interlaced, each block of 8 rows is sent as up to
916 * 14 rows, i.e., 6 additional rows, each with a filter byte
917 * and possibly a padding byte
918 */
919 if (png_ptr->interlaced)
920 uncompressed_idat_size += ((png_ptr->height + 7)/8) *
921 (png_ptr->bit_depth < 8 ? 12 : 6);
922
Glenn Randers-Pehrsone99107b2011-04-02 05:49:03 -0500923 z_cinfo = z_cmf >> 4;
924 half_z_window_size = 1 << (z_cinfo + 7);
925
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500926 while (uncompressed_idat_size <= half_z_window_size &&
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600927 half_z_window_size >= 256)
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500928 {
929 z_cinfo--;
930 half_z_window_size >>= 1;
931 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500932
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500933 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500934
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500935 if (data[0] != z_cmf)
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500936 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500937 int tmp;
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500938 data[0] = (png_byte)z_cmf;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -0500939 tmp = data[1] & 0xe0;
940 tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
941 data[1] = (png_byte)tmp;
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500942 }
943 }
944 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500945
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500946 else
947 png_error(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600948 "Invalid zlib compression method or flags in IDAT");
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500949 }
950
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500951 png_write_chunk(png_ptr, png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500952 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500953}
954
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500955/* Write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500956void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600957png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500958{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600959 PNG_IEND;
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500960
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500961 png_debug(1, "in png_write_IEND");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500962
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500963 png_write_chunk(png_ptr, png_IEND, NULL, (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600964 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500965}
966
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500967#ifdef PNG_WRITE_gAMA_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500968/* Write a gAMA chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500969void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600970png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600971{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600972 PNG_gAMA;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600973 png_byte buf[4];
974
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500975 png_debug(1, "in png_write_gAMA");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500976
Glenn Randers-Pehrson626afd62009-08-20 22:52:51 -0500977 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500978 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500979 png_write_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600980}
981#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500982
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500983#ifdef PNG_WRITE_sRGB_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500984/* Write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500985void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600986png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600987{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600988 PNG_sRGB;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600989 png_byte buf[1];
990
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500991 png_debug(1, "in png_write_sRGB");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500992
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500993 if (srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600994 png_warning(png_ptr,
995 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500996
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600997 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500998 png_write_chunk(png_ptr, png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600999}
1000#endif
1001
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001002#ifdef PNG_WRITE_iCCP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001003/* Write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001004void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001005png_write_iCCP(png_structp png_ptr, png_const_charp name, int compression_type,
1006 png_const_charp profile, int profile_len)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001007{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001008 PNG_iCCP;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001009 png_size_t name_len;
1010 png_charp new_name;
1011 compression_state comp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001012 int embedded_profile_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001013
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001014 png_debug(1, "in png_write_iCCP");
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001015
1016 comp.num_output_ptr = 0;
1017 comp.max_output_ptr = 0;
1018 comp.output_ptr = NULL;
1019 comp.input = NULL;
1020 comp.input_len = 0;
1021
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001022 if ((name_len = png_check_keyword(png_ptr, name, &new_name)) == 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001023 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001024
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001025 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001026 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001027
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -05001028 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001029 profile_len = 0;
1030
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001031 if (profile_len > 3)
Glenn Randers-Pehrson7edd4582006-12-07 19:16:44 -06001032 embedded_profile_len =
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001033 ((*( (png_const_bytep)profile ))<<24) |
1034 ((*( (png_const_bytep)profile + 1))<<16) |
1035 ((*( (png_const_bytep)profile + 2))<< 8) |
1036 ((*( (png_const_bytep)profile + 3)) );
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001037
Glenn Randers-Pehrson3a054e12009-08-01 08:53:23 -05001038 if (embedded_profile_len < 0)
1039 {
1040 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001041 "Embedded profile length in iCCP chunk is negative");
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001042
Glenn Randers-Pehrson3a054e12009-08-01 08:53:23 -05001043 png_free(png_ptr, new_name);
1044 return;
1045 }
1046
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001047 if (profile_len < embedded_profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001048 {
1049 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001050 "Embedded profile length too large in iCCP chunk");
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001051
Glenn Randers-Pehrsona6cc6272009-04-01 14:18:43 -05001052 png_free(png_ptr, new_name);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001053 return;
1054 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001055
1056 if (profile_len > embedded_profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001057 {
1058 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001059 "Truncating profile to actual length in iCCP chunk");
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001060
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001061 profile_len = embedded_profile_len;
1062 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001063
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001064 if (profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001065 profile_len = png_text_compress(png_ptr, profile,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001066 (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001067
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001068 /* Make sure we include the NULL after the name and the compression type */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001069 png_write_chunk_start(png_ptr, png_iCCP,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001070 (png_uint_32)(name_len + profile_len + 2));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001071
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001072 new_name[name_len + 1] = 0x00;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001073
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001074 png_write_chunk_data(png_ptr, (png_bytep)new_name,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001075 (png_size_t)(name_len + 2));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001076
1077 if (profile_len)
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -05001078 {
1079 comp.input_len = profile_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001080 png_write_compressed_data_out(png_ptr, &comp);
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -05001081 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001082
1083 png_write_chunk_end(png_ptr);
1084 png_free(png_ptr, new_name);
1085}
1086#endif
1087
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001088#ifdef PNG_WRITE_sPLT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001089/* Write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001090void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001091png_write_sPLT(png_structp png_ptr, png_const_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001092{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001093 PNG_sPLT;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001094 png_size_t name_len;
1095 png_charp new_name;
1096 png_byte entrybuf[10];
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05001097 png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
1098 png_size_t palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001099 png_sPLT_entryp ep;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001100#ifndef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001101 int i;
1102#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001103
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001104 png_debug(1, "in png_write_sPLT");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001105
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001106 if ((name_len = png_check_keyword(png_ptr,spalette->name, &new_name))==0)
1107 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001108
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001109 /* Make sure we include the NULL after the name */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001110 png_write_chunk_start(png_ptr, png_sPLT,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001111 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001112
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001113 png_write_chunk_data(png_ptr, (png_bytep)new_name,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001114 (png_size_t)(name_len + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001115
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001116 png_write_chunk_data(png_ptr, &spalette->depth, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001117
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001118 /* Loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001119#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001120 for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001121 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001122 if (spalette->depth == 8)
1123 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001124 entrybuf[0] = (png_byte)ep->red;
1125 entrybuf[1] = (png_byte)ep->green;
1126 entrybuf[2] = (png_byte)ep->blue;
1127 entrybuf[3] = (png_byte)ep->alpha;
1128 png_save_uint_16(entrybuf + 4, ep->frequency);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001129 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001130
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001131 else
1132 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001133 png_save_uint_16(entrybuf + 0, ep->red);
1134 png_save_uint_16(entrybuf + 2, ep->green);
1135 png_save_uint_16(entrybuf + 4, ep->blue);
1136 png_save_uint_16(entrybuf + 6, ep->alpha);
1137 png_save_uint_16(entrybuf + 8, ep->frequency);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001138 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001139
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001140 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001141 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001142#else
1143 ep=spalette->entries;
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001144 for (i = 0; i>spalette->nentries; i++)
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001145 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001146 if (spalette->depth == 8)
1147 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001148 entrybuf[0] = (png_byte)ep[i].red;
1149 entrybuf[1] = (png_byte)ep[i].green;
1150 entrybuf[2] = (png_byte)ep[i].blue;
1151 entrybuf[3] = (png_byte)ep[i].alpha;
1152 png_save_uint_16(entrybuf + 4, ep[i].frequency);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001153 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001154
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001155 else
1156 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001157 png_save_uint_16(entrybuf + 0, ep[i].red);
1158 png_save_uint_16(entrybuf + 2, ep[i].green);
1159 png_save_uint_16(entrybuf + 4, ep[i].blue);
1160 png_save_uint_16(entrybuf + 6, ep[i].alpha);
1161 png_save_uint_16(entrybuf + 8, ep[i].frequency);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001162 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001163
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001164 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001165 }
1166#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001167
1168 png_write_chunk_end(png_ptr);
1169 png_free(png_ptr, new_name);
1170}
1171#endif
1172
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001173#ifdef PNG_WRITE_sBIT_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001174/* Write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001175void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001176png_write_sBIT(png_structp png_ptr, png_const_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001177{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001178 PNG_sBIT;
Guy Schalnat0d580581995-07-20 02:43:20 -05001179 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001180 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -05001181
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001182 png_debug(1, "in png_write_sBIT");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001183
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001184 /* Make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001185 if (color_type & PNG_COLOR_MASK_COLOR)
1186 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001187 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -05001188
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001189 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001190 png_ptr->usr_bit_depth);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001191
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001192 if (sbit->red == 0 || sbit->red > maxbits ||
1193 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -05001194 sbit->blue == 0 || sbit->blue > maxbits)
1195 {
1196 png_warning(png_ptr, "Invalid sBIT depth specified");
1197 return;
1198 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001199
Guy Schalnat0d580581995-07-20 02:43:20 -05001200 buf[0] = sbit->red;
1201 buf[1] = sbit->green;
1202 buf[2] = sbit->blue;
1203 size = 3;
1204 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001205
Guy Schalnat0d580581995-07-20 02:43:20 -05001206 else
1207 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001208 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
1209 {
1210 png_warning(png_ptr, "Invalid sBIT depth specified");
1211 return;
1212 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001213
Guy Schalnat0d580581995-07-20 02:43:20 -05001214 buf[0] = sbit->gray;
1215 size = 1;
1216 }
1217
1218 if (color_type & PNG_COLOR_MASK_ALPHA)
1219 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001220 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
1221 {
1222 png_warning(png_ptr, "Invalid sBIT depth specified");
1223 return;
1224 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001225
Guy Schalnat0d580581995-07-20 02:43:20 -05001226 buf[size++] = sbit->alpha;
1227 }
1228
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001229 png_write_chunk(png_ptr, png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -05001230}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001231#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001232
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001233#ifdef PNG_WRITE_cHRM_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001234/* Write the cHRM chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001235void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001236png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001237 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
1238 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
1239 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001240{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001241 PNG_cHRM;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001242 png_byte buf[32];
1243
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001244 png_debug(1, "in png_write_cHRM");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001245
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001246 /* Each value is saved in 1/100,000ths */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001247#ifdef PNG_CHECK_cHRM_SUPPORTED
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001248 if (png_check_cHRM_fixed(png_ptr, white_x, white_y, red_x, red_y,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001249 green_x, green_y, blue_x, blue_y))
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001250#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001251 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001252 png_save_uint_32(buf, (png_uint_32)white_x);
1253 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001254
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001255 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1256 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001257
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001258 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1259 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001260
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001261 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1262 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001263
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001264 png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001265 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001266}
1267#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001268
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001269#ifdef PNG_WRITE_tRNS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001270/* Write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001271void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001272png_write_tRNS(png_structp png_ptr, png_const_bytep trans_alpha,
1273 png_const_color_16p tran, int num_trans, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001274{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001275 PNG_tRNS;
Guy Schalnat0d580581995-07-20 02:43:20 -05001276 png_byte buf[6];
1277
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001278 png_debug(1, "in png_write_tRNS");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001279
Guy Schalnat0d580581995-07-20 02:43:20 -05001280 if (color_type == PNG_COLOR_TYPE_PALETTE)
1281 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001282 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001283 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001284 png_warning(png_ptr, "Invalid number of transparent colors specified");
Guy Schalnate5a37791996-06-05 15:50:50 -05001285 return;
1286 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001287
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001288 /* Write the chunk out as it is */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001289 png_write_chunk(png_ptr, png_tRNS, trans_alpha, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001290 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001291
Guy Schalnat0d580581995-07-20 02:43:20 -05001292 else if (color_type == PNG_COLOR_TYPE_GRAY)
1293 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001294 /* One 16 bit value */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001295 if (tran->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001296 {
1297 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001298 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001299
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001300 return;
1301 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001302
Guy Schalnat0d580581995-07-20 02:43:20 -05001303 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001304 png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001305 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001306
Guy Schalnat0d580581995-07-20 02:43:20 -05001307 else if (color_type == PNG_COLOR_TYPE_RGB)
1308 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001309 /* Three 16 bit values */
Guy Schalnat0d580581995-07-20 02:43:20 -05001310 png_save_uint_16(buf, tran->red);
1311 png_save_uint_16(buf + 2, tran->green);
1312 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -05001313#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001314 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -05001315#else
1316 if (buf[0] | buf[2] | buf[4])
1317#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001318 {
1319 png_warning(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001320 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001321 return;
1322 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001323
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001324 png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001325 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001326
Guy Schalnate5a37791996-06-05 15:50:50 -05001327 else
1328 {
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001329 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001330 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001331}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001332#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001333
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001334#ifdef PNG_WRITE_bKGD_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001335/* Write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001336void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001337png_write_bKGD(png_structp png_ptr, png_const_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001338{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001339 PNG_bKGD;
Guy Schalnat0d580581995-07-20 02:43:20 -05001340 png_byte buf[6];
1341
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001342 png_debug(1, "in png_write_bKGD");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001343
Guy Schalnat0d580581995-07-20 02:43:20 -05001344 if (color_type == PNG_COLOR_TYPE_PALETTE)
1345 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001346 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001347#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001348 (png_ptr->num_palette ||
1349 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001350#endif
Glenn Randers-Pehrsond6d80752008-12-02 09:49:43 -06001351 back->index >= png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001352 {
1353 png_warning(png_ptr, "Invalid background palette index");
1354 return;
1355 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001356
Guy Schalnat0d580581995-07-20 02:43:20 -05001357 buf[0] = back->index;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001358 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001359 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001360
Guy Schalnat0d580581995-07-20 02:43:20 -05001361 else if (color_type & PNG_COLOR_MASK_COLOR)
1362 {
1363 png_save_uint_16(buf, back->red);
1364 png_save_uint_16(buf + 2, back->green);
1365 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -05001366#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001367 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -05001368#else
1369 if (buf[0] | buf[2] | buf[4])
1370#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001371 {
1372 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001373 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001374
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001375 return;
1376 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001377
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001378 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001379 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001380
Guy Schalnat0d580581995-07-20 02:43:20 -05001381 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001382 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001383 if (back->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001384 {
1385 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001386 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001387
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001388 return;
1389 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001390
Guy Schalnat0d580581995-07-20 02:43:20 -05001391 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001392 png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001393 }
1394}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001395#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001396
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001397#ifdef PNG_WRITE_hIST_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001398/* Write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001399void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001400png_write_hIST(png_structp png_ptr, png_const_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001401{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001402 PNG_hIST;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001403 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001404 png_byte buf[3];
1405
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001406 png_debug(1, "in png_write_hIST");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001407
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001408 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001409 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001410 png_debug2(3, "num_hist = %d, num_palette = %d", num_hist,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001411 png_ptr->num_palette);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001412
Guy Schalnate5a37791996-06-05 15:50:50 -05001413 png_warning(png_ptr, "Invalid number of histogram entries specified");
1414 return;
1415 }
1416
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001417 png_write_chunk_start(png_ptr, png_hIST, (png_uint_32)(num_hist * 2));
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001418
Andreas Dilger47a0c421997-05-16 02:46:07 -05001419 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001420 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001421 png_save_uint_16(buf, hist[i]);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001422 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001423 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001424
Guy Schalnat0d580581995-07-20 02:43:20 -05001425 png_write_chunk_end(png_ptr);
1426}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001427#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001428
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001429#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1430 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001431/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1432 * and if invalid, correct the keyword rather than discarding the entire
1433 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1434 * length, forbids leading or trailing whitespace, multiple internal spaces,
1435 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001436 *
1437 * The new_key is allocated to hold the corrected keyword and must be freed
1438 * by the calling routine. This avoids problems with trying to write to
1439 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001440 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001441png_size_t /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001442png_check_keyword(png_structp png_ptr, png_const_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001443{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001444 png_size_t key_len;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001445 png_const_charp ikp;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001446 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001447 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001448 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001449
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001450 png_debug(1, "in png_check_keyword");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001451
Andreas Dilger47a0c421997-05-16 02:46:07 -05001452 *new_key = NULL;
1453
1454 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001455 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001456 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001457 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001458 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001459
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001460 png_debug1(2, "Keyword to be checked is '%s'", key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001461
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001462 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001463
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001464 if (*new_key == NULL)
1465 {
1466 png_warning(png_ptr, "Out of memory while procesing keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001467 return ((png_size_t)0);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001468 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001469
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001470 /* Replace non-printing characters with a blank and print a warning */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001471 for (ikp = key, dp = *new_key; *ikp != '\0'; ikp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001472 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001473 if ((png_byte)*ikp < 0x20 ||
1474 ((png_byte)*ikp > 0x7E && (png_byte)*ikp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001475 {
Glenn Randers-Pehrson6a9e4802010-02-19 09:47:43 -06001476#ifdef PNG_CONSOLE_IO_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001477 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001478
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001479 png_snprintf(msg, 40,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001480 "invalid keyword character 0x%02X", (png_byte)*ikp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001481 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001482#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001483 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001484#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001485 *dp = ' ';
1486 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001487
Andreas Dilger47a0c421997-05-16 02:46:07 -05001488 else
1489 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001490 *dp = *ikp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001491 }
1492 }
1493 *dp = '\0';
1494
1495 /* Remove any trailing white space. */
1496 kp = *new_key + key_len - 1;
1497 if (*kp == ' ')
1498 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001499 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001500
1501 while (*kp == ' ')
1502 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001503 *(kp--) = '\0';
1504 key_len--;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001505 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001506 }
1507
1508 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001509 kp = *new_key;
1510 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001511 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001512 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001513
1514 while (*kp == ' ')
1515 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001516 kp++;
1517 key_len--;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001518 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001519 }
1520
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001521 png_debug1(2, "Checking for multiple internal spaces in '%s'", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001522
Andreas Dilger47a0c421997-05-16 02:46:07 -05001523 /* Remove multiple internal spaces. */
1524 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001525 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001526 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001527 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001528 *(dp++) = *kp;
1529 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001530 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001531
Andreas Dilger47a0c421997-05-16 02:46:07 -05001532 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001533 {
1534 key_len--;
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001535 kwarn = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001536 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001537
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001538 else
1539 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001540 *(dp++) = *kp;
1541 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001542 }
1543 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001544 *dp = '\0';
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001545 if (kwarn)
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001546 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001547
1548 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001549 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001550 png_free(png_ptr, *new_key);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001551 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001552 }
1553
1554 if (key_len > 79)
1555 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001556 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001557 (*new_key)[79] = '\0';
Andreas Dilger47a0c421997-05-16 02:46:07 -05001558 key_len = 79;
1559 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001560
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001561 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001562}
1563#endif
1564
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001565#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001566/* Write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001567void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001568png_write_tEXt(png_structp png_ptr, png_const_charp key, png_const_charp text,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001569 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001570{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001571 PNG_tEXt;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001572 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001573 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001574
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001575 png_debug(1, "in png_write_tEXt");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001576
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001577 if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001578 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001579
Andreas Dilger47a0c421997-05-16 02:46:07 -05001580 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001581 text_len = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001582
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001583 else
1584 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001585
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001586 /* Make sure we include the 0 after the key */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001587 png_write_chunk_start(png_ptr, png_tEXt,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001588 (png_uint_32)(key_len + text_len + 1));
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001589 /*
1590 * We leave it to the application to meet PNG-1.0 requirements on the
1591 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1592 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001593 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001594 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001595 png_write_chunk_data(png_ptr, (png_bytep)new_key,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001596 (png_size_t)(key_len + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001597
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001598 if (text_len)
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001599 png_write_chunk_data(png_ptr, (png_const_bytep)text,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001600 (png_size_t)text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001601
Guy Schalnat0d580581995-07-20 02:43:20 -05001602 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001603 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001604}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001605#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001606
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001607#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001608/* Write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001609void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001610png_write_zTXt(png_structp png_ptr, png_const_charp key, png_const_charp text,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001611 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001612{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001613 PNG_zTXt;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001614 png_size_t key_len;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001615 png_byte buf;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001616 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001617 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001618
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001619 png_debug(1, "in png_write_zTXt");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001620
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001621 comp.num_output_ptr = 0;
1622 comp.max_output_ptr = 0;
1623 comp.output_ptr = NULL;
1624 comp.input = NULL;
1625 comp.input_len = 0;
1626
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001627 if ((key_len = png_check_keyword(png_ptr, key, &new_key)) == 0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001628 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001629 png_free(png_ptr, new_key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001630 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001631 }
1632
Andreas Dilger47a0c421997-05-16 02:46:07 -05001633 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1634 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001635 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001636 png_free(png_ptr, new_key);
1637 return;
1638 }
1639
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001640 text_len = png_strlen(text);
1641
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001642 /* Compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001643 text_len = png_text_compress(png_ptr, text, text_len, compression,
1644 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001645
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001646 /* Write start of chunk */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001647 png_write_chunk_start(png_ptr, png_zTXt,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001648 (png_uint_32)(key_len+text_len + 2));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001649
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001650 /* Write key */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001651 png_write_chunk_data(png_ptr, (png_bytep)new_key,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001652 (png_size_t)(key_len + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001653
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001654 png_free(png_ptr, new_key);
1655
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001656 buf = (png_byte)compression;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001657
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001658 /* Write compression */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001659 png_write_chunk_data(png_ptr, &buf, (png_size_t)1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001660
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001661 /* Write the compressed data */
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -05001662 comp.input_len = text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001663 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001664
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001665 /* Close the chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -05001666 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001667}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001668#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001669
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001670#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001671/* Write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001672void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001673png_write_iTXt(png_structp png_ptr, int compression, png_const_charp key,
1674 png_const_charp lang, png_const_charp lang_key, png_const_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001675{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001676 PNG_iTXt;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001677 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001678 png_charp new_lang;
1679 png_charp new_key = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001680 png_byte cbuf[2];
1681 compression_state comp;
1682
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001683 png_debug(1, "in png_write_iTXt");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001684
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001685 comp.num_output_ptr = 0;
1686 comp.max_output_ptr = 0;
1687 comp.output_ptr = NULL;
1688 comp.input = NULL;
1689
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001690 if ((key_len = png_check_keyword(png_ptr, key, &new_key)) == 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001691 return;
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001692
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001693 if ((lang_len = png_check_keyword(png_ptr, lang, &new_lang)) == 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001694 {
1695 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001696 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001697 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001698 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001699
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001700 if (lang_key == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001701 lang_key_len = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001702
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001703 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001704 lang_key_len = png_strlen(lang_key);
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001705
1706 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001707 text_len = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001708
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001709 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001710 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001711
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001712 /* Compute the compressed data; do it now for the length */
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001713 text_len = png_text_compress(png_ptr, text, text_len, compression - 2,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001714 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001715
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001716
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001717 /* Make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001718 * and the NULs after the key, lang, and lang_key parts
1719 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001720
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001721 png_write_chunk_start(png_ptr, png_iTXt, (png_uint_32)(
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001722 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1723 + key_len
1724 + lang_len
1725 + lang_key_len
1726 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001727
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001728 /* We leave it to the application to meet PNG-1.0 requirements on the
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001729 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1730 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1731 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1732 */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001733 png_write_chunk_data(png_ptr, (png_bytep)new_key, (png_size_t)(key_len + 1));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001734
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001735 /* Set the compression flag */
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001736 if (compression == PNG_ITXT_COMPRESSION_NONE ||
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001737 compression == PNG_TEXT_COMPRESSION_NONE)
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001738 cbuf[0] = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001739
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001740 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001741 cbuf[0] = 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001742
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001743 /* Set the compression method */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001744 cbuf[1] = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001745
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001746 png_write_chunk_data(png_ptr, cbuf, (png_size_t)2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001747
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001748 cbuf[0] = 0;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001749 png_write_chunk_data(png_ptr, (new_lang ? (png_const_bytep)new_lang : cbuf),
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001750 (png_size_t)(lang_len + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001751
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001752 png_write_chunk_data(png_ptr, (lang_key ? (png_const_bytep)lang_key : cbuf),
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001753 (png_size_t)(lang_key_len + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001754
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001755 png_write_compressed_data_out(png_ptr, &comp);
1756
1757 png_write_chunk_end(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001758
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001759 png_free(png_ptr, new_key);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001760 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001761}
1762#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001763
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001764#ifdef PNG_WRITE_oFFs_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001765/* Write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001766void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001767png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001768 int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001769{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001770 PNG_oFFs;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001771 png_byte buf[9];
1772
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001773 png_debug(1, "in png_write_oFFs");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001774
Andreas Dilger47a0c421997-05-16 02:46:07 -05001775 if (unit_type >= PNG_OFFSET_LAST)
1776 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1777
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001778 png_save_int_32(buf, x_offset);
1779 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001780 buf[8] = (png_byte)unit_type;
1781
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001782 png_write_chunk(png_ptr, png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001783}
1784#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001785#ifdef PNG_WRITE_pCAL_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001786/* Write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001787void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001788png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001789 png_int_32 X1, int type, int nparams, png_const_charp units,
1790 png_charpp params)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001791{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001792 PNG_pCAL;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001793 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001794 png_uint_32p params_len;
1795 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001796 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001797 int i;
1798
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001799 png_debug1(1, "in png_write_pCAL (%d parameters)", nparams);
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001800
Andreas Dilger47a0c421997-05-16 02:46:07 -05001801 if (type >= PNG_EQUATION_LAST)
1802 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1803
1804 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001805 png_debug1(3, "pCAL purpose length = %d", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001806 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001807 png_debug1(3, "pCAL units length = %d", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001808 total_len = purpose_len + units_len + 10;
1809
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001810 params_len = (png_uint_32p)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001811 (png_alloc_size_t)(nparams * png_sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001812
1813 /* Find the length of each parameter, making sure we don't count the
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001814 * null terminator for the last parameter.
1815 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001816 for (i = 0; i < nparams; i++)
1817 {
1818 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001819 png_debug2(3, "pCAL parameter %d length = %lu", i,
Glenn Randers-Pehrsond2332872010-10-12 19:19:28 -05001820 (unsigned long)params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001821 total_len += (png_size_t)params_len[i];
1822 }
1823
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001824 png_debug1(3, "pCAL total length = %d", (int)total_len);
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001825 png_write_chunk_start(png_ptr, png_pCAL, (png_uint_32)total_len);
1826 png_write_chunk_data(png_ptr, (png_const_bytep)new_purpose,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001827 (png_size_t)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001828 png_save_int_32(buf, X0);
1829 png_save_int_32(buf + 4, X1);
1830 buf[8] = (png_byte)type;
1831 buf[9] = (png_byte)nparams;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001832 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001833 png_write_chunk_data(png_ptr, (png_const_bytep)units, (png_size_t)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001834
1835 png_free(png_ptr, new_purpose);
1836
1837 for (i = 0; i < nparams; i++)
1838 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001839 png_write_chunk_data(png_ptr, (png_const_bytep)params[i],
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001840 (png_size_t)params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001841 }
1842
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001843 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001844 png_write_chunk_end(png_ptr);
1845}
1846#endif
1847
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001848#ifdef PNG_WRITE_sCAL_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001849/* Write the sCAL chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001850void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001851png_write_sCAL_s(png_structp png_ptr, int unit, png_const_charp width,
1852 png_const_charp height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001853{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001854 PNG_sCAL;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001855 png_byte buf[64];
1856 png_size_t wlen, hlen, total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001857
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001858 png_debug(1, "in png_write_sCAL_s");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001859
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001860 wlen = png_strlen(width);
1861 hlen = png_strlen(height);
1862 total_len = wlen + hlen + 2;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001863
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001864 if (total_len > 64)
1865 {
1866 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
1867 return;
1868 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001869
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001870 buf[0] = (png_byte)unit;
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001871 png_memcpy(buf + 1, width, wlen + 1); /* Append the '\0' here */
1872 png_memcpy(buf + wlen + 2, height, hlen); /* Do NOT append the '\0' here */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001873
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001874 png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001875 png_write_chunk(png_ptr, png_sCAL, buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001876}
1877#endif
1878
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001879#ifdef PNG_WRITE_pHYs_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001880/* Write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001881void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001882png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001883 png_uint_32 y_pixels_per_unit,
1884 int unit_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001885{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001886 PNG_pHYs;
Guy Schalnat0d580581995-07-20 02:43:20 -05001887 png_byte buf[9];
1888
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001889 png_debug(1, "in png_write_pHYs");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001890
Guy Schalnate5a37791996-06-05 15:50:50 -05001891 if (unit_type >= PNG_RESOLUTION_LAST)
1892 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1893
Guy Schalnat0d580581995-07-20 02:43:20 -05001894 png_save_uint_32(buf, x_pixels_per_unit);
1895 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001896 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001897
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001898 png_write_chunk(png_ptr, png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001899}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001900#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001901
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001902#ifdef PNG_WRITE_tIME_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001903/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1904 * or png_convert_from_time_t(), or fill in the structure yourself.
1905 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001906void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001907png_write_tIME(png_structp png_ptr, png_const_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001908{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001909 PNG_tIME;
Guy Schalnat0d580581995-07-20 02:43:20 -05001910 png_byte buf[7];
1911
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001912 png_debug(1, "in png_write_tIME");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001913
Guy Schalnate5a37791996-06-05 15:50:50 -05001914 if (mod_time->month > 12 || mod_time->month < 1 ||
1915 mod_time->day > 31 || mod_time->day < 1 ||
1916 mod_time->hour > 23 || mod_time->second > 60)
1917 {
1918 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1919 return;
1920 }
1921
Guy Schalnat0d580581995-07-20 02:43:20 -05001922 png_save_uint_16(buf, mod_time->year);
1923 buf[2] = mod_time->month;
1924 buf[3] = mod_time->day;
1925 buf[4] = mod_time->hour;
1926 buf[5] = mod_time->minute;
1927 buf[6] = mod_time->second;
1928
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001929 png_write_chunk(png_ptr, png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001930}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001931#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001932
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001933/* Initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001934void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001935png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001936{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001937#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001938 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001939
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001940 /* Start of interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001941 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001942
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001943 /* Offset to next interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001944 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001945
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001946 /* Start of interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001947 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001948
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001949 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001950 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001951#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001952
Andreas Dilger47a0c421997-05-16 02:46:07 -05001953 png_size_t buf_size;
1954
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001955 png_debug(1, "in png_write_start_row");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001956
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001957 buf_size = (png_size_t)(PNG_ROWBYTES(
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001958 png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001959
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001960 /* Set up row buffer */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001961 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001962 (png_alloc_size_t)buf_size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001963
Andreas Dilger47a0c421997-05-16 02:46:07 -05001964 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001965
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001966#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001967 /* Set up filtering buffer, if using this filter */
Guy Schalnate5a37791996-06-05 15:50:50 -05001968 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001969 {
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05001970 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, png_ptr->rowbytes + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001971
Andreas Dilger47a0c421997-05-16 02:46:07 -05001972 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001973 }
1974
Andreas Dilger47a0c421997-05-16 02:46:07 -05001975 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001976 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1977 {
Glenn Randers-Pehrsondfa99af2009-10-29 23:33:46 -05001978 /* Set up previous row buffer */
1979 png_ptr->prev_row = (png_bytep)png_calloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001980 (png_alloc_size_t)buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001981
1982 if (png_ptr->do_filter & PNG_FILTER_UP)
1983 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001984 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05001985 png_ptr->rowbytes + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001986
Andreas Dilger47a0c421997-05-16 02:46:07 -05001987 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001988 }
1989
1990 if (png_ptr->do_filter & PNG_FILTER_AVG)
1991 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001992 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05001993 png_ptr->rowbytes + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001994
Andreas Dilger47a0c421997-05-16 02:46:07 -05001995 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001996 }
1997
1998 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1999 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05002000 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002001 png_ptr->rowbytes + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002002
Andreas Dilger47a0c421997-05-16 02:46:07 -05002003 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05002004 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002005 }
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002006#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05002007
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002008#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002009 /* If interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002010 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05002011 {
2012 if (!(png_ptr->transformations & PNG_INTERLACE))
2013 {
2014 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002015 png_pass_ystart[0]) / png_pass_yinc[0];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002016
Andreas Dilger47a0c421997-05-16 02:46:07 -05002017 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002018 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05002019 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002020
Guy Schalnat0d580581995-07-20 02:43:20 -05002021 else
2022 {
2023 png_ptr->num_rows = png_ptr->height;
2024 png_ptr->usr_width = png_ptr->width;
2025 }
2026 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002027
Guy Schalnat0d580581995-07-20 02:43:20 -05002028 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002029#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002030 {
Guy Schalnat0d580581995-07-20 02:43:20 -05002031 png_ptr->num_rows = png_ptr->height;
2032 png_ptr->usr_width = png_ptr->width;
2033 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002034
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002035 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
2036 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05002037}
2038
Andreas Dilger47a0c421997-05-16 02:46:07 -05002039/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002040void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06002041png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05002042{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002043#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002044 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002045
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002046 /* Start of interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06002047 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002048
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002049 /* Offset to next interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06002050 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002051
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002052 /* Start of interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06002053 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002054
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002055 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06002056 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06002057#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002058
Guy Schalnat0d580581995-07-20 02:43:20 -05002059 int ret;
2060
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002061 png_debug(1, "in png_write_finish_row");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05002062
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002063 /* Next row */
Guy Schalnat0d580581995-07-20 02:43:20 -05002064 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002065
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002066 /* See if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06002067 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002068 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002069
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002070#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002071 /* If interlaced, go to next pass */
Guy Schalnat0d580581995-07-20 02:43:20 -05002072 if (png_ptr->interlaced)
2073 {
2074 png_ptr->row_number = 0;
2075 if (png_ptr->transformations & PNG_INTERLACE)
2076 {
2077 png_ptr->pass++;
2078 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002079
Guy Schalnat0d580581995-07-20 02:43:20 -05002080 else
2081 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002082 /* Loop until we find a non-zero width or height pass */
Guy Schalnat0d580581995-07-20 02:43:20 -05002083 do
2084 {
2085 png_ptr->pass++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002086
Guy Schalnat0d580581995-07-20 02:43:20 -05002087 if (png_ptr->pass >= 7)
2088 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002089
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002090 png_ptr->usr_width = (png_ptr->width +
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002091 png_pass_inc[png_ptr->pass] - 1 -
2092 png_pass_start[png_ptr->pass]) /
2093 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002094
Guy Schalnat0d580581995-07-20 02:43:20 -05002095 png_ptr->num_rows = (png_ptr->height +
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002096 png_pass_yinc[png_ptr->pass] - 1 -
2097 png_pass_ystart[png_ptr->pass]) /
2098 png_pass_yinc[png_ptr->pass];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002099
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002100 if (png_ptr->transformations & PNG_INTERLACE)
2101 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002102
Guy Schalnat0d580581995-07-20 02:43:20 -05002103 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
2104
2105 }
2106
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002107 /* Reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002108 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002109 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002110 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002111 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002112 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
2113 png_ptr->usr_bit_depth, png_ptr->width)) + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002114
Guy Schalnat0d580581995-07-20 02:43:20 -05002115 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002116 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002117 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002118#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002119
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002120 /* If we get here, we've just written the last row, so we need
Guy Schalnat0d580581995-07-20 02:43:20 -05002121 to flush the compressor */
2122 do
2123 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002124 /* Tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002125 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002126
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002127 /* Check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05002128 if (ret == Z_OK)
2129 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002130 /* Check to see if we need more room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05002131 if (!(png_ptr->zstream.avail_out))
2132 {
2133 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
2134 png_ptr->zstream.next_out = png_ptr->zbuf;
2135 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
2136 }
2137 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002138
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05002139 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05002140 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002141 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002142 png_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002143
Guy Schalnat0d580581995-07-20 02:43:20 -05002144 else
Guy Schalnat6d764711995-12-19 03:22:19 -06002145 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05002146 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002147 } while (ret != Z_STREAM_END);
2148
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002149 /* Write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002150 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05002151 {
2152 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002153 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05002154 }
2155
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002156 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -06002157 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat0d580581995-07-20 02:43:20 -05002158}
2159
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002160#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002161/* Pick out the correct pixels for the interlace pass.
2162 * The basic idea here is to go through the row with a source
2163 * pointer and a destination pointer (sp and dp), and copy the
2164 * correct pixels for the pass. As the row gets compacted,
2165 * sp will always be >= dp, so we should never overwrite anything.
2166 * See the default: case for the easiest code to understand.
2167 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002168void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06002169png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05002170{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002171 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002172
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002173 /* Start of interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06002174 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002175
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002176 /* Offset to next interlace block */
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06002177 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002178
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002179 png_debug(1, "in png_do_write_interlace");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05002180
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002181 /* We don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002182 if (pass < 6)
Guy Schalnat0d580581995-07-20 02:43:20 -05002183 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002184 /* Each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05002185 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002186 {
Guy Schalnat0d580581995-07-20 02:43:20 -05002187 case 1:
2188 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002189 png_bytep sp;
2190 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05002191 int shift;
2192 int d;
2193 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002194 png_uint_32 i;
2195 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002196
2197 dp = row;
2198 d = 0;
2199 shift = 7;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002200
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002201 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002202 i += png_pass_inc[pass])
2203 {
2204 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002205 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05002206 d |= (value << shift);
2207
2208 if (shift == 0)
2209 {
2210 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002211 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002212 d = 0;
2213 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002214
Guy Schalnat0d580581995-07-20 02:43:20 -05002215 else
2216 shift--;
2217
2218 }
2219 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002220 *dp = (png_byte)d;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002221
Guy Schalnat0d580581995-07-20 02:43:20 -05002222 break;
2223 }
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002224
Guy Schalnat0d580581995-07-20 02:43:20 -05002225 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002226 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002227 png_bytep sp;
2228 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05002229 int shift;
2230 int d;
2231 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002232 png_uint_32 i;
2233 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002234
2235 dp = row;
2236 shift = 6;
2237 d = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002238
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002239 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002240 i += png_pass_inc[pass])
2241 {
2242 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002243 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05002244 d |= (value << shift);
2245
2246 if (shift == 0)
2247 {
2248 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002249 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002250 d = 0;
2251 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002252
Guy Schalnat0d580581995-07-20 02:43:20 -05002253 else
2254 shift -= 2;
2255 }
2256 if (shift != 6)
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002257 *dp = (png_byte)d;
2258
Guy Schalnat0d580581995-07-20 02:43:20 -05002259 break;
2260 }
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002261
Guy Schalnat0d580581995-07-20 02:43:20 -05002262 case 4:
2263 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002264 png_bytep sp;
2265 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002266 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05002267 int d;
2268 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002269 png_uint_32 i;
2270 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002271
2272 dp = row;
2273 shift = 4;
2274 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002275 for (i = png_pass_start[pass]; i < row_width;
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002276 i += png_pass_inc[pass])
Guy Schalnat0d580581995-07-20 02:43:20 -05002277 {
2278 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002279 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05002280 d |= (value << shift);
2281
2282 if (shift == 0)
2283 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002284 shift = 4;
2285 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002286 d = 0;
2287 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002288
Guy Schalnat0d580581995-07-20 02:43:20 -05002289 else
2290 shift -= 4;
2291 }
2292 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002293 *dp = (png_byte)d;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002294
Guy Schalnat0d580581995-07-20 02:43:20 -05002295 break;
2296 }
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002297
Guy Schalnat0d580581995-07-20 02:43:20 -05002298 default:
2299 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002300 png_bytep sp;
2301 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002302 png_uint_32 i;
2303 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002304 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002305
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002306 /* Start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002307 dp = row;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002308
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002309 /* Find out how many bytes each pixel takes up */
Guy Schalnat0d580581995-07-20 02:43:20 -05002310 pixel_bytes = (row_info->pixel_depth >> 3);
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002311
2312 /* Loop through the row, only looking at the pixels that matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002313 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002314 i += png_pass_inc[pass])
2315 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002316 /* Find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002317 sp = row + (png_size_t)i * pixel_bytes;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002318
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002319 /* Move the pixel */
Guy Schalnat0d580581995-07-20 02:43:20 -05002320 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002321 png_memcpy(dp, sp, pixel_bytes);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002322
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002323 /* Next pixel */
Guy Schalnat0d580581995-07-20 02:43:20 -05002324 dp += pixel_bytes;
2325 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002326 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002327 }
2328 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002329 /* Set new row width */
Guy Schalnat0d580581995-07-20 02:43:20 -05002330 row_info->width = (row_info->width +
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002331 png_pass_inc[pass] - 1 -
2332 png_pass_start[pass]) /
2333 png_pass_inc[pass];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002334
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002335 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2336 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002337 }
2338}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002339#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002340
Andreas Dilger47a0c421997-05-16 02:46:07 -05002341/* This filters the row, chooses which filter to use, if it has not already
2342 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002343 * chosen filter.
2344 */
Glenn Randers-Pehrson78067772004-11-02 21:49:39 -06002345#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002346#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002347#define PNG_LOMASK ((png_uint_32)0xffffL)
2348#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002349void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002350png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002351{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002352 png_bytep best_row;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002353#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002354 png_bytep prev_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002355 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002356 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002357 png_size_t row_bytes = row_info->rowbytes;
Glenn Randers-Pehrson9d8b41e2009-07-19 14:45:43 -05002358#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002359 int num_p_filters = (int)png_ptr->num_prev_filters;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -05002360#endif
Glenn Randers-Pehrson9d8b41e2009-07-19 14:45:43 -05002361
2362 png_debug(1, "in png_write_find_filter");
2363
2364#ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson4ace0e12009-07-19 15:04:35 -05002365 if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS)
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05002366 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002367 /* These will never be selected so we need not test them. */
2368 filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_PAETH);
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05002369 }
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -05002370#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002371
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002372 /* Find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002373 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002374
2375 prev_row = png_ptr->prev_row;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002376#endif
2377 best_row = png_ptr->row_buf;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002378#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002379 row_buf = best_row;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002380 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002381
Andreas Dilger47a0c421997-05-16 02:46:07 -05002382 /* The prediction method we use is to find which method provides the
2383 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002384 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002385 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002386 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002387 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002388 * predictive" method (not implemented yet), which does test compressions
2389 * of lines using different filter methods, and then chooses the
2390 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002391 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002392 *
2393 * GRR 980525: consider also
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002394 *
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002395 * (1) minimum sum of absolute differences from running average (i.e.,
2396 * keep running sum of non-absolute differences & count of bytes)
2397 * [track dispersion, too? restart average if dispersion too large?]
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002398 *
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002399 * (1b) minimum sum of absolute differences from sliding average, probably
2400 * with window size <= deflate window (usually 32K)
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002401 *
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002402 * (2) minimum sum of squared differences from zero or running average
2403 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002404 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002405
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002406
Guy Schalnate5a37791996-06-05 15:50:50 -05002407 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002408 * that has been chosen, as it doesn't actually do anything to the data.
2409 */
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002410 if ((filter_to_do & PNG_FILTER_NONE) && filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002411 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002412 png_bytep rp;
2413 png_uint_32 sum = 0;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002414 png_size_t i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002415 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002416
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002417 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002418 {
2419 v = *rp;
2420 sum += (v < 128) ? v : 256 - v;
2421 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002422
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002423#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002424 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2425 {
2426 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002427 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002428 sumlo = sum & PNG_LOMASK;
2429 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2430
2431 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002432 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002433 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002434 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002435 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002436 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002437 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002438
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002439 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002440 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002441 }
2442 }
2443
2444 /* Factor in the cost of this filter (this is here for completeness,
2445 * but it makes no sense to have a "cost" for the NONE filter, as
2446 * it has the minimum possible computational cost - none).
2447 */
2448 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002449 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002450
Andreas Dilger47a0c421997-05-16 02:46:07 -05002451 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002452 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002453
2454 if (sumhi > PNG_HIMASK)
2455 sum = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002456
Andreas Dilger47a0c421997-05-16 02:46:07 -05002457 else
2458 sum = (sumhi << PNG_HISHIFT) + sumlo;
2459 }
2460#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002461 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002462 }
2463
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002464 /* Sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002465 if (filter_to_do == PNG_FILTER_SUB)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002466 /* It's the only filter so no testing is needed */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002467 {
2468 png_bytep rp, lp, dp;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002469 png_size_t i;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002470
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002471 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2472 i++, rp++, dp++)
2473 {
2474 *dp = *rp;
2475 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002476
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002477 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002478 i++, rp++, lp++, dp++)
2479 {
2480 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2481 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002482
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002483 best_row = png_ptr->sub_row;
2484 }
2485
2486 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002487 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002488 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002489 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002490 png_size_t i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002491 int v;
2492
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002493#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002494 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002495 * would reduce the sum of this filter, so that we can do the
2496 * early exit comparison without scaling the sum each time.
2497 */
2498 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2499 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002500 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002501 png_uint_32 lmhi, lmlo;
2502 lmlo = lmins & PNG_LOMASK;
2503 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2504
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002505 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002506 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002507 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002508 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002509 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002510 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002511
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002512 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002513 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002514 }
2515 }
2516
2517 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002518 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002519
Andreas Dilger47a0c421997-05-16 02:46:07 -05002520 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002521 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002522
2523 if (lmhi > PNG_HIMASK)
2524 lmins = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002525
Andreas Dilger47a0c421997-05-16 02:46:07 -05002526 else
2527 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2528 }
2529#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002530
Guy Schalnate5a37791996-06-05 15:50:50 -05002531 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2532 i++, rp++, dp++)
2533 {
2534 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002535
Guy Schalnate5a37791996-06-05 15:50:50 -05002536 sum += (v < 128) ? v : 256 - v;
2537 }
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002538
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002539 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002540 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002541 {
2542 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002543
Guy Schalnate5a37791996-06-05 15:50:50 -05002544 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002545
2546 if (sum > lmins) /* We are already worse, don't continue. */
2547 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002548 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002549
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002550#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002551 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2552 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002553 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002554 png_uint_32 sumhi, sumlo;
2555 sumlo = sum & PNG_LOMASK;
2556 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2557
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002558 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002559 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002560 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002561 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002562 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002563 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002564
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002565 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002566 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002567 }
2568 }
2569
2570 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002571 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002572
Andreas Dilger47a0c421997-05-16 02:46:07 -05002573 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002574 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002575
2576 if (sumhi > PNG_HIMASK)
2577 sum = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002578
Andreas Dilger47a0c421997-05-16 02:46:07 -05002579 else
2580 sum = (sumhi << PNG_HISHIFT) + sumlo;
2581 }
2582#endif
2583
Guy Schalnate5a37791996-06-05 15:50:50 -05002584 if (sum < mins)
2585 {
2586 mins = sum;
2587 best_row = png_ptr->sub_row;
2588 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002589 }
2590
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002591 /* Up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002592 if (filter_to_do == PNG_FILTER_UP)
2593 {
2594 png_bytep rp, dp, pp;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002595 png_size_t i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002596
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002597 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002598 pp = prev_row + 1; i < row_bytes;
2599 i++, rp++, pp++, dp++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002600 {
2601 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2602 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002603
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002604 best_row = png_ptr->up_row;
2605 }
2606
2607 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002608 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002609 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002610 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002611 png_size_t i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002612 int v;
2613
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002614
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002615#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002616 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2617 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002618 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002619 png_uint_32 lmhi, lmlo;
2620 lmlo = lmins & PNG_LOMASK;
2621 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2622
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002623 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002624 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002625 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002626 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002627 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002628 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002629
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002630 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002631 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002632 }
2633 }
2634
2635 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002636 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002637
Andreas Dilger47a0c421997-05-16 02:46:07 -05002638 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002639 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002640
2641 if (lmhi > PNG_HIMASK)
2642 lmins = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002643
Andreas Dilger47a0c421997-05-16 02:46:07 -05002644 else
2645 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2646 }
2647#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002648
2649 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002650 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002651 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002652 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002653
2654 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002655
2656 if (sum > lmins) /* We are already worse, don't continue. */
2657 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002658 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002659
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002660#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002661 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2662 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002663 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002664 png_uint_32 sumhi, sumlo;
2665 sumlo = sum & PNG_LOMASK;
2666 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2667
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002668 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002669 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002670 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002671 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002672 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002673 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002674
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002675 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002676 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002677 }
2678 }
2679
2680 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002681 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002682
Andreas Dilger47a0c421997-05-16 02:46:07 -05002683 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002684 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002685
2686 if (sumhi > PNG_HIMASK)
2687 sum = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002688
Andreas Dilger47a0c421997-05-16 02:46:07 -05002689 else
2690 sum = (sumhi << PNG_HISHIFT) + sumlo;
2691 }
2692#endif
2693
Guy Schalnate5a37791996-06-05 15:50:50 -05002694 if (sum < mins)
2695 {
2696 mins = sum;
2697 best_row = png_ptr->up_row;
2698 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002699 }
2700
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002701 /* Avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002702 if (filter_to_do == PNG_FILTER_AVG)
2703 {
2704 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002705 png_uint_32 i;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002706
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002707 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002708 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002709 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002710 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002711 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002712
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002713 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002714 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002715 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2716 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002717 }
2718 best_row = png_ptr->avg_row;
2719 }
2720
2721 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002722 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002723 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002724 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002725 png_size_t i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002726 int v;
2727
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002728#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002729 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2730 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002731 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002732 png_uint_32 lmhi, lmlo;
2733 lmlo = lmins & PNG_LOMASK;
2734 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2735
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002736 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002737 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002738 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002739 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002740 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002741 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002742
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002743 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002744 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002745 }
2746 }
2747
2748 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002749 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002750
Andreas Dilger47a0c421997-05-16 02:46:07 -05002751 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002752 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002753
2754 if (lmhi > PNG_HIMASK)
2755 lmins = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002756
Andreas Dilger47a0c421997-05-16 02:46:07 -05002757 else
2758 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2759 }
2760#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002761
2762 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002763 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002764 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002765 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002766
2767 sum += (v < 128) ? v : 256 - v;
2768 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002769
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002770 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002771 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002772 v = *dp++ =
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002773 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002774
2775 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002776
2777 if (sum > lmins) /* We are already worse, don't continue. */
2778 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002779 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002780
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002781#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002782 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2783 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002784 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002785 png_uint_32 sumhi, sumlo;
2786 sumlo = sum & PNG_LOMASK;
2787 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2788
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002789 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002790 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002791 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002792 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002793 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002794 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002795
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002796 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002797 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002798 }
2799 }
2800
2801 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002802 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002803
Andreas Dilger47a0c421997-05-16 02:46:07 -05002804 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002805 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002806
2807 if (sumhi > PNG_HIMASK)
2808 sum = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002809
Andreas Dilger47a0c421997-05-16 02:46:07 -05002810 else
2811 sum = (sumhi << PNG_HISHIFT) + sumlo;
2812 }
2813#endif
2814
Guy Schalnate5a37791996-06-05 15:50:50 -05002815 if (sum < mins)
2816 {
2817 mins = sum;
2818 best_row = png_ptr->avg_row;
2819 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002820 }
2821
Andreas Dilger47a0c421997-05-16 02:46:07 -05002822 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002823 if (filter_to_do == PNG_FILTER_PAETH)
2824 {
2825 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002826 png_size_t i;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002827
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002828 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002829 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002830 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002831 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002832 }
2833
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002834 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002835 {
2836 int a, b, c, pa, pb, pc, p;
2837
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002838 b = *pp++;
2839 c = *cp++;
2840 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002841
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002842 p = b - c;
2843 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002844
2845#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002846 pa = abs(p);
2847 pb = abs(pc);
2848 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002849#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002850 pa = p < 0 ? -p : p;
2851 pb = pc < 0 ? -pc : pc;
2852 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002853#endif
2854
2855 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2856
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002857 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002858 }
2859 best_row = png_ptr->paeth_row;
2860 }
2861
2862 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002863 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002864 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002865 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002866 png_size_t i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002867 int v;
2868
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002869#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002870 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2871 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002872 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002873 png_uint_32 lmhi, lmlo;
2874 lmlo = lmins & PNG_LOMASK;
2875 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2876
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002877 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002878 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002879 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002880 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002881 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002882 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002883
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002884 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002885 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002886 }
2887 }
2888
2889 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002890 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002891
Andreas Dilger47a0c421997-05-16 02:46:07 -05002892 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002893 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002894
2895 if (lmhi > PNG_HIMASK)
2896 lmins = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002897
Andreas Dilger47a0c421997-05-16 02:46:07 -05002898 else
2899 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2900 }
2901#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002902
2903 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002904 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002905 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002906 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002907
2908 sum += (v < 128) ? v : 256 - v;
2909 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002910
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002911 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002912 {
2913 int a, b, c, pa, pb, pc, p;
2914
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002915 b = *pp++;
2916 c = *cp++;
2917 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002918
2919#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002920 p = b - c;
2921 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002922#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002923 pa = abs(p);
2924 pb = abs(pc);
2925 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002926#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002927 pa = p < 0 ? -p : p;
2928 pb = pc < 0 ? -pc : pc;
2929 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002930#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002931 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2932#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002933 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002934 pa = abs(p - a);
2935 pb = abs(p - b);
2936 pc = abs(p - c);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002937
Guy Schalnate5a37791996-06-05 15:50:50 -05002938 if (pa <= pb && pa <= pc)
2939 p = a;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002940
Guy Schalnate5a37791996-06-05 15:50:50 -05002941 else if (pb <= pc)
2942 p = b;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002943
Guy Schalnate5a37791996-06-05 15:50:50 -05002944 else
2945 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002946#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002947
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002948 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002949
2950 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002951
2952 if (sum > lmins) /* We are already worse, don't continue. */
2953 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002954 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002955
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002956#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002957 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2958 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002959 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002960 png_uint_32 sumhi, sumlo;
2961 sumlo = sum & PNG_LOMASK;
2962 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2963
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002964 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002965 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002966 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002967 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002968 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002969 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002970
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002971 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002972 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002973 }
2974 }
2975
2976 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002977 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002978
Andreas Dilger47a0c421997-05-16 02:46:07 -05002979 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002980 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002981
2982 if (sumhi > PNG_HIMASK)
2983 sum = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002984
Andreas Dilger47a0c421997-05-16 02:46:07 -05002985 else
2986 sum = (sumhi << PNG_HISHIFT) + sumlo;
2987 }
2988#endif
2989
Guy Schalnate5a37791996-06-05 15:50:50 -05002990 if (sum < mins)
2991 {
2992 best_row = png_ptr->paeth_row;
2993 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002994 }
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002995#endif /* PNG_WRITE_FILTER_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002996 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002997
Guy Schalnate5a37791996-06-05 15:50:50 -05002998 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002999
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05003000#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003001#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003002 /* Save the type of filter we picked this time for future calculations */
3003 if (png_ptr->num_prev_filters > 0)
3004 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003005 int j;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003006
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003007 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05003008 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003009 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05003010 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05003011
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003012 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05003013 }
3014#endif
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05003015#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -05003016}
3017
3018
Andreas Dilger47a0c421997-05-16 02:46:07 -05003019/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003020void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05003021png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
3022{
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05003023 png_size_t avail;
3024
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003025 png_debug(1, "in png_write_filtered_row");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05003026
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003027 png_debug1(2, "filter = %d", filtered_row[0]);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003028 /* Set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06003029
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003030 png_ptr->zstream.next_in = filtered_row;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05003031 png_ptr->zstream.avail_in = 0;
3032 avail = png_ptr->row_info.rowbytes + 1;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003033 /* Repeat until we have compressed all the data */
Guy Schalnate5a37791996-06-05 15:50:50 -05003034 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003035 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003036 int ret; /* Return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05003037
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05003038 /* Record the number of bytes available - zlib supports at least 65535
3039 * bytes at one step, depending on the size of the zlib type 'uInt', the
3040 * maximum size zlib can write at once is ZLIB_IO_MAX (from pngpriv.h).
3041 * Use this because on 16 bit systems 'rowbytes' can be up to 65536 (i.e.
3042 * one more than 16 bits) and, in this case 'rowbytes+1' can overflow a
3043 * uInt. ZLIB_IO_MAX can be safely reduced to cause zlib to be called
3044 * with smaller chunks of data.
3045 */
3046 if (png_ptr->zstream.avail_in == 0)
3047 {
3048 if (avail > ZLIB_IO_MAX)
3049 {
3050 png_ptr->zstream.avail_in = ZLIB_IO_MAX;
3051 avail -= ZLIB_IO_MAX;
3052 }
3053
3054 else
3055 {
3056 /* So this will fit in the available uInt space: */
3057 png_ptr->zstream.avail_in = (uInt)avail;
3058 avail = 0;
3059 }
3060 }
3061
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003062 /* Compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003063 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003064
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003065 /* Check for compression errors */
Guy Schalnate5a37791996-06-05 15:50:50 -05003066 if (ret != Z_OK)
3067 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05003068 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003069 png_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003070
Guy Schalnate5a37791996-06-05 15:50:50 -05003071 else
3072 png_error(png_ptr, "zlib error");
3073 }
Guy Schalnat0d580581995-07-20 02:43:20 -05003074
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003075 /* See if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05003076 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05003077 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003078 /* Write the IDAT and reset the zlib output buffer */
Guy Schalnate5a37791996-06-05 15:50:50 -05003079 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003080 png_ptr->zstream.next_out = png_ptr->zbuf;
3081 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05003082 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003083 /* Repeat until all data has been compressed */
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05003084 } while (avail > 0 || png_ptr->zstream.avail_in > 0);
Guy Schalnate5a37791996-06-05 15:50:50 -05003085
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003086 /* Swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05003087 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05003088 {
3089 png_bytep tptr;
3090
3091 tptr = png_ptr->prev_row;
3092 png_ptr->prev_row = png_ptr->row_buf;
3093 png_ptr->row_buf = tptr;
3094 }
3095
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003096 /* Finish row - updates counters and flushes zlib if last row */
Guy Schalnate5a37791996-06-05 15:50:50 -05003097 png_write_finish_row(png_ptr);
3098
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003099#ifdef PNG_WRITE_FLUSH_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05003100 png_ptr->flush_rows++;
3101
3102 if (png_ptr->flush_dist > 0 &&
3103 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05003104 {
Guy Schalnate5a37791996-06-05 15:50:50 -05003105 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05003106 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003107#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003108}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05003109#endif /* PNG_WRITE_SUPPORTED */