blob: 93943060cf358f5ca6a5b9b9ea436d4d90bfac80 [file] [log] [blame]
Guy Schalnat0d580581995-07-20 02:43:20 -05001
Andreas Dilger47a0c421997-05-16 02:46:07 -05002/* pngwutil.c - utilities to write a PNG file
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06004 * Last changed in libpng 1.4.0 [November 13, 2008]
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05006 * Copyright (c) 1998-2008 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05007 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 */
Andreas Dilger47a0c421997-05-16 02:46:07 -050010
Guy Schalnat0d580581995-07-20 02:43:20 -050011#include "png.h"
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -050012#ifdef PNG_WRITE_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050013#include "pngpriv.h"
Guy Schalnat0d580581995-07-20 02:43:20 -050014
Andreas Dilger47a0c421997-05-16 02:46:07 -050015/* Place a 32-bit number into a buffer in PNG byte order. We work
16 * with unsigned numbers for convenience, although one supported
17 * ancillary chunk uses signed (two's complement) numbers.
18 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060019void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -060020png_save_uint_32(png_bytep buf, png_uint_32 i)
Guy Schalnat0d580581995-07-20 02:43:20 -050021{
22 buf[0] = (png_byte)((i >> 24) & 0xff);
23 buf[1] = (png_byte)((i >> 16) & 0xff);
24 buf[2] = (png_byte)((i >> 8) & 0xff);
25 buf[3] = (png_byte)(i & 0xff);
26}
27
Glenn Randers-Pehrson86dc9812006-05-10 07:27:44 -050028#if defined(PNG_SAVE_INT_32_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -050029/* The png_save_int_32 function assumes integers are stored in two's
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060030 * complement format. If this isn't the case, then this routine needs to
31 * be modified to write data in two's complement format.
32 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060033void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -050034png_save_int_32(png_bytep buf, png_int_32 i)
35{
36 buf[0] = (png_byte)((i >> 24) & 0xff);
37 buf[1] = (png_byte)((i >> 16) & 0xff);
38 buf[2] = (png_byte)((i >> 8) & 0xff);
39 buf[3] = (png_byte)(i & 0xff);
40}
Glenn Randers-Pehrson86dc9812006-05-10 07:27:44 -050041#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050042
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060043/* Place a 16-bit number into a buffer in PNG byte order.
44 * The parameter is declared unsigned int, not png_uint_16,
45 * just to avoid potential problems on pre-ANSI C compilers.
46 */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060047void PNGAPI
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060048png_save_uint_16(png_bytep buf, unsigned int i)
Guy Schalnat0d580581995-07-20 02:43:20 -050049{
50 buf[0] = (png_byte)((i >> 8) & 0xff);
51 buf[1] = (png_byte)(i & 0xff);
52}
53
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050054/* Simple function to write the signature. If we have already written
55 * the magic bytes of the signature, or more likely, the PNG stream is
56 * being embedded into another stream and doesn't need its own signature,
57 * we should call png_set_sig_bytes() to tell libpng how many of the
58 * bytes have already been written.
59 */
60void PNGAPI
61png_write_sig(png_structp png_ptr)
62{
63 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050064
65#ifdef PNG_IO_STATE_SUPPORTED
66 /* inform the I/O callback that the signature is being written */
67 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_SIGNATURE;
68#endif
69
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050070 /* write the rest of the 8 byte signature */
71 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050072 (png_size_t)(8 - png_ptr->sig_bytes));
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050073 if (png_ptr->sig_bytes < 3)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -050074 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
75}
76
Andreas Dilger47a0c421997-05-16 02:46:07 -050077/* Write a PNG chunk all at once. The type is an array of ASCII characters
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060078 * representing the chunk name. The array must be at least 4 bytes in
79 * length, and does not need to be null terminated. To be safe, pass the
80 * pre-defined chunk names here, and if you need a new one, define it
81 * where the others are defined. The length is the length of the data.
82 * All the data must be present. If that is not possible, use the
83 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
84 * functions instead.
85 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050086void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060087png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
Andreas Dilger47a0c421997-05-16 02:46:07 -050088 png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -050089{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050090 if (png_ptr == NULL) return;
Andreas Dilger47a0c421997-05-16 02:46:07 -050091 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050092 png_write_chunk_data(png_ptr, data, (png_size_t)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060093 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050094}
95
Andreas Dilger47a0c421997-05-16 02:46:07 -050096/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060097 * The total_length is the sum of the lengths of all the data you will be
98 * passing in png_write_chunk_data().
99 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500100void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600101png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
102 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500103{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500104 png_byte buf[8];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500105
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500106#ifdef PNG_IO_STATE_SUPPORTED
107 /* Inform the I/O callback that the chunk header is being written.
108 * PNG_IO_CHUNK_HDR requires a single I/O call.
109 */
110 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR;
111#endif
112
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500113 png_debug2(0, "Writing %s chunk, length = %lu", chunk_name,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500114 (unsigned long)length);
115 if (png_ptr == NULL) return;
116
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500117 /* write the length and the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500118 png_save_uint_32(buf, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500119 png_memcpy(buf + 4, chunk_name, 4);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500120 png_write_data(png_ptr, buf, (png_size_t)8);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500121 /* put the chunk name into png_ptr->chunk_name */
122 png_memcpy(png_ptr->chunk_name, chunk_name, 4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500123 /* reset the crc and run it over the chunk name */
124 png_reset_crc(png_ptr);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500125 png_calculate_crc(png_ptr, chunk_name, 4);
126
127#ifdef PNG_IO_STATE_SUPPORTED
128 /* Inform the I/O callback that chunk data will (possibly) be written.
129 * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.
130 */
131 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA;
132#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500133}
134
Andreas Dilger47a0c421997-05-16 02:46:07 -0500135/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600136 * Note that multiple calls to this function are allowed, and that the
137 * sum of the lengths from these calls *must* add up to the total_length
138 * given to png_write_chunk_start().
139 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500140void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500141png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500142{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500143 /* write the data, and run the CRC over it */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500144 if (png_ptr == NULL) return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500145 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500146 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600147 png_write_data(png_ptr, data, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500148 /* update the CRC after writing the data,
149 * in case that the user I/O routine alters it.
150 */
151 png_calculate_crc(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500152 }
153}
154
Andreas Dilger47a0c421997-05-16 02:46:07 -0500155/* Finish a chunk started with png_write_chunk_start(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500156void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600157png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500158{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500159 png_byte buf[4];
160
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500161 if (png_ptr == NULL) return;
162
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500163#ifdef PNG_IO_STATE_SUPPORTED
164 /* Inform the I/O callback that the chunk CRC is being written.
165 * PNG_IO_CHUNK_CRC requires a single I/O function call.
166 */
167 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC;
168#endif
169
170 /* write the crc in a single operation */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500171 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500172
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500173 png_write_data(png_ptr, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500174}
175
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600176#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600177/*
178 * This pair of functions encapsulates the operation of (a) compressing a
179 * text string, and (b) issuing it later as a series of chunk data writes.
180 * The compression_state structure is shared context for these functions
181 * set up by the caller in order to make the whole mess thread-safe.
182 */
183
184typedef struct
185{
186 char *input; /* the uncompressed input data */
187 int input_len; /* its length */
188 int num_output_ptr; /* number of output pointers used */
189 int max_output_ptr; /* size of output_ptr */
190 png_charpp output_ptr; /* array of pointers to output */
191} compression_state;
192
193/* compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500194static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600195png_text_compress(png_structp png_ptr,
196 png_charp text, png_size_t text_len, int compression,
197 compression_state *comp)
198{
199 int ret;
200
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600201 comp->num_output_ptr = 0;
202 comp->max_output_ptr = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600203 comp->output_ptr = NULL;
204 comp->input = NULL;
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600205 comp->input_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600206
207 /* we may just want to pass the text right through */
208 if (compression == PNG_TEXT_COMPRESSION_NONE)
209 {
210 comp->input = text;
211 comp->input_len = text_len;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500212 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600213 }
214
215 if (compression >= PNG_TEXT_COMPRESSION_LAST)
216 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500217#if !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600218 char msg[50];
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500219 png_snprintf(msg, 50, "Unknown compression type %d", compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600220 png_warning(png_ptr, msg);
221#else
222 png_warning(png_ptr, "Unknown compression type");
223#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600224 }
225
226 /* We can't write the chunk until we find out how much data we have,
227 * which means we need to run the compressor first and save the
228 * output. This shouldn't be a problem, as the vast majority of
229 * comments should be reasonable, but we will set up an array of
230 * malloc'd pointers to be sure.
231 *
232 * If we knew the application was well behaved, we could simplify this
233 * greatly by assuming we can always malloc an output buffer large
234 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
235 * and malloc this directly. The only time this would be a bad idea is
236 * if we can't malloc more than 64K and we have 64K of random input
237 * data, or if the input string is incredibly large (although this
238 * wouldn't cause a failure, just a slowdown due to swapping).
239 */
240
241 /* set up the compression buffers */
242 png_ptr->zstream.avail_in = (uInt)text_len;
243 png_ptr->zstream.next_in = (Bytef *)text;
244 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
245 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
246
247 /* this is the same compression loop as in png_write_row() */
248 do
249 {
250 /* compress the data */
251 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
252 if (ret != Z_OK)
253 {
254 /* error */
255 if (png_ptr->zstream.msg != NULL)
256 png_error(png_ptr, png_ptr->zstream.msg);
257 else
258 png_error(png_ptr, "zlib error");
259 }
260 /* check to see if we need more room */
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600261 if (!(png_ptr->zstream.avail_out))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600262 {
263 /* make sure the output array has room */
264 if (comp->num_output_ptr >= comp->max_output_ptr)
265 {
266 int old_max;
267
268 old_max = comp->max_output_ptr;
269 comp->max_output_ptr = comp->num_output_ptr + 4;
270 if (comp->output_ptr != NULL)
271 {
272 png_charpp old_ptr;
273
274 old_ptr = comp->output_ptr;
275 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500276 (png_size_t)
277 (comp->max_output_ptr * png_sizeof(png_charpp)));
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500278 png_memcpy(comp->output_ptr, old_ptr, old_max
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500279 * png_sizeof(png_charp));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600280 png_free(png_ptr, old_ptr);
281 }
282 else
283 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500284 (png_size_t)
285 (comp->max_output_ptr * png_sizeof(png_charp)));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600286 }
287
288 /* save the data */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500289 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500290 (png_charp)png_malloc(png_ptr,
291 png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500292 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
293 png_ptr->zbuf_size);
294 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600295
296 /* and reset the buffer */
297 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
298 png_ptr->zstream.next_out = png_ptr->zbuf;
299 }
300 /* continue until we don't have any more to compress */
301 } while (png_ptr->zstream.avail_in);
302
303 /* finish the compression */
304 do
305 {
306 /* tell zlib we are finished */
307 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500308
309 if (ret == Z_OK)
310 {
311 /* check to see if we need more room */
312 if (!(png_ptr->zstream.avail_out))
313 {
314 /* check to make sure our output array has room */
315 if (comp->num_output_ptr >= comp->max_output_ptr)
316 {
317 int old_max;
318
319 old_max = comp->max_output_ptr;
320 comp->max_output_ptr = comp->num_output_ptr + 4;
321 if (comp->output_ptr != NULL)
322 {
323 png_charpp old_ptr;
324
325 old_ptr = comp->output_ptr;
326 /* This could be optimized to realloc() */
327 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500328 (png_size_t)(comp->max_output_ptr *
329 png_sizeof(png_charp)));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500330 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500331 old_max * png_sizeof(png_charp));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500332 png_free(png_ptr, old_ptr);
333 }
334 else
335 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500336 (png_size_t)(comp->max_output_ptr *
337 png_sizeof(png_charp)));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500338 }
339
340 /* save off the data */
341 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500342 (png_charp)png_malloc(png_ptr,
343 (png_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500344 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
345 png_ptr->zbuf_size);
346 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500347
348 /* and reset the buffer pointers */
349 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
350 png_ptr->zstream.next_out = png_ptr->zbuf;
351 }
352 }
353 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600354 {
355 /* we got an error */
356 if (png_ptr->zstream.msg != NULL)
357 png_error(png_ptr, png_ptr->zstream.msg);
358 else
359 png_error(png_ptr, "zlib error");
360 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600361 } while (ret != Z_STREAM_END);
362
363 /* text length is number of buffers plus last buffer */
364 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
365 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
366 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
367
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500368 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600369}
370
371/* ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500372static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600373png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
374{
375 int i;
376
377 /* handle the no-compression case */
378 if (comp->input)
379 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500380 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
381 (png_size_t)comp->input_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600382 return;
383 }
384
385 /* write saved output buffers, if any */
386 for (i = 0; i < comp->num_output_ptr; i++)
387 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500388 png_write_chunk_data(png_ptr, (png_bytep)comp->output_ptr[i],
389 (png_size_t)png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600390 png_free(png_ptr, comp->output_ptr[i]);
391 }
392 if (comp->max_output_ptr != 0)
393 png_free(png_ptr, comp->output_ptr);
394 /* write anything left in zbuf */
395 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
396 png_write_chunk_data(png_ptr, png_ptr->zbuf,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500397 (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600398
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600399 /* reset zlib for another zTXt/iTXt or image data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600400 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600401 png_ptr->zstream.data_type = Z_BINARY;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600402}
403#endif
404
Guy Schalnat0d580581995-07-20 02:43:20 -0500405/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600406 * information. Note that the rest of this code depends upon this
407 * information being correct.
408 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500409void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600410png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500411 int bit_depth, int color_type, int compression_type, int filter_type,
412 int interlace_type)
413{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600414#ifdef PNG_USE_LOCAL_ARRAYS
415 PNG_IHDR;
416#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500417 int ret;
418
Guy Schalnat0d580581995-07-20 02:43:20 -0500419 png_byte buf[13]; /* buffer to store the IHDR info */
420
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500421 png_debug(1, "in png_write_IHDR");
Guy Schalnate5a37791996-06-05 15:50:50 -0500422 /* Check that we have valid input data from the application info */
423 switch (color_type)
424 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500425 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500426 switch (bit_depth)
427 {
428 case 1:
429 case 2:
430 case 4:
431 case 8:
432 case 16: png_ptr->channels = 1; break;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500433 default: png_error(png_ptr, "Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500434 }
435 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500436 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500437 if (bit_depth != 8 && bit_depth != 16)
438 png_error(png_ptr, "Invalid bit depth for RGB image");
439 png_ptr->channels = 3;
440 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500441 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500442 switch (bit_depth)
443 {
444 case 1:
445 case 2:
446 case 4:
447 case 8: png_ptr->channels = 1; break;
448 default: png_error(png_ptr, "Invalid bit depth for paletted image");
449 }
450 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500451 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500452 if (bit_depth != 8 && bit_depth != 16)
453 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
454 png_ptr->channels = 2;
455 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500456 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500457 if (bit_depth != 8 && bit_depth != 16)
458 png_error(png_ptr, "Invalid bit depth for RGBA image");
459 png_ptr->channels = 4;
460 break;
461 default:
462 png_error(png_ptr, "Invalid image color type specified");
463 }
464
Andreas Dilger47a0c421997-05-16 02:46:07 -0500465 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500466 {
467 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500468 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500469 }
470
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600471 /* Write filter_method 64 (intrapixel differencing) only if
472 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
473 * 2. Libpng did not write a PNG signature (this filter_method is only
474 * used in PNG datastreams that are embedded in MNG datastreams) and
475 * 3. The application called png_permit_mng_features with a mask that
476 * included PNG_FLAG_MNG_FILTER_64 and
477 * 4. The filter_method is 64 and
478 * 5. The color_type is RGB or RGBA
479 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600480 if (
481#if defined(PNG_MNG_FEATURES_SUPPORTED)
482 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600483 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500484 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600485 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600486 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
487#endif
488 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500489 {
490 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500491 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500492 }
493
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600494#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500495 if (interlace_type != PNG_INTERLACE_NONE &&
496 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500497 {
498 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500499 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500500 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600501#else
502 interlace_type=PNG_INTERLACE_NONE;
503#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500504
505 /* save off the relevent information */
506 png_ptr->bit_depth = (png_byte)bit_depth;
507 png_ptr->color_type = (png_byte)color_type;
508 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500509#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600510 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500511#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500512 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500513 png_ptr->width = width;
514 png_ptr->height = height;
515
516 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500517 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
Guy Schalnate5a37791996-06-05 15:50:50 -0500518 /* set the usr info, so any transformations can modify it */
519 png_ptr->usr_width = png_ptr->width;
520 png_ptr->usr_bit_depth = png_ptr->bit_depth;
521 png_ptr->usr_channels = png_ptr->channels;
522
Guy Schalnat0d580581995-07-20 02:43:20 -0500523 /* pack the header information into the buffer */
524 png_save_uint_32(buf, width);
525 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600526 buf[8] = (png_byte)bit_depth;
527 buf[9] = (png_byte)color_type;
528 buf[10] = (png_byte)compression_type;
529 buf[11] = (png_byte)filter_type;
530 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500531
532 /* write the chunk */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500533 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500534
Andreas Dilger47a0c421997-05-16 02:46:07 -0500535 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600536 png_ptr->zstream.zalloc = png_zalloc;
537 png_ptr->zstream.zfree = png_zfree;
538 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500539 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500540 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500541 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
542 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500543 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500544 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500545 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500546 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500547 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500548 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500549 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500550 png_ptr->zlib_strategy = Z_FILTERED;
551 else
552 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
553 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500554 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500555 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500556 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500557 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500558 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500559 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500560 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500561 png_ptr->zlib_method = 8;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500562 ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
563 png_ptr->zlib_method, png_ptr->zlib_window_bits,
564 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
565 if (ret != Z_OK)
566 {
567 if (ret == Z_VERSION_ERROR) png_error(png_ptr,
568 "zlib failed to initialize compressor -- version error");
569 if (ret == Z_STREAM_ERROR) png_error(png_ptr,
570 "zlib failed to initialize compressor -- stream error");
571 if (ret == Z_MEM_ERROR) png_error(png_ptr,
572 "zlib failed to initialize compressor -- mem error");
573 png_error(png_ptr, "zlib failed to initialize compressor");
574 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600575 png_ptr->zstream.next_out = png_ptr->zbuf;
576 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600577 /* libpng is not interested in zstream.data_type */
578 /* set it to a predefined value, to avoid its evaluation inside zlib */
579 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500580
Guy Schalnate5a37791996-06-05 15:50:50 -0500581 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500582}
583
584/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500585 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600586 * structure.
587 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500588void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500589png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500590{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600591#ifdef PNG_USE_LOCAL_ARRAYS
592 PNG_PLTE;
593#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500594 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600595 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500596 png_byte buf[3];
597
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500598 png_debug(1, "in png_write_PLTE");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500599 if ((
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500600#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600601 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500602#endif
603 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500604 {
605 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500606 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500607 png_error(png_ptr, "Invalid number of colors in palette");
608 }
609 else
610 {
611 png_warning(png_ptr, "Invalid number of colors in palette");
612 return;
613 }
614 }
615
616 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
617 {
618 png_warning(png_ptr,
619 "Ignoring request to write a PLTE chunk in grayscale PNG");
620 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500621 }
622
Andreas Dilger47a0c421997-05-16 02:46:07 -0500623 png_ptr->num_palette = (png_uint_16)num_pal;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500624 png_debug1(3, "num_palette = %d", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500625
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500626 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE,
627 (png_uint_32)(num_pal * 3));
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500628#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500629 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500630 {
631 buf[0] = pal_ptr->red;
632 buf[1] = pal_ptr->green;
633 buf[2] = pal_ptr->blue;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500634 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500635 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500636#else
637 /* This is a little slower but some buggy compilers need to do this instead */
638 pal_ptr=palette;
639 for (i = 0; i < num_pal; i++)
640 {
641 buf[0] = pal_ptr[i].red;
642 buf[1] = pal_ptr[i].green;
643 buf[2] = pal_ptr[i].blue;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500644 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500645 }
646#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500647 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500648 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500649}
650
651/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500652void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500653png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500654{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600655#ifdef PNG_USE_LOCAL_ARRAYS
656 PNG_IDAT;
657#endif
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500658 png_debug(1, "in png_write_IDAT");
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500659
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500660 /* Optimize the CMF field in the zlib stream. */
661 /* This hack of the zlib stream is compliant to the stream specification. */
662 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
663 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
664 {
665 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
666 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
667 {
668 /* Avoid memory underflows and multiplication overflows. */
669 /* The conditions below are practically always satisfied;
670 however, they still must be checked. */
671 if (length >= 2 &&
672 png_ptr->height < 16384 && png_ptr->width < 16384)
673 {
674 png_uint_32 uncompressed_idat_size = png_ptr->height *
675 ((png_ptr->width *
676 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
677 unsigned int z_cinfo = z_cmf >> 4;
678 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
679 while (uncompressed_idat_size <= half_z_window_size &&
680 half_z_window_size >= 256)
681 {
682 z_cinfo--;
683 half_z_window_size >>= 1;
684 }
685 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
686 if (data[0] != (png_byte)z_cmf)
687 {
688 data[0] = (png_byte)z_cmf;
689 data[1] &= 0xe0;
690 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
691 }
692 }
693 }
694 else
695 png_error(png_ptr,
696 "Invalid zlib compression method or flags in IDAT");
697 }
698
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600699 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500700 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500701}
702
703/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500704void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600705png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500706{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600707#ifdef PNG_USE_LOCAL_ARRAYS
708 PNG_IEND;
709#endif
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500710 png_debug(1, "in png_write_IEND");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500711 png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL,
712 (png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600713 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500714}
715
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500716#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500717/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600718#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500719void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500720png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500721{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600722#ifdef PNG_USE_LOCAL_ARRAYS
723 PNG_gAMA;
724#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500725 png_uint_32 igamma;
726 png_byte buf[4];
727
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500728 png_debug(1, "in png_write_gAMA");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600729 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600730 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500731 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500732 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500733}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500734#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600735#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500736void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600737png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600738{
739#ifdef PNG_USE_LOCAL_ARRAYS
740 PNG_gAMA;
741#endif
742 png_byte buf[4];
743
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500744 png_debug(1, "in png_write_gAMA");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600745 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500746 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500747 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600748}
749#endif
750#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500751
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600752#if defined(PNG_WRITE_sRGB_SUPPORTED)
753/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500754void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600755png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600756{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600757#ifdef PNG_USE_LOCAL_ARRAYS
758 PNG_sRGB;
759#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600760 png_byte buf[1];
761
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500762 png_debug(1, "in png_write_sRGB");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500763 if (srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600764 png_warning(png_ptr,
765 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600766 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500767 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600768}
769#endif
770
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600771#if defined(PNG_WRITE_iCCP_SUPPORTED)
772/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500773void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600774png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
775 png_charp profile, int profile_len)
776{
777#ifdef PNG_USE_LOCAL_ARRAYS
778 PNG_iCCP;
779#endif
780 png_size_t name_len;
781 png_charp new_name;
782 compression_state comp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500783 int embedded_profile_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600784
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500785 png_debug(1, "in png_write_iCCP");
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600786
787 comp.num_output_ptr = 0;
788 comp.max_output_ptr = 0;
789 comp.output_ptr = NULL;
790 comp.input = NULL;
791 comp.input_len = 0;
792
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600793 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
794 &new_name)) == 0)
795 {
796 png_warning(png_ptr, "Empty keyword in iCCP chunk");
797 return;
798 }
799
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600800 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600801 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600802
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500803 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600804 profile_len = 0;
805
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500806 if (profile_len > 3)
Glenn Randers-Pehrson7edd4582006-12-07 19:16:44 -0600807 embedded_profile_len =
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500808 ((*( (png_bytep)profile ))<<24) |
809 ((*( (png_bytep)profile + 1))<<16) |
810 ((*( (png_bytep)profile + 2))<< 8) |
811 ((*( (png_bytep)profile + 3)) );
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500812
813 if (profile_len < embedded_profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500814 {
815 png_warning(png_ptr,
816 "Embedded profile length too large in iCCP chunk");
817 return;
818 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500819
820 if (profile_len > embedded_profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500821 {
822 png_warning(png_ptr,
823 "Truncating profile to actual length in iCCP chunk");
824 profile_len = embedded_profile_len;
825 }
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500826
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600827 if (profile_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500828 profile_len = png_text_compress(png_ptr, profile,
829 (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600830
831 /* make sure we include the NULL after the name and the compression type */
832 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500833 (png_uint_32)(name_len + profile_len + 2));
834 new_name[name_len + 1] = 0x00;
835 png_write_chunk_data(png_ptr, (png_bytep)new_name,
836 (png_size_t)(name_len + 2));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600837
838 if (profile_len)
839 png_write_compressed_data_out(png_ptr, &comp);
840
841 png_write_chunk_end(png_ptr);
842 png_free(png_ptr, new_name);
843}
844#endif
845
846#if defined(PNG_WRITE_sPLT_SUPPORTED)
847/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500848void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600849png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600850{
851#ifdef PNG_USE_LOCAL_ARRAYS
852 PNG_sPLT;
853#endif
854 png_size_t name_len;
855 png_charp new_name;
856 png_byte entrybuf[10];
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500857 png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
858 png_size_t palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600859 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500860#ifdef PNG_NO_POINTER_INDEXING
861 int i;
862#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600863
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500864 png_debug(1, "in png_write_sPLT");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600865 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
866 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600867 {
868 png_warning(png_ptr, "Empty keyword in sPLT chunk");
869 return;
870 }
871
872 /* make sure we include the NULL after the name */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500873 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500874 (png_uint_32)(name_len + 2 + palette_size));
875 png_write_chunk_data(png_ptr, (png_bytep)new_name,
876 (png_size_t)(name_len + 1));
877 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600878
879 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500880#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500881 for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600882 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500883 if (spalette->depth == 8)
884 {
885 entrybuf[0] = (png_byte)ep->red;
886 entrybuf[1] = (png_byte)ep->green;
887 entrybuf[2] = (png_byte)ep->blue;
888 entrybuf[3] = (png_byte)ep->alpha;
889 png_save_uint_16(entrybuf + 4, ep->frequency);
890 }
891 else
892 {
893 png_save_uint_16(entrybuf + 0, ep->red);
894 png_save_uint_16(entrybuf + 2, ep->green);
895 png_save_uint_16(entrybuf + 4, ep->blue);
896 png_save_uint_16(entrybuf + 6, ep->alpha);
897 png_save_uint_16(entrybuf + 8, ep->frequency);
898 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500899 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600900 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500901#else
902 ep=spalette->entries;
903 for (i=0; i>spalette->nentries; i++)
904 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500905 if (spalette->depth == 8)
906 {
907 entrybuf[0] = (png_byte)ep[i].red;
908 entrybuf[1] = (png_byte)ep[i].green;
909 entrybuf[2] = (png_byte)ep[i].blue;
910 entrybuf[3] = (png_byte)ep[i].alpha;
911 png_save_uint_16(entrybuf + 4, ep[i].frequency);
912 }
913 else
914 {
915 png_save_uint_16(entrybuf + 0, ep[i].red);
916 png_save_uint_16(entrybuf + 2, ep[i].green);
917 png_save_uint_16(entrybuf + 4, ep[i].blue);
918 png_save_uint_16(entrybuf + 6, ep[i].alpha);
919 png_save_uint_16(entrybuf + 8, ep[i].frequency);
920 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500921 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500922 }
923#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600924
925 png_write_chunk_end(png_ptr);
926 png_free(png_ptr, new_name);
927}
928#endif
929
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500930#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500931/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500932void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600933png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500934{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600935#ifdef PNG_USE_LOCAL_ARRAYS
936 PNG_sBIT;
937#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500938 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500939 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500940
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500941 png_debug(1, "in png_write_sBIT");
Guy Schalnat6d764711995-12-19 03:22:19 -0600942 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500943 if (color_type & PNG_COLOR_MASK_COLOR)
944 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600945 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500946
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500947 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
948 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600949 if (sbit->red == 0 || sbit->red > maxbits ||
950 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500951 sbit->blue == 0 || sbit->blue > maxbits)
952 {
953 png_warning(png_ptr, "Invalid sBIT depth specified");
954 return;
955 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500956 buf[0] = sbit->red;
957 buf[1] = sbit->green;
958 buf[2] = sbit->blue;
959 size = 3;
960 }
961 else
962 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500963 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
964 {
965 png_warning(png_ptr, "Invalid sBIT depth specified");
966 return;
967 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500968 buf[0] = sbit->gray;
969 size = 1;
970 }
971
972 if (color_type & PNG_COLOR_MASK_ALPHA)
973 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500974 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
975 {
976 png_warning(png_ptr, "Invalid sBIT depth specified");
977 return;
978 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500979 buf[size++] = sbit->alpha;
980 }
981
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600982 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500983}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500984#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500985
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500986#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500987/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600988#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500989void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500990png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600991 double red_x, double red_y, double green_x, double green_y,
992 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500993{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600994#ifdef PNG_USE_LOCAL_ARRAYS
995 PNG_cHRM;
996#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500997 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600998 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500999
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001000 png_debug(1, "in png_write_cHRM");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001001 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001002 if (png_check_cHRM(png_ptr, white_x, white_y, red_x, red_y,
1003 green_x, green_y, blue_x, blue_y))
Guy Schalnate5a37791996-06-05 15:50:50 -05001004 {
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001005 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001006 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001007 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001008 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -05001009
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001010 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001011 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001012 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001013 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -05001014
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001015 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001016 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001017 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001018 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -05001019
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001020 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001021 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001022 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001023 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -05001024
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001025 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001026 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001027}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001028#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001029#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001030void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001031png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
1032 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
1033 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
1034 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001035{
1036#ifdef PNG_USE_LOCAL_ARRAYS
1037 PNG_cHRM;
1038#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001039 png_byte buf[32];
1040
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001041 png_debug(1, "in png_write_cHRM");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001042 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001043 if (png_check_cHRM_fixed(png_ptr, white_x, white_y, red_x, red_y,
1044 green_x, green_y, blue_x, blue_y))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001045 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001046 png_save_uint_32(buf, (png_uint_32)white_x);
1047 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001048
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001049 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1050 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001051
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001052 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1053 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001054
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001055 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1056 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001057
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001058 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Glenn Randers-Pehrsonf7831012008-11-13 06:05:13 -06001059 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001060}
1061#endif
1062#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001063
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001064#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001065/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001066void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001067png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -05001068 int num_trans, int color_type)
1069{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001070#ifdef PNG_USE_LOCAL_ARRAYS
1071 PNG_tRNS;
1072#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001073 png_byte buf[6];
1074
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001075 png_debug(1, "in png_write_tRNS");
Guy Schalnat0d580581995-07-20 02:43:20 -05001076 if (color_type == PNG_COLOR_TYPE_PALETTE)
1077 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001078 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001079 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001080 png_warning(png_ptr, "Invalid number of transparent colors specified");
Guy Schalnate5a37791996-06-05 15:50:50 -05001081 return;
1082 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001083 /* write the chunk out as it is */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001084 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans,
1085 (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001086 }
1087 else if (color_type == PNG_COLOR_TYPE_GRAY)
1088 {
1089 /* one 16 bit value */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001090 if (tran->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001091 {
1092 png_warning(png_ptr,
1093 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1094 return;
1095 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001096 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001097 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001098 }
1099 else if (color_type == PNG_COLOR_TYPE_RGB)
1100 {
1101 /* three 16 bit values */
1102 png_save_uint_16(buf, tran->red);
1103 png_save_uint_16(buf + 2, tran->green);
1104 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001105 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001106 {
1107 png_warning(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001108 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001109 return;
1110 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001111 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001112 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001113 else
1114 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001115 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001116 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001117}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001118#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001119
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001120#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001121/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001122void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001123png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001124{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001125#ifdef PNG_USE_LOCAL_ARRAYS
1126 PNG_bKGD;
1127#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001128 png_byte buf[6];
1129
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001130 png_debug(1, "in png_write_bKGD");
Guy Schalnat0d580581995-07-20 02:43:20 -05001131 if (color_type == PNG_COLOR_TYPE_PALETTE)
1132 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001133 if (
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001134#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001135 (png_ptr->num_palette ||
1136 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001137#endif
1138 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001139 {
1140 png_warning(png_ptr, "Invalid background palette index");
1141 return;
1142 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001143 buf[0] = back->index;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001144 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001145 }
1146 else if (color_type & PNG_COLOR_MASK_COLOR)
1147 {
1148 png_save_uint_16(buf, back->red);
1149 png_save_uint_16(buf + 2, back->green);
1150 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001151 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001152 {
1153 png_warning(png_ptr,
1154 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1155 return;
1156 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001157 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001158 }
1159 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001160 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001161 if (back->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001162 {
1163 png_warning(png_ptr,
1164 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1165 return;
1166 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001167 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001168 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001169 }
1170}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001171#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001172
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001173#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001174/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001175void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001176png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001177{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001178#ifdef PNG_USE_LOCAL_ARRAYS
1179 PNG_hIST;
1180#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001181 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001182 png_byte buf[3];
1183
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001184 png_debug(1, "in png_write_hIST");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001185 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001186 {
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001187 png_debug2(3, "num_hist = %d, num_palette = %d", num_hist,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001188 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001189 png_warning(png_ptr, "Invalid number of histogram entries specified");
1190 return;
1191 }
1192
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001193 png_write_chunk_start(png_ptr, (png_bytep)png_hIST,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001194 (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001195 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001196 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001197 png_save_uint_16(buf, hist[i]);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001198 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001199 }
1200 png_write_chunk_end(png_ptr);
1201}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001202#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001203
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001204#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1205 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001206/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1207 * and if invalid, correct the keyword rather than discarding the entire
1208 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1209 * length, forbids leading or trailing whitespace, multiple internal spaces,
1210 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001211 *
1212 * The new_key is allocated to hold the corrected keyword and must be freed
1213 * by the calling routine. This avoids problems with trying to write to
1214 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001215 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001216png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001217png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001218{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001219 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001220 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001221 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001222 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001223
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001224 png_debug(1, "in png_check_keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001225 *new_key = NULL;
1226
1227 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001228 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001229 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001230 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001231 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001232
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001233 png_debug1(2, "Keyword to be checked is '%s'", key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001234
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001235 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001236 if (*new_key == NULL)
1237 {
1238 png_warning(png_ptr, "Out of memory while procesing keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001239 return ((png_size_t)0);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001240 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001241
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001242 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001243 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001244 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001245 if ((png_byte)*kp < 0x20 ||
1246 ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001247 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001248#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001249 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001250
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001251 png_snprintf(msg, 40,
1252 "invalid keyword character 0x%02X", (png_byte)*kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001253 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001254#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001255 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001256#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001257 *dp = ' ';
1258 }
1259 else
1260 {
1261 *dp = *kp;
1262 }
1263 }
1264 *dp = '\0';
1265
1266 /* Remove any trailing white space. */
1267 kp = *new_key + key_len - 1;
1268 if (*kp == ' ')
1269 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001270 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001271
1272 while (*kp == ' ')
1273 {
1274 *(kp--) = '\0';
1275 key_len--;
1276 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001277 }
1278
1279 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001280 kp = *new_key;
1281 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001282 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001283 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001284
1285 while (*kp == ' ')
1286 {
1287 kp++;
1288 key_len--;
1289 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001290 }
1291
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001292 png_debug1(2, "Checking for multiple internal spaces in '%s'", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001293
Andreas Dilger47a0c421997-05-16 02:46:07 -05001294 /* Remove multiple internal spaces. */
1295 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001296 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001297 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001298 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001299 *(dp++) = *kp;
1300 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001301 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001302 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001303 {
1304 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001305 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001306 }
1307 else
1308 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001309 *(dp++) = *kp;
1310 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001311 }
1312 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001313 *dp = '\0';
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001314 if (kwarn)
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001315 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001316
1317 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001318 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001319 png_free(png_ptr, *new_key);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001320 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001321 }
1322
1323 if (key_len > 79)
1324 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001325 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001326 new_key[79] = '\0';
Andreas Dilger47a0c421997-05-16 02:46:07 -05001327 key_len = 79;
1328 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001329
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001330 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001331}
1332#endif
1333
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001334#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001335/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001336void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001337png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001338 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001339{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001340#ifdef PNG_USE_LOCAL_ARRAYS
1341 PNG_tEXt;
1342#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001343 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001344 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001345
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001346 png_debug(1, "in png_write_tEXt");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001347 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1348 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001349 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001350 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001351 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001352
Andreas Dilger47a0c421997-05-16 02:46:07 -05001353 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001354 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001355 else
1356 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001357
1358 /* make sure we include the 0 after the key */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001359 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001360 (png_uint_32)(key_len + text_len + 1));
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001361 /*
1362 * We leave it to the application to meet PNG-1.0 requirements on the
1363 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1364 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001365 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001366 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001367 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1368 (png_size_t)(key_len + 1));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001369 if (text_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001370 png_write_chunk_data(png_ptr, (png_bytep)text, (png_size_t)text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001371
Guy Schalnat0d580581995-07-20 02:43:20 -05001372 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001373 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001374}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001375#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001376
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001377#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001378/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001379void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001380png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001381 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001382{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001383#ifdef PNG_USE_LOCAL_ARRAYS
1384 PNG_zTXt;
1385#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001386 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001387 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001388 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001389 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001390
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001391 png_debug(1, "in png_write_zTXt");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001392
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001393 comp.num_output_ptr = 0;
1394 comp.max_output_ptr = 0;
1395 comp.output_ptr = NULL;
1396 comp.input = NULL;
1397 comp.input_len = 0;
1398
Andreas Dilger47a0c421997-05-16 02:46:07 -05001399 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001400 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001401 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001402 png_free(png_ptr, new_key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001403 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001404 }
1405
Andreas Dilger47a0c421997-05-16 02:46:07 -05001406 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1407 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001408 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001409 png_free(png_ptr, new_key);
1410 return;
1411 }
1412
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001413 text_len = png_strlen(text);
1414
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001415 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001416 text_len = png_text_compress(png_ptr, text, text_len, compression,
1417 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001418
1419 /* write start of chunk */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001420 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt,
1421 (png_uint_32)(key_len+text_len + 2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001422 /* write key */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001423 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1424 (png_size_t)(key_len + 1));
1425 png_free(png_ptr, new_key);
1426
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001427 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001428 /* write compression */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001429 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001430 /* write the compressed data */
1431 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001432
Guy Schalnat0d580581995-07-20 02:43:20 -05001433 /* close the chunk */
1434 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001435}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001436#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001437
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001438#if defined(PNG_WRITE_iTXt_SUPPORTED)
1439/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001440void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001441png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001442 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001443{
1444#ifdef PNG_USE_LOCAL_ARRAYS
1445 PNG_iTXt;
1446#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001447 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001448 png_charp new_lang, new_key;
1449 png_byte cbuf[2];
1450 compression_state comp;
1451
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001452 png_debug(1, "in png_write_iTXt");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001453
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001454 comp.num_output_ptr = 0;
1455 comp.max_output_ptr = 0;
1456 comp.output_ptr = NULL;
1457 comp.input = NULL;
1458
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001459 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1460 {
1461 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1462 return;
1463 }
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001464 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001465 {
1466 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001467 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001468 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001469 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001470
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001471 if (lang_key == NULL)
1472 lang_key_len = 0;
1473 else
1474 lang_key_len = png_strlen(lang_key);
1475
1476 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001477 text_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001478 else
1479 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001480
1481 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001482 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1483 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001484
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001485
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001486 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001487 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001488
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001489 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1490 (png_uint_32)(
1491 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1492 + key_len
1493 + lang_len
1494 + lang_key_len
1495 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001496
1497 /*
1498 * We leave it to the application to meet PNG-1.0 requirements on the
1499 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1500 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1501 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1502 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001503 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1504 (png_size_t)(key_len + 1));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001505
1506 /* set the compression flag */
1507 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1508 compression == PNG_TEXT_COMPRESSION_NONE)
1509 cbuf[0] = 0;
1510 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1511 cbuf[0] = 1;
1512 /* set the compression method */
1513 cbuf[1] = 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001514 png_write_chunk_data(png_ptr, cbuf, (png_size_t)2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001515
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001516 cbuf[0] = 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001517 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf),
1518 (png_size_t)(lang_len + 1));
1519 png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf),
1520 (png_size_t)(lang_key_len + 1));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001521 png_write_compressed_data_out(png_ptr, &comp);
1522
1523 png_write_chunk_end(png_ptr);
1524 png_free(png_ptr, new_key);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001525 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001526}
1527#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001528
1529#if defined(PNG_WRITE_oFFs_SUPPORTED)
1530/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001531void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001532png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001533 int unit_type)
1534{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001535#ifdef PNG_USE_LOCAL_ARRAYS
1536 PNG_oFFs;
1537#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001538 png_byte buf[9];
1539
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001540 png_debug(1, "in png_write_oFFs");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001541 if (unit_type >= PNG_OFFSET_LAST)
1542 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1543
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001544 png_save_int_32(buf, x_offset);
1545 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001546 buf[8] = (png_byte)unit_type;
1547
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001548 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001549}
1550#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001551#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001552/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001553void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001554png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1555 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1556{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001557#ifdef PNG_USE_LOCAL_ARRAYS
1558 PNG_pCAL;
1559#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001560 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001561 png_uint_32p params_len;
1562 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001563 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001564 int i;
1565
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001566 png_debug1(1, "in png_write_pCAL (%d parameters)", nparams);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001567 if (type >= PNG_EQUATION_LAST)
1568 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1569
1570 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001571 png_debug1(3, "pCAL purpose length = %d", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001572 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001573 png_debug1(3, "pCAL units length = %d", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001574 total_len = purpose_len + units_len + 10;
1575
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001576 params_len = (png_uint_32p)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001577 (png_size_t)(nparams * png_sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001578
1579 /* Find the length of each parameter, making sure we don't count the
1580 null terminator for the last parameter. */
1581 for (i = 0; i < nparams; i++)
1582 {
1583 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001584 png_debug2(3, "pCAL parameter %d length = %lu", i,
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001585 (unsigned long) params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001586 total_len += (png_size_t)params_len[i];
1587 }
1588
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001589 png_debug1(3, "pCAL total length = %d", (int)total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001590 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001591 png_write_chunk_data(png_ptr, (png_bytep)new_purpose,
1592 (png_size_t)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001593 png_save_int_32(buf, X0);
1594 png_save_int_32(buf + 4, X1);
1595 buf[8] = (png_byte)type;
1596 buf[9] = (png_byte)nparams;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001597 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1598 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001599
1600 png_free(png_ptr, new_purpose);
1601
1602 for (i = 0; i < nparams; i++)
1603 {
1604 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1605 (png_size_t)params_len[i]);
1606 }
1607
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001608 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001609 png_write_chunk_end(png_ptr);
1610}
1611#endif
1612
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001613#if defined(PNG_WRITE_sCAL_SUPPORTED)
1614/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001615#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001616void /* PRIVATE */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001617png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001618{
1619#ifdef PNG_USE_LOCAL_ARRAYS
1620 PNG_sCAL;
1621#endif
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001622 char buf[64];
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001623 png_size_t total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001624
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001625 png_debug(1, "in png_write_sCAL");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001626
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001627 buf[0] = (char)unit;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001628 png_snprintf(buf + 1, 63, "%12.12e", width);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001629 total_len = 1 + png_strlen(buf + 1) + 1;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001630 png_snprintf(buf + total_len, 64-total_len, "%12.12e", height);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001631 total_len += png_strlen(buf + total_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001632
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001633 png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001634 png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001635}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001636#else
1637#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001638void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001639png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001640 png_charp height)
1641{
1642#ifdef PNG_USE_LOCAL_ARRAYS
1643 PNG_sCAL;
1644#endif
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001645 png_byte buf[64];
1646 png_size_t wlen, hlen, total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001647
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001648 png_debug(1, "in png_write_sCAL_s");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001649
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001650 wlen = png_strlen(width);
1651 hlen = png_strlen(height);
1652 total_len = wlen + hlen + 2;
1653 if (total_len > 64)
1654 {
1655 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
1656 return;
1657 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001658
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001659 buf[0] = (png_byte)unit;
1660 png_memcpy(buf + 1, width, wlen + 1); /* append the '\0' here */
1661 png_memcpy(buf + wlen + 2, height, hlen); /* do NOT append the '\0' here */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001662
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001663 png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001664 png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001665}
1666#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001667#endif
1668#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001669
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001670#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001671/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001672void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001673png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001674 png_uint_32 y_pixels_per_unit,
1675 int unit_type)
1676{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001677#ifdef PNG_USE_LOCAL_ARRAYS
1678 PNG_pHYs;
1679#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001680 png_byte buf[9];
1681
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001682 png_debug(1, "in png_write_pHYs");
Guy Schalnate5a37791996-06-05 15:50:50 -05001683 if (unit_type >= PNG_RESOLUTION_LAST)
1684 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1685
Guy Schalnat0d580581995-07-20 02:43:20 -05001686 png_save_uint_32(buf, x_pixels_per_unit);
1687 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001688 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001689
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001690 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001691}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001692#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001693
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001694#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001695/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1696 * or png_convert_from_time_t(), or fill in the structure yourself.
1697 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001698void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001699png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001700{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001701#ifdef PNG_USE_LOCAL_ARRAYS
1702 PNG_tIME;
1703#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001704 png_byte buf[7];
1705
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001706 png_debug(1, "in png_write_tIME");
Guy Schalnate5a37791996-06-05 15:50:50 -05001707 if (mod_time->month > 12 || mod_time->month < 1 ||
1708 mod_time->day > 31 || mod_time->day < 1 ||
1709 mod_time->hour > 23 || mod_time->second > 60)
1710 {
1711 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1712 return;
1713 }
1714
Guy Schalnat0d580581995-07-20 02:43:20 -05001715 png_save_uint_16(buf, mod_time->year);
1716 buf[2] = mod_time->month;
1717 buf[3] = mod_time->day;
1718 buf[4] = mod_time->hour;
1719 buf[5] = mod_time->minute;
1720 buf[6] = mod_time->second;
1721
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001722 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001723}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001724#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001725
1726/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001727void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001728png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001729{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001730#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001731#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001732 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001733
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001734 /* start of interlace block */
1735 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001736
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001737 /* offset to next interlace block */
1738 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001739
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001740 /* start of interlace block in the y direction */
1741 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001742
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001743 /* offset to next interlace block in the y direction */
1744 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001745#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001746#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001747
Andreas Dilger47a0c421997-05-16 02:46:07 -05001748 png_size_t buf_size;
1749
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001750 png_debug(1, "in png_write_start_row");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001751 buf_size = (png_size_t)(PNG_ROWBYTES(
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001752 png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001753
Guy Schalnat0d580581995-07-20 02:43:20 -05001754 /* set up row buffer */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001755 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr,
1756 (png_size_t)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001757 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001758
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001759#ifndef PNG_NO_WRITE_FILTER
Guy Schalnate5a37791996-06-05 15:50:50 -05001760 /* set up filtering buffer, if using this filter */
1761 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001762 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001763 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001764 (png_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001765 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001766 }
1767
Andreas Dilger47a0c421997-05-16 02:46:07 -05001768 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001769 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1770 {
1771 /* set up previous row buffer */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001772 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr,
1773 (png_size_t)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001774 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001775
1776 if (png_ptr->do_filter & PNG_FILTER_UP)
1777 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001778 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001779 (png_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001780 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001781 }
1782
1783 if (png_ptr->do_filter & PNG_FILTER_AVG)
1784 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001785 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001786 (png_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001787 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001788 }
1789
1790 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1791 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001792 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001793 (png_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001794 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001795 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001796 }
Glenn Randers-Pehrsoneb580912008-07-30 14:47:09 -05001797#endif /* PNG_NO_WRITE_FILTER */
Guy Schalnat0d580581995-07-20 02:43:20 -05001798
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001799#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001800 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001801 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001802 {
1803 if (!(png_ptr->transformations & PNG_INTERLACE))
1804 {
1805 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1806 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001807 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1808 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001809 }
1810 else
1811 {
1812 png_ptr->num_rows = png_ptr->height;
1813 png_ptr->usr_width = png_ptr->width;
1814 }
1815 }
1816 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001817#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001818 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001819 png_ptr->num_rows = png_ptr->height;
1820 png_ptr->usr_width = png_ptr->width;
1821 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001822 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1823 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001824}
1825
Andreas Dilger47a0c421997-05-16 02:46:07 -05001826/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001827void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001828png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001829{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001830#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001831#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001832 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001833
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001834 /* start of interlace block */
1835 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001836
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001837 /* offset to next interlace block */
1838 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001839
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001840 /* start of interlace block in the y direction */
1841 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001842
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001843 /* offset to next interlace block in the y direction */
1844 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001845#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001846#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001847
Guy Schalnat0d580581995-07-20 02:43:20 -05001848 int ret;
1849
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001850 png_debug(1, "in png_write_finish_row");
Guy Schalnat0d580581995-07-20 02:43:20 -05001851 /* next row */
1852 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001853
Guy Schalnat0d580581995-07-20 02:43:20 -05001854 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001855 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001856 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001857
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001858#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001859 /* if interlaced, go to next pass */
1860 if (png_ptr->interlaced)
1861 {
1862 png_ptr->row_number = 0;
1863 if (png_ptr->transformations & PNG_INTERLACE)
1864 {
1865 png_ptr->pass++;
1866 }
1867 else
1868 {
1869 /* loop until we find a non-zero width or height pass */
1870 do
1871 {
1872 png_ptr->pass++;
1873 if (png_ptr->pass >= 7)
1874 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001875 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001876 png_pass_inc[png_ptr->pass] - 1 -
1877 png_pass_start[png_ptr->pass]) /
1878 png_pass_inc[png_ptr->pass];
1879 png_ptr->num_rows = (png_ptr->height +
1880 png_pass_yinc[png_ptr->pass] - 1 -
1881 png_pass_ystart[png_ptr->pass]) /
1882 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001883 if (png_ptr->transformations & PNG_INTERLACE)
1884 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001885 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1886
1887 }
1888
Guy Schalnate5a37791996-06-05 15:50:50 -05001889 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001890 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001891 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001892 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001893 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001894 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001895 png_ptr->usr_bit_depth, png_ptr->width)) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001896 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001897 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001898 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001899#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001900
1901 /* if we get here, we've just written the last row, so we need
1902 to flush the compressor */
1903 do
1904 {
1905 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001906 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001907 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001908 if (ret == Z_OK)
1909 {
1910 /* check to see if we need more room */
1911 if (!(png_ptr->zstream.avail_out))
1912 {
1913 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1914 png_ptr->zstream.next_out = png_ptr->zbuf;
1915 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1916 }
1917 }
1918 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001919 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001920 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001921 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001922 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001923 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001924 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001925 } while (ret != Z_STREAM_END);
1926
1927 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001928 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001929 {
1930 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001931 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001932 }
1933
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001934 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -06001935 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat0d580581995-07-20 02:43:20 -05001936}
1937
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001938#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001939/* Pick out the correct pixels for the interlace pass.
1940 * The basic idea here is to go through the row with a source
1941 * pointer and a destination pointer (sp and dp), and copy the
1942 * correct pixels for the pass. As the row gets compacted,
1943 * sp will always be >= dp, so we should never overwrite anything.
1944 * See the default: case for the easiest code to understand.
1945 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001946void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001947png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001948{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001949#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001950 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001951
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001952 /* start of interlace block */
1953 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001954
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001955 /* offset to next interlace block */
1956 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001957#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001958
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001959 png_debug(1, "in png_do_write_interlace");
Guy Schalnat0d580581995-07-20 02:43:20 -05001960 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001961 if (pass < 6)
Guy Schalnat0d580581995-07-20 02:43:20 -05001962 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001963 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001964 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001965 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001966 case 1:
1967 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001968 png_bytep sp;
1969 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001970 int shift;
1971 int d;
1972 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001973 png_uint_32 i;
1974 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001975
1976 dp = row;
1977 d = 0;
1978 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001979 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001980 i += png_pass_inc[pass])
1981 {
1982 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001983 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001984 d |= (value << shift);
1985
1986 if (shift == 0)
1987 {
1988 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001989 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001990 d = 0;
1991 }
1992 else
1993 shift--;
1994
1995 }
1996 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001997 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001998 break;
1999 }
2000 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002001 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002002 png_bytep sp;
2003 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05002004 int shift;
2005 int d;
2006 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002007 png_uint_32 i;
2008 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002009
2010 dp = row;
2011 shift = 6;
2012 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002013 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002014 i += png_pass_inc[pass])
2015 {
2016 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002017 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05002018 d |= (value << shift);
2019
2020 if (shift == 0)
2021 {
2022 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002023 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002024 d = 0;
2025 }
2026 else
2027 shift -= 2;
2028 }
2029 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002030 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002031 break;
2032 }
2033 case 4:
2034 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002035 png_bytep sp;
2036 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002037 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05002038 int d;
2039 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002040 png_uint_32 i;
2041 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002042
2043 dp = row;
2044 shift = 4;
2045 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002046 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002047 i += png_pass_inc[pass])
2048 {
2049 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002050 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05002051 d |= (value << shift);
2052
2053 if (shift == 0)
2054 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002055 shift = 4;
2056 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002057 d = 0;
2058 }
2059 else
2060 shift -= 4;
2061 }
2062 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002063 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002064 break;
2065 }
2066 default:
2067 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002068 png_bytep sp;
2069 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002070 png_uint_32 i;
2071 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002072 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002073
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002074 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002075 dp = row;
2076 /* find out how many bytes each pixel takes up */
2077 pixel_bytes = (row_info->pixel_depth >> 3);
2078 /* loop through the row, only looking at the pixels that
2079 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002080 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002081 i += png_pass_inc[pass])
2082 {
2083 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002084 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002085 /* move the pixel */
2086 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002087 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05002088 /* next pixel */
2089 dp += pixel_bytes;
2090 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002091 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002092 }
2093 }
2094 /* set new row width */
2095 row_info->width = (row_info->width +
2096 png_pass_inc[pass] - 1 -
2097 png_pass_start[pass]) /
2098 png_pass_inc[pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002099 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2100 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002101 }
2102}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002103#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002104
Andreas Dilger47a0c421997-05-16 02:46:07 -05002105/* This filters the row, chooses which filter to use, if it has not already
2106 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002107 * chosen filter.
2108 */
Glenn Randers-Pehrson78067772004-11-02 21:49:39 -06002109#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002110#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002111#define PNG_LOMASK ((png_uint_32)0xffffL)
2112#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002113void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002114png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002115{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002116 png_bytep best_row;
2117#ifndef PNG_NO_WRITE_FILTER
2118 png_bytep prev_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002119 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002120 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002121 png_uint_32 row_bytes = row_info->rowbytes;
2122#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2123 int num_p_filters = (int)png_ptr->num_prev_filters;
2124#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002125
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002126 png_debug(1, "in png_write_find_filter");
Guy Schalnat0d580581995-07-20 02:43:20 -05002127 /* find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002128 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002129
2130 prev_row = png_ptr->prev_row;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002131#endif
2132 best_row = png_ptr->row_buf;
2133#ifndef PNG_NO_WRITE_FILTER
2134 row_buf = best_row;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002135 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002136
Andreas Dilger47a0c421997-05-16 02:46:07 -05002137 /* The prediction method we use is to find which method provides the
2138 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002139 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002140 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002141 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002142 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002143 * predictive" method (not implemented yet), which does test compressions
2144 * of lines using different filter methods, and then chooses the
2145 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002146 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002147 *
2148 * GRR 980525: consider also
2149 * (1) minimum sum of absolute differences from running average (i.e.,
2150 * keep running sum of non-absolute differences & count of bytes)
2151 * [track dispersion, too? restart average if dispersion too large?]
2152 * (1b) minimum sum of absolute differences from sliding average, probably
2153 * with window size <= deflate window (usually 32K)
2154 * (2) minimum sum of squared differences from zero or running average
2155 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002156 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002157
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002158
Guy Schalnate5a37791996-06-05 15:50:50 -05002159 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002160 * that has been chosen, as it doesn't actually do anything to the data.
2161 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002162 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002163 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002164 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002165 png_bytep rp;
2166 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002167 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002168 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002169
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002170 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002171 {
2172 v = *rp;
2173 sum += (v < 128) ? v : 256 - v;
2174 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002175
2176#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2177 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2178 {
2179 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002180 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002181 sumlo = sum & PNG_LOMASK;
2182 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2183
2184 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002185 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002186 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002187 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002188 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002189 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002190 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002191 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002192 PNG_WEIGHT_SHIFT;
2193 }
2194 }
2195
2196 /* Factor in the cost of this filter (this is here for completeness,
2197 * but it makes no sense to have a "cost" for the NONE filter, as
2198 * it has the minimum possible computational cost - none).
2199 */
2200 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2201 PNG_COST_SHIFT;
2202 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2203 PNG_COST_SHIFT;
2204
2205 if (sumhi > PNG_HIMASK)
2206 sum = PNG_MAXSUM;
2207 else
2208 sum = (sumhi << PNG_HISHIFT) + sumlo;
2209 }
2210#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002211 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002212 }
2213
Guy Schalnate5a37791996-06-05 15:50:50 -05002214 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002215 if (filter_to_do == PNG_FILTER_SUB)
2216 /* it's the only filter so no testing is needed */
2217 {
2218 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002219 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002220 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2221 i++, rp++, dp++)
2222 {
2223 *dp = *rp;
2224 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002225 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002226 i++, rp++, lp++, dp++)
2227 {
2228 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2229 }
2230 best_row = png_ptr->sub_row;
2231 }
2232
2233 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002234 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002235 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002236 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002237 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002238 int v;
2239
2240#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002241 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002242 * would reduce the sum of this filter, so that we can do the
2243 * early exit comparison without scaling the sum each time.
2244 */
2245 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2246 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002247 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002248 png_uint_32 lmhi, lmlo;
2249 lmlo = lmins & PNG_LOMASK;
2250 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2251
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002252 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002253 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002254 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002255 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002256 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002257 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002258 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002259 PNG_WEIGHT_SHIFT;
2260 }
2261 }
2262
2263 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2264 PNG_COST_SHIFT;
2265 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2266 PNG_COST_SHIFT;
2267
2268 if (lmhi > PNG_HIMASK)
2269 lmins = PNG_MAXSUM;
2270 else
2271 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2272 }
2273#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002274
Guy Schalnate5a37791996-06-05 15:50:50 -05002275 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2276 i++, rp++, dp++)
2277 {
2278 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002279
Guy Schalnate5a37791996-06-05 15:50:50 -05002280 sum += (v < 128) ? v : 256 - v;
2281 }
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002282 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002283 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002284 {
2285 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002286
Guy Schalnate5a37791996-06-05 15:50:50 -05002287 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002288
2289 if (sum > lmins) /* We are already worse, don't continue. */
2290 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002291 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002292
2293#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2294 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2295 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002296 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002297 png_uint_32 sumhi, sumlo;
2298 sumlo = sum & PNG_LOMASK;
2299 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2300
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002301 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002302 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002303 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002304 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002305 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002306 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002307 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002308 PNG_WEIGHT_SHIFT;
2309 }
2310 }
2311
2312 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2313 PNG_COST_SHIFT;
2314 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2315 PNG_COST_SHIFT;
2316
2317 if (sumhi > PNG_HIMASK)
2318 sum = PNG_MAXSUM;
2319 else
2320 sum = (sumhi << PNG_HISHIFT) + sumlo;
2321 }
2322#endif
2323
Guy Schalnate5a37791996-06-05 15:50:50 -05002324 if (sum < mins)
2325 {
2326 mins = sum;
2327 best_row = png_ptr->sub_row;
2328 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002329 }
2330
Guy Schalnate5a37791996-06-05 15:50:50 -05002331 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002332 if (filter_to_do == PNG_FILTER_UP)
2333 {
2334 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002335 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002336
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002337 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002338 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002339 i++, rp++, pp++, dp++)
2340 {
2341 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2342 }
2343 best_row = png_ptr->up_row;
2344 }
2345
2346 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002347 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002348 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002349 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002350 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002351 int v;
2352
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002353
Andreas Dilger47a0c421997-05-16 02:46:07 -05002354#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2355 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2356 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002357 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002358 png_uint_32 lmhi, lmlo;
2359 lmlo = lmins & PNG_LOMASK;
2360 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2361
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002362 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002363 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002364 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002365 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002366 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002367 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002368 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002369 PNG_WEIGHT_SHIFT;
2370 }
2371 }
2372
2373 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2374 PNG_COST_SHIFT;
2375 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2376 PNG_COST_SHIFT;
2377
2378 if (lmhi > PNG_HIMASK)
2379 lmins = PNG_MAXSUM;
2380 else
2381 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2382 }
2383#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002384
2385 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002386 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002387 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002388 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002389
2390 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002391
2392 if (sum > lmins) /* We are already worse, don't continue. */
2393 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002394 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002395
2396#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2397 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2398 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002399 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002400 png_uint_32 sumhi, sumlo;
2401 sumlo = sum & PNG_LOMASK;
2402 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2403
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002404 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002405 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002406 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002407 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002408 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002409 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002410 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002411 PNG_WEIGHT_SHIFT;
2412 }
2413 }
2414
2415 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2416 PNG_COST_SHIFT;
2417 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2418 PNG_COST_SHIFT;
2419
2420 if (sumhi > PNG_HIMASK)
2421 sum = PNG_MAXSUM;
2422 else
2423 sum = (sumhi << PNG_HISHIFT) + sumlo;
2424 }
2425#endif
2426
Guy Schalnate5a37791996-06-05 15:50:50 -05002427 if (sum < mins)
2428 {
2429 mins = sum;
2430 best_row = png_ptr->up_row;
2431 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002432 }
2433
Guy Schalnate5a37791996-06-05 15:50:50 -05002434 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002435 if (filter_to_do == PNG_FILTER_AVG)
2436 {
2437 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002438 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002439 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002440 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002441 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002442 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002443 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002444 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002445 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002446 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2447 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002448 }
2449 best_row = png_ptr->avg_row;
2450 }
2451
2452 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002453 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002454 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002455 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002456 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002457 int v;
2458
2459#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2460 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2461 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002462 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002463 png_uint_32 lmhi, lmlo;
2464 lmlo = lmins & PNG_LOMASK;
2465 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2466
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002467 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002468 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002469 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002470 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002471 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002472 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002473 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002474 PNG_WEIGHT_SHIFT;
2475 }
2476 }
2477
2478 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2479 PNG_COST_SHIFT;
2480 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2481 PNG_COST_SHIFT;
2482
2483 if (lmhi > PNG_HIMASK)
2484 lmins = PNG_MAXSUM;
2485 else
2486 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2487 }
2488#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002489
2490 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002491 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002492 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002493 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002494
2495 sum += (v < 128) ? v : 256 - v;
2496 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002497 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002498 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002499 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002500 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002501
2502 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002503
2504 if (sum > lmins) /* We are already worse, don't continue. */
2505 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002506 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002507
2508#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2509 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2510 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002511 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002512 png_uint_32 sumhi, sumlo;
2513 sumlo = sum & PNG_LOMASK;
2514 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2515
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002516 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002517 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002518 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002519 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002520 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002521 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002522 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002523 PNG_WEIGHT_SHIFT;
2524 }
2525 }
2526
2527 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2528 PNG_COST_SHIFT;
2529 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2530 PNG_COST_SHIFT;
2531
2532 if (sumhi > PNG_HIMASK)
2533 sum = PNG_MAXSUM;
2534 else
2535 sum = (sumhi << PNG_HISHIFT) + sumlo;
2536 }
2537#endif
2538
Guy Schalnate5a37791996-06-05 15:50:50 -05002539 if (sum < mins)
2540 {
2541 mins = sum;
2542 best_row = png_ptr->avg_row;
2543 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002544 }
2545
Andreas Dilger47a0c421997-05-16 02:46:07 -05002546 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002547 if (filter_to_do == PNG_FILTER_PAETH)
2548 {
2549 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002550 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002551 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002552 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002553 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002554 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002555 }
2556
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002557 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002558 {
2559 int a, b, c, pa, pb, pc, p;
2560
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002561 b = *pp++;
2562 c = *cp++;
2563 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002564
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002565 p = b - c;
2566 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002567
2568#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002569 pa = abs(p);
2570 pb = abs(pc);
2571 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002572#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002573 pa = p < 0 ? -p : p;
2574 pb = pc < 0 ? -pc : pc;
2575 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002576#endif
2577
2578 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2579
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002580 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002581 }
2582 best_row = png_ptr->paeth_row;
2583 }
2584
2585 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002586 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002587 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002588 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002589 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002590 int v;
2591
2592#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2593 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2594 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002595 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002596 png_uint_32 lmhi, lmlo;
2597 lmlo = lmins & PNG_LOMASK;
2598 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2599
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002600 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002601 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002602 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002603 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002604 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002605 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002606 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002607 PNG_WEIGHT_SHIFT;
2608 }
2609 }
2610
2611 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2612 PNG_COST_SHIFT;
2613 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2614 PNG_COST_SHIFT;
2615
2616 if (lmhi > PNG_HIMASK)
2617 lmins = PNG_MAXSUM;
2618 else
2619 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2620 }
2621#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002622
2623 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002624 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002625 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002626 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002627
2628 sum += (v < 128) ? v : 256 - v;
2629 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002630
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002631 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002632 {
2633 int a, b, c, pa, pb, pc, p;
2634
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002635 b = *pp++;
2636 c = *cp++;
2637 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002638
2639#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002640 p = b - c;
2641 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002642#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002643 pa = abs(p);
2644 pb = abs(pc);
2645 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002646#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002647 pa = p < 0 ? -p : p;
2648 pb = pc < 0 ? -pc : pc;
2649 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002650#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002651 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2652#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002653 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002654 pa = abs(p - a);
2655 pb = abs(p - b);
2656 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002657 if (pa <= pb && pa <= pc)
2658 p = a;
2659 else if (pb <= pc)
2660 p = b;
2661 else
2662 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002663#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002664
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002665 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002666
2667 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002668
2669 if (sum > lmins) /* We are already worse, don't continue. */
2670 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002671 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002672
2673#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2674 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2675 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002676 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002677 png_uint_32 sumhi, sumlo;
2678 sumlo = sum & PNG_LOMASK;
2679 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2680
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002681 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002682 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002683 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002684 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002685 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002686 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002687 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002688 PNG_WEIGHT_SHIFT;
2689 }
2690 }
2691
2692 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2693 PNG_COST_SHIFT;
2694 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2695 PNG_COST_SHIFT;
2696
2697 if (sumhi > PNG_HIMASK)
2698 sum = PNG_MAXSUM;
2699 else
2700 sum = (sumhi << PNG_HISHIFT) + sumlo;
2701 }
2702#endif
2703
Guy Schalnate5a37791996-06-05 15:50:50 -05002704 if (sum < mins)
2705 {
2706 best_row = png_ptr->paeth_row;
2707 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002708 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002709#endif /* PNG_NO_WRITE_FILTER */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002710 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002711
Guy Schalnate5a37791996-06-05 15:50:50 -05002712 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002713
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002714#ifndef PNG_NO_WRITE_FILTER
Andreas Dilger47a0c421997-05-16 02:46:07 -05002715#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2716 /* Save the type of filter we picked this time for future calculations */
2717 if (png_ptr->num_prev_filters > 0)
2718 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002719 int j;
2720 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002721 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002722 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002723 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002724 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002725 }
2726#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002727#endif /* PNG_NO_WRITE_FILTER */
Guy Schalnate5a37791996-06-05 15:50:50 -05002728}
2729
2730
Andreas Dilger47a0c421997-05-16 02:46:07 -05002731/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002732void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002733png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2734{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002735 png_debug(1, "in png_write_filtered_row");
2736 png_debug1(2, "filter = %d", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002737 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002738
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002739 png_ptr->zstream.next_in = filtered_row;
2740 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002741 /* repeat until we have compressed all the data */
2742 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002743 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002744 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002745
Guy Schalnate5a37791996-06-05 15:50:50 -05002746 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002747 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002748 /* check for compression errors */
2749 if (ret != Z_OK)
2750 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002751 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002752 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002753 else
2754 png_error(png_ptr, "zlib error");
2755 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002756
Guy Schalnate5a37791996-06-05 15:50:50 -05002757 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002758 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002759 {
2760 /* write the IDAT and reset the zlib output buffer */
2761 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002762 png_ptr->zstream.next_out = png_ptr->zbuf;
2763 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002764 }
2765 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002766 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002767
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002768 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002769 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002770 {
2771 png_bytep tptr;
2772
2773 tptr = png_ptr->prev_row;
2774 png_ptr->prev_row = png_ptr->row_buf;
2775 png_ptr->row_buf = tptr;
2776 }
2777
Guy Schalnate5a37791996-06-05 15:50:50 -05002778 /* finish row - updates counters and flushes zlib if last row */
2779 png_write_finish_row(png_ptr);
2780
2781#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2782 png_ptr->flush_rows++;
2783
2784 if (png_ptr->flush_dist > 0 &&
2785 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002786 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002787 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002788 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002789#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002790}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05002791#endif /* PNG_WRITE_SUPPORTED */