blob: 2554d51ec574540d06db178cb223897c004c6313 [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-Pehrsonbb5cb142011-09-22 12:41:58 -05004 * Last changed in libpng 1.5.6 [(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 the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060086 * The total_length is the sum of the lengths of all the data you will be
87 * passing in png_write_chunk_data().
88 */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -050089static void
90png_write_chunk_header(png_structp png_ptr, png_uint_32 chunk_name,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -060091 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -050092{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050093 png_byte buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -050094
Glenn Randers-Pehrson5b3b54e2011-10-12 06:36:56 -050095#if defined(PNG_DEBUG) && (PNG_DEBUG > 0)
Glenn Randers-Pehrsonba55c072011-10-11 21:21:37 -050096 PNG_CSTRING_FROM_CHUNK(buf, chunk_name);
97 png_debug2(0, "Writing %s chunk, length = %lu", buf, (unsigned long)length);
98#endif
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050099
100 if (png_ptr == NULL)
101 return;
102
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500103#ifdef PNG_IO_STATE_SUPPORTED
104 /* Inform the I/O callback that the chunk header is being written.
105 * PNG_IO_CHUNK_HDR requires a single I/O call.
106 */
107 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR;
108#endif
109
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500110 /* Write the length and the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500111 png_save_uint_32(buf, length);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500112 png_save_uint_32(buf + 4, chunk_name);
113 png_write_data(png_ptr, buf, 8);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500114
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500115 /* Put the chunk name into png_ptr->chunk_name */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500116 png_ptr->chunk_name = chunk_name;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500117
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500118 /* Reset the crc and run it over the chunk name */
Guy Schalnat0d580581995-07-20 02:43:20 -0500119 png_reset_crc(png_ptr);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500120
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500121 png_calculate_crc(png_ptr, buf + 4, 4);
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500122
123#ifdef PNG_IO_STATE_SUPPORTED
124 /* Inform the I/O callback that chunk data will (possibly) be written.
125 * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.
126 */
127 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA;
128#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500129}
130
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500131void PNGAPI
132png_write_chunk_start(png_structp png_ptr, png_const_bytep chunk_string,
133 png_uint_32 length)
134{
135 png_write_chunk_header(png_ptr, PNG_CHUNK_FROM_STRING(chunk_string), length);
136}
137
138/* Write the data of a PNG chunk started with png_write_chunk_header().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600139 * Note that multiple calls to this function are allowed, and that the
140 * sum of the lengths from these calls *must* add up to the total_length
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500141 * given to png_write_chunk_header().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600142 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500143void PNGAPI
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500144png_write_chunk_data(png_structp png_ptr, png_const_bytep data,
145 png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500146{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500147 /* Write the data, and run the CRC over it */
148 if (png_ptr == NULL)
149 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500150
Andreas Dilger47a0c421997-05-16 02:46:07 -0500151 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500152 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600153 png_write_data(png_ptr, data, length);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500154
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500155 /* Update the CRC after writing the data,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500156 * in case that the user I/O routine alters it.
157 */
158 png_calculate_crc(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500159 }
160}
161
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500162/* Finish a chunk started with png_write_chunk_header(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500163void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600164png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500165{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500166 png_byte buf[4];
167
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500168 if (png_ptr == NULL) return;
169
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -0500170#ifdef PNG_IO_STATE_SUPPORTED
171 /* Inform the I/O callback that the chunk CRC is being written.
172 * PNG_IO_CHUNK_CRC requires a single I/O function call.
173 */
174 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC;
175#endif
176
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500177 /* Write the crc in a single operation */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500178 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500179
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500180 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500181}
182
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500183/* Write a PNG chunk all at once. The type is an array of ASCII characters
184 * representing the chunk name. The array must be at least 4 bytes in
185 * length, and does not need to be null terminated. To be safe, pass the
186 * pre-defined chunk names here, and if you need a new one, define it
187 * where the others are defined. The length is the length of the data.
188 * All the data must be present. If that is not possible, use the
189 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
190 * functions instead.
191 */
192static void
193png_write_complete_chunk(png_structp png_ptr, png_uint_32 chunk_name,
194 png_const_bytep data, png_size_t length)
195{
196 if (png_ptr == NULL)
197 return;
198
199 /* On 64 bit architectures 'length' may not fit in a png_uint_32. */
200 if (length > PNG_UINT_32_MAX)
201 png_error(png_ptr, "length exceeds PNG maxima");
202
203 png_write_chunk_header(png_ptr, chunk_name, (png_uint_32)length);
204 png_write_chunk_data(png_ptr, data, length);
205 png_write_chunk_end(png_ptr);
206}
207
208/* This is the API that calls the internal function above. */
209void PNGAPI
210png_write_chunk(png_structp png_ptr, png_const_bytep chunk_string,
211 png_const_bytep data, png_size_t length)
212{
213 png_write_complete_chunk(png_ptr, PNG_CHUNK_FROM_STRING(chunk_string), data,
214 length);
215}
216
John Bowlerc5bef942011-05-05 17:35:39 -0500217/* Initialize the compressor for the appropriate type of compression. */
218static void
219png_zlib_claim(png_structp png_ptr, png_uint_32 state)
220{
221 if (!(png_ptr->zlib_state & PNG_ZLIB_IN_USE))
222 {
223 /* If already initialized for 'state' do not re-init. */
224 if (png_ptr->zlib_state != state)
225 {
226 int ret = Z_OK;
227 png_const_charp who = "-";
228
229 /* If actually initialized for another state do a deflateEnd. */
230 if (png_ptr->zlib_state != PNG_ZLIB_UNINITIALIZED)
231 {
232 ret = deflateEnd(&png_ptr->zstream);
233 who = "end";
234 png_ptr->zlib_state = PNG_ZLIB_UNINITIALIZED;
235 }
236
237 /* zlib itself detects an incomplete state on deflateEnd */
238 if (ret == Z_OK) switch (state)
239 {
240# ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
241 case PNG_ZLIB_FOR_TEXT:
242 ret = deflateInit2(&png_ptr->zstream,
243 png_ptr->zlib_text_level, png_ptr->zlib_text_method,
244 png_ptr->zlib_text_window_bits,
245 png_ptr->zlib_text_mem_level, png_ptr->zlib_text_strategy);
246 who = "text";
247 break;
248# endif
249
250 case PNG_ZLIB_FOR_IDAT:
251 ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
252 png_ptr->zlib_method, png_ptr->zlib_window_bits,
253 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
254 who = "IDAT";
255 break;
256
257 default:
258 png_error(png_ptr, "invalid zlib state");
259 }
260
261 if (ret == Z_OK)
262 png_ptr->zlib_state = state;
263
264 else /* an error in deflateEnd or deflateInit2 */
265 {
266 size_t pos = 0;
267 char msg[64];
268
269 pos = png_safecat(msg, sizeof msg, pos,
270 "zlib failed to initialize compressor (");
271 pos = png_safecat(msg, sizeof msg, pos, who);
272
273 switch (ret)
274 {
275 case Z_VERSION_ERROR:
276 pos = png_safecat(msg, sizeof msg, pos, ") version error");
277 break;
278
279 case Z_STREAM_ERROR:
280 pos = png_safecat(msg, sizeof msg, pos, ") stream error");
281 break;
282
283 case Z_MEM_ERROR:
284 pos = png_safecat(msg, sizeof msg, pos, ") memory error");
285 break;
286
287 default:
288 pos = png_safecat(msg, sizeof msg, pos, ") unknown error");
289 break;
290 }
291
292 png_error(png_ptr, msg);
293 }
294 }
295
296 /* Here on success, claim the zstream: */
297 png_ptr->zlib_state |= PNG_ZLIB_IN_USE;
298 }
299
300 else
301 png_error(png_ptr, "zstream already in use (internal error)");
302}
303
304/* The opposite: release the stream. It is also reset, this API will warn on
305 * error but will not fail.
306 */
307static void
308png_zlib_release(png_structp png_ptr)
309{
310 if (png_ptr->zlib_state & PNG_ZLIB_IN_USE)
311 {
312 int ret = deflateReset(&png_ptr->zstream);
313
314 png_ptr->zlib_state &= ~PNG_ZLIB_IN_USE;
315
316 if (ret != Z_OK)
317 {
318 png_const_charp err;
Glenn Randers-Pehrson492ef9c2011-05-07 21:42:25 -0500319 PNG_WARNING_PARAMETERS(p)
John Bowlerc5bef942011-05-05 17:35:39 -0500320
321 switch (ret)
322 {
323 case Z_VERSION_ERROR:
324 err = "version";
325 break;
326
327 case Z_STREAM_ERROR:
328 err = "stream";
329 break;
330
331 case Z_MEM_ERROR:
332 err = "memory";
333 break;
334
335 default:
336 err = "unknown";
337 break;
338 }
339
John Bowlerd273ad22011-05-07 21:00:28 -0500340 png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_d, ret);
John Bowlerc5bef942011-05-05 17:35:39 -0500341 png_warning_parameter(p, 2, err);
342
343 if (png_ptr->zstream.msg)
344 err = png_ptr->zstream.msg;
345 else
346 err = "[no zlib message]";
347
348 png_warning_parameter(p, 3, err);
349
350 png_formatted_warning(png_ptr, p,
351 "zlib failed to reset compressor: @1(@2): @3");
352 }
353 }
354
355 else
356 png_warning(png_ptr, "zstream not in use (internal error)");
357}
358
Glenn Randers-Pehrson205483d2011-04-01 12:33:42 -0500359#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500360/* This pair of functions encapsulates the operation of (a) compressing a
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600361 * text string, and (b) issuing it later as a series of chunk data writes.
362 * The compression_state structure is shared context for these functions
363 * set up by the caller in order to make the whole mess thread-safe.
364 */
365
366typedef struct
367{
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500368 png_const_bytep input; /* The uncompressed input data */
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500369 png_size_t input_len; /* Its length */
370 int num_output_ptr; /* Number of output pointers used */
371 int max_output_ptr; /* Size of output_ptr */
372 png_bytep *output_ptr; /* Array of pointers to output */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600373} compression_state;
374
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500375/* Compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500376static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600377png_text_compress(png_structp png_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500378 png_const_charp text, png_size_t text_len, int compression,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600379 compression_state *comp)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600380{
381 int ret;
382
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600383 comp->num_output_ptr = 0;
384 comp->max_output_ptr = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600385 comp->output_ptr = NULL;
386 comp->input = NULL;
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -0500387 comp->input_len = text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600388
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500389 /* We may just want to pass the text right through */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600390 if (compression == PNG_TEXT_COMPRESSION_NONE)
391 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500392 comp->input = (png_const_bytep)text;
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600393 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600394 }
395
396 if (compression >= PNG_TEXT_COMPRESSION_LAST)
397 {
John Bowler88b77cc2011-05-05 06:49:55 -0500398 PNG_WARNING_PARAMETERS(p)
399
400 png_warning_parameter_signed(p, 1, PNG_NUMBER_FORMAT_d,
401 compression);
402 png_formatted_warning(png_ptr, p, "Unknown compression type @1");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600403 }
404
405 /* We can't write the chunk until we find out how much data we have,
406 * which means we need to run the compressor first and save the
407 * output. This shouldn't be a problem, as the vast majority of
408 * comments should be reasonable, but we will set up an array of
409 * malloc'd pointers to be sure.
410 *
411 * If we knew the application was well behaved, we could simplify this
412 * greatly by assuming we can always malloc an output buffer large
413 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
414 * and malloc this directly. The only time this would be a bad idea is
415 * if we can't malloc more than 64K and we have 64K of random input
416 * data, or if the input string is incredibly large (although this
417 * wouldn't cause a failure, just a slowdown due to swapping).
418 */
John Bowlerc5bef942011-05-05 17:35:39 -0500419 png_zlib_claim(png_ptr, PNG_ZLIB_FOR_TEXT);
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500420
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500421 /* Set up the compression buffers */
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600422 /* TODO: the following cast hides a potential overflow problem. */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600423 png_ptr->zstream.avail_in = (uInt)text_len;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500424
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600425 /* NOTE: assume zlib doesn't overwrite the input */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600426 png_ptr->zstream.next_in = (Bytef *)text;
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600427 png_ptr->zstream.avail_out = png_ptr->zbuf_size;
428 png_ptr->zstream.next_out = png_ptr->zbuf;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600429
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500430 /* This is the same compression loop as in png_write_row() */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600431 do
432 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500433 /* Compress the data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600434 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500435
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600436 if (ret != Z_OK)
437 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500438 /* Error */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600439 if (png_ptr->zstream.msg != NULL)
440 png_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500441
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600442 else
443 png_error(png_ptr, "zlib error");
444 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500445
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500446 /* Check to see if we need more room */
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600447 if (!(png_ptr->zstream.avail_out))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600448 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500449 /* Make sure the output array has room */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600450 if (comp->num_output_ptr >= comp->max_output_ptr)
451 {
452 int old_max;
453
454 old_max = comp->max_output_ptr;
455 comp->max_output_ptr = comp->num_output_ptr + 4;
456 if (comp->output_ptr != NULL)
457 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500458 png_bytepp old_ptr;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600459
460 old_ptr = comp->output_ptr;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500461
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500462 comp->output_ptr = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600463 (png_alloc_size_t)
464 (comp->max_output_ptr * png_sizeof(png_charpp)));
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500465
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500466 png_memcpy(comp->output_ptr, old_ptr, old_max
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600467 * png_sizeof(png_charp));
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500468
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600469 png_free(png_ptr, old_ptr);
470 }
471 else
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500472 comp->output_ptr = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600473 (png_alloc_size_t)
474 (comp->max_output_ptr * png_sizeof(png_charp)));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600475 }
476
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500477 /* Save the data */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500478 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500479 (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600480 (png_alloc_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500481
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500482 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600483 png_ptr->zbuf_size);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500484
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500485 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600486
487 /* and reset the buffer */
488 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
489 png_ptr->zstream.next_out = png_ptr->zbuf;
490 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500491 /* Continue until we don't have any more to compress */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600492 } while (png_ptr->zstream.avail_in);
493
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500494 /* Finish the compression */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600495 do
496 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500497 /* Tell zlib we are finished */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600498 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500499
500 if (ret == Z_OK)
501 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500502 /* Check to see if we need more room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500503 if (!(png_ptr->zstream.avail_out))
504 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500505 /* Check to make sure our output array has room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500506 if (comp->num_output_ptr >= comp->max_output_ptr)
507 {
508 int old_max;
509
510 old_max = comp->max_output_ptr;
511 comp->max_output_ptr = comp->num_output_ptr + 4;
512 if (comp->output_ptr != NULL)
513 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500514 png_bytepp old_ptr;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500515
516 old_ptr = comp->output_ptr;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500517
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500518 /* This could be optimized to realloc() */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500519 comp->output_ptr = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600520 (png_alloc_size_t)(comp->max_output_ptr *
521 png_sizeof(png_charp)));
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500522
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500523 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600524 old_max * png_sizeof(png_charp));
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500525
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500526 png_free(png_ptr, old_ptr);
527 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500528
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500529 else
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500530 comp->output_ptr = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600531 (png_alloc_size_t)(comp->max_output_ptr *
532 png_sizeof(png_charp)));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500533 }
534
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500535 /* Save the data */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500536 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500537 (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600538 (png_alloc_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500539
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500540 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600541 png_ptr->zbuf_size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500542
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500543 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500544
545 /* and reset the buffer pointers */
546 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
547 png_ptr->zstream.next_out = png_ptr->zbuf;
548 }
549 }
550 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600551 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500552 /* We got an error */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600553 if (png_ptr->zstream.msg != NULL)
554 png_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500555
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600556 else
557 png_error(png_ptr, "zlib error");
558 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600559 } while (ret != Z_STREAM_END);
560
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500561 /* Text length is number of buffers plus last buffer */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600562 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500563
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600564 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
565 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
566
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500567 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600568}
569
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500570/* Ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500571static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600572png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
573{
574 int i;
575
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500576 /* Handle the no-compression case */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600577 if (comp->input)
578 {
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -0500579 png_write_chunk_data(png_ptr, comp->input, comp->input_len);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500580
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500581 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600582 }
583
Glenn Randers-Pehrsonc559bb52011-05-05 16:55:20 -0500584#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -0500585 if (comp->input_len >= 2 && comp->input_len < 16384)
586 {
587 unsigned int z_cmf; /* zlib compression method and flags */
588
589 /* Optimize the CMF field in the zlib stream. This hack of the zlib
590 * stream is compliant to the stream specification.
591 */
592
593 if (comp->num_output_ptr)
594 z_cmf = comp->output_ptr[0][0];
595 else
596 z_cmf = png_ptr->zbuf[0];
597
598 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
599 {
600 unsigned int z_cinfo;
601 unsigned int half_z_window_size;
Glenn Randers-Pehrsonc559bb52011-05-05 16:55:20 -0500602 png_size_t uncompressed_text_size = comp->input_len;
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -0500603
604 z_cinfo = z_cmf >> 4;
605 half_z_window_size = 1 << (z_cinfo + 7);
606
Glenn Randers-Pehrsonc559bb52011-05-05 16:55:20 -0500607 while (uncompressed_text_size <= half_z_window_size &&
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -0500608 half_z_window_size >= 256)
609 {
610 z_cinfo--;
611 half_z_window_size >>= 1;
612 }
613
614 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
615
616 if (comp->num_output_ptr)
617 {
618
619 if (comp->output_ptr[0][0] != z_cmf)
620 {
621 int tmp;
622
623 comp->output_ptr[0][0] = (png_byte)z_cmf;
624 tmp = comp->output_ptr[0][1] & 0xe0;
625 tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
626 comp->output_ptr[0][1] = (png_byte)tmp;
627 }
628 }
629 else
630 {
631 int tmp;
632
633 png_ptr->zbuf[0] = (png_byte)z_cmf;
634 tmp = png_ptr->zbuf[1] & 0xe0;
635 tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
636 png_ptr->zbuf[1] = (png_byte)tmp;
637 }
638 }
639
640 else
641 png_error(png_ptr,
642 "Invalid zlib compression method or flags in non-IDAT chunk");
643 }
Glenn Randers-Pehrsonc559bb52011-05-05 16:55:20 -0500644#endif /* PNG_WRITE_OPTIMIZE_CMF_SUPPORTED */
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -0500645
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500646 /* Write saved output buffers, if any */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600647 for (i = 0; i < comp->num_output_ptr; i++)
648 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500649 png_write_chunk_data(png_ptr, comp->output_ptr[i],
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600650 (png_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500651
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600652 png_free(png_ptr, comp->output_ptr[i]);
653 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500654
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600655 if (comp->max_output_ptr != 0)
656 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500657
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500658 /* Write anything left in zbuf */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600659 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
660 png_write_chunk_data(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600661 (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600662
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500663 /* Reset zlib for another zTXt/iTXt or image data */
John Bowlerc5bef942011-05-05 17:35:39 -0500664 png_zlib_release(png_ptr);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600665}
Glenn Randers-Pehrson205483d2011-04-01 12:33:42 -0500666#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600667
Guy Schalnat0d580581995-07-20 02:43:20 -0500668/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600669 * information. Note that the rest of this code depends upon this
670 * information being correct.
671 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500672void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600673png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600674 int bit_depth, int color_type, int compression_type, int filter_type,
675 int interlace_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500676{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500677 png_byte buf[13]; /* Buffer to store the IHDR info */
Guy Schalnat0d580581995-07-20 02:43:20 -0500678
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500679 png_debug(1, "in png_write_IHDR");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500680
Guy Schalnate5a37791996-06-05 15:50:50 -0500681 /* Check that we have valid input data from the application info */
682 switch (color_type)
683 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500684 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500685 switch (bit_depth)
686 {
687 case 1:
688 case 2:
689 case 4:
690 case 8:
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500691#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600692 case 16:
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500693#endif
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600694 png_ptr->channels = 1; break;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500695
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600696 default:
697 png_error(png_ptr,
698 "Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500699 }
700 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500701
Andreas Dilger47a0c421997-05-16 02:46:07 -0500702 case PNG_COLOR_TYPE_RGB:
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500703#ifdef PNG_WRITE_16BIT_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500704 if (bit_depth != 8 && bit_depth != 16)
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500705#else
706 if (bit_depth != 8)
707#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500708 png_error(png_ptr, "Invalid bit depth for RGB image");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500709
Guy Schalnate5a37791996-06-05 15:50:50 -0500710 png_ptr->channels = 3;
711 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500712
Andreas Dilger47a0c421997-05-16 02:46:07 -0500713 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500714 switch (bit_depth)
715 {
716 case 1:
717 case 2:
718 case 4:
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600719 case 8:
720 png_ptr->channels = 1;
721 break;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500722
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600723 default:
724 png_error(png_ptr, "Invalid bit depth for paletted image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500725 }
726 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500727
Andreas Dilger47a0c421997-05-16 02:46:07 -0500728 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500729 if (bit_depth != 8 && bit_depth != 16)
730 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500731
Guy Schalnate5a37791996-06-05 15:50:50 -0500732 png_ptr->channels = 2;
733 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500734
Andreas Dilger47a0c421997-05-16 02:46:07 -0500735 case PNG_COLOR_TYPE_RGB_ALPHA:
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500736#ifdef PNG_WRITE_16BIT_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500737 if (bit_depth != 8 && bit_depth != 16)
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500738#else
739 if (bit_depth != 8)
740#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500741 png_error(png_ptr, "Invalid bit depth for RGBA image");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500742
Guy Schalnate5a37791996-06-05 15:50:50 -0500743 png_ptr->channels = 4;
744 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500745
Guy Schalnate5a37791996-06-05 15:50:50 -0500746 default:
747 png_error(png_ptr, "Invalid image color type specified");
748 }
749
Andreas Dilger47a0c421997-05-16 02:46:07 -0500750 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500751 {
752 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500753 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500754 }
755
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600756 /* Write filter_method 64 (intrapixel differencing) only if
757 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
758 * 2. Libpng did not write a PNG signature (this filter_method is only
759 * used in PNG datastreams that are embedded in MNG datastreams) and
760 * 3. The application called png_permit_mng_features with a mask that
761 * included PNG_FLAG_MNG_FILTER_64 and
762 * 4. The filter_method is 64 and
763 * 5. The color_type is RGB or RGBA
764 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600765 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500766#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600767 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
768 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
769 (color_type == PNG_COLOR_TYPE_RGB ||
770 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
771 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600772#endif
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600773 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500774 {
775 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500776 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500777 }
778
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600779#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500780 if (interlace_type != PNG_INTERLACE_NONE &&
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500781 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500782 {
783 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500784 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500785 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600786#else
787 interlace_type=PNG_INTERLACE_NONE;
788#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500789
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500790 /* Save the relevent information */
Guy Schalnate5a37791996-06-05 15:50:50 -0500791 png_ptr->bit_depth = (png_byte)bit_depth;
792 png_ptr->color_type = (png_byte)color_type;
793 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500794#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600795 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500796#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500797 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500798 png_ptr->width = width;
799 png_ptr->height = height;
800
801 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500802 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500803 /* Set the usr info, so any transformations can modify it */
Guy Schalnate5a37791996-06-05 15:50:50 -0500804 png_ptr->usr_width = png_ptr->width;
805 png_ptr->usr_bit_depth = png_ptr->bit_depth;
806 png_ptr->usr_channels = png_ptr->channels;
807
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500808 /* Pack the header information into the buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500809 png_save_uint_32(buf, width);
810 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600811 buf[8] = (png_byte)bit_depth;
812 buf[9] = (png_byte)color_type;
813 buf[10] = (png_byte)compression_type;
814 buf[11] = (png_byte)filter_type;
815 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500816
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500817 /* Write the chunk */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500818 png_write_complete_chunk(png_ptr, png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500819
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500820 /* Initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600821 png_ptr->zstream.zalloc = png_zalloc;
822 png_ptr->zstream.zfree = png_zfree;
823 png_ptr->zstream.opaque = (voidpf)png_ptr;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500824
Guy Schalnate5a37791996-06-05 15:50:50 -0500825 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500826 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500827 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600828 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500829 png_ptr->do_filter = PNG_FILTER_NONE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500830
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500831 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500832 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500833 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500834
Guy Schalnate5a37791996-06-05 15:50:50 -0500835 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500836 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500837 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500838 png_ptr->zlib_strategy = Z_FILTERED;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500839
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500840 else
841 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
842 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500843
Guy Schalnate5a37791996-06-05 15:50:50 -0500844 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500845 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500846
Guy Schalnate5a37791996-06-05 15:50:50 -0500847 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500848 png_ptr->zlib_mem_level = 8;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500849
Guy Schalnate5a37791996-06-05 15:50:50 -0500850 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500851 png_ptr->zlib_window_bits = 15;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500852
Guy Schalnate5a37791996-06-05 15:50:50 -0500853 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500854 png_ptr->zlib_method = 8;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500855
Glenn Randers-Pehrson205483d2011-04-01 12:33:42 -0500856#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
John Bowlerb2bee332011-06-10 23:24:58 -0500857#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500858 if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_STRATEGY))
859 png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY;
860
861 if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_LEVEL))
Glenn Randers-Pehrsonc6831002011-04-01 15:24:18 -0500862 png_ptr->zlib_text_level = png_ptr->zlib_level;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500863
864 if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_MEM_LEVEL))
Glenn Randers-Pehrsonc6831002011-04-01 15:24:18 -0500865 png_ptr->zlib_text_mem_level = png_ptr->zlib_mem_level;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500866
867 if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrsonc6831002011-04-01 15:24:18 -0500868 png_ptr->zlib_text_window_bits = png_ptr->zlib_window_bits;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500869
870 if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_METHOD))
Glenn Randers-Pehrsonc6831002011-04-01 15:24:18 -0500871 png_ptr->zlib_text_method = png_ptr->zlib_method;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500872#else
873 png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY;
Glenn Randers-Pehrsonc6831002011-04-01 15:24:18 -0500874 png_ptr->zlib_text_level = png_ptr->zlib_level;
875 png_ptr->zlib_text_mem_level = png_ptr->zlib_mem_level;
876 png_ptr->zlib_text_window_bits = png_ptr->zlib_window_bits;
877 png_ptr->zlib_text_method = png_ptr->zlib_method;
John Bowlerb2bee332011-06-10 23:24:58 -0500878#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */
Glenn Randers-Pehrson205483d2011-04-01 12:33:42 -0500879#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500880
John Bowlerc5bef942011-05-05 17:35:39 -0500881 /* Record that the compressor has not yet been initialized. */
882 png_ptr->zlib_state = PNG_ZLIB_UNINITIALIZED;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500883
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500884 png_ptr->mode = PNG_HAVE_IHDR; /* not READY_FOR_ZTXT */
Guy Schalnat0d580581995-07-20 02:43:20 -0500885}
886
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500887/* Write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500888 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600889 * structure.
890 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500891void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500892png_write_PLTE(png_structp png_ptr, png_const_colorp palette,
893 png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500894{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500895 png_uint_32 i;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500896 png_const_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500897 png_byte buf[3];
898
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500899 png_debug(1, "in png_write_PLTE");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500900
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500901 if ((
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500902#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600903 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500904#endif
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600905 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500906 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600907 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
908 {
909 png_error(png_ptr, "Invalid number of colors in palette");
910 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500911
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600912 else
913 {
914 png_warning(png_ptr, "Invalid number of colors in palette");
915 return;
916 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500917 }
918
919 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
920 {
921 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600922 "Ignoring request to write a PLTE chunk in grayscale PNG");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500923
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500924 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500925 }
926
Andreas Dilger47a0c421997-05-16 02:46:07 -0500927 png_ptr->num_palette = (png_uint_16)num_pal;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500928 png_debug1(3, "num_palette = %d", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500929
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500930 png_write_chunk_header(png_ptr, png_PLTE, (png_uint_32)(num_pal * 3));
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500931#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500932
Andreas Dilger47a0c421997-05-16 02:46:07 -0500933 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500934 {
935 buf[0] = pal_ptr->red;
936 buf[1] = pal_ptr->green;
937 buf[2] = pal_ptr->blue;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500938 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500939 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500940
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500941#else
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600942 /* This is a little slower but some buggy compilers need to do this
943 * instead
944 */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500945 pal_ptr=palette;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500946
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500947 for (i = 0; i < num_pal; i++)
948 {
949 buf[0] = pal_ptr[i].red;
950 buf[1] = pal_ptr[i].green;
951 buf[2] = pal_ptr[i].blue;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500952 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500953 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500954
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500955#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500956 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500957 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500958}
959
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500960/* Write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500961void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500962png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500963{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500964 png_debug(1, "in png_write_IDAT");
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500965
John Bowlerc5bef942011-05-05 17:35:39 -0500966#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500967 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
968 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
969 {
Glenn Randers-Pehrsonec8296a2011-04-01 15:09:05 -0500970 /* Optimize the CMF field in the zlib stream. This hack of the zlib
971 * stream is compliant to the stream specification.
972 */
John Bowlerc5bef942011-05-05 17:35:39 -0500973 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500974
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500975 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
976 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500977 /* Avoid memory underflows and multiplication overflows.
978 *
979 * The conditions below are practically always satisfied;
980 * however, they still must be checked.
981 */
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500982 if (length >= 2 &&
983 png_ptr->height < 16384 && png_ptr->width < 16384)
984 {
Glenn Randers-Pehrsonc3b32402011-04-01 22:10:41 -0500985 /* Compute the maximum possible length of the datastream */
986
Glenn Randers-Pehrson3fceee02011-05-23 06:31:33 -0500987 /* Number of pixels, plus for each row a filter byte
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -0500988 * and possibly a padding byte, so increase the maximum
989 * size to account for these.
Glenn Randers-Pehrsonec8296a2011-04-01 15:09:05 -0500990 */
Glenn Randers-Pehrsone99107b2011-04-02 05:49:03 -0500991 unsigned int z_cinfo;
992 unsigned int half_z_window_size;
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500993 png_uint_32 uncompressed_idat_size = png_ptr->height *
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600994 ((png_ptr->width *
995 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
Glenn Randers-Pehrsonc3b32402011-04-01 22:10:41 -0500996
997 /* If it's interlaced, each block of 8 rows is sent as up to
998 * 14 rows, i.e., 6 additional rows, each with a filter byte
999 * and possibly a padding byte
1000 */
1001 if (png_ptr->interlaced)
1002 uncompressed_idat_size += ((png_ptr->height + 7)/8) *
1003 (png_ptr->bit_depth < 8 ? 12 : 6);
1004
Glenn Randers-Pehrsone99107b2011-04-02 05:49:03 -05001005 z_cinfo = z_cmf >> 4;
1006 half_z_window_size = 1 << (z_cinfo + 7);
1007
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -05001008 while (uncompressed_idat_size <= half_z_window_size &&
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001009 half_z_window_size >= 256)
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -05001010 {
1011 z_cinfo--;
1012 half_z_window_size >>= 1;
1013 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001014
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -05001015 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001016
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001017 if (data[0] != z_cmf)
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -05001018 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001019 int tmp;
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -05001020 data[0] = (png_byte)z_cmf;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001021 tmp = data[1] & 0xe0;
1022 tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
1023 data[1] = (png_byte)tmp;
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -05001024 }
1025 }
1026 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001027
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -05001028 else
1029 png_error(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001030 "Invalid zlib compression method or flags in IDAT");
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -05001031 }
John Bowlerc5bef942011-05-05 17:35:39 -05001032#endif /* PNG_WRITE_OPTIMIZE_CMF_SUPPORTED */
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -05001033
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001034 png_write_complete_chunk(png_ptr, png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -05001035 png_ptr->mode |= PNG_HAVE_IDAT;
John Bowlerc5bef942011-05-05 17:35:39 -05001036
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001037 /* Prior to 1.5.4 this code was replicated in every caller (except at the
John Bowlerc5bef942011-05-05 17:35:39 -05001038 * end, where it isn't technically necessary). Since this function has
1039 * flushed the data we can safely reset the zlib output buffer here.
1040 */
1041 png_ptr->zstream.next_out = png_ptr->zbuf;
1042 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat0d580581995-07-20 02:43:20 -05001043}
1044
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001045/* Write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001046void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001047png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001048{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001049 png_debug(1, "in png_write_IEND");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001050
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001051 png_write_complete_chunk(png_ptr, png_IEND, NULL, (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001052 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -05001053}
1054
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001055#ifdef PNG_WRITE_gAMA_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001056/* Write a gAMA chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001057void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001058png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001059{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001060 png_byte buf[4];
1061
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001062 png_debug(1, "in png_write_gAMA");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001063
Glenn Randers-Pehrson626afd62009-08-20 22:52:51 -05001064 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001065 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001066 png_write_complete_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001067}
1068#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001069
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001070#ifdef PNG_WRITE_sRGB_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001071/* Write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001072void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001073png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001074{
1075 png_byte buf[1];
1076
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001077 png_debug(1, "in png_write_sRGB");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001078
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001079 if (srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001080 png_warning(png_ptr,
1081 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001082
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001083 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001084 png_write_complete_chunk(png_ptr, png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001085}
1086#endif
1087
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001088#ifdef PNG_WRITE_iCCP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001089/* Write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001090void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001091png_write_iCCP(png_structp png_ptr, png_const_charp name, int compression_type,
1092 png_const_charp profile, int profile_len)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001093{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001094 png_size_t name_len;
1095 png_charp new_name;
1096 compression_state comp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001097 int embedded_profile_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001098
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001099 png_debug(1, "in png_write_iCCP");
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001100
1101 comp.num_output_ptr = 0;
1102 comp.max_output_ptr = 0;
1103 comp.output_ptr = NULL;
1104 comp.input = NULL;
1105 comp.input_len = 0;
1106
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001107 if ((name_len = png_check_keyword(png_ptr, name, &new_name)) == 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001108 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001109
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001110 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001111 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001112
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -05001113 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001114 profile_len = 0;
1115
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001116 if (profile_len > 3)
Glenn Randers-Pehrson7edd4582006-12-07 19:16:44 -06001117 embedded_profile_len =
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001118 ((*( (png_const_bytep)profile ))<<24) |
1119 ((*( (png_const_bytep)profile + 1))<<16) |
1120 ((*( (png_const_bytep)profile + 2))<< 8) |
1121 ((*( (png_const_bytep)profile + 3)) );
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001122
Glenn Randers-Pehrson3a054e12009-08-01 08:53:23 -05001123 if (embedded_profile_len < 0)
1124 {
1125 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001126 "Embedded profile length in iCCP chunk is negative");
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001127
Glenn Randers-Pehrson3a054e12009-08-01 08:53:23 -05001128 png_free(png_ptr, new_name);
1129 return;
1130 }
1131
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001132 if (profile_len < embedded_profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001133 {
1134 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001135 "Embedded profile length too large in iCCP chunk");
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001136
Glenn Randers-Pehrsona6cc6272009-04-01 14:18:43 -05001137 png_free(png_ptr, new_name);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001138 return;
1139 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001140
1141 if (profile_len > embedded_profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001142 {
1143 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001144 "Truncating profile to actual length in iCCP chunk");
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001145
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001146 profile_len = embedded_profile_len;
1147 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001148
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001149 if (profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001150 profile_len = png_text_compress(png_ptr, profile,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001151 (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001152
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001153 /* Make sure we include the NULL after the name and the compression type */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001154 png_write_chunk_header(png_ptr, png_iCCP,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001155 (png_uint_32)(name_len + profile_len + 2));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001156
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001157 new_name[name_len + 1] = 0x00;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001158
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001159 png_write_chunk_data(png_ptr, (png_bytep)new_name,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001160 (png_size_t)(name_len + 2));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001161
1162 if (profile_len)
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -05001163 {
1164 comp.input_len = profile_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001165 png_write_compressed_data_out(png_ptr, &comp);
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -05001166 }
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_sPLT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001174/* Write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001175void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001176png_write_sPLT(png_structp png_ptr, png_const_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001177{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001178 png_size_t name_len;
1179 png_charp new_name;
1180 png_byte entrybuf[10];
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05001181 png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
1182 png_size_t palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001183 png_sPLT_entryp ep;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001184#ifndef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001185 int i;
1186#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001187
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001188 png_debug(1, "in png_write_sPLT");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001189
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001190 if ((name_len = png_check_keyword(png_ptr,spalette->name, &new_name))==0)
1191 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001192
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001193 /* Make sure we include the NULL after the name */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001194 png_write_chunk_header(png_ptr, png_sPLT,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001195 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001196
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001197 png_write_chunk_data(png_ptr, (png_bytep)new_name,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001198 (png_size_t)(name_len + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001199
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001200 png_write_chunk_data(png_ptr, &spalette->depth, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001201
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001202 /* Loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001203#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001204 for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001205 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001206 if (spalette->depth == 8)
1207 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001208 entrybuf[0] = (png_byte)ep->red;
1209 entrybuf[1] = (png_byte)ep->green;
1210 entrybuf[2] = (png_byte)ep->blue;
1211 entrybuf[3] = (png_byte)ep->alpha;
1212 png_save_uint_16(entrybuf + 4, ep->frequency);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001213 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001214
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001215 else
1216 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001217 png_save_uint_16(entrybuf + 0, ep->red);
1218 png_save_uint_16(entrybuf + 2, ep->green);
1219 png_save_uint_16(entrybuf + 4, ep->blue);
1220 png_save_uint_16(entrybuf + 6, ep->alpha);
1221 png_save_uint_16(entrybuf + 8, ep->frequency);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001222 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001223
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001224 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001225 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001226#else
1227 ep=spalette->entries;
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001228 for (i = 0; i>spalette->nentries; i++)
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001229 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001230 if (spalette->depth == 8)
1231 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001232 entrybuf[0] = (png_byte)ep[i].red;
1233 entrybuf[1] = (png_byte)ep[i].green;
1234 entrybuf[2] = (png_byte)ep[i].blue;
1235 entrybuf[3] = (png_byte)ep[i].alpha;
1236 png_save_uint_16(entrybuf + 4, ep[i].frequency);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001237 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001238
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001239 else
1240 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001241 png_save_uint_16(entrybuf + 0, ep[i].red);
1242 png_save_uint_16(entrybuf + 2, ep[i].green);
1243 png_save_uint_16(entrybuf + 4, ep[i].blue);
1244 png_save_uint_16(entrybuf + 6, ep[i].alpha);
1245 png_save_uint_16(entrybuf + 8, ep[i].frequency);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001246 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001247
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001248 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001249 }
1250#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001251
1252 png_write_chunk_end(png_ptr);
1253 png_free(png_ptr, new_name);
1254}
1255#endif
1256
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001257#ifdef PNG_WRITE_sBIT_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001258/* Write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001259void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001260png_write_sBIT(png_structp png_ptr, png_const_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001261{
1262 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001263 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -05001264
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001265 png_debug(1, "in png_write_sBIT");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001266
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001267 /* Make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001268 if (color_type & PNG_COLOR_MASK_COLOR)
1269 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001270 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -05001271
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001272 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001273 png_ptr->usr_bit_depth);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001274
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001275 if (sbit->red == 0 || sbit->red > maxbits ||
1276 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -05001277 sbit->blue == 0 || sbit->blue > maxbits)
1278 {
1279 png_warning(png_ptr, "Invalid sBIT depth specified");
1280 return;
1281 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001282
Guy Schalnat0d580581995-07-20 02:43:20 -05001283 buf[0] = sbit->red;
1284 buf[1] = sbit->green;
1285 buf[2] = sbit->blue;
1286 size = 3;
1287 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001288
Guy Schalnat0d580581995-07-20 02:43:20 -05001289 else
1290 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001291 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
1292 {
1293 png_warning(png_ptr, "Invalid sBIT depth specified");
1294 return;
1295 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001296
Guy Schalnat0d580581995-07-20 02:43:20 -05001297 buf[0] = sbit->gray;
1298 size = 1;
1299 }
1300
1301 if (color_type & PNG_COLOR_MASK_ALPHA)
1302 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001303 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
1304 {
1305 png_warning(png_ptr, "Invalid sBIT depth specified");
1306 return;
1307 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001308
Guy Schalnat0d580581995-07-20 02:43:20 -05001309 buf[size++] = sbit->alpha;
1310 }
1311
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001312 png_write_complete_chunk(png_ptr, png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -05001313}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001314#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001315
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001316#ifdef PNG_WRITE_cHRM_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001317/* Write the cHRM chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001318void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001319png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001320 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
1321 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
1322 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001323{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001324 png_byte buf[32];
1325
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001326 png_debug(1, "in png_write_cHRM");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001327
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001328 /* Each value is saved in 1/100,000ths */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001329#ifdef PNG_CHECK_cHRM_SUPPORTED
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001330 if (png_check_cHRM_fixed(png_ptr, white_x, white_y, red_x, red_y,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001331 green_x, green_y, blue_x, blue_y))
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001332#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001333 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001334 png_save_uint_32(buf, (png_uint_32)white_x);
1335 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001336
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001337 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1338 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001339
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001340 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1341 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001342
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001343 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1344 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001345
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001346 png_write_complete_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001347 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001348}
1349#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001350
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001351#ifdef PNG_WRITE_tRNS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001352/* Write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001353void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001354png_write_tRNS(png_structp png_ptr, png_const_bytep trans_alpha,
1355 png_const_color_16p tran, int num_trans, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001356{
1357 png_byte buf[6];
1358
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001359 png_debug(1, "in png_write_tRNS");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001360
Guy Schalnat0d580581995-07-20 02:43:20 -05001361 if (color_type == PNG_COLOR_TYPE_PALETTE)
1362 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001363 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001364 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001365 png_warning(png_ptr, "Invalid number of transparent colors specified");
Guy Schalnate5a37791996-06-05 15:50:50 -05001366 return;
1367 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001368
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001369 /* Write the chunk out as it is */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001370 png_write_complete_chunk(png_ptr, png_tRNS, trans_alpha, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001371 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001372
Guy Schalnat0d580581995-07-20 02:43:20 -05001373 else if (color_type == PNG_COLOR_TYPE_GRAY)
1374 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001375 /* One 16 bit value */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001376 if (tran->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001377 {
1378 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001379 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001380
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001381 return;
1382 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001383
Guy Schalnat0d580581995-07-20 02:43:20 -05001384 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001385 png_write_complete_chunk(png_ptr, png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001386 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001387
Guy Schalnat0d580581995-07-20 02:43:20 -05001388 else if (color_type == PNG_COLOR_TYPE_RGB)
1389 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001390 /* Three 16 bit values */
Guy Schalnat0d580581995-07-20 02:43:20 -05001391 png_save_uint_16(buf, tran->red);
1392 png_save_uint_16(buf + 2, tran->green);
1393 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -05001394#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001395 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -05001396#else
1397 if (buf[0] | buf[2] | buf[4])
1398#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001399 {
1400 png_warning(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001401 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001402 return;
1403 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001404
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001405 png_write_complete_chunk(png_ptr, png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001406 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001407
Guy Schalnate5a37791996-06-05 15:50:50 -05001408 else
1409 {
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001410 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001411 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001412}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001413#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001414
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001415#ifdef PNG_WRITE_bKGD_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001416/* Write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001417void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001418png_write_bKGD(png_structp png_ptr, png_const_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001419{
1420 png_byte buf[6];
1421
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001422 png_debug(1, "in png_write_bKGD");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001423
Guy Schalnat0d580581995-07-20 02:43:20 -05001424 if (color_type == PNG_COLOR_TYPE_PALETTE)
1425 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001426 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001427#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001428 (png_ptr->num_palette ||
1429 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001430#endif
Glenn Randers-Pehrsond6d80752008-12-02 09:49:43 -06001431 back->index >= png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001432 {
1433 png_warning(png_ptr, "Invalid background palette index");
1434 return;
1435 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001436
Guy Schalnat0d580581995-07-20 02:43:20 -05001437 buf[0] = back->index;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001438 png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001439 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001440
Guy Schalnat0d580581995-07-20 02:43:20 -05001441 else if (color_type & PNG_COLOR_MASK_COLOR)
1442 {
1443 png_save_uint_16(buf, back->red);
1444 png_save_uint_16(buf + 2, back->green);
1445 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -05001446#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001447 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -05001448#else
1449 if (buf[0] | buf[2] | buf[4])
1450#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001451 {
1452 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001453 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001454
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001455 return;
1456 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001457
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001458 png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001459 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001460
Guy Schalnat0d580581995-07-20 02:43:20 -05001461 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001462 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001463 if (back->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001464 {
1465 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001466 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001467
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001468 return;
1469 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001470
Guy Schalnat0d580581995-07-20 02:43:20 -05001471 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001472 png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001473 }
1474}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001475#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001476
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001477#ifdef PNG_WRITE_hIST_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001478/* Write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001479void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001480png_write_hIST(png_structp png_ptr, png_const_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001481{
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001482 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001483 png_byte buf[3];
1484
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001485 png_debug(1, "in png_write_hIST");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001486
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001487 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001488 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001489 png_debug2(3, "num_hist = %d, num_palette = %d", num_hist,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001490 png_ptr->num_palette);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001491
Guy Schalnate5a37791996-06-05 15:50:50 -05001492 png_warning(png_ptr, "Invalid number of histogram entries specified");
1493 return;
1494 }
1495
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001496 png_write_chunk_header(png_ptr, png_hIST, (png_uint_32)(num_hist * 2));
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001497
Andreas Dilger47a0c421997-05-16 02:46:07 -05001498 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001499 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001500 png_save_uint_16(buf, hist[i]);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001501 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001502 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001503
Guy Schalnat0d580581995-07-20 02:43:20 -05001504 png_write_chunk_end(png_ptr);
1505}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001506#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001507
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001508#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1509 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001510/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1511 * and if invalid, correct the keyword rather than discarding the entire
1512 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1513 * length, forbids leading or trailing whitespace, multiple internal spaces,
1514 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001515 *
1516 * The new_key is allocated to hold the corrected keyword and must be freed
1517 * by the calling routine. This avoids problems with trying to write to
1518 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001519 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001520png_size_t /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001521png_check_keyword(png_structp png_ptr, png_const_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001522{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001523 png_size_t key_len;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001524 png_const_charp ikp;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001525 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001526 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001527 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001528
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001529 png_debug(1, "in png_check_keyword");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001530
Andreas Dilger47a0c421997-05-16 02:46:07 -05001531 *new_key = NULL;
1532
1533 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001534 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001535 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001536 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001537 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001538
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001539 png_debug1(2, "Keyword to be checked is '%s'", key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001540
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001541 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001542
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001543 if (*new_key == NULL)
1544 {
1545 png_warning(png_ptr, "Out of memory while procesing keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001546 return ((png_size_t)0);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001547 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001548
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001549 /* Replace non-printing characters with a blank and print a warning */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001550 for (ikp = key, dp = *new_key; *ikp != '\0'; ikp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001551 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001552 if ((png_byte)*ikp < 0x20 ||
1553 ((png_byte)*ikp > 0x7E && (png_byte)*ikp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001554 {
John Bowler88b77cc2011-05-05 06:49:55 -05001555 PNG_WARNING_PARAMETERS(p)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001556
John Bowler88b77cc2011-05-05 06:49:55 -05001557 png_warning_parameter_unsigned(p, 1, PNG_NUMBER_FORMAT_02x,
John Bowlerd273ad22011-05-07 21:00:28 -05001558 (png_byte)*ikp);
John Bowler88b77cc2011-05-05 06:49:55 -05001559 png_formatted_warning(png_ptr, p, "invalid keyword character 0x@1");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001560 *dp = ' ';
1561 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001562
Andreas Dilger47a0c421997-05-16 02:46:07 -05001563 else
1564 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001565 *dp = *ikp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001566 }
1567 }
1568 *dp = '\0';
1569
1570 /* Remove any trailing white space. */
1571 kp = *new_key + key_len - 1;
1572 if (*kp == ' ')
1573 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001574 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001575
1576 while (*kp == ' ')
1577 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001578 *(kp--) = '\0';
1579 key_len--;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001580 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001581 }
1582
1583 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001584 kp = *new_key;
1585 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001586 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001587 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001588
1589 while (*kp == ' ')
1590 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001591 kp++;
1592 key_len--;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001593 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001594 }
1595
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001596 png_debug1(2, "Checking for multiple internal spaces in '%s'", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001597
Andreas Dilger47a0c421997-05-16 02:46:07 -05001598 /* Remove multiple internal spaces. */
1599 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001600 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001601 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001602 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001603 *(dp++) = *kp;
1604 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001605 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001606
Andreas Dilger47a0c421997-05-16 02:46:07 -05001607 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001608 {
1609 key_len--;
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001610 kwarn = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001611 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001612
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001613 else
1614 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001615 *(dp++) = *kp;
1616 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001617 }
1618 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001619 *dp = '\0';
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001620 if (kwarn)
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001621 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001622
1623 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001624 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001625 png_free(png_ptr, *new_key);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001626 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001627 }
1628
1629 if (key_len > 79)
1630 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001631 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001632 (*new_key)[79] = '\0';
Andreas Dilger47a0c421997-05-16 02:46:07 -05001633 key_len = 79;
1634 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001635
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001636 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001637}
1638#endif
1639
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001640#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001641/* Write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001642void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001643png_write_tEXt(png_structp png_ptr, png_const_charp key, png_const_charp text,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001644 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001645{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001646 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001647 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001648
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001649 png_debug(1, "in png_write_tEXt");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001650
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001651 if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001652 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001653
Andreas Dilger47a0c421997-05-16 02:46:07 -05001654 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001655 text_len = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001656
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001657 else
1658 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001659
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001660 /* Make sure we include the 0 after the key */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001661 png_write_chunk_header(png_ptr, png_tEXt,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001662 (png_uint_32)(key_len + text_len + 1));
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001663 /*
1664 * We leave it to the application to meet PNG-1.0 requirements on the
1665 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1666 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001667 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001668 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001669 png_write_chunk_data(png_ptr, (png_bytep)new_key,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001670 (png_size_t)(key_len + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001671
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001672 if (text_len)
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001673 png_write_chunk_data(png_ptr, (png_const_bytep)text,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001674 (png_size_t)text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001675
Guy Schalnat0d580581995-07-20 02:43:20 -05001676 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001677 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001678}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001679#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001680
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001681#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001682/* Write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001683void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001684png_write_zTXt(png_structp png_ptr, png_const_charp key, png_const_charp text,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001685 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001686{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001687 png_size_t key_len;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001688 png_byte buf;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001689 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001690 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001691
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001692 png_debug(1, "in png_write_zTXt");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001693
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001694 comp.num_output_ptr = 0;
1695 comp.max_output_ptr = 0;
1696 comp.output_ptr = NULL;
1697 comp.input = NULL;
1698 comp.input_len = 0;
1699
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001700 if ((key_len = png_check_keyword(png_ptr, key, &new_key)) == 0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001701 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001702 png_free(png_ptr, new_key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001703 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001704 }
1705
Andreas Dilger47a0c421997-05-16 02:46:07 -05001706 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1707 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001708 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001709 png_free(png_ptr, new_key);
1710 return;
1711 }
1712
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001713 text_len = png_strlen(text);
1714
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001715 /* Compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001716 text_len = png_text_compress(png_ptr, text, text_len, compression,
1717 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001718
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001719 /* Write start of chunk */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001720 png_write_chunk_header(png_ptr, png_zTXt,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001721 (png_uint_32)(key_len+text_len + 2));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001722
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001723 /* Write key */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001724 png_write_chunk_data(png_ptr, (png_bytep)new_key,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001725 (png_size_t)(key_len + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001726
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001727 png_free(png_ptr, new_key);
1728
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001729 buf = (png_byte)compression;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001730
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001731 /* Write compression */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001732 png_write_chunk_data(png_ptr, &buf, (png_size_t)1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001733
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001734 /* Write the compressed data */
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -05001735 comp.input_len = text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001736 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001737
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001738 /* Close the chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -05001739 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001740}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001741#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001742
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001743#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001744/* Write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001745void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001746png_write_iTXt(png_structp png_ptr, int compression, png_const_charp key,
1747 png_const_charp lang, png_const_charp lang_key, png_const_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001748{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001749 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001750 png_charp new_lang;
1751 png_charp new_key = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001752 png_byte cbuf[2];
1753 compression_state comp;
1754
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001755 png_debug(1, "in png_write_iTXt");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001756
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001757 comp.num_output_ptr = 0;
1758 comp.max_output_ptr = 0;
1759 comp.output_ptr = NULL;
1760 comp.input = NULL;
1761
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001762 if ((key_len = png_check_keyword(png_ptr, key, &new_key)) == 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001763 return;
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001764
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001765 if ((lang_len = png_check_keyword(png_ptr, lang, &new_lang)) == 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001766 {
1767 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001768 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001769 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001770 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001771
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001772 if (lang_key == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001773 lang_key_len = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001774
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001775 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001776 lang_key_len = png_strlen(lang_key);
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001777
1778 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001779 text_len = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001780
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001781 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001782 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001783
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001784 /* Compute the compressed data; do it now for the length */
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001785 text_len = png_text_compress(png_ptr, text, text_len, compression - 2,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001786 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001787
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001788
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001789 /* Make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001790 * and the NULs after the key, lang, and lang_key parts
1791 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001792
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001793 png_write_chunk_header(png_ptr, png_iTXt, (png_uint_32)(
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001794 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1795 + key_len
1796 + lang_len
1797 + lang_key_len
1798 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001799
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001800 /* We leave it to the application to meet PNG-1.0 requirements on the
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001801 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1802 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1803 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1804 */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001805 png_write_chunk_data(png_ptr, (png_bytep)new_key, (png_size_t)(key_len + 1));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001806
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001807 /* Set the compression flag */
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001808 if (compression == PNG_ITXT_COMPRESSION_NONE ||
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001809 compression == PNG_TEXT_COMPRESSION_NONE)
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001810 cbuf[0] = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001811
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001812 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001813 cbuf[0] = 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001814
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001815 /* Set the compression method */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001816 cbuf[1] = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001817
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001818 png_write_chunk_data(png_ptr, cbuf, (png_size_t)2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001819
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001820 cbuf[0] = 0;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001821 png_write_chunk_data(png_ptr, (new_lang ? (png_const_bytep)new_lang : cbuf),
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001822 (png_size_t)(lang_len + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001823
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001824 png_write_chunk_data(png_ptr, (lang_key ? (png_const_bytep)lang_key : cbuf),
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001825 (png_size_t)(lang_key_len + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001826
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001827 png_write_compressed_data_out(png_ptr, &comp);
1828
1829 png_write_chunk_end(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001830
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001831 png_free(png_ptr, new_key);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001832 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001833}
1834#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001835
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001836#ifdef PNG_WRITE_oFFs_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001837/* Write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001838void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001839png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001840 int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001841{
1842 png_byte buf[9];
1843
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001844 png_debug(1, "in png_write_oFFs");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001845
Andreas Dilger47a0c421997-05-16 02:46:07 -05001846 if (unit_type >= PNG_OFFSET_LAST)
1847 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1848
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001849 png_save_int_32(buf, x_offset);
1850 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001851 buf[8] = (png_byte)unit_type;
1852
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001853 png_write_complete_chunk(png_ptr, png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001854}
1855#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001856#ifdef PNG_WRITE_pCAL_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001857/* Write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001858void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001859png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001860 png_int_32 X1, int type, int nparams, png_const_charp units,
1861 png_charpp params)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001862{
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001863 png_size_t purpose_len, units_len, total_len;
John Bowlerf3f7e142011-09-09 07:32:37 -05001864 png_size_tp params_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001865 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001866 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001867 int i;
1868
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001869 png_debug1(1, "in png_write_pCAL (%d parameters)", nparams);
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001870
Andreas Dilger47a0c421997-05-16 02:46:07 -05001871 if (type >= PNG_EQUATION_LAST)
1872 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1873
1874 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001875 png_debug1(3, "pCAL purpose length = %d", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001876 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001877 png_debug1(3, "pCAL units length = %d", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001878 total_len = purpose_len + units_len + 10;
1879
John Bowlerf3f7e142011-09-09 07:32:37 -05001880 params_len = (png_size_tp)png_malloc(png_ptr,
1881 (png_alloc_size_t)(nparams * png_sizeof(png_size_t)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001882
1883 /* Find the length of each parameter, making sure we don't count the
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001884 * null terminator for the last parameter.
1885 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001886 for (i = 0; i < nparams; i++)
1887 {
1888 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001889 png_debug2(3, "pCAL parameter %d length = %lu", i,
Glenn Randers-Pehrsond2332872010-10-12 19:19:28 -05001890 (unsigned long)params_len[i]);
John Bowlerf3f7e142011-09-09 07:32:37 -05001891 total_len += params_len[i];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001892 }
1893
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001894 png_debug1(3, "pCAL total length = %d", (int)total_len);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001895 png_write_chunk_header(png_ptr, png_pCAL, (png_uint_32)total_len);
John Bowlerf3f7e142011-09-09 07:32:37 -05001896 png_write_chunk_data(png_ptr, (png_const_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001897 png_save_int_32(buf, X0);
1898 png_save_int_32(buf + 4, X1);
1899 buf[8] = (png_byte)type;
1900 buf[9] = (png_byte)nparams;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001901 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001902 png_write_chunk_data(png_ptr, (png_const_bytep)units, (png_size_t)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001903
1904 png_free(png_ptr, new_purpose);
1905
1906 for (i = 0; i < nparams; i++)
1907 {
John Bowlerf3f7e142011-09-09 07:32:37 -05001908 png_write_chunk_data(png_ptr, (png_const_bytep)params[i], params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001909 }
1910
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001911 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001912 png_write_chunk_end(png_ptr);
1913}
1914#endif
1915
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001916#ifdef PNG_WRITE_sCAL_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001917/* Write the sCAL chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001918void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001919png_write_sCAL_s(png_structp png_ptr, int unit, png_const_charp width,
1920 png_const_charp height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001921{
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001922 png_byte buf[64];
1923 png_size_t wlen, hlen, total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001924
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001925 png_debug(1, "in png_write_sCAL_s");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001926
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001927 wlen = png_strlen(width);
1928 hlen = png_strlen(height);
1929 total_len = wlen + hlen + 2;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001930
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001931 if (total_len > 64)
1932 {
1933 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
1934 return;
1935 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001936
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001937 buf[0] = (png_byte)unit;
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001938 png_memcpy(buf + 1, width, wlen + 1); /* Append the '\0' here */
1939 png_memcpy(buf + wlen + 2, height, hlen); /* Do NOT append the '\0' here */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001940
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001941 png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001942 png_write_complete_chunk(png_ptr, png_sCAL, buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001943}
1944#endif
1945
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001946#ifdef PNG_WRITE_pHYs_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001947/* Write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001948void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001949png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001950 png_uint_32 y_pixels_per_unit,
1951 int unit_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001952{
1953 png_byte buf[9];
1954
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001955 png_debug(1, "in png_write_pHYs");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001956
Guy Schalnate5a37791996-06-05 15:50:50 -05001957 if (unit_type >= PNG_RESOLUTION_LAST)
1958 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1959
Guy Schalnat0d580581995-07-20 02:43:20 -05001960 png_save_uint_32(buf, x_pixels_per_unit);
1961 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001962 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001963
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001964 png_write_complete_chunk(png_ptr, png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001965}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001966#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001967
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001968#ifdef PNG_WRITE_tIME_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001969/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1970 * or png_convert_from_time_t(), or fill in the structure yourself.
1971 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001972void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001973png_write_tIME(png_structp png_ptr, png_const_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001974{
1975 png_byte buf[7];
1976
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001977 png_debug(1, "in png_write_tIME");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001978
Guy Schalnate5a37791996-06-05 15:50:50 -05001979 if (mod_time->month > 12 || mod_time->month < 1 ||
1980 mod_time->day > 31 || mod_time->day < 1 ||
1981 mod_time->hour > 23 || mod_time->second > 60)
1982 {
1983 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1984 return;
1985 }
1986
Guy Schalnat0d580581995-07-20 02:43:20 -05001987 png_save_uint_16(buf, mod_time->year);
1988 buf[2] = mod_time->month;
1989 buf[3] = mod_time->day;
1990 buf[4] = mod_time->hour;
1991 buf[5] = mod_time->minute;
1992 buf[6] = mod_time->second;
1993
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001994 png_write_complete_chunk(png_ptr, png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001995}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001996#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001997
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001998/* Initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001999void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06002000png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05002001{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002002#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002003 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002004
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002005 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002006 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002007
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002008 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002009 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002010
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002011 /* Start of interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002012 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002013
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002014 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002015 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06002016#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002017
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002018 png_alloc_size_t buf_size;
2019 int usr_pixel_depth;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002020
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002021 png_debug(1, "in png_write_start_row");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05002022
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002023 usr_pixel_depth = png_ptr->usr_channels * png_ptr->usr_bit_depth;
2024 buf_size = PNG_ROWBYTES(usr_pixel_depth, png_ptr->width) + 1;
2025
2026 /* 1.5.6: added to allow checking in the row write code. */
2027 png_ptr->transformed_pixel_depth = png_ptr->pixel_depth;
2028 png_ptr->maximum_pixel_depth = (png_byte)usr_pixel_depth;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002029
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002030 /* Set up row buffer */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002031 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, buf_size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002032
Andreas Dilger47a0c421997-05-16 02:46:07 -05002033 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05002034
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002035#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002036 /* Set up filtering buffer, if using this filter */
Guy Schalnate5a37791996-06-05 15:50:50 -05002037 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002038 {
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002039 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, png_ptr->rowbytes + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002040
Andreas Dilger47a0c421997-05-16 02:46:07 -05002041 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05002042 }
2043
Andreas Dilger47a0c421997-05-16 02:46:07 -05002044 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05002045 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
2046 {
Glenn Randers-Pehrsondfa99af2009-10-29 23:33:46 -05002047 /* Set up previous row buffer */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002048 png_ptr->prev_row = (png_bytep)png_calloc(png_ptr, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05002049
2050 if (png_ptr->do_filter & PNG_FILTER_UP)
2051 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05002052 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002053 png_ptr->rowbytes + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002054
Andreas Dilger47a0c421997-05-16 02:46:07 -05002055 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05002056 }
2057
2058 if (png_ptr->do_filter & PNG_FILTER_AVG)
2059 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002060 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002061 png_ptr->rowbytes + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002062
Andreas Dilger47a0c421997-05-16 02:46:07 -05002063 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05002064 }
2065
2066 if (png_ptr->do_filter & PNG_FILTER_PAETH)
2067 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05002068 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002069 png_ptr->rowbytes + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002070
Andreas Dilger47a0c421997-05-16 02:46:07 -05002071 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05002072 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002073 }
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002074#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05002075
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002076#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002077 /* If interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002078 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05002079 {
2080 if (!(png_ptr->transformations & PNG_INTERLACE))
2081 {
2082 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002083 png_pass_ystart[0]) / png_pass_yinc[0];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002084
Andreas Dilger47a0c421997-05-16 02:46:07 -05002085 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002086 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05002087 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002088
Guy Schalnat0d580581995-07-20 02:43:20 -05002089 else
2090 {
2091 png_ptr->num_rows = png_ptr->height;
2092 png_ptr->usr_width = png_ptr->width;
2093 }
2094 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002095
Guy Schalnat0d580581995-07-20 02:43:20 -05002096 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002097#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002098 {
Guy Schalnat0d580581995-07-20 02:43:20 -05002099 png_ptr->num_rows = png_ptr->height;
2100 png_ptr->usr_width = png_ptr->width;
2101 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002102
John Bowlerc5bef942011-05-05 17:35:39 -05002103 png_zlib_claim(png_ptr, PNG_ZLIB_FOR_IDAT);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002104 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
2105 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05002106}
2107
Andreas Dilger47a0c421997-05-16 02:46:07 -05002108/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002109void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06002110png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05002111{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002112#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002113 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002114
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002115 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002116 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002117
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002118 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002119 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002120
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002121 /* Start of interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002122 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002123
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002124 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002125 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06002126#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002127
Guy Schalnat0d580581995-07-20 02:43:20 -05002128 int ret;
2129
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002130 png_debug(1, "in png_write_finish_row");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05002131
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002132 /* Next row */
Guy Schalnat0d580581995-07-20 02:43:20 -05002133 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002134
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002135 /* See if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06002136 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002137 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002138
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002139#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002140 /* If interlaced, go to next pass */
Guy Schalnat0d580581995-07-20 02:43:20 -05002141 if (png_ptr->interlaced)
2142 {
2143 png_ptr->row_number = 0;
2144 if (png_ptr->transformations & PNG_INTERLACE)
2145 {
2146 png_ptr->pass++;
2147 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002148
Guy Schalnat0d580581995-07-20 02:43:20 -05002149 else
2150 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002151 /* Loop until we find a non-zero width or height pass */
Guy Schalnat0d580581995-07-20 02:43:20 -05002152 do
2153 {
2154 png_ptr->pass++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002155
Guy Schalnat0d580581995-07-20 02:43:20 -05002156 if (png_ptr->pass >= 7)
2157 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002158
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002159 png_ptr->usr_width = (png_ptr->width +
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002160 png_pass_inc[png_ptr->pass] - 1 -
2161 png_pass_start[png_ptr->pass]) /
2162 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002163
Guy Schalnat0d580581995-07-20 02:43:20 -05002164 png_ptr->num_rows = (png_ptr->height +
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002165 png_pass_yinc[png_ptr->pass] - 1 -
2166 png_pass_ystart[png_ptr->pass]) /
2167 png_pass_yinc[png_ptr->pass];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002168
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002169 if (png_ptr->transformations & PNG_INTERLACE)
2170 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002171
Guy Schalnat0d580581995-07-20 02:43:20 -05002172 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
2173
2174 }
2175
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002176 /* Reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002177 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002178 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002179 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002180 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002181 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
2182 png_ptr->usr_bit_depth, png_ptr->width)) + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002183
Guy Schalnat0d580581995-07-20 02:43:20 -05002184 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002185 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002186 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002187#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002188
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002189 /* If we get here, we've just written the last row, so we need
Guy Schalnat0d580581995-07-20 02:43:20 -05002190 to flush the compressor */
2191 do
2192 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002193 /* Tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002194 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002195
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002196 /* Check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05002197 if (ret == Z_OK)
2198 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002199 /* Check to see if we need more room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05002200 if (!(png_ptr->zstream.avail_out))
2201 {
2202 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
2203 png_ptr->zstream.next_out = png_ptr->zbuf;
2204 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
2205 }
2206 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002207
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05002208 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05002209 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002210 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002211 png_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002212
Guy Schalnat0d580581995-07-20 02:43:20 -05002213 else
Guy Schalnat6d764711995-12-19 03:22:19 -06002214 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05002215 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002216 } while (ret != Z_STREAM_END);
2217
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002218 /* Write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002219 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05002220 {
2221 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002222 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05002223 }
2224
John Bowlerc5bef942011-05-05 17:35:39 -05002225 png_zlib_release(png_ptr);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -06002226 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat0d580581995-07-20 02:43:20 -05002227}
2228
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002229#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002230/* Pick out the correct pixels for the interlace pass.
2231 * The basic idea here is to go through the row with a source
2232 * pointer and a destination pointer (sp and dp), and copy the
2233 * correct pixels for the pass. As the row gets compacted,
2234 * sp will always be >= dp, so we should never overwrite anything.
2235 * See the default: case for the easiest code to understand.
2236 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002237void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06002238png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05002239{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002240 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002241
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002242 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002243 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002244
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002245 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002246 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002247
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002248 png_debug(1, "in png_do_write_interlace");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05002249
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002250 /* We don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002251 if (pass < 6)
Guy Schalnat0d580581995-07-20 02:43:20 -05002252 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002253 /* Each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05002254 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002255 {
Guy Schalnat0d580581995-07-20 02:43:20 -05002256 case 1:
2257 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002258 png_bytep sp;
2259 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05002260 int shift;
2261 int d;
2262 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002263 png_uint_32 i;
2264 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002265
2266 dp = row;
2267 d = 0;
2268 shift = 7;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002269
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002270 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002271 i += png_pass_inc[pass])
2272 {
2273 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002274 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05002275 d |= (value << shift);
2276
2277 if (shift == 0)
2278 {
2279 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002280 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002281 d = 0;
2282 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002283
Guy Schalnat0d580581995-07-20 02:43:20 -05002284 else
2285 shift--;
2286
2287 }
2288 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002289 *dp = (png_byte)d;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002290
Guy Schalnat0d580581995-07-20 02:43:20 -05002291 break;
2292 }
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002293
Guy Schalnat0d580581995-07-20 02:43:20 -05002294 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002295 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002296 png_bytep sp;
2297 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05002298 int shift;
2299 int d;
2300 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002301 png_uint_32 i;
2302 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002303
2304 dp = row;
2305 shift = 6;
2306 d = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002307
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002308 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002309 i += png_pass_inc[pass])
2310 {
2311 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002312 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05002313 d |= (value << shift);
2314
2315 if (shift == 0)
2316 {
2317 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002318 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002319 d = 0;
2320 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002321
Guy Schalnat0d580581995-07-20 02:43:20 -05002322 else
2323 shift -= 2;
2324 }
2325 if (shift != 6)
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002326 *dp = (png_byte)d;
2327
Guy Schalnat0d580581995-07-20 02:43:20 -05002328 break;
2329 }
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002330
Guy Schalnat0d580581995-07-20 02:43:20 -05002331 case 4:
2332 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002333 png_bytep sp;
2334 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002335 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05002336 int d;
2337 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002338 png_uint_32 i;
2339 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002340
2341 dp = row;
2342 shift = 4;
2343 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002344 for (i = png_pass_start[pass]; i < row_width;
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002345 i += png_pass_inc[pass])
Guy Schalnat0d580581995-07-20 02:43:20 -05002346 {
2347 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002348 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05002349 d |= (value << shift);
2350
2351 if (shift == 0)
2352 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002353 shift = 4;
2354 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002355 d = 0;
2356 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002357
Guy Schalnat0d580581995-07-20 02:43:20 -05002358 else
2359 shift -= 4;
2360 }
2361 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002362 *dp = (png_byte)d;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002363
Guy Schalnat0d580581995-07-20 02:43:20 -05002364 break;
2365 }
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002366
Guy Schalnat0d580581995-07-20 02:43:20 -05002367 default:
2368 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002369 png_bytep sp;
2370 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002371 png_uint_32 i;
2372 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002373 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002374
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002375 /* Start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002376 dp = row;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002377
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002378 /* Find out how many bytes each pixel takes up */
Guy Schalnat0d580581995-07-20 02:43:20 -05002379 pixel_bytes = (row_info->pixel_depth >> 3);
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002380
2381 /* Loop through the row, only looking at the pixels that matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002382 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002383 i += png_pass_inc[pass])
2384 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002385 /* Find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002386 sp = row + (png_size_t)i * pixel_bytes;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002387
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002388 /* Move the pixel */
Guy Schalnat0d580581995-07-20 02:43:20 -05002389 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002390 png_memcpy(dp, sp, pixel_bytes);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002391
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002392 /* Next pixel */
Guy Schalnat0d580581995-07-20 02:43:20 -05002393 dp += pixel_bytes;
2394 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002395 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002396 }
2397 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002398 /* Set new row width */
Guy Schalnat0d580581995-07-20 02:43:20 -05002399 row_info->width = (row_info->width +
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002400 png_pass_inc[pass] - 1 -
2401 png_pass_start[pass]) /
2402 png_pass_inc[pass];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002403
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002404 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2405 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002406 }
2407}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002408#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002409
Andreas Dilger47a0c421997-05-16 02:46:07 -05002410/* This filters the row, chooses which filter to use, if it has not already
2411 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002412 * chosen filter.
2413 */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002414static void png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row,
2415 png_size_t row_bytes);
John Bowlerc5bef942011-05-05 17:35:39 -05002416
Glenn Randers-Pehrson78067772004-11-02 21:49:39 -06002417#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002418#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002419#define PNG_LOMASK ((png_uint_32)0xffffL)
2420#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002421void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002422png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002423{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002424 png_bytep best_row;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002425#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002426 png_bytep prev_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002427 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002428 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002429 png_size_t row_bytes = row_info->rowbytes;
Glenn Randers-Pehrson9d8b41e2009-07-19 14:45:43 -05002430#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002431 int num_p_filters = png_ptr->num_prev_filters;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -05002432#endif
Glenn Randers-Pehrson9d8b41e2009-07-19 14:45:43 -05002433
2434 png_debug(1, "in png_write_find_filter");
2435
2436#ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson4ace0e12009-07-19 15:04:35 -05002437 if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS)
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05002438 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002439 /* These will never be selected so we need not test them. */
2440 filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_PAETH);
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05002441 }
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -05002442#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002443
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002444 /* Find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002445 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002446
2447 prev_row = png_ptr->prev_row;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002448#endif
2449 best_row = png_ptr->row_buf;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002450#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002451 row_buf = best_row;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002452 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002453
Andreas Dilger47a0c421997-05-16 02:46:07 -05002454 /* The prediction method we use is to find which method provides the
2455 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002456 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002457 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002458 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002459 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002460 * predictive" method (not implemented yet), which does test compressions
2461 * of lines using different filter methods, and then chooses the
2462 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002463 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002464 *
2465 * GRR 980525: consider also
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002466 *
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002467 * (1) minimum sum of absolute differences from running average (i.e.,
2468 * keep running sum of non-absolute differences & count of bytes)
2469 * [track dispersion, too? restart average if dispersion too large?]
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002470 *
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002471 * (1b) minimum sum of absolute differences from sliding average, probably
2472 * with window size <= deflate window (usually 32K)
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002473 *
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002474 * (2) minimum sum of squared differences from zero or running average
2475 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002476 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002477
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002478
Guy Schalnate5a37791996-06-05 15:50:50 -05002479 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002480 * that has been chosen, as it doesn't actually do anything to the data.
2481 */
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002482 if ((filter_to_do & PNG_FILTER_NONE) && filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002483 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002484 png_bytep rp;
2485 png_uint_32 sum = 0;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002486 png_size_t i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002487 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002488
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002489 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002490 {
2491 v = *rp;
2492 sum += (v < 128) ? v : 256 - v;
2493 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002494
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002495#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002496 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2497 {
2498 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002499 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002500 sumlo = sum & PNG_LOMASK;
2501 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2502
2503 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002504 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002505 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002506 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002507 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002508 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002509 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002510
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002511 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002512 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002513 }
2514 }
2515
2516 /* Factor in the cost of this filter (this is here for completeness,
2517 * but it makes no sense to have a "cost" for the NONE filter, as
2518 * it has the minimum possible computational cost - none).
2519 */
2520 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002521 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002522
Andreas Dilger47a0c421997-05-16 02:46:07 -05002523 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002524 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002525
2526 if (sumhi > PNG_HIMASK)
2527 sum = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002528
Andreas Dilger47a0c421997-05-16 02:46:07 -05002529 else
2530 sum = (sumhi << PNG_HISHIFT) + sumlo;
2531 }
2532#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002533 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002534 }
2535
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002536 /* Sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002537 if (filter_to_do == PNG_FILTER_SUB)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002538 /* It's the only filter so no testing is needed */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002539 {
2540 png_bytep rp, lp, dp;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002541 png_size_t i;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002542
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002543 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2544 i++, rp++, dp++)
2545 {
2546 *dp = *rp;
2547 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002548
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002549 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002550 i++, rp++, lp++, dp++)
2551 {
2552 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2553 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002554
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002555 best_row = png_ptr->sub_row;
2556 }
2557
2558 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002559 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002560 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002561 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002562 png_size_t i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002563 int v;
2564
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002565#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002566 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002567 * would reduce the sum of this filter, so that we can do the
2568 * early exit comparison without scaling the sum each time.
2569 */
2570 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2571 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002572 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002573 png_uint_32 lmhi, lmlo;
2574 lmlo = lmins & PNG_LOMASK;
2575 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2576
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002577 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002578 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002579 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002580 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002581 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002582 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002583
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002584 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002585 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002586 }
2587 }
2588
2589 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002590 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002591
Andreas Dilger47a0c421997-05-16 02:46:07 -05002592 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002593 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002594
2595 if (lmhi > PNG_HIMASK)
2596 lmins = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002597
Andreas Dilger47a0c421997-05-16 02:46:07 -05002598 else
2599 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2600 }
2601#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002602
Guy Schalnate5a37791996-06-05 15:50:50 -05002603 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2604 i++, rp++, dp++)
2605 {
2606 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002607
Guy Schalnate5a37791996-06-05 15:50:50 -05002608 sum += (v < 128) ? v : 256 - v;
2609 }
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002610
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002611 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002612 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002613 {
2614 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002615
Guy Schalnate5a37791996-06-05 15:50:50 -05002616 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002617
2618 if (sum > lmins) /* We are already worse, don't continue. */
2619 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002620 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002621
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002622#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002623 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2624 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002625 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002626 png_uint_32 sumhi, sumlo;
2627 sumlo = sum & PNG_LOMASK;
2628 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2629
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002630 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002631 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002632 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002633 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002634 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002635 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002636
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002637 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002638 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002639 }
2640 }
2641
2642 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002643 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002644
Andreas Dilger47a0c421997-05-16 02:46:07 -05002645 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002646 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002647
2648 if (sumhi > PNG_HIMASK)
2649 sum = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002650
Andreas Dilger47a0c421997-05-16 02:46:07 -05002651 else
2652 sum = (sumhi << PNG_HISHIFT) + sumlo;
2653 }
2654#endif
2655
Guy Schalnate5a37791996-06-05 15:50:50 -05002656 if (sum < mins)
2657 {
2658 mins = sum;
2659 best_row = png_ptr->sub_row;
2660 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002661 }
2662
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002663 /* Up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002664 if (filter_to_do == PNG_FILTER_UP)
2665 {
2666 png_bytep rp, dp, pp;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002667 png_size_t i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002668
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002669 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002670 pp = prev_row + 1; i < row_bytes;
2671 i++, rp++, pp++, dp++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002672 {
2673 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2674 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002675
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002676 best_row = png_ptr->up_row;
2677 }
2678
2679 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002680 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002681 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002682 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002683 png_size_t i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002684 int v;
2685
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002686
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002687#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002688 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2689 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002690 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002691 png_uint_32 lmhi, lmlo;
2692 lmlo = lmins & PNG_LOMASK;
2693 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2694
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002695 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002696 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002697 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002698 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002699 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002700 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002701
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002702 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002703 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002704 }
2705 }
2706
2707 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002708 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002709
Andreas Dilger47a0c421997-05-16 02:46:07 -05002710 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002711 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002712
2713 if (lmhi > PNG_HIMASK)
2714 lmins = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002715
Andreas Dilger47a0c421997-05-16 02:46:07 -05002716 else
2717 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2718 }
2719#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002720
2721 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002722 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002723 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002724 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002725
2726 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002727
2728 if (sum > lmins) /* We are already worse, don't continue. */
2729 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002730 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002731
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002732#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002733 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2734 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002735 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002736 png_uint_32 sumhi, sumlo;
2737 sumlo = sum & PNG_LOMASK;
2738 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2739
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002740 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002741 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002742 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002743 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002744 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002745 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002746
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002747 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002748 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002749 }
2750 }
2751
2752 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002753 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002754
Andreas Dilger47a0c421997-05-16 02:46:07 -05002755 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002756 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002757
2758 if (sumhi > PNG_HIMASK)
2759 sum = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002760
Andreas Dilger47a0c421997-05-16 02:46:07 -05002761 else
2762 sum = (sumhi << PNG_HISHIFT) + sumlo;
2763 }
2764#endif
2765
Guy Schalnate5a37791996-06-05 15:50:50 -05002766 if (sum < mins)
2767 {
2768 mins = sum;
2769 best_row = png_ptr->up_row;
2770 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002771 }
2772
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002773 /* Avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002774 if (filter_to_do == PNG_FILTER_AVG)
2775 {
2776 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002777 png_uint_32 i;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002778
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002779 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002780 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002781 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002782 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002783 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002784
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002785 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002786 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002787 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2788 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002789 }
2790 best_row = png_ptr->avg_row;
2791 }
2792
2793 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002794 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002795 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002796 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002797 png_size_t i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002798 int v;
2799
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002800#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002801 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2802 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002803 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002804 png_uint_32 lmhi, lmlo;
2805 lmlo = lmins & PNG_LOMASK;
2806 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2807
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002808 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002809 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002810 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002811 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002812 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002813 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002814
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002815 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002816 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002817 }
2818 }
2819
2820 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002821 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002822
Andreas Dilger47a0c421997-05-16 02:46:07 -05002823 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002824 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002825
2826 if (lmhi > PNG_HIMASK)
2827 lmins = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002828
Andreas Dilger47a0c421997-05-16 02:46:07 -05002829 else
2830 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2831 }
2832#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002833
2834 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002835 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002836 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002837 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002838
2839 sum += (v < 128) ? v : 256 - v;
2840 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002841
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002842 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002843 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002844 v = *dp++ =
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002845 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002846
2847 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002848
2849 if (sum > lmins) /* We are already worse, don't continue. */
2850 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002851 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002852
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002853#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002854 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2855 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002856 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002857 png_uint_32 sumhi, sumlo;
2858 sumlo = sum & PNG_LOMASK;
2859 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2860
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002861 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002862 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002863 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002864 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002865 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002866 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002867
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002868 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002869 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002870 }
2871 }
2872
2873 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002874 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002875
Andreas Dilger47a0c421997-05-16 02:46:07 -05002876 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002877 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002878
2879 if (sumhi > PNG_HIMASK)
2880 sum = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002881
Andreas Dilger47a0c421997-05-16 02:46:07 -05002882 else
2883 sum = (sumhi << PNG_HISHIFT) + sumlo;
2884 }
2885#endif
2886
Guy Schalnate5a37791996-06-05 15:50:50 -05002887 if (sum < mins)
2888 {
2889 mins = sum;
2890 best_row = png_ptr->avg_row;
2891 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002892 }
2893
Andreas Dilger47a0c421997-05-16 02:46:07 -05002894 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002895 if (filter_to_do == PNG_FILTER_PAETH)
2896 {
2897 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002898 png_size_t i;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002899
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002900 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002901 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002902 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002903 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002904 }
2905
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002906 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002907 {
2908 int a, b, c, pa, pb, pc, p;
2909
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002910 b = *pp++;
2911 c = *cp++;
2912 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002913
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002914 p = b - c;
2915 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002916
2917#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002918 pa = abs(p);
2919 pb = abs(pc);
2920 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002921#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002922 pa = p < 0 ? -p : p;
2923 pb = pc < 0 ? -pc : pc;
2924 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002925#endif
2926
2927 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2928
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002929 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002930 }
2931 best_row = png_ptr->paeth_row;
2932 }
2933
2934 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002935 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002936 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002937 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002938 png_size_t i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002939 int v;
2940
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002941#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002942 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2943 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002944 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002945 png_uint_32 lmhi, lmlo;
2946 lmlo = lmins & PNG_LOMASK;
2947 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2948
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002949 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002950 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002951 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002952 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002953 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002954 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002955
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002956 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002957 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002958 }
2959 }
2960
2961 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002962 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002963
Andreas Dilger47a0c421997-05-16 02:46:07 -05002964 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002965 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002966
2967 if (lmhi > PNG_HIMASK)
2968 lmins = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002969
Andreas Dilger47a0c421997-05-16 02:46:07 -05002970 else
2971 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2972 }
2973#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002974
2975 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002976 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002977 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002978 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002979
2980 sum += (v < 128) ? v : 256 - v;
2981 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002982
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002983 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002984 {
2985 int a, b, c, pa, pb, pc, p;
2986
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002987 b = *pp++;
2988 c = *cp++;
2989 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002990
2991#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002992 p = b - c;
2993 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002994#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002995 pa = abs(p);
2996 pb = abs(pc);
2997 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002998#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002999 pa = p < 0 ? -p : p;
3000 pb = pc < 0 ? -pc : pc;
3001 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003002#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003003 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
3004#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003005 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003006 pa = abs(p - a);
3007 pb = abs(p - b);
3008 pc = abs(p - c);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05003009
Guy Schalnate5a37791996-06-05 15:50:50 -05003010 if (pa <= pb && pa <= pc)
3011 p = a;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05003012
Guy Schalnate5a37791996-06-05 15:50:50 -05003013 else if (pb <= pc)
3014 p = b;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05003015
Guy Schalnate5a37791996-06-05 15:50:50 -05003016 else
3017 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003018#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05003019
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003020 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05003021
3022 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003023
3024 if (sum > lmins) /* We are already worse, don't continue. */
3025 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05003026 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003027
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003028#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003029 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
3030 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05003031 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003032 png_uint_32 sumhi, sumlo;
3033 sumlo = sum & PNG_LOMASK;
3034 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
3035
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003036 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05003037 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003038 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05003039 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003040 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06003041 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003042
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003043 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06003044 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003045 }
3046 }
3047
3048 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06003049 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003050
Andreas Dilger47a0c421997-05-16 02:46:07 -05003051 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06003052 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003053
3054 if (sumhi > PNG_HIMASK)
3055 sum = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003056
Andreas Dilger47a0c421997-05-16 02:46:07 -05003057 else
3058 sum = (sumhi << PNG_HISHIFT) + sumlo;
3059 }
3060#endif
3061
Guy Schalnate5a37791996-06-05 15:50:50 -05003062 if (sum < mins)
3063 {
3064 best_row = png_ptr->paeth_row;
3065 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003066 }
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05003067#endif /* PNG_WRITE_FILTER_SUPPORTED */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003068
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003069 /* Do the actual writing of the filtered row data from the chosen filter. */
3070 png_write_filtered_row(png_ptr, best_row, row_info->rowbytes+1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05003071
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05003072#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003073#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003074 /* Save the type of filter we picked this time for future calculations */
3075 if (png_ptr->num_prev_filters > 0)
3076 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003077 int j;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003078
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003079 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05003080 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003081 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05003082 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05003083
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003084 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05003085 }
3086#endif
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05003087#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -05003088}
3089
3090
Andreas Dilger47a0c421997-05-16 02:46:07 -05003091/* Do the actual writing of a previously filtered row. */
John Bowlerc5bef942011-05-05 17:35:39 -05003092static void
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003093png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row,
3094 png_size_t avail/*includes filter byte*/)
Guy Schalnate5a37791996-06-05 15:50:50 -05003095{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003096 png_debug(1, "in png_write_filtered_row");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05003097
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003098 png_debug1(2, "filter = %d", filtered_row[0]);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003099 /* Set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06003100
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003101 png_ptr->zstream.next_in = filtered_row;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05003102 png_ptr->zstream.avail_in = 0;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003103 /* Repeat until we have compressed all the data */
Guy Schalnate5a37791996-06-05 15:50:50 -05003104 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003105 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003106 int ret; /* Return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05003107
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05003108 /* Record the number of bytes available - zlib supports at least 65535
3109 * bytes at one step, depending on the size of the zlib type 'uInt', the
3110 * maximum size zlib can write at once is ZLIB_IO_MAX (from pngpriv.h).
3111 * Use this because on 16 bit systems 'rowbytes' can be up to 65536 (i.e.
3112 * one more than 16 bits) and, in this case 'rowbytes+1' can overflow a
3113 * uInt. ZLIB_IO_MAX can be safely reduced to cause zlib to be called
3114 * with smaller chunks of data.
3115 */
3116 if (png_ptr->zstream.avail_in == 0)
3117 {
3118 if (avail > ZLIB_IO_MAX)
3119 {
3120 png_ptr->zstream.avail_in = ZLIB_IO_MAX;
3121 avail -= ZLIB_IO_MAX;
3122 }
3123
3124 else
3125 {
3126 /* So this will fit in the available uInt space: */
3127 png_ptr->zstream.avail_in = (uInt)avail;
3128 avail = 0;
3129 }
3130 }
3131
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003132 /* Compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003133 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003134
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003135 /* Check for compression errors */
Guy Schalnate5a37791996-06-05 15:50:50 -05003136 if (ret != Z_OK)
3137 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05003138 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003139 png_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003140
Guy Schalnate5a37791996-06-05 15:50:50 -05003141 else
3142 png_error(png_ptr, "zlib error");
3143 }
Guy Schalnat0d580581995-07-20 02:43:20 -05003144
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003145 /* See if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05003146 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05003147 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003148 /* Write the IDAT and reset the zlib output buffer */
Guy Schalnate5a37791996-06-05 15:50:50 -05003149 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05003150 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003151 /* Repeat until all data has been compressed */
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05003152 } while (avail > 0 || png_ptr->zstream.avail_in > 0);
Guy Schalnate5a37791996-06-05 15:50:50 -05003153
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003154 /* Swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05003155 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05003156 {
3157 png_bytep tptr;
3158
3159 tptr = png_ptr->prev_row;
3160 png_ptr->prev_row = png_ptr->row_buf;
3161 png_ptr->row_buf = tptr;
3162 }
3163
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003164 /* Finish row - updates counters and flushes zlib if last row */
Guy Schalnate5a37791996-06-05 15:50:50 -05003165 png_write_finish_row(png_ptr);
3166
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003167#ifdef PNG_WRITE_FLUSH_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05003168 png_ptr->flush_rows++;
3169
3170 if (png_ptr->flush_dist > 0 &&
3171 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05003172 {
Guy Schalnate5a37791996-06-05 15:50:50 -05003173 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05003174 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003175#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003176}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05003177#endif /* PNG_WRITE_SUPPORTED */