blob: b3ed84f9c01388bf7692fbfc5d42306dd85e6711 [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 *
John Bowler5d567862011-12-24 09:12:00 -06004 * Last changed in libpng 1.6.0 [(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
John Bowler5d567862011-12-24 09:12:00 -060068png_write_sig(png_structrp png_ptr)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050069{
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
John Bowler5d567862011-12-24 09:12:00 -060090png_write_chunk_header(png_structrp 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
John Bowler5d567862011-12-24 09:12:00 -0600132png_write_chunk_start(png_structrp png_ptr, png_const_bytep chunk_string,
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500133 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
John Bowler5d567862011-12-24 09:12:00 -0600144png_write_chunk_data(png_structrp png_ptr, png_const_bytep data,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500145 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
John Bowler5d567862011-12-24 09:12:00 -0600164png_write_chunk_end(png_structrp 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
John Bowler5d567862011-12-24 09:12:00 -0600193png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name,
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500194 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
John Bowler5d567862011-12-24 09:12:00 -0600210png_write_chunk(png_structrp png_ptr, png_const_bytep chunk_string,
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500211 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
John Bowler5d567862011-12-24 09:12:00 -0600219png_zlib_claim(png_structrp png_ptr, png_uint_32 state)
John Bowlerc5bef942011-05-05 17:35:39 -0500220{
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
John Bowler5d567862011-12-24 09:12:00 -0600308png_zlib_release(png_structrp png_ptr)
John Bowlerc5bef942011-05-05 17:35:39 -0500309{
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 */
John Bowler5d567862011-12-24 09:12:00 -0600377png_text_compress(png_structrp 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 */
John Bowler5d567862011-12-24 09:12:00 -0600572png_write_compressed_data_out(png_structrp png_ptr, compression_state *comp)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600573{
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
John Bowler5c1905c2011-10-14 12:33:52 -0500585 /* The zbuf_size test is because the code below doesn't work if zbuf_size is
586 * '1'; simply skip it to avoid memory overwrite.
587 */
588 if (comp->input_len >= 2 && comp->input_len < 16384 && png_ptr->zbuf_size > 1)
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -0500589 {
590 unsigned int z_cmf; /* zlib compression method and flags */
591
592 /* Optimize the CMF field in the zlib stream. This hack of the zlib
593 * stream is compliant to the stream specification.
594 */
595
596 if (comp->num_output_ptr)
597 z_cmf = comp->output_ptr[0][0];
598 else
599 z_cmf = png_ptr->zbuf[0];
600
601 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
602 {
603 unsigned int z_cinfo;
604 unsigned int half_z_window_size;
Glenn Randers-Pehrsonc559bb52011-05-05 16:55:20 -0500605 png_size_t uncompressed_text_size = comp->input_len;
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -0500606
607 z_cinfo = z_cmf >> 4;
608 half_z_window_size = 1 << (z_cinfo + 7);
609
Glenn Randers-Pehrsonc559bb52011-05-05 16:55:20 -0500610 while (uncompressed_text_size <= half_z_window_size &&
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -0500611 half_z_window_size >= 256)
612 {
613 z_cinfo--;
614 half_z_window_size >>= 1;
615 }
616
617 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
618
619 if (comp->num_output_ptr)
620 {
621
622 if (comp->output_ptr[0][0] != z_cmf)
623 {
624 int tmp;
625
626 comp->output_ptr[0][0] = (png_byte)z_cmf;
627 tmp = comp->output_ptr[0][1] & 0xe0;
628 tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
629 comp->output_ptr[0][1] = (png_byte)tmp;
630 }
631 }
632 else
633 {
634 int tmp;
635
636 png_ptr->zbuf[0] = (png_byte)z_cmf;
637 tmp = png_ptr->zbuf[1] & 0xe0;
638 tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
639 png_ptr->zbuf[1] = (png_byte)tmp;
640 }
641 }
642
643 else
644 png_error(png_ptr,
645 "Invalid zlib compression method or flags in non-IDAT chunk");
646 }
Glenn Randers-Pehrsonc559bb52011-05-05 16:55:20 -0500647#endif /* PNG_WRITE_OPTIMIZE_CMF_SUPPORTED */
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -0500648
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500649 /* Write saved output buffers, if any */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600650 for (i = 0; i < comp->num_output_ptr; i++)
651 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500652 png_write_chunk_data(png_ptr, comp->output_ptr[i],
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600653 (png_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500654
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600655 png_free(png_ptr, comp->output_ptr[i]);
656 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500657
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600658 if (comp->max_output_ptr != 0)
659 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500660
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500661 /* Write anything left in zbuf */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600662 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
663 png_write_chunk_data(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600664 (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600665
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500666 /* Reset zlib for another zTXt/iTXt or image data */
John Bowlerc5bef942011-05-05 17:35:39 -0500667 png_zlib_release(png_ptr);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600668}
Glenn Randers-Pehrson205483d2011-04-01 12:33:42 -0500669#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600670
Guy Schalnat0d580581995-07-20 02:43:20 -0500671/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600672 * information. Note that the rest of this code depends upon this
673 * information being correct.
674 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500675void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600676png_write_IHDR(png_structrp png_ptr, png_uint_32 width, png_uint_32 height,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600677 int bit_depth, int color_type, int compression_type, int filter_type,
678 int interlace_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500679{
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500680 png_byte buf[13]; /* Buffer to store the IHDR info */
Guy Schalnat0d580581995-07-20 02:43:20 -0500681
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500682 png_debug(1, "in png_write_IHDR");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500683
Guy Schalnate5a37791996-06-05 15:50:50 -0500684 /* Check that we have valid input data from the application info */
685 switch (color_type)
686 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500687 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500688 switch (bit_depth)
689 {
690 case 1:
691 case 2:
692 case 4:
693 case 8:
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500694#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600695 case 16:
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500696#endif
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600697 png_ptr->channels = 1; break;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500698
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600699 default:
700 png_error(png_ptr,
701 "Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500702 }
703 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500704
Andreas Dilger47a0c421997-05-16 02:46:07 -0500705 case PNG_COLOR_TYPE_RGB:
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500706#ifdef PNG_WRITE_16BIT_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500707 if (bit_depth != 8 && bit_depth != 16)
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500708#else
709 if (bit_depth != 8)
710#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500711 png_error(png_ptr, "Invalid bit depth for RGB image");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500712
Guy Schalnate5a37791996-06-05 15:50:50 -0500713 png_ptr->channels = 3;
714 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500715
Andreas Dilger47a0c421997-05-16 02:46:07 -0500716 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500717 switch (bit_depth)
718 {
719 case 1:
720 case 2:
721 case 4:
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600722 case 8:
723 png_ptr->channels = 1;
724 break;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500725
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600726 default:
727 png_error(png_ptr, "Invalid bit depth for paletted image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500728 }
729 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500730
Andreas Dilger47a0c421997-05-16 02:46:07 -0500731 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500732 if (bit_depth != 8 && bit_depth != 16)
733 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500734
Guy Schalnate5a37791996-06-05 15:50:50 -0500735 png_ptr->channels = 2;
736 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500737
Andreas Dilger47a0c421997-05-16 02:46:07 -0500738 case PNG_COLOR_TYPE_RGB_ALPHA:
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500739#ifdef PNG_WRITE_16BIT_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500740 if (bit_depth != 8 && bit_depth != 16)
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -0500741#else
742 if (bit_depth != 8)
743#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500744 png_error(png_ptr, "Invalid bit depth for RGBA image");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500745
Guy Schalnate5a37791996-06-05 15:50:50 -0500746 png_ptr->channels = 4;
747 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500748
Guy Schalnate5a37791996-06-05 15:50:50 -0500749 default:
750 png_error(png_ptr, "Invalid image color type specified");
751 }
752
Andreas Dilger47a0c421997-05-16 02:46:07 -0500753 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500754 {
755 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500756 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500757 }
758
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600759 /* Write filter_method 64 (intrapixel differencing) only if
760 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
761 * 2. Libpng did not write a PNG signature (this filter_method is only
762 * used in PNG datastreams that are embedded in MNG datastreams) and
763 * 3. The application called png_permit_mng_features with a mask that
764 * included PNG_FLAG_MNG_FILTER_64 and
765 * 4. The filter_method is 64 and
766 * 5. The color_type is RGB or RGBA
767 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600768 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500769#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600770 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
771 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
772 (color_type == PNG_COLOR_TYPE_RGB ||
773 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
774 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600775#endif
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600776 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500777 {
778 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500779 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500780 }
781
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600782#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500783 if (interlace_type != PNG_INTERLACE_NONE &&
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500784 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500785 {
786 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500787 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500788 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600789#else
790 interlace_type=PNG_INTERLACE_NONE;
791#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500792
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500793 /* Save the relevent information */
Guy Schalnate5a37791996-06-05 15:50:50 -0500794 png_ptr->bit_depth = (png_byte)bit_depth;
795 png_ptr->color_type = (png_byte)color_type;
796 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500797#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600798 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500799#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500800 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500801 png_ptr->width = width;
802 png_ptr->height = height;
803
804 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500805 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500806 /* Set the usr info, so any transformations can modify it */
Guy Schalnate5a37791996-06-05 15:50:50 -0500807 png_ptr->usr_width = png_ptr->width;
808 png_ptr->usr_bit_depth = png_ptr->bit_depth;
809 png_ptr->usr_channels = png_ptr->channels;
810
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500811 /* Pack the header information into the buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500812 png_save_uint_32(buf, width);
813 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600814 buf[8] = (png_byte)bit_depth;
815 buf[9] = (png_byte)color_type;
816 buf[10] = (png_byte)compression_type;
817 buf[11] = (png_byte)filter_type;
818 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500819
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500820 /* Write the chunk */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500821 png_write_complete_chunk(png_ptr, png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500822
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500823 /* Initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600824 png_ptr->zstream.zalloc = png_zalloc;
825 png_ptr->zstream.zfree = png_zfree;
826 png_ptr->zstream.opaque = (voidpf)png_ptr;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500827
Guy Schalnate5a37791996-06-05 15:50:50 -0500828 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500829 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500830 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600831 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500832 png_ptr->do_filter = PNG_FILTER_NONE;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500833
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500834 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500835 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500836 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500837
Guy Schalnate5a37791996-06-05 15:50:50 -0500838 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500839 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500840 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500841 png_ptr->zlib_strategy = Z_FILTERED;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500842
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500843 else
844 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
845 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -0500846
Guy Schalnate5a37791996-06-05 15:50:50 -0500847 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500848 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
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_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500851 png_ptr->zlib_mem_level = 8;
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_WINDOW_BITS))
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500854 png_ptr->zlib_window_bits = 15;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500855
Guy Schalnate5a37791996-06-05 15:50:50 -0500856 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500857 png_ptr->zlib_method = 8;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500858
Glenn Randers-Pehrson205483d2011-04-01 12:33:42 -0500859#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
John Bowlerb2bee332011-06-10 23:24:58 -0500860#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500861 if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_STRATEGY))
862 png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY;
863
864 if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_LEVEL))
Glenn Randers-Pehrsonc6831002011-04-01 15:24:18 -0500865 png_ptr->zlib_text_level = png_ptr->zlib_level;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500866
867 if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_MEM_LEVEL))
Glenn Randers-Pehrsonc6831002011-04-01 15:24:18 -0500868 png_ptr->zlib_text_mem_level = png_ptr->zlib_mem_level;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500869
870 if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrsonc6831002011-04-01 15:24:18 -0500871 png_ptr->zlib_text_window_bits = png_ptr->zlib_window_bits;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500872
873 if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_METHOD))
Glenn Randers-Pehrsonc6831002011-04-01 15:24:18 -0500874 png_ptr->zlib_text_method = png_ptr->zlib_method;
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500875#else
876 png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY;
Glenn Randers-Pehrsonc6831002011-04-01 15:24:18 -0500877 png_ptr->zlib_text_level = png_ptr->zlib_level;
878 png_ptr->zlib_text_mem_level = png_ptr->zlib_mem_level;
879 png_ptr->zlib_text_window_bits = png_ptr->zlib_window_bits;
880 png_ptr->zlib_text_method = png_ptr->zlib_method;
John Bowlerb2bee332011-06-10 23:24:58 -0500881#endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED */
Glenn Randers-Pehrson205483d2011-04-01 12:33:42 -0500882#endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500883
John Bowlerc5bef942011-05-05 17:35:39 -0500884 /* Record that the compressor has not yet been initialized. */
885 png_ptr->zlib_state = PNG_ZLIB_UNINITIALIZED;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500886
Glenn Randers-Pehrsonf8378312011-03-31 22:06:04 -0500887 png_ptr->mode = PNG_HAVE_IHDR; /* not READY_FOR_ZTXT */
Guy Schalnat0d580581995-07-20 02:43:20 -0500888}
889
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500890/* Write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500891 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600892 * structure.
893 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500894void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600895png_write_PLTE(png_structrp png_ptr, png_const_colorp palette,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500896 png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500897{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500898 png_uint_32 i;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500899 png_const_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500900 png_byte buf[3];
901
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500902 png_debug(1, "in png_write_PLTE");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -0500903
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500904 if ((
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500905#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600906 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500907#endif
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600908 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500909 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600910 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
911 {
912 png_error(png_ptr, "Invalid number of colors in palette");
913 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500914
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600915 else
916 {
917 png_warning(png_ptr, "Invalid number of colors in palette");
918 return;
919 }
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500920 }
921
922 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
923 {
924 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600925 "Ignoring request to write a PLTE chunk in grayscale PNG");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500926
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500927 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500928 }
929
Andreas Dilger47a0c421997-05-16 02:46:07 -0500930 png_ptr->num_palette = (png_uint_16)num_pal;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500931 png_debug1(3, "num_palette = %d", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500932
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -0500933 png_write_chunk_header(png_ptr, png_PLTE, (png_uint_32)(num_pal * 3));
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -0500934#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500935
Andreas Dilger47a0c421997-05-16 02:46:07 -0500936 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500937 {
938 buf[0] = pal_ptr->red;
939 buf[1] = pal_ptr->green;
940 buf[2] = pal_ptr->blue;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500941 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500942 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500943
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500944#else
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600945 /* This is a little slower but some buggy compilers need to do this
946 * instead
947 */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500948 pal_ptr=palette;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500949
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500950 for (i = 0; i < num_pal; i++)
951 {
952 buf[0] = pal_ptr[i].red;
953 buf[1] = pal_ptr[i].green;
954 buf[2] = pal_ptr[i].blue;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500955 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500956 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500957
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500958#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500959 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500960 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500961}
962
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500963/* Write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500964void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -0600965png_write_IDAT(png_structrp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500966{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500967 png_debug(1, "in png_write_IDAT");
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500968
John Bowlerc5bef942011-05-05 17:35:39 -0500969#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500970 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
971 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
972 {
Glenn Randers-Pehrsonec8296a2011-04-01 15:09:05 -0500973 /* Optimize the CMF field in the zlib stream. This hack of the zlib
974 * stream is compliant to the stream specification.
975 */
John Bowlerc5bef942011-05-05 17:35:39 -0500976 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
Glenn Randers-Pehrson6b3d50b2011-03-31 20:14:29 -0500977
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500978 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
979 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500980 /* Avoid memory underflows and multiplication overflows.
981 *
982 * The conditions below are practically always satisfied;
983 * however, they still must be checked.
984 */
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500985 if (length >= 2 &&
986 png_ptr->height < 16384 && png_ptr->width < 16384)
987 {
Glenn Randers-Pehrsonc3b32402011-04-01 22:10:41 -0500988 /* Compute the maximum possible length of the datastream */
989
Glenn Randers-Pehrson3fceee02011-05-23 06:31:33 -0500990 /* Number of pixels, plus for each row a filter byte
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -0500991 * and possibly a padding byte, so increase the maximum
992 * size to account for these.
Glenn Randers-Pehrsonec8296a2011-04-01 15:09:05 -0500993 */
Glenn Randers-Pehrsone99107b2011-04-02 05:49:03 -0500994 unsigned int z_cinfo;
995 unsigned int half_z_window_size;
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500996 png_uint_32 uncompressed_idat_size = png_ptr->height *
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -0600997 ((png_ptr->width *
998 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
Glenn Randers-Pehrsonc3b32402011-04-01 22:10:41 -0500999
1000 /* If it's interlaced, each block of 8 rows is sent as up to
1001 * 14 rows, i.e., 6 additional rows, each with a filter byte
1002 * and possibly a padding byte
1003 */
1004 if (png_ptr->interlaced)
1005 uncompressed_idat_size += ((png_ptr->height + 7)/8) *
1006 (png_ptr->bit_depth < 8 ? 12 : 6);
1007
Glenn Randers-Pehrsone99107b2011-04-02 05:49:03 -05001008 z_cinfo = z_cmf >> 4;
1009 half_z_window_size = 1 << (z_cinfo + 7);
1010
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -05001011 while (uncompressed_idat_size <= half_z_window_size &&
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001012 half_z_window_size >= 256)
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -05001013 {
1014 z_cinfo--;
1015 half_z_window_size >>= 1;
1016 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001017
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -05001018 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001019
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001020 if (data[0] != z_cmf)
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -05001021 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001022 int tmp;
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -05001023 data[0] = (png_byte)z_cmf;
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05001024 tmp = data[1] & 0xe0;
1025 tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
1026 data[1] = (png_byte)tmp;
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -05001027 }
1028 }
1029 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001030
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -05001031 else
1032 png_error(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001033 "Invalid zlib compression method or flags in IDAT");
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -05001034 }
John Bowlerc5bef942011-05-05 17:35:39 -05001035#endif /* PNG_WRITE_OPTIMIZE_CMF_SUPPORTED */
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -05001036
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001037 png_write_complete_chunk(png_ptr, png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -05001038 png_ptr->mode |= PNG_HAVE_IDAT;
John Bowlerc5bef942011-05-05 17:35:39 -05001039
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001040 /* Prior to 1.5.4 this code was replicated in every caller (except at the
John Bowlerc5bef942011-05-05 17:35:39 -05001041 * end, where it isn't technically necessary). Since this function has
1042 * flushed the data we can safely reset the zlib output buffer here.
1043 */
1044 png_ptr->zstream.next_out = png_ptr->zbuf;
1045 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnat0d580581995-07-20 02:43:20 -05001046}
1047
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001048/* Write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001049void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001050png_write_IEND(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001051{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001052 png_debug(1, "in png_write_IEND");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001053
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001054 png_write_complete_chunk(png_ptr, png_IEND, NULL, (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001055 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -05001056}
1057
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001058#ifdef PNG_WRITE_gAMA_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001059/* Write a gAMA chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001060void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001061png_write_gAMA_fixed(png_structrp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001062{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001063 png_byte buf[4];
1064
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001065 png_debug(1, "in png_write_gAMA");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001066
Glenn Randers-Pehrson626afd62009-08-20 22:52:51 -05001067 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001068 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001069 png_write_complete_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001070}
1071#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001072
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001073#ifdef PNG_WRITE_sRGB_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001074/* Write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001075void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001076png_write_sRGB(png_structrp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001077{
1078 png_byte buf[1];
1079
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001080 png_debug(1, "in png_write_sRGB");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001081
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001082 if (srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001083 png_warning(png_ptr,
1084 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001085
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001086 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001087 png_write_complete_chunk(png_ptr, png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001088}
1089#endif
1090
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001091#ifdef PNG_WRITE_iCCP_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001092/* Write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001093void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001094png_write_iCCP(png_structrp png_ptr, png_const_charp name, int compression_type,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001095 png_const_charp profile, int profile_len)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001096{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001097 png_size_t name_len;
1098 png_charp new_name;
1099 compression_state comp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001100 int embedded_profile_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001101
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001102 png_debug(1, "in png_write_iCCP");
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001103
1104 comp.num_output_ptr = 0;
1105 comp.max_output_ptr = 0;
1106 comp.output_ptr = NULL;
1107 comp.input = NULL;
1108 comp.input_len = 0;
1109
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001110 if ((name_len = png_check_keyword(png_ptr, name, &new_name)) == 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001111 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001112
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -06001113 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001114 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001115
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -05001116 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001117 profile_len = 0;
1118
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001119 if (profile_len > 3)
Glenn Randers-Pehrson7edd4582006-12-07 19:16:44 -06001120 embedded_profile_len =
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001121 ((*( (png_const_bytep)profile ))<<24) |
1122 ((*( (png_const_bytep)profile + 1))<<16) |
1123 ((*( (png_const_bytep)profile + 2))<< 8) |
1124 ((*( (png_const_bytep)profile + 3)) );
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001125
Glenn Randers-Pehrson3a054e12009-08-01 08:53:23 -05001126 if (embedded_profile_len < 0)
1127 {
1128 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001129 "Embedded profile length in iCCP chunk is negative");
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001130
Glenn Randers-Pehrson3a054e12009-08-01 08:53:23 -05001131 png_free(png_ptr, new_name);
1132 return;
1133 }
1134
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001135 if (profile_len < embedded_profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001136 {
1137 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001138 "Embedded profile length too large in iCCP chunk");
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001139
Glenn Randers-Pehrsona6cc6272009-04-01 14:18:43 -05001140 png_free(png_ptr, new_name);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001141 return;
1142 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001143
1144 if (profile_len > embedded_profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001145 {
1146 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001147 "Truncating profile to actual length in iCCP chunk");
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001148
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001149 profile_len = embedded_profile_len;
1150 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001151
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001152 if (profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001153 profile_len = png_text_compress(png_ptr, profile,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001154 (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001155
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001156 /* Make sure we include the NULL after the name and the compression type */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001157 png_write_chunk_header(png_ptr, png_iCCP,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001158 (png_uint_32)(name_len + profile_len + 2));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001159
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001160 new_name[name_len + 1] = 0x00;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001161
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001162 png_write_chunk_data(png_ptr, (png_bytep)new_name,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001163 (png_size_t)(name_len + 2));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001164
1165 if (profile_len)
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -05001166 {
1167 comp.input_len = profile_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001168 png_write_compressed_data_out(png_ptr, &comp);
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -05001169 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001170
1171 png_write_chunk_end(png_ptr);
1172 png_free(png_ptr, new_name);
1173}
1174#endif
1175
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001176#ifdef PNG_WRITE_sPLT_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001177/* Write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001178void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001179png_write_sPLT(png_structrp png_ptr, png_const_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001180{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001181 png_size_t name_len;
1182 png_charp new_name;
1183 png_byte entrybuf[10];
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05001184 png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
1185 png_size_t palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001186 png_sPLT_entryp ep;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001187#ifndef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001188 int i;
1189#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001190
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001191 png_debug(1, "in png_write_sPLT");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001192
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001193 if ((name_len = png_check_keyword(png_ptr,spalette->name, &new_name))==0)
1194 return;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001195
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001196 /* Make sure we include the NULL after the name */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001197 png_write_chunk_header(png_ptr, png_sPLT,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001198 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001199
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001200 png_write_chunk_data(png_ptr, (png_bytep)new_name,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001201 (png_size_t)(name_len + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001202
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001203 png_write_chunk_data(png_ptr, &spalette->depth, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001204
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001205 /* Loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05001206#ifdef PNG_POINTER_INDEXING_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001207 for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001208 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001209 if (spalette->depth == 8)
1210 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001211 entrybuf[0] = (png_byte)ep->red;
1212 entrybuf[1] = (png_byte)ep->green;
1213 entrybuf[2] = (png_byte)ep->blue;
1214 entrybuf[3] = (png_byte)ep->alpha;
1215 png_save_uint_16(entrybuf + 4, ep->frequency);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001216 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001217
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001218 else
1219 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001220 png_save_uint_16(entrybuf + 0, ep->red);
1221 png_save_uint_16(entrybuf + 2, ep->green);
1222 png_save_uint_16(entrybuf + 4, ep->blue);
1223 png_save_uint_16(entrybuf + 6, ep->alpha);
1224 png_save_uint_16(entrybuf + 8, ep->frequency);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001225 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001226
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001227 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001228 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001229#else
1230 ep=spalette->entries;
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001231 for (i = 0; i>spalette->nentries; i++)
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001232 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001233 if (spalette->depth == 8)
1234 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001235 entrybuf[0] = (png_byte)ep[i].red;
1236 entrybuf[1] = (png_byte)ep[i].green;
1237 entrybuf[2] = (png_byte)ep[i].blue;
1238 entrybuf[3] = (png_byte)ep[i].alpha;
1239 png_save_uint_16(entrybuf + 4, ep[i].frequency);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001240 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001241
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001242 else
1243 {
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001244 png_save_uint_16(entrybuf + 0, ep[i].red);
1245 png_save_uint_16(entrybuf + 2, ep[i].green);
1246 png_save_uint_16(entrybuf + 4, ep[i].blue);
1247 png_save_uint_16(entrybuf + 6, ep[i].alpha);
1248 png_save_uint_16(entrybuf + 8, ep[i].frequency);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001249 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001250
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001251 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05001252 }
1253#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001254
1255 png_write_chunk_end(png_ptr);
1256 png_free(png_ptr, new_name);
1257}
1258#endif
1259
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001260#ifdef PNG_WRITE_sBIT_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001261/* Write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001262void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001263png_write_sBIT(png_structrp png_ptr, png_const_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001264{
1265 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001266 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -05001267
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001268 png_debug(1, "in png_write_sBIT");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001269
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001270 /* Make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001271 if (color_type & PNG_COLOR_MASK_COLOR)
1272 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001273 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -05001274
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001275 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001276 png_ptr->usr_bit_depth);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001277
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001278 if (sbit->red == 0 || sbit->red > maxbits ||
1279 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -05001280 sbit->blue == 0 || sbit->blue > maxbits)
1281 {
1282 png_warning(png_ptr, "Invalid sBIT depth specified");
1283 return;
1284 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001285
Guy Schalnat0d580581995-07-20 02:43:20 -05001286 buf[0] = sbit->red;
1287 buf[1] = sbit->green;
1288 buf[2] = sbit->blue;
1289 size = 3;
1290 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001291
Guy Schalnat0d580581995-07-20 02:43:20 -05001292 else
1293 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001294 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
1295 {
1296 png_warning(png_ptr, "Invalid sBIT depth specified");
1297 return;
1298 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001299
Guy Schalnat0d580581995-07-20 02:43:20 -05001300 buf[0] = sbit->gray;
1301 size = 1;
1302 }
1303
1304 if (color_type & PNG_COLOR_MASK_ALPHA)
1305 {
Guy Schalnate5a37791996-06-05 15:50:50 -05001306 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
1307 {
1308 png_warning(png_ptr, "Invalid sBIT depth specified");
1309 return;
1310 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001311
Guy Schalnat0d580581995-07-20 02:43:20 -05001312 buf[size++] = sbit->alpha;
1313 }
1314
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001315 png_write_complete_chunk(png_ptr, png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -05001316}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001317#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001318
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001319#ifdef PNG_WRITE_cHRM_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001320/* Write the cHRM chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001321void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001322png_write_cHRM_fixed(png_structrp png_ptr, png_fixed_point white_x,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001323 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
1324 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
1325 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001326{
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001327 png_byte buf[32];
1328
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001329 png_debug(1, "in png_write_cHRM");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001330
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001331 /* Each value is saved in 1/100,000ths */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001332#ifdef PNG_CHECK_cHRM_SUPPORTED
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001333 if (png_check_cHRM_fixed(png_ptr, white_x, white_y, red_x, red_y,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001334 green_x, green_y, blue_x, blue_y))
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001335#endif
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, (png_uint_32)white_x);
1338 png_save_uint_32(buf + 4, (png_uint_32)white_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 + 8, (png_uint_32)red_x);
1341 png_save_uint_32(buf + 12, (png_uint_32)red_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 + 16, (png_uint_32)green_x);
1344 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001345
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001346 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1347 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001348
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001349 png_write_complete_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001350 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001351}
1352#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001353
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001354#ifdef PNG_WRITE_tRNS_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001355/* Write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001356void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001357png_write_tRNS(png_structrp png_ptr, png_const_bytep trans_alpha,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001358 png_const_color_16p tran, int num_trans, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001359{
1360 png_byte buf[6];
1361
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001362 png_debug(1, "in png_write_tRNS");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001363
Guy Schalnat0d580581995-07-20 02:43:20 -05001364 if (color_type == PNG_COLOR_TYPE_PALETTE)
1365 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001366 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001367 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001368 png_warning(png_ptr, "Invalid number of transparent colors specified");
Guy Schalnate5a37791996-06-05 15:50:50 -05001369 return;
1370 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001371
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001372 /* Write the chunk out as it is */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001373 png_write_complete_chunk(png_ptr, png_tRNS, trans_alpha, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001374 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001375
Guy Schalnat0d580581995-07-20 02:43:20 -05001376 else if (color_type == PNG_COLOR_TYPE_GRAY)
1377 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001378 /* One 16 bit value */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001379 if (tran->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001380 {
1381 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001382 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001383
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001384 return;
1385 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001386
Guy Schalnat0d580581995-07-20 02:43:20 -05001387 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001388 png_write_complete_chunk(png_ptr, png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001389 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001390
Guy Schalnat0d580581995-07-20 02:43:20 -05001391 else if (color_type == PNG_COLOR_TYPE_RGB)
1392 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001393 /* Three 16 bit values */
Guy Schalnat0d580581995-07-20 02:43:20 -05001394 png_save_uint_16(buf, tran->red);
1395 png_save_uint_16(buf + 2, tran->green);
1396 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -05001397#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001398 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -05001399#else
1400 if (buf[0] | buf[2] | buf[4])
1401#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001402 {
1403 png_warning(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001404 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001405 return;
1406 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001407
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001408 png_write_complete_chunk(png_ptr, png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001409 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001410
Guy Schalnate5a37791996-06-05 15:50:50 -05001411 else
1412 {
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001413 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001414 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001415}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001416#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001417
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001418#ifdef PNG_WRITE_bKGD_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001419/* Write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001420void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001421png_write_bKGD(png_structrp png_ptr, png_const_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001422{
1423 png_byte buf[6];
1424
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001425 png_debug(1, "in png_write_bKGD");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001426
Guy Schalnat0d580581995-07-20 02:43:20 -05001427 if (color_type == PNG_COLOR_TYPE_PALETTE)
1428 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001429 if (
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001430#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001431 (png_ptr->num_palette ||
1432 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001433#endif
Glenn Randers-Pehrsond6d80752008-12-02 09:49:43 -06001434 back->index >= png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001435 {
1436 png_warning(png_ptr, "Invalid background palette index");
1437 return;
1438 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001439
Guy Schalnat0d580581995-07-20 02:43:20 -05001440 buf[0] = back->index;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001441 png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001442 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001443
Guy Schalnat0d580581995-07-20 02:43:20 -05001444 else if (color_type & PNG_COLOR_MASK_COLOR)
1445 {
1446 png_save_uint_16(buf, back->red);
1447 png_save_uint_16(buf + 2, back->green);
1448 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -05001449#ifdef PNG_WRITE_16BIT_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001450 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrson7d3e6732010-08-26 17:14:07 -05001451#else
1452 if (buf[0] | buf[2] | buf[4])
1453#endif
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001454 {
1455 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001456 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001457
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001458 return;
1459 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001460
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001461 png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001462 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001463
Guy Schalnat0d580581995-07-20 02:43:20 -05001464 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001465 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001466 if (back->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001467 {
1468 png_warning(png_ptr,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001469 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001470
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001471 return;
1472 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001473
Guy Schalnat0d580581995-07-20 02:43:20 -05001474 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001475 png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001476 }
1477}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001478#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001479
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001480#ifdef PNG_WRITE_hIST_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001481/* Write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001482void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001483png_write_hIST(png_structrp png_ptr, png_const_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001484{
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001485 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001486 png_byte buf[3];
1487
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001488 png_debug(1, "in png_write_hIST");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001489
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001490 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001491 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001492 png_debug2(3, "num_hist = %d, num_palette = %d", num_hist,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001493 png_ptr->num_palette);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001494
Guy Schalnate5a37791996-06-05 15:50:50 -05001495 png_warning(png_ptr, "Invalid number of histogram entries specified");
1496 return;
1497 }
1498
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001499 png_write_chunk_header(png_ptr, png_hIST, (png_uint_32)(num_hist * 2));
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001500
Andreas Dilger47a0c421997-05-16 02:46:07 -05001501 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001502 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001503 png_save_uint_16(buf, hist[i]);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001504 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001505 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001506
Guy Schalnat0d580581995-07-20 02:43:20 -05001507 png_write_chunk_end(png_ptr);
1508}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001509#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001510
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001511#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1512 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001513/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1514 * and if invalid, correct the keyword rather than discarding the entire
1515 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1516 * length, forbids leading or trailing whitespace, multiple internal spaces,
1517 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001518 *
1519 * The new_key is allocated to hold the corrected keyword and must be freed
1520 * by the calling routine. This avoids problems with trying to write to
1521 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001522 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001523png_size_t /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001524png_check_keyword(png_structrp png_ptr, png_const_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001525{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001526 png_size_t key_len;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001527 png_const_charp ikp;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001528 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001529 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001530 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001531
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001532 png_debug(1, "in png_check_keyword");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001533
Andreas Dilger47a0c421997-05-16 02:46:07 -05001534 *new_key = NULL;
1535
1536 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001537 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001538 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001539 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001540 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001541
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001542 png_debug1(2, "Keyword to be checked is '%s'", key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001543
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001544 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001545
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001546 if (*new_key == NULL)
1547 {
1548 png_warning(png_ptr, "Out of memory while procesing keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001549 return ((png_size_t)0);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001550 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001551
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001552 /* Replace non-printing characters with a blank and print a warning */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001553 for (ikp = key, dp = *new_key; *ikp != '\0'; ikp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001554 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001555 if ((png_byte)*ikp < 0x20 ||
1556 ((png_byte)*ikp > 0x7E && (png_byte)*ikp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001557 {
John Bowler88b77cc2011-05-05 06:49:55 -05001558 PNG_WARNING_PARAMETERS(p)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001559
John Bowler88b77cc2011-05-05 06:49:55 -05001560 png_warning_parameter_unsigned(p, 1, PNG_NUMBER_FORMAT_02x,
John Bowlerd273ad22011-05-07 21:00:28 -05001561 (png_byte)*ikp);
John Bowler88b77cc2011-05-05 06:49:55 -05001562 png_formatted_warning(png_ptr, p, "invalid keyword character 0x@1");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001563 *dp = ' ';
1564 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001565
Andreas Dilger47a0c421997-05-16 02:46:07 -05001566 else
1567 {
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001568 *dp = *ikp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001569 }
1570 }
1571 *dp = '\0';
1572
1573 /* Remove any trailing white space. */
1574 kp = *new_key + key_len - 1;
1575 if (*kp == ' ')
1576 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001577 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001578
1579 while (*kp == ' ')
1580 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001581 *(kp--) = '\0';
1582 key_len--;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001583 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001584 }
1585
1586 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001587 kp = *new_key;
1588 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001589 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001590 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001591
1592 while (*kp == ' ')
1593 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001594 kp++;
1595 key_len--;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001596 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001597 }
1598
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001599 png_debug1(2, "Checking for multiple internal spaces in '%s'", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001600
Andreas Dilger47a0c421997-05-16 02:46:07 -05001601 /* Remove multiple internal spaces. */
1602 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001603 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001604 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001605 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001606 *(dp++) = *kp;
1607 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001608 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001609
Andreas Dilger47a0c421997-05-16 02:46:07 -05001610 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001611 {
1612 key_len--;
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001613 kwarn = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001614 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001615
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001616 else
1617 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001618 *(dp++) = *kp;
1619 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001620 }
1621 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001622 *dp = '\0';
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001623 if (kwarn)
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001624 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001625
1626 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001627 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001628 png_free(png_ptr, *new_key);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001629 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001630 }
1631
1632 if (key_len > 79)
1633 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001634 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Glenn Randers-Pehrson71a3c1f2008-11-26 12:00:38 -06001635 (*new_key)[79] = '\0';
Andreas Dilger47a0c421997-05-16 02:46:07 -05001636 key_len = 79;
1637 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001638
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001639 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001640}
1641#endif
1642
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001643#ifdef PNG_WRITE_tEXt_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001644/* Write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001645void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001646png_write_tEXt(png_structrp png_ptr, png_const_charp key, png_const_charp text,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001647 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001648{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001649 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001650 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001651
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001652 png_debug(1, "in png_write_tEXt");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001653
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001654 if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001655 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001656
Andreas Dilger47a0c421997-05-16 02:46:07 -05001657 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001658 text_len = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001659
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001660 else
1661 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001662
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001663 /* Make sure we include the 0 after the key */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001664 png_write_chunk_header(png_ptr, png_tEXt,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001665 (png_uint_32)(key_len + text_len + 1));
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001666 /*
1667 * We leave it to the application to meet PNG-1.0 requirements on the
1668 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1669 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001670 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001671 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001672 png_write_chunk_data(png_ptr, (png_bytep)new_key,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001673 (png_size_t)(key_len + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001674
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001675 if (text_len)
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001676 png_write_chunk_data(png_ptr, (png_const_bytep)text,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001677 (png_size_t)text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001678
Guy Schalnat0d580581995-07-20 02:43:20 -05001679 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001680 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001681}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001682#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001683
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001684#ifdef PNG_WRITE_zTXt_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001685/* Write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001686void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001687png_write_zTXt(png_structrp png_ptr, png_const_charp key, png_const_charp text,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001688 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001689{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001690 png_size_t key_len;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001691 png_byte buf;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001692 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001693 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001694
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001695 png_debug(1, "in png_write_zTXt");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001696
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001697 comp.num_output_ptr = 0;
1698 comp.max_output_ptr = 0;
1699 comp.output_ptr = NULL;
1700 comp.input = NULL;
1701 comp.input_len = 0;
1702
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001703 if ((key_len = png_check_keyword(png_ptr, key, &new_key)) == 0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001704 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001705 png_free(png_ptr, new_key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001706 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001707 }
1708
Andreas Dilger47a0c421997-05-16 02:46:07 -05001709 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1710 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001711 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001712 png_free(png_ptr, new_key);
1713 return;
1714 }
1715
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001716 text_len = png_strlen(text);
1717
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001718 /* Compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001719 text_len = png_text_compress(png_ptr, text, text_len, compression,
1720 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001721
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001722 /* Write start of chunk */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001723 png_write_chunk_header(png_ptr, png_zTXt,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001724 (png_uint_32)(key_len+text_len + 2));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001725
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001726 /* Write key */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001727 png_write_chunk_data(png_ptr, (png_bytep)new_key,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001728 (png_size_t)(key_len + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001729
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001730 png_free(png_ptr, new_key);
1731
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001732 buf = (png_byte)compression;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001733
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001734 /* Write compression */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001735 png_write_chunk_data(png_ptr, &buf, (png_size_t)1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001736
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001737 /* Write the compressed data */
Glenn Randers-Pehrson67858562011-04-02 08:26:42 -05001738 comp.input_len = text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001739 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001740
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001741 /* Close the chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -05001742 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001743}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001744#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001745
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001746#ifdef PNG_WRITE_iTXt_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001747/* Write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001748void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001749png_write_iTXt(png_structrp png_ptr, int compression, png_const_charp key,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001750 png_const_charp lang, png_const_charp lang_key, png_const_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001751{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001752 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001753 png_charp new_lang;
1754 png_charp new_key = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001755 png_byte cbuf[2];
1756 compression_state comp;
1757
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001758 png_debug(1, "in png_write_iTXt");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001759
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001760 comp.num_output_ptr = 0;
1761 comp.max_output_ptr = 0;
1762 comp.output_ptr = NULL;
1763 comp.input = NULL;
1764
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001765 if ((key_len = png_check_keyword(png_ptr, key, &new_key)) == 0)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001766 return;
Glenn Randers-Pehrson5ca69e42008-12-06 05:38:27 -06001767
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001768 if ((lang_len = png_check_keyword(png_ptr, lang, &new_lang)) == 0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001769 {
1770 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001771 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001772 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001773 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001774
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001775 if (lang_key == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001776 lang_key_len = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001777
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001778 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001779 lang_key_len = png_strlen(lang_key);
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001780
1781 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001782 text_len = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001783
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001784 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001785 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001786
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001787 /* Compute the compressed data; do it now for the length */
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001788 text_len = png_text_compress(png_ptr, text, text_len, compression - 2,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001789 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001790
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001791
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001792 /* Make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001793 * and the NULs after the key, lang, and lang_key parts
1794 */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001795
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001796 png_write_chunk_header(png_ptr, png_iTXt, (png_uint_32)(
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001797 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1798 + key_len
1799 + lang_len
1800 + lang_key_len
1801 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001802
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001803 /* We leave it to the application to meet PNG-1.0 requirements on the
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001804 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1805 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1806 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1807 */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001808 png_write_chunk_data(png_ptr, (png_bytep)new_key, (png_size_t)(key_len + 1));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001809
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001810 /* Set the compression flag */
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001811 if (compression == PNG_ITXT_COMPRESSION_NONE ||
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001812 compression == PNG_TEXT_COMPRESSION_NONE)
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001813 cbuf[0] = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001814
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001815 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001816 cbuf[0] = 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001817
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001818 /* Set the compression method */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001819 cbuf[1] = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001820
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001821 png_write_chunk_data(png_ptr, cbuf, (png_size_t)2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001822
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001823 cbuf[0] = 0;
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001824 png_write_chunk_data(png_ptr, (new_lang ? (png_const_bytep)new_lang : cbuf),
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001825 (png_size_t)(lang_len + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001826
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001827 png_write_chunk_data(png_ptr, (lang_key ? (png_const_bytep)lang_key : cbuf),
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001828 (png_size_t)(lang_key_len + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001829
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001830 png_write_compressed_data_out(png_ptr, &comp);
1831
1832 png_write_chunk_end(png_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001833
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001834 png_free(png_ptr, new_key);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001835 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001836}
1837#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001838
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001839#ifdef PNG_WRITE_oFFs_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001840/* Write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001841void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001842png_write_oFFs(png_structrp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001843 int unit_type)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001844{
1845 png_byte buf[9];
1846
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001847 png_debug(1, "in png_write_oFFs");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001848
Andreas Dilger47a0c421997-05-16 02:46:07 -05001849 if (unit_type >= PNG_OFFSET_LAST)
1850 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1851
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001852 png_save_int_32(buf, x_offset);
1853 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001854 buf[8] = (png_byte)unit_type;
1855
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001856 png_write_complete_chunk(png_ptr, png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001857}
1858#endif
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001859#ifdef PNG_WRITE_pCAL_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001860/* Write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001861void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001862png_write_pCAL(png_structrp png_ptr, png_charp purpose, png_int_32 X0,
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05001863 png_int_32 X1, int type, int nparams, png_const_charp units,
1864 png_charpp params)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001865{
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001866 png_size_t purpose_len, units_len, total_len;
John Bowlerf3f7e142011-09-09 07:32:37 -05001867 png_size_tp params_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001868 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001869 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001870 int i;
1871
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001872 png_debug1(1, "in png_write_pCAL (%d parameters)", nparams);
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001873
Andreas Dilger47a0c421997-05-16 02:46:07 -05001874 if (type >= PNG_EQUATION_LAST)
1875 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1876
1877 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001878 png_debug1(3, "pCAL purpose length = %d", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001879 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001880 png_debug1(3, "pCAL units length = %d", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001881 total_len = purpose_len + units_len + 10;
1882
John Bowlerf3f7e142011-09-09 07:32:37 -05001883 params_len = (png_size_tp)png_malloc(png_ptr,
1884 (png_alloc_size_t)(nparams * png_sizeof(png_size_t)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001885
1886 /* Find the length of each parameter, making sure we don't count the
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001887 * null terminator for the last parameter.
1888 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001889 for (i = 0; i < nparams; i++)
1890 {
1891 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001892 png_debug2(3, "pCAL parameter %d length = %lu", i,
Glenn Randers-Pehrsond2332872010-10-12 19:19:28 -05001893 (unsigned long)params_len[i]);
John Bowlerf3f7e142011-09-09 07:32:37 -05001894 total_len += params_len[i];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001895 }
1896
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001897 png_debug1(3, "pCAL total length = %d", (int)total_len);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001898 png_write_chunk_header(png_ptr, png_pCAL, (png_uint_32)total_len);
John Bowlerf3f7e142011-09-09 07:32:37 -05001899 png_write_chunk_data(png_ptr, (png_const_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001900 png_save_int_32(buf, X0);
1901 png_save_int_32(buf + 4, X1);
1902 buf[8] = (png_byte)type;
1903 buf[9] = (png_byte)nparams;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001904 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001905 png_write_chunk_data(png_ptr, (png_const_bytep)units, (png_size_t)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001906
1907 png_free(png_ptr, new_purpose);
1908
1909 for (i = 0; i < nparams; i++)
1910 {
John Bowlerf3f7e142011-09-09 07:32:37 -05001911 png_write_chunk_data(png_ptr, (png_const_bytep)params[i], params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001912 }
1913
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001914 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001915 png_write_chunk_end(png_ptr);
1916}
1917#endif
1918
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001919#ifdef PNG_WRITE_sCAL_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001920/* Write the sCAL chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001921void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001922png_write_sCAL_s(png_structrp png_ptr, int unit, png_const_charp width,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05001923 png_const_charp height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001924{
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001925 png_byte buf[64];
1926 png_size_t wlen, hlen, total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001927
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001928 png_debug(1, "in png_write_sCAL_s");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001929
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001930 wlen = png_strlen(width);
1931 hlen = png_strlen(height);
1932 total_len = wlen + hlen + 2;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001933
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001934 if (total_len > 64)
1935 {
1936 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
1937 return;
1938 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001939
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001940 buf[0] = (png_byte)unit;
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001941 png_memcpy(buf + 1, width, wlen + 1); /* Append the '\0' here */
1942 png_memcpy(buf + wlen + 2, height, hlen); /* Do NOT append the '\0' here */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001943
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001944 png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001945 png_write_complete_chunk(png_ptr, png_sCAL, buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001946}
1947#endif
1948
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001949#ifdef PNG_WRITE_pHYs_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001950/* Write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001951void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001952png_write_pHYs(png_structrp png_ptr, png_uint_32 x_pixels_per_unit,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06001953 png_uint_32 y_pixels_per_unit,
1954 int unit_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001955{
1956 png_byte buf[9];
1957
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001958 png_debug(1, "in png_write_pHYs");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001959
Guy Schalnate5a37791996-06-05 15:50:50 -05001960 if (unit_type >= PNG_RESOLUTION_LAST)
1961 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1962
Guy Schalnat0d580581995-07-20 02:43:20 -05001963 png_save_uint_32(buf, x_pixels_per_unit);
1964 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001965 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001966
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001967 png_write_complete_chunk(png_ptr, png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001968}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001969#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001970
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001971#ifdef PNG_WRITE_tIME_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001972/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1973 * or png_convert_from_time_t(), or fill in the structure yourself.
1974 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001975void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001976png_write_tIME(png_structrp png_ptr, png_const_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001977{
1978 png_byte buf[7];
1979
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001980 png_debug(1, "in png_write_tIME");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05001981
Guy Schalnate5a37791996-06-05 15:50:50 -05001982 if (mod_time->month > 12 || mod_time->month < 1 ||
1983 mod_time->day > 31 || mod_time->day < 1 ||
1984 mod_time->hour > 23 || mod_time->second > 60)
1985 {
1986 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1987 return;
1988 }
1989
Guy Schalnat0d580581995-07-20 02:43:20 -05001990 png_save_uint_16(buf, mod_time->year);
1991 buf[2] = mod_time->month;
1992 buf[3] = mod_time->day;
1993 buf[4] = mod_time->hour;
1994 buf[5] = mod_time->minute;
1995 buf[6] = mod_time->second;
1996
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001997 png_write_complete_chunk(png_ptr, png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001998}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001999#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002000
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002001/* Initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002002void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002003png_write_start_row(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05002004{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002005#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002006 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002007
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002008 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002009 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002010
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002011 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002012 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002013
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002014 /* Start of interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002015 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002016
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002017 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002018 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06002019#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002020
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002021 png_alloc_size_t buf_size;
2022 int usr_pixel_depth;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002023
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002024 png_debug(1, "in png_write_start_row");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05002025
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002026 usr_pixel_depth = png_ptr->usr_channels * png_ptr->usr_bit_depth;
2027 buf_size = PNG_ROWBYTES(usr_pixel_depth, png_ptr->width) + 1;
2028
2029 /* 1.5.6: added to allow checking in the row write code. */
2030 png_ptr->transformed_pixel_depth = png_ptr->pixel_depth;
2031 png_ptr->maximum_pixel_depth = (png_byte)usr_pixel_depth;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002032
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002033 /* Set up row buffer */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002034 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, buf_size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002035
Andreas Dilger47a0c421997-05-16 02:46:07 -05002036 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05002037
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002038#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002039 /* Set up filtering buffer, if using this filter */
Guy Schalnate5a37791996-06-05 15:50:50 -05002040 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002041 {
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002042 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, png_ptr->rowbytes + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002043
Andreas Dilger47a0c421997-05-16 02:46:07 -05002044 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05002045 }
2046
Andreas Dilger47a0c421997-05-16 02:46:07 -05002047 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05002048 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
2049 {
Glenn Randers-Pehrsondfa99af2009-10-29 23:33:46 -05002050 /* Set up previous row buffer */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002051 png_ptr->prev_row = (png_bytep)png_calloc(png_ptr, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05002052
2053 if (png_ptr->do_filter & PNG_FILTER_UP)
2054 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05002055 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002056 png_ptr->rowbytes + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002057
Andreas Dilger47a0c421997-05-16 02:46:07 -05002058 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05002059 }
2060
2061 if (png_ptr->do_filter & PNG_FILTER_AVG)
2062 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002063 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002064 png_ptr->rowbytes + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002065
Andreas Dilger47a0c421997-05-16 02:46:07 -05002066 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05002067 }
2068
2069 if (png_ptr->do_filter & PNG_FILTER_PAETH)
2070 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05002071 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002072 png_ptr->rowbytes + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002073
Andreas Dilger47a0c421997-05-16 02:46:07 -05002074 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05002075 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002076 }
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002077#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05002078
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002079#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002080 /* If interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002081 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05002082 {
2083 if (!(png_ptr->transformations & PNG_INTERLACE))
2084 {
2085 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002086 png_pass_ystart[0]) / png_pass_yinc[0];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002087
Andreas Dilger47a0c421997-05-16 02:46:07 -05002088 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002089 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05002090 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002091
Guy Schalnat0d580581995-07-20 02:43:20 -05002092 else
2093 {
2094 png_ptr->num_rows = png_ptr->height;
2095 png_ptr->usr_width = png_ptr->width;
2096 }
2097 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002098
Guy Schalnat0d580581995-07-20 02:43:20 -05002099 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002100#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002101 {
Guy Schalnat0d580581995-07-20 02:43:20 -05002102 png_ptr->num_rows = png_ptr->height;
2103 png_ptr->usr_width = png_ptr->width;
2104 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002105
John Bowlerc5bef942011-05-05 17:35:39 -05002106 png_zlib_claim(png_ptr, PNG_ZLIB_FOR_IDAT);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002107 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
2108 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05002109}
2110
Andreas Dilger47a0c421997-05-16 02:46:07 -05002111/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002112void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002113png_write_finish_row(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05002114{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002115#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002116 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002117
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002118 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002119 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002120
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002121 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002122 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002123
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002124 /* Start of interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002125 static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002126
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002127 /* Offset to next interlace block in the y direction */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002128 static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06002129#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002130
Guy Schalnat0d580581995-07-20 02:43:20 -05002131 int ret;
2132
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002133 png_debug(1, "in png_write_finish_row");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05002134
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002135 /* Next row */
Guy Schalnat0d580581995-07-20 02:43:20 -05002136 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002137
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002138 /* See if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06002139 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002140 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05002141
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002142#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002143 /* If interlaced, go to next pass */
Guy Schalnat0d580581995-07-20 02:43:20 -05002144 if (png_ptr->interlaced)
2145 {
2146 png_ptr->row_number = 0;
2147 if (png_ptr->transformations & PNG_INTERLACE)
2148 {
2149 png_ptr->pass++;
2150 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002151
Guy Schalnat0d580581995-07-20 02:43:20 -05002152 else
2153 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002154 /* Loop until we find a non-zero width or height pass */
Guy Schalnat0d580581995-07-20 02:43:20 -05002155 do
2156 {
2157 png_ptr->pass++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002158
Guy Schalnat0d580581995-07-20 02:43:20 -05002159 if (png_ptr->pass >= 7)
2160 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002161
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002162 png_ptr->usr_width = (png_ptr->width +
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002163 png_pass_inc[png_ptr->pass] - 1 -
2164 png_pass_start[png_ptr->pass]) /
2165 png_pass_inc[png_ptr->pass];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002166
Guy Schalnat0d580581995-07-20 02:43:20 -05002167 png_ptr->num_rows = (png_ptr->height +
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002168 png_pass_yinc[png_ptr->pass] - 1 -
2169 png_pass_ystart[png_ptr->pass]) /
2170 png_pass_yinc[png_ptr->pass];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002171
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002172 if (png_ptr->transformations & PNG_INTERLACE)
2173 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002174
Guy Schalnat0d580581995-07-20 02:43:20 -05002175 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
2176
2177 }
2178
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002179 /* Reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002180 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002181 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002182 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002183 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002184 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
2185 png_ptr->usr_bit_depth, png_ptr->width)) + 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002186
Guy Schalnat0d580581995-07-20 02:43:20 -05002187 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002188 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002189 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06002190#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002191
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002192 /* If we get here, we've just written the last row, so we need
Guy Schalnat0d580581995-07-20 02:43:20 -05002193 to flush the compressor */
2194 do
2195 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002196 /* Tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002197 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002198
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002199 /* Check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05002200 if (ret == Z_OK)
2201 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002202 /* Check to see if we need more room */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05002203 if (!(png_ptr->zstream.avail_out))
2204 {
2205 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
2206 png_ptr->zstream.next_out = png_ptr->zbuf;
2207 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
2208 }
2209 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002210
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05002211 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05002212 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002213 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002214 png_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002215
Guy Schalnat0d580581995-07-20 02:43:20 -05002216 else
Guy Schalnat6d764711995-12-19 03:22:19 -06002217 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05002218 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002219 } while (ret != Z_STREAM_END);
2220
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002221 /* Write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002222 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05002223 {
2224 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002225 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05002226 }
2227
John Bowlerc5bef942011-05-05 17:35:39 -05002228 png_zlib_release(png_ptr);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -06002229 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat0d580581995-07-20 02:43:20 -05002230}
2231
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002232#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002233/* Pick out the correct pixels for the interlace pass.
2234 * The basic idea here is to go through the row with a source
2235 * pointer and a destination pointer (sp and dp), and copy the
2236 * correct pixels for the pass. As the row gets compacted,
2237 * sp will always be >= dp, so we should never overwrite anything.
2238 * See the default: case for the easiest code to understand.
2239 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002240void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06002241png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05002242{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002243 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002244
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002245 /* Start of interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002246 static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002247
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002248 /* Offset to next interlace block */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002249 static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002250
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002251 png_debug(1, "in png_do_write_interlace");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05002252
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002253 /* We don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002254 if (pass < 6)
Guy Schalnat0d580581995-07-20 02:43:20 -05002255 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002256 /* Each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05002257 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002258 {
Guy Schalnat0d580581995-07-20 02:43:20 -05002259 case 1:
2260 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002261 png_bytep sp;
2262 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05002263 int shift;
2264 int d;
2265 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002266 png_uint_32 i;
2267 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002268
2269 dp = row;
2270 d = 0;
2271 shift = 7;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002272
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002273 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002274 i += png_pass_inc[pass])
2275 {
2276 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002277 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05002278 d |= (value << shift);
2279
2280 if (shift == 0)
2281 {
2282 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002283 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002284 d = 0;
2285 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002286
Guy Schalnat0d580581995-07-20 02:43:20 -05002287 else
2288 shift--;
2289
2290 }
2291 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002292 *dp = (png_byte)d;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002293
Guy Schalnat0d580581995-07-20 02:43:20 -05002294 break;
2295 }
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002296
Guy Schalnat0d580581995-07-20 02:43:20 -05002297 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002298 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002299 png_bytep sp;
2300 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05002301 int shift;
2302 int d;
2303 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002304 png_uint_32 i;
2305 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002306
2307 dp = row;
2308 shift = 6;
2309 d = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002310
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002311 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002312 i += png_pass_inc[pass])
2313 {
2314 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002315 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05002316 d |= (value << shift);
2317
2318 if (shift == 0)
2319 {
2320 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002321 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002322 d = 0;
2323 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002324
Guy Schalnat0d580581995-07-20 02:43:20 -05002325 else
2326 shift -= 2;
2327 }
2328 if (shift != 6)
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002329 *dp = (png_byte)d;
2330
Guy Schalnat0d580581995-07-20 02:43:20 -05002331 break;
2332 }
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002333
Guy Schalnat0d580581995-07-20 02:43:20 -05002334 case 4:
2335 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002336 png_bytep sp;
2337 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002338 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05002339 int d;
2340 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002341 png_uint_32 i;
2342 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002343
2344 dp = row;
2345 shift = 4;
2346 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002347 for (i = png_pass_start[pass]; i < row_width;
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002348 i += png_pass_inc[pass])
Guy Schalnat0d580581995-07-20 02:43:20 -05002349 {
2350 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002351 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05002352 d |= (value << shift);
2353
2354 if (shift == 0)
2355 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002356 shift = 4;
2357 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002358 d = 0;
2359 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002360
Guy Schalnat0d580581995-07-20 02:43:20 -05002361 else
2362 shift -= 4;
2363 }
2364 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002365 *dp = (png_byte)d;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002366
Guy Schalnat0d580581995-07-20 02:43:20 -05002367 break;
2368 }
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002369
Guy Schalnat0d580581995-07-20 02:43:20 -05002370 default:
2371 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002372 png_bytep sp;
2373 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002374 png_uint_32 i;
2375 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002376 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002377
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002378 /* Start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002379 dp = row;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002380
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002381 /* Find out how many bytes each pixel takes up */
Guy Schalnat0d580581995-07-20 02:43:20 -05002382 pixel_bytes = (row_info->pixel_depth >> 3);
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002383
2384 /* Loop through the row, only looking at the pixels that matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002385 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002386 i += png_pass_inc[pass])
2387 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002388 /* Find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002389 sp = row + (png_size_t)i * pixel_bytes;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002390
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002391 /* Move the pixel */
Guy Schalnat0d580581995-07-20 02:43:20 -05002392 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002393 png_memcpy(dp, sp, pixel_bytes);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002394
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002395 /* Next pixel */
Guy Schalnat0d580581995-07-20 02:43:20 -05002396 dp += pixel_bytes;
2397 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002398 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002399 }
2400 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002401 /* Set new row width */
Guy Schalnat0d580581995-07-20 02:43:20 -05002402 row_info->width = (row_info->width +
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002403 png_pass_inc[pass] - 1 -
2404 png_pass_start[pass]) /
2405 png_pass_inc[pass];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002406
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002407 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2408 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002409 }
2410}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002411#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002412
Andreas Dilger47a0c421997-05-16 02:46:07 -05002413/* This filters the row, chooses which filter to use, if it has not already
2414 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002415 * chosen filter.
2416 */
John Bowler5d567862011-12-24 09:12:00 -06002417static void png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row,
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002418 png_size_t row_bytes);
John Bowlerc5bef942011-05-05 17:35:39 -05002419
Glenn Randers-Pehrson78067772004-11-02 21:49:39 -06002420#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002421#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002422#define PNG_LOMASK ((png_uint_32)0xffffL)
2423#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002424void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06002425png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002426{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002427 png_bytep best_row;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002428#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002429 png_bytep prev_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002430 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002431 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002432 png_size_t row_bytes = row_info->rowbytes;
Glenn Randers-Pehrson9d8b41e2009-07-19 14:45:43 -05002433#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002434 int num_p_filters = png_ptr->num_prev_filters;
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -05002435#endif
Glenn Randers-Pehrson9d8b41e2009-07-19 14:45:43 -05002436
2437 png_debug(1, "in png_write_find_filter");
2438
2439#ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson4ace0e12009-07-19 15:04:35 -05002440 if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS)
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05002441 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002442 /* These will never be selected so we need not test them. */
2443 filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_PAETH);
Glenn Randers-Pehrson9c90d7f2009-07-19 13:11:25 -05002444 }
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -05002445#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002446
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002447 /* Find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002448 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002449
2450 prev_row = png_ptr->prev_row;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002451#endif
2452 best_row = png_ptr->row_buf;
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05002453#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002454 row_buf = best_row;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002455 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002456
Andreas Dilger47a0c421997-05-16 02:46:07 -05002457 /* The prediction method we use is to find which method provides the
2458 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002459 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002460 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002461 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002462 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002463 * predictive" method (not implemented yet), which does test compressions
2464 * of lines using different filter methods, and then chooses the
2465 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002466 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002467 *
2468 * GRR 980525: consider also
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002469 *
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002470 * (1) minimum sum of absolute differences from running average (i.e.,
2471 * keep running sum of non-absolute differences & count of bytes)
2472 * [track dispersion, too? restart average if dispersion too large?]
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002473 *
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002474 * (1b) minimum sum of absolute differences from sliding average, probably
2475 * with window size <= deflate window (usually 32K)
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002476 *
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002477 * (2) minimum sum of squared differences from zero or running average
2478 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002479 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002480
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002481
Guy Schalnate5a37791996-06-05 15:50:50 -05002482 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002483 * that has been chosen, as it doesn't actually do anything to the data.
2484 */
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002485 if ((filter_to_do & PNG_FILTER_NONE) && filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002486 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002487 png_bytep rp;
2488 png_uint_32 sum = 0;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002489 png_size_t i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002490 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002491
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002492 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002493 {
2494 v = *rp;
2495 sum += (v < 128) ? v : 256 - v;
2496 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002497
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002498#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002499 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2500 {
2501 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002502 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002503 sumlo = sum & PNG_LOMASK;
2504 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2505
2506 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002507 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002508 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002509 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002510 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002511 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002512 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002513
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002514 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002515 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002516 }
2517 }
2518
2519 /* Factor in the cost of this filter (this is here for completeness,
2520 * but it makes no sense to have a "cost" for the NONE filter, as
2521 * it has the minimum possible computational cost - none).
2522 */
2523 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002524 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002525
Andreas Dilger47a0c421997-05-16 02:46:07 -05002526 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002527 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002528
2529 if (sumhi > PNG_HIMASK)
2530 sum = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002531
Andreas Dilger47a0c421997-05-16 02:46:07 -05002532 else
2533 sum = (sumhi << PNG_HISHIFT) + sumlo;
2534 }
2535#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002536 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002537 }
2538
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002539 /* Sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002540 if (filter_to_do == PNG_FILTER_SUB)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002541 /* It's the only filter so no testing is needed */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002542 {
2543 png_bytep rp, lp, dp;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002544 png_size_t i;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002545
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002546 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2547 i++, rp++, dp++)
2548 {
2549 *dp = *rp;
2550 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002551
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002552 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002553 i++, rp++, lp++, dp++)
2554 {
2555 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2556 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002557
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002558 best_row = png_ptr->sub_row;
2559 }
2560
2561 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002562 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002563 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002564 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002565 png_size_t i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002566 int v;
2567
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002568#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002569 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002570 * would reduce the sum of this filter, so that we can do the
2571 * early exit comparison without scaling the sum each time.
2572 */
2573 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2574 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002575 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002576 png_uint_32 lmhi, lmlo;
2577 lmlo = lmins & PNG_LOMASK;
2578 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2579
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002580 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002581 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002582 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002583 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002584 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002585 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002586
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002587 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002588 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002589 }
2590 }
2591
2592 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002593 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002594
Andreas Dilger47a0c421997-05-16 02:46:07 -05002595 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002596 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002597
2598 if (lmhi > PNG_HIMASK)
2599 lmins = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002600
Andreas Dilger47a0c421997-05-16 02:46:07 -05002601 else
2602 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2603 }
2604#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002605
Guy Schalnate5a37791996-06-05 15:50:50 -05002606 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2607 i++, rp++, dp++)
2608 {
2609 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002610
Guy Schalnate5a37791996-06-05 15:50:50 -05002611 sum += (v < 128) ? v : 256 - v;
2612 }
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05002613
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002614 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002615 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002616 {
2617 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002618
Guy Schalnate5a37791996-06-05 15:50:50 -05002619 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002620
2621 if (sum > lmins) /* We are already worse, don't continue. */
2622 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002623 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002624
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002625#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002626 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2627 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002628 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002629 png_uint_32 sumhi, sumlo;
2630 sumlo = sum & PNG_LOMASK;
2631 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2632
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002633 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002634 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002635 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002636 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002637 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002638 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002639
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002640 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002641 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002642 }
2643 }
2644
2645 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002646 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002647
Andreas Dilger47a0c421997-05-16 02:46:07 -05002648 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002649 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002650
2651 if (sumhi > PNG_HIMASK)
2652 sum = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002653
Andreas Dilger47a0c421997-05-16 02:46:07 -05002654 else
2655 sum = (sumhi << PNG_HISHIFT) + sumlo;
2656 }
2657#endif
2658
Guy Schalnate5a37791996-06-05 15:50:50 -05002659 if (sum < mins)
2660 {
2661 mins = sum;
2662 best_row = png_ptr->sub_row;
2663 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002664 }
2665
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002666 /* Up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002667 if (filter_to_do == PNG_FILTER_UP)
2668 {
2669 png_bytep rp, dp, pp;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002670 png_size_t i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002671
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002672 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002673 pp = prev_row + 1; i < row_bytes;
2674 i++, rp++, pp++, dp++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002675 {
2676 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2677 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05002678
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002679 best_row = png_ptr->up_row;
2680 }
2681
2682 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002683 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002684 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002685 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002686 png_size_t i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002687 int v;
2688
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002689
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002690#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002691 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2692 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002693 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002694 png_uint_32 lmhi, lmlo;
2695 lmlo = lmins & PNG_LOMASK;
2696 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2697
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002698 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002699 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002700 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002701 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002702 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002703 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002704
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002705 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002706 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002707 }
2708 }
2709
2710 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002711 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002712
Andreas Dilger47a0c421997-05-16 02:46:07 -05002713 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002714 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002715
2716 if (lmhi > PNG_HIMASK)
2717 lmins = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002718
Andreas Dilger47a0c421997-05-16 02:46:07 -05002719 else
2720 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2721 }
2722#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002723
2724 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002725 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002726 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002727 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002728
2729 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002730
2731 if (sum > lmins) /* We are already worse, don't continue. */
2732 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002733 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002734
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002735#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002736 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2737 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002738 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002739 png_uint_32 sumhi, sumlo;
2740 sumlo = sum & PNG_LOMASK;
2741 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2742
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002743 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002744 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002745 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002746 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002747 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002748 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002749
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002750 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002751 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002752 }
2753 }
2754
2755 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002756 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002757
Andreas Dilger47a0c421997-05-16 02:46:07 -05002758 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002759 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002760
2761 if (sumhi > PNG_HIMASK)
2762 sum = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002763
Andreas Dilger47a0c421997-05-16 02:46:07 -05002764 else
2765 sum = (sumhi << PNG_HISHIFT) + sumlo;
2766 }
2767#endif
2768
Guy Schalnate5a37791996-06-05 15:50:50 -05002769 if (sum < mins)
2770 {
2771 mins = sum;
2772 best_row = png_ptr->up_row;
2773 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002774 }
2775
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002776 /* Avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002777 if (filter_to_do == PNG_FILTER_AVG)
2778 {
2779 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002780 png_uint_32 i;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002781
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002782 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002783 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002784 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002785 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002786 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002787
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002788 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002789 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002790 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2791 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002792 }
2793 best_row = png_ptr->avg_row;
2794 }
2795
2796 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002797 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002798 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002799 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002800 png_size_t i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002801 int v;
2802
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002803#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002804 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2805 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002806 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002807 png_uint_32 lmhi, lmlo;
2808 lmlo = lmins & PNG_LOMASK;
2809 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2810
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002811 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002812 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002813 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002814 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002815 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002816 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002817
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002818 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002819 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002820 }
2821 }
2822
2823 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002824 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002825
Andreas Dilger47a0c421997-05-16 02:46:07 -05002826 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002827 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002828
2829 if (lmhi > PNG_HIMASK)
2830 lmins = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002831
Andreas Dilger47a0c421997-05-16 02:46:07 -05002832 else
2833 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2834 }
2835#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002836
2837 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002838 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002839 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002840 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002841
2842 sum += (v < 128) ? v : 256 - v;
2843 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002844
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002845 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002846 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002847 v = *dp++ =
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002848 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002849
2850 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002851
2852 if (sum > lmins) /* We are already worse, don't continue. */
2853 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002854 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002855
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002856#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002857 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2858 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002859 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002860 png_uint_32 sumhi, sumlo;
2861 sumlo = sum & PNG_LOMASK;
2862 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2863
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002864 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002865 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002866 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002867 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002868 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002869 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002870
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002871 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002872 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002873 }
2874 }
2875
2876 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002877 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002878
Andreas Dilger47a0c421997-05-16 02:46:07 -05002879 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002880 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002881
2882 if (sumhi > PNG_HIMASK)
2883 sum = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002884
Andreas Dilger47a0c421997-05-16 02:46:07 -05002885 else
2886 sum = (sumhi << PNG_HISHIFT) + sumlo;
2887 }
2888#endif
2889
Guy Schalnate5a37791996-06-05 15:50:50 -05002890 if (sum < mins)
2891 {
2892 mins = sum;
2893 best_row = png_ptr->avg_row;
2894 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002895 }
2896
Andreas Dilger47a0c421997-05-16 02:46:07 -05002897 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002898 if (filter_to_do == PNG_FILTER_PAETH)
2899 {
2900 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002901 png_size_t i;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002902
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002903 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002904 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002905 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002906 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002907 }
2908
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002909 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002910 {
2911 int a, b, c, pa, pb, pc, p;
2912
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002913 b = *pp++;
2914 c = *cp++;
2915 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002916
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002917 p = b - c;
2918 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002919
2920#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002921 pa = abs(p);
2922 pb = abs(pc);
2923 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002924#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002925 pa = p < 0 ? -p : p;
2926 pb = pc < 0 ? -pc : pc;
2927 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002928#endif
2929
2930 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2931
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002932 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002933 }
2934 best_row = png_ptr->paeth_row;
2935 }
2936
2937 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002938 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002939 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002940 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002941 png_size_t i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002942 int v;
2943
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002944#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002945 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2946 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002947 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002948 png_uint_32 lmhi, lmlo;
2949 lmlo = lmins & PNG_LOMASK;
2950 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2951
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002952 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002953 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002954 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002955 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002956 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002957 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002958
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002959 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002960 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002961 }
2962 }
2963
2964 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002965 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002966
Andreas Dilger47a0c421997-05-16 02:46:07 -05002967 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002968 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002969
2970 if (lmhi > PNG_HIMASK)
2971 lmins = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002972
Andreas Dilger47a0c421997-05-16 02:46:07 -05002973 else
2974 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2975 }
2976#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002977
2978 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06002979 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002980 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002981 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002982
2983 sum += (v < 128) ? v : 256 - v;
2984 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002985
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002986 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002987 {
2988 int a, b, c, pa, pb, pc, p;
2989
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002990 b = *pp++;
2991 c = *cp++;
2992 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002993
2994#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002995 p = b - c;
2996 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002997#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002998 pa = abs(p);
2999 pb = abs(pc);
3000 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003001#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003002 pa = p < 0 ? -p : p;
3003 pb = pc < 0 ? -pc : pc;
3004 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003005#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003006 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
3007#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003008 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003009 pa = abs(p - a);
3010 pb = abs(p - b);
3011 pc = abs(p - c);
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05003012
Guy Schalnate5a37791996-06-05 15:50:50 -05003013 if (pa <= pb && pa <= pc)
3014 p = a;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05003015
Guy Schalnate5a37791996-06-05 15:50:50 -05003016 else if (pb <= pc)
3017 p = b;
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05003018
Guy Schalnate5a37791996-06-05 15:50:50 -05003019 else
3020 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003021#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05003022
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003023 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05003024
3025 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003026
3027 if (sum > lmins) /* We are already worse, don't continue. */
3028 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05003029 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003030
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003031#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003032 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
3033 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05003034 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003035 png_uint_32 sumhi, sumlo;
3036 sumlo = sum & PNG_LOMASK;
3037 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
3038
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003039 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05003040 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003041 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05003042 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003043 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06003044 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003045
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003046 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06003047 PNG_WEIGHT_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003048 }
3049 }
3050
3051 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06003052 PNG_COST_SHIFT;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003053
Andreas Dilger47a0c421997-05-16 02:46:07 -05003054 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
Glenn Randers-Pehrson16908a12010-03-06 07:34:28 -06003055 PNG_COST_SHIFT;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003056
3057 if (sumhi > PNG_HIMASK)
3058 sum = PNG_MAXSUM;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003059
Andreas Dilger47a0c421997-05-16 02:46:07 -05003060 else
3061 sum = (sumhi << PNG_HISHIFT) + sumlo;
3062 }
3063#endif
3064
Guy Schalnate5a37791996-06-05 15:50:50 -05003065 if (sum < mins)
3066 {
3067 best_row = png_ptr->paeth_row;
3068 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003069 }
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05003070#endif /* PNG_WRITE_FILTER_SUPPORTED */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003071
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003072 /* Do the actual writing of the filtered row data from the chosen filter. */
3073 png_write_filtered_row(png_ptr, best_row, row_info->rowbytes+1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05003074
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05003075#ifdef PNG_WRITE_FILTER_SUPPORTED
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003076#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003077 /* Save the type of filter we picked this time for future calculations */
3078 if (png_ptr->num_prev_filters > 0)
3079 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003080 int j;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003081
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003082 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05003083 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003084 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05003085 }
Glenn Randers-Pehrsonccadcae2010-10-23 17:29:13 -05003086
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003087 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05003088 }
3089#endif
Glenn Randers-Pehrsondbd40142009-08-31 08:42:02 -05003090#endif /* PNG_WRITE_FILTER_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -05003091}
3092
3093
Andreas Dilger47a0c421997-05-16 02:46:07 -05003094/* Do the actual writing of a previously filtered row. */
John Bowlerc5bef942011-05-05 17:35:39 -05003095static void
John Bowler5d567862011-12-24 09:12:00 -06003096png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row,
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003097 png_size_t avail/*includes filter byte*/)
Guy Schalnate5a37791996-06-05 15:50:50 -05003098{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003099 png_debug(1, "in png_write_filtered_row");
Glenn Randers-Pehrson3358a982009-08-13 18:04:26 -05003100
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003101 png_debug1(2, "filter = %d", filtered_row[0]);
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003102 /* Set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06003103
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003104 png_ptr->zstream.next_in = filtered_row;
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05003105 png_ptr->zstream.avail_in = 0;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003106 /* Repeat until we have compressed all the data */
Guy Schalnate5a37791996-06-05 15:50:50 -05003107 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003108 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003109 int ret; /* Return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05003110
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05003111 /* Record the number of bytes available - zlib supports at least 65535
3112 * bytes at one step, depending on the size of the zlib type 'uInt', the
3113 * maximum size zlib can write at once is ZLIB_IO_MAX (from pngpriv.h).
3114 * Use this because on 16 bit systems 'rowbytes' can be up to 65536 (i.e.
3115 * one more than 16 bits) and, in this case 'rowbytes+1' can overflow a
3116 * uInt. ZLIB_IO_MAX can be safely reduced to cause zlib to be called
3117 * with smaller chunks of data.
3118 */
3119 if (png_ptr->zstream.avail_in == 0)
3120 {
3121 if (avail > ZLIB_IO_MAX)
3122 {
3123 png_ptr->zstream.avail_in = ZLIB_IO_MAX;
3124 avail -= ZLIB_IO_MAX;
3125 }
3126
3127 else
3128 {
3129 /* So this will fit in the available uInt space: */
3130 png_ptr->zstream.avail_in = (uInt)avail;
3131 avail = 0;
3132 }
3133 }
3134
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003135 /* Compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003136 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003137
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003138 /* Check for compression errors */
Guy Schalnate5a37791996-06-05 15:50:50 -05003139 if (ret != Z_OK)
3140 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05003141 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06003142 png_error(png_ptr, png_ptr->zstream.msg);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003143
Guy Schalnate5a37791996-06-05 15:50:50 -05003144 else
3145 png_error(png_ptr, "zlib error");
3146 }
Guy Schalnat0d580581995-07-20 02:43:20 -05003147
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003148 /* See if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05003149 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05003150 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003151 /* Write the IDAT and reset the zlib output buffer */
Guy Schalnate5a37791996-06-05 15:50:50 -05003152 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05003153 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003154 /* Repeat until all data has been compressed */
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05003155 } while (avail > 0 || png_ptr->zstream.avail_in > 0);
Guy Schalnate5a37791996-06-05 15:50:50 -05003156
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003157 /* Swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05003158 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05003159 {
3160 png_bytep tptr;
3161
3162 tptr = png_ptr->prev_row;
3163 png_ptr->prev_row = png_ptr->row_buf;
3164 png_ptr->row_buf = tptr;
3165 }
3166
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003167 /* Finish row - updates counters and flushes zlib if last row */
Guy Schalnate5a37791996-06-05 15:50:50 -05003168 png_write_finish_row(png_ptr);
3169
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003170#ifdef PNG_WRITE_FLUSH_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05003171 png_ptr->flush_rows++;
3172
3173 if (png_ptr->flush_dist > 0 &&
3174 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05003175 {
Guy Schalnate5a37791996-06-05 15:50:50 -05003176 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05003177 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06003178#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003179}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05003180#endif /* PNG_WRITE_SUPPORTED */