blob: c272f8776d38abc49caf9498694c8b67255d376f [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-Pehrsoneb580912008-07-30 14:47:09 -05004 * Last changed in libpng 1.4.0 [July 30, 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-Pehrson145f5c82008-07-10 09:13:13 -0500113 png_debug2(0, "Writing %s chunk, length = %lu\n", chunk_name,
114 (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
Andreas Dilger47a0c421997-05-16 02:46:07 -0500421 png_debug(1, "in png_write_IHDR\n");
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
Andreas Dilger47a0c421997-05-16 02:46:07 -0500598 png_debug(1, "in png_write_PLTE\n");
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;
624 png_debug1(3, "num_palette = %d\n", 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
Andreas Dilger47a0c421997-05-16 02:46:07 -0500658 png_debug(1, "in png_write_IDAT\n");
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
Andreas Dilger47a0c421997-05-16 02:46:07 -0500710 png_debug(1, "in png_write_IEND\n");
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
Andreas Dilger47a0c421997-05-16 02:46:07 -0500728 png_debug(1, "in png_write_gAMA\n");
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
744 png_debug(1, "in png_write_gAMA\n");
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
762 png_debug(1, "in png_write_sRGB\n");
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
785 png_debug(1, "in png_write_iCCP\n");
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
864 png_debug(1, "in png_write_sPLT\n");
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
Andreas Dilger47a0c421997-05-16 02:46:07 -0500941 png_debug(1, "in png_write_sBIT\n");
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
Andreas Dilger47a0c421997-05-16 02:46:07 -05001000 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001001 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -05001002 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
1003 white_x + white_y > 1.0)
1004 {
1005 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001006#if !defined(PNG_NO_CONSOLE_IO)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001007 fprintf(stderr, "white_x=%f, white_y=%f\n", white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001008#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001009 return;
1010 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001011 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001012 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001013 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001014 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -05001015
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001016 if (red_x < 0 || red_y < 0 || red_x + red_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001017 {
1018 png_warning(png_ptr, "Invalid cHRM red point specified");
1019 return;
1020 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001021 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001022 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001023 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001024 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -05001025
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001026 if (green_x < 0 || green_y < 0 || green_x + green_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001027 {
1028 png_warning(png_ptr, "Invalid cHRM green point specified");
1029 return;
1030 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001031 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001032 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001033 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001034 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -05001035
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001036 if (blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001037 {
1038 png_warning(png_ptr, "Invalid cHRM blue point specified");
1039 return;
1040 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001041 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001042 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001043 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001044 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -05001045
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001046 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Guy Schalnat0d580581995-07-20 02:43:20 -05001047}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001048#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001049#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001050void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001051png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
1052 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
1053 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
1054 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001055{
1056#ifdef PNG_USE_LOCAL_ARRAYS
1057 PNG_cHRM;
1058#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001059 png_byte buf[32];
1060
1061 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001062 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001063 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
1064 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001065 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001066#if !defined(PNG_NO_CONSOLE_IO)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001067 fprintf(stderr, "white_x=%ld, white_y=%ld\n", (unsigned long)white_x,
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001068 (unsigned long)white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001069#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001070 return;
1071 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001072 png_save_uint_32(buf, (png_uint_32)white_x);
1073 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001074
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001075 if (red_x + red_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001076 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001077 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001078 return;
1079 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001080 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1081 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001082
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001083 if (green_x + green_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001084 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001085 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001086 return;
1087 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001088 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1089 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001090
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001091 if (blue_x + blue_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001092 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001093 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001094 return;
1095 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001096 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1097 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001098
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001099 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001100}
1101#endif
1102#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001103
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001104#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001105/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001106void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001107png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -05001108 int num_trans, int color_type)
1109{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001110#ifdef PNG_USE_LOCAL_ARRAYS
1111 PNG_tRNS;
1112#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001113 png_byte buf[6];
1114
Andreas Dilger47a0c421997-05-16 02:46:07 -05001115 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001116 if (color_type == PNG_COLOR_TYPE_PALETTE)
1117 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001118 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001119 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001120 png_warning(png_ptr, "Invalid number of transparent colors specified");
Guy Schalnate5a37791996-06-05 15:50:50 -05001121 return;
1122 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001123 /* write the chunk out as it is */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001124 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans,
1125 (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001126 }
1127 else if (color_type == PNG_COLOR_TYPE_GRAY)
1128 {
1129 /* one 16 bit value */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001130 if (tran->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001131 {
1132 png_warning(png_ptr,
1133 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1134 return;
1135 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001136 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001137 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001138 }
1139 else if (color_type == PNG_COLOR_TYPE_RGB)
1140 {
1141 /* three 16 bit values */
1142 png_save_uint_16(buf, tran->red);
1143 png_save_uint_16(buf + 2, tran->green);
1144 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001145 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001146 {
1147 png_warning(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001148 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001149 return;
1150 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001151 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001152 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001153 else
1154 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001155 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001156 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001157}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001158#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001159
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001160#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001161/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001162void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001163png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001164{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001165#ifdef PNG_USE_LOCAL_ARRAYS
1166 PNG_bKGD;
1167#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001168 png_byte buf[6];
1169
Andreas Dilger47a0c421997-05-16 02:46:07 -05001170 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001171 if (color_type == PNG_COLOR_TYPE_PALETTE)
1172 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001173 if (
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001174#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001175 (png_ptr->num_palette ||
1176 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001177#endif
1178 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001179 {
1180 png_warning(png_ptr, "Invalid background palette index");
1181 return;
1182 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001183 buf[0] = back->index;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001184 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001185 }
1186 else if (color_type & PNG_COLOR_MASK_COLOR)
1187 {
1188 png_save_uint_16(buf, back->red);
1189 png_save_uint_16(buf + 2, back->green);
1190 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001191 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001192 {
1193 png_warning(png_ptr,
1194 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1195 return;
1196 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001197 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001198 }
1199 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001200 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001201 if (back->gray >= (1 << png_ptr->bit_depth))
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001202 {
1203 png_warning(png_ptr,
1204 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1205 return;
1206 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001207 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001208 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001209 }
1210}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001211#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001212
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001213#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001214/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001215void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001216png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001217{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001218#ifdef PNG_USE_LOCAL_ARRAYS
1219 PNG_hIST;
1220#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001221 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001222 png_byte buf[3];
1223
Andreas Dilger47a0c421997-05-16 02:46:07 -05001224 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001225 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001226 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001227 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1228 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001229 png_warning(png_ptr, "Invalid number of histogram entries specified");
1230 return;
1231 }
1232
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001233 png_write_chunk_start(png_ptr, (png_bytep)png_hIST,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001234 (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001235 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001236 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001237 png_save_uint_16(buf, hist[i]);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001238 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001239 }
1240 png_write_chunk_end(png_ptr);
1241}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001242#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001243
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001244#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1245 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001246/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1247 * and if invalid, correct the keyword rather than discarding the entire
1248 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1249 * length, forbids leading or trailing whitespace, multiple internal spaces,
1250 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001251 *
1252 * The new_key is allocated to hold the corrected keyword and must be freed
1253 * by the calling routine. This avoids problems with trying to write to
1254 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001255 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001256png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001257png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001258{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001259 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001260 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001261 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001262 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001263
Andreas Dilger47a0c421997-05-16 02:46:07 -05001264 png_debug(1, "in png_check_keyword\n");
1265 *new_key = NULL;
1266
1267 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001268 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001269 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001270 return ((png_size_t)0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001271 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001272
Andreas Dilger47a0c421997-05-16 02:46:07 -05001273 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1274
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001275 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001276 if (*new_key == NULL)
1277 {
1278 png_warning(png_ptr, "Out of memory while procesing keyword");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001279 return ((png_size_t)0);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001280 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001281
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001282 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001283 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001284 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001285 if ((png_byte)*kp < 0x20 ||
1286 ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001287 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001288#if !defined(PNG_NO_STDIO)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001289 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001290
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001291 png_snprintf(msg, 40,
1292 "invalid keyword character 0x%02X", (png_byte)*kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001293 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001294#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001295 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001296#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001297 *dp = ' ';
1298 }
1299 else
1300 {
1301 *dp = *kp;
1302 }
1303 }
1304 *dp = '\0';
1305
1306 /* Remove any trailing white space. */
1307 kp = *new_key + key_len - 1;
1308 if (*kp == ' ')
1309 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001310 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001311
1312 while (*kp == ' ')
1313 {
1314 *(kp--) = '\0';
1315 key_len--;
1316 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001317 }
1318
1319 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001320 kp = *new_key;
1321 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001322 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001323 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001324
1325 while (*kp == ' ')
1326 {
1327 kp++;
1328 key_len--;
1329 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001330 }
1331
Andreas Dilger47a0c421997-05-16 02:46:07 -05001332 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001333
Andreas Dilger47a0c421997-05-16 02:46:07 -05001334 /* Remove multiple internal spaces. */
1335 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001336 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001337 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001338 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001339 *(dp++) = *kp;
1340 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001341 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001342 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001343 {
1344 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001345 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001346 }
1347 else
1348 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001349 *(dp++) = *kp;
1350 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001351 }
1352 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001353 *dp = '\0';
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001354 if (kwarn)
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001355 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001356
1357 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001358 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001359 png_free(png_ptr, *new_key);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001360 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001361 }
1362
1363 if (key_len > 79)
1364 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001365 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001366 new_key[79] = '\0';
Andreas Dilger47a0c421997-05-16 02:46:07 -05001367 key_len = 79;
1368 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001369
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001370 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001371}
1372#endif
1373
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001374#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001375/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001376void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001377png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001378 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001379{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001380#ifdef PNG_USE_LOCAL_ARRAYS
1381 PNG_tEXt;
1382#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001383 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001384 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001385
Andreas Dilger47a0c421997-05-16 02:46:07 -05001386 png_debug(1, "in png_write_tEXt\n");
1387 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1388 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001389 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001390 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001391 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001392
Andreas Dilger47a0c421997-05-16 02:46:07 -05001393 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001394 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001395 else
1396 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001397
1398 /* make sure we include the 0 after the key */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001399 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001400 (png_uint_32)(key_len + text_len + 1));
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001401 /*
1402 * We leave it to the application to meet PNG-1.0 requirements on the
1403 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1404 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001405 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001406 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001407 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1408 (png_size_t)(key_len + 1));
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001409 if (text_len)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001410 png_write_chunk_data(png_ptr, (png_bytep)text, (png_size_t)text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001411
Guy Schalnat0d580581995-07-20 02:43:20 -05001412 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001413 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001414}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001415#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001416
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001417#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001418/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001419void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001420png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001421 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001422{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001423#ifdef PNG_USE_LOCAL_ARRAYS
1424 PNG_zTXt;
1425#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001426 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001427 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001428 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001429 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001430
Andreas Dilger47a0c421997-05-16 02:46:07 -05001431 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001432
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001433 comp.num_output_ptr = 0;
1434 comp.max_output_ptr = 0;
1435 comp.output_ptr = NULL;
1436 comp.input = NULL;
1437 comp.input_len = 0;
1438
Andreas Dilger47a0c421997-05-16 02:46:07 -05001439 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001440 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001441 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001442 png_free(png_ptr, new_key);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001443 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001444 }
1445
Andreas Dilger47a0c421997-05-16 02:46:07 -05001446 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1447 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001448 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001449 png_free(png_ptr, new_key);
1450 return;
1451 }
1452
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001453 text_len = png_strlen(text);
1454
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001455 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001456 text_len = png_text_compress(png_ptr, text, text_len, compression,
1457 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001458
1459 /* write start of chunk */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001460 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt,
1461 (png_uint_32)(key_len+text_len + 2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001462 /* write key */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001463 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1464 (png_size_t)(key_len + 1));
1465 png_free(png_ptr, new_key);
1466
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001467 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001468 /* write compression */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001469 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001470 /* write the compressed data */
1471 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001472
Guy Schalnat0d580581995-07-20 02:43:20 -05001473 /* close the chunk */
1474 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001475}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001476#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001477
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001478#if defined(PNG_WRITE_iTXt_SUPPORTED)
1479/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001480void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001481png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001482 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001483{
1484#ifdef PNG_USE_LOCAL_ARRAYS
1485 PNG_iTXt;
1486#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001487 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001488 png_charp new_lang, new_key;
1489 png_byte cbuf[2];
1490 compression_state comp;
1491
1492 png_debug(1, "in png_write_iTXt\n");
1493
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001494 comp.num_output_ptr = 0;
1495 comp.max_output_ptr = 0;
1496 comp.output_ptr = NULL;
1497 comp.input = NULL;
1498
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001499 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1500 {
1501 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1502 return;
1503 }
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001504 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001505 {
1506 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001507 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001508 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001509 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001510
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001511 if (lang_key == NULL)
1512 lang_key_len = 0;
1513 else
1514 lang_key_len = png_strlen(lang_key);
1515
1516 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001517 text_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001518 else
1519 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001520
1521 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001522 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1523 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001524
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001525
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001526 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001527 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001528
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001529 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1530 (png_uint_32)(
1531 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1532 + key_len
1533 + lang_len
1534 + lang_key_len
1535 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001536
1537 /*
1538 * We leave it to the application to meet PNG-1.0 requirements on the
1539 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1540 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1541 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1542 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001543 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1544 (png_size_t)(key_len + 1));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001545
1546 /* set the compression flag */
1547 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1548 compression == PNG_TEXT_COMPRESSION_NONE)
1549 cbuf[0] = 0;
1550 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1551 cbuf[0] = 1;
1552 /* set the compression method */
1553 cbuf[1] = 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001554 png_write_chunk_data(png_ptr, cbuf, (png_size_t)2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001555
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001556 cbuf[0] = 0;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001557 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf),
1558 (png_size_t)(lang_len + 1));
1559 png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf),
1560 (png_size_t)(lang_key_len + 1));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001561 png_write_compressed_data_out(png_ptr, &comp);
1562
1563 png_write_chunk_end(png_ptr);
1564 png_free(png_ptr, new_key);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001565 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001566}
1567#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001568
1569#if defined(PNG_WRITE_oFFs_SUPPORTED)
1570/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001571void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001572png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001573 int unit_type)
1574{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001575#ifdef PNG_USE_LOCAL_ARRAYS
1576 PNG_oFFs;
1577#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001578 png_byte buf[9];
1579
1580 png_debug(1, "in png_write_oFFs\n");
1581 if (unit_type >= PNG_OFFSET_LAST)
1582 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1583
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001584 png_save_int_32(buf, x_offset);
1585 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001586 buf[8] = (png_byte)unit_type;
1587
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001588 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001589}
1590#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001591#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001592/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001593void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001594png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1595 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1596{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001597#ifdef PNG_USE_LOCAL_ARRAYS
1598 PNG_pCAL;
1599#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001600 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001601 png_uint_32p params_len;
1602 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001603 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001604 int i;
1605
1606 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1607 if (type >= PNG_EQUATION_LAST)
1608 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1609
1610 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001611 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001612 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001613 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001614 total_len = purpose_len + units_len + 10;
1615
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001616 params_len = (png_uint_32p)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001617 (png_size_t)(nparams * png_sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001618
1619 /* Find the length of each parameter, making sure we don't count the
1620 null terminator for the last parameter. */
1621 for (i = 0; i < nparams; i++)
1622 {
1623 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001624 png_debug2(3, "pCAL parameter %d length = %lu\n", i,
1625 (unsigned long) params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001626 total_len += (png_size_t)params_len[i];
1627 }
1628
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001629 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001630 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001631 png_write_chunk_data(png_ptr, (png_bytep)new_purpose,
1632 (png_size_t)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001633 png_save_int_32(buf, X0);
1634 png_save_int_32(buf + 4, X1);
1635 buf[8] = (png_byte)type;
1636 buf[9] = (png_byte)nparams;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001637 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1638 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001639
1640 png_free(png_ptr, new_purpose);
1641
1642 for (i = 0; i < nparams; i++)
1643 {
1644 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1645 (png_size_t)params_len[i]);
1646 }
1647
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001648 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001649 png_write_chunk_end(png_ptr);
1650}
1651#endif
1652
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001653#if defined(PNG_WRITE_sCAL_SUPPORTED)
1654/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001655#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001656void /* PRIVATE */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001657png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001658{
1659#ifdef PNG_USE_LOCAL_ARRAYS
1660 PNG_sCAL;
1661#endif
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001662 char buf[64];
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001663 png_size_t total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001664
1665 png_debug(1, "in png_write_sCAL\n");
1666
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001667 buf[0] = (char)unit;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001668 png_snprintf(buf + 1, 63, "%12.12e", width);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001669 total_len = 1 + png_strlen(buf + 1) + 1;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001670 png_snprintf(buf + total_len, 64-total_len, "%12.12e", height);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001671 total_len += png_strlen(buf + total_len);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001672
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001673 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
1674 png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001675}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001676#else
1677#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001678void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001679png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001680 png_charp height)
1681{
1682#ifdef PNG_USE_LOCAL_ARRAYS
1683 PNG_sCAL;
1684#endif
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001685 png_byte buf[64];
1686 png_size_t wlen, hlen, total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001687
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001688 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001689
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001690 wlen = png_strlen(width);
1691 hlen = png_strlen(height);
1692 total_len = wlen + hlen + 2;
1693 if (total_len > 64)
1694 {
1695 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
1696 return;
1697 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001698
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001699 buf[0] = (png_byte)unit;
1700 png_memcpy(buf + 1, width, wlen + 1); /* append the '\0' here */
1701 png_memcpy(buf + wlen + 2, height, hlen); /* do NOT append the '\0' here */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001702
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001703 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
1704 png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001705}
1706#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001707#endif
1708#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001709
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001710#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001711/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001712void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001713png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001714 png_uint_32 y_pixels_per_unit,
1715 int unit_type)
1716{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001717#ifdef PNG_USE_LOCAL_ARRAYS
1718 PNG_pHYs;
1719#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001720 png_byte buf[9];
1721
Andreas Dilger47a0c421997-05-16 02:46:07 -05001722 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001723 if (unit_type >= PNG_RESOLUTION_LAST)
1724 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1725
Guy Schalnat0d580581995-07-20 02:43:20 -05001726 png_save_uint_32(buf, x_pixels_per_unit);
1727 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001728 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001729
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001730 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001731}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001732#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001733
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001734#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001735/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1736 * or png_convert_from_time_t(), or fill in the structure yourself.
1737 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001738void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001739png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001740{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001741#ifdef PNG_USE_LOCAL_ARRAYS
1742 PNG_tIME;
1743#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001744 png_byte buf[7];
1745
Andreas Dilger47a0c421997-05-16 02:46:07 -05001746 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001747 if (mod_time->month > 12 || mod_time->month < 1 ||
1748 mod_time->day > 31 || mod_time->day < 1 ||
1749 mod_time->hour > 23 || mod_time->second > 60)
1750 {
1751 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1752 return;
1753 }
1754
Guy Schalnat0d580581995-07-20 02:43:20 -05001755 png_save_uint_16(buf, mod_time->year);
1756 buf[2] = mod_time->month;
1757 buf[3] = mod_time->day;
1758 buf[4] = mod_time->hour;
1759 buf[5] = mod_time->minute;
1760 buf[6] = mod_time->second;
1761
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001762 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001763}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001764#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001765
1766/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001767void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001768png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001769{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001770#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001771#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001772 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001773
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001774 /* start of interlace block */
1775 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001776
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001777 /* offset to next interlace block */
1778 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001779
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001780 /* start of interlace block in the y direction */
1781 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001782
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001783 /* offset to next interlace block in the y direction */
1784 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001785#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001786#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001787
Andreas Dilger47a0c421997-05-16 02:46:07 -05001788 png_size_t buf_size;
1789
1790 png_debug(1, "in png_write_start_row\n");
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001791 buf_size = (png_size_t)(PNG_ROWBYTES(
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001792 png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001793
Guy Schalnat0d580581995-07-20 02:43:20 -05001794 /* set up row buffer */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001795 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr,
1796 (png_size_t)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001797 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001798
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001799#ifndef PNG_NO_WRITE_FILTER
Guy Schalnate5a37791996-06-05 15:50:50 -05001800 /* set up filtering buffer, if using this filter */
1801 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001802 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001803 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001804 (png_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001805 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001806 }
1807
Andreas Dilger47a0c421997-05-16 02:46:07 -05001808 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001809 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1810 {
1811 /* set up previous row buffer */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001812 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr,
1813 (png_size_t)buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001814 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001815
1816 if (png_ptr->do_filter & PNG_FILTER_UP)
1817 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001818 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001819 (png_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001820 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001821 }
1822
1823 if (png_ptr->do_filter & PNG_FILTER_AVG)
1824 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001825 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001826 (png_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001827 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001828 }
1829
1830 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1831 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001832 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001833 (png_size_t)(png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001834 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001835 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001836 }
Glenn Randers-Pehrsoneb580912008-07-30 14:47:09 -05001837#endif /* PNG_NO_WRITE_FILTER */
Guy Schalnat0d580581995-07-20 02:43:20 -05001838
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001839#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001840 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001841 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001842 {
1843 if (!(png_ptr->transformations & PNG_INTERLACE))
1844 {
1845 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1846 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001847 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1848 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001849 }
1850 else
1851 {
1852 png_ptr->num_rows = png_ptr->height;
1853 png_ptr->usr_width = png_ptr->width;
1854 }
1855 }
1856 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001857#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001858 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001859 png_ptr->num_rows = png_ptr->height;
1860 png_ptr->usr_width = png_ptr->width;
1861 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001862 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1863 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001864}
1865
Andreas Dilger47a0c421997-05-16 02:46:07 -05001866/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001867void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001868png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001869{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001870#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001871#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001872 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001873
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001874 /* start of interlace block */
1875 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001876
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001877 /* offset to next interlace block */
1878 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001879
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001880 /* start of interlace block in the y direction */
1881 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001882
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001883 /* offset to next interlace block in the y direction */
1884 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001885#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001886#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001887
Guy Schalnat0d580581995-07-20 02:43:20 -05001888 int ret;
1889
Andreas Dilger47a0c421997-05-16 02:46:07 -05001890 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001891 /* next row */
1892 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001893
Guy Schalnat0d580581995-07-20 02:43:20 -05001894 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001895 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001896 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001897
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001898#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001899 /* if interlaced, go to next pass */
1900 if (png_ptr->interlaced)
1901 {
1902 png_ptr->row_number = 0;
1903 if (png_ptr->transformations & PNG_INTERLACE)
1904 {
1905 png_ptr->pass++;
1906 }
1907 else
1908 {
1909 /* loop until we find a non-zero width or height pass */
1910 do
1911 {
1912 png_ptr->pass++;
1913 if (png_ptr->pass >= 7)
1914 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001915 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001916 png_pass_inc[png_ptr->pass] - 1 -
1917 png_pass_start[png_ptr->pass]) /
1918 png_pass_inc[png_ptr->pass];
1919 png_ptr->num_rows = (png_ptr->height +
1920 png_pass_yinc[png_ptr->pass] - 1 -
1921 png_pass_ystart[png_ptr->pass]) /
1922 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001923 if (png_ptr->transformations & PNG_INTERLACE)
1924 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001925 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1926
1927 }
1928
Guy Schalnate5a37791996-06-05 15:50:50 -05001929 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001930 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001931 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001932 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001933 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001934 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001935 png_ptr->usr_bit_depth, png_ptr->width)) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001936 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001937 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001938 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001939#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001940
1941 /* if we get here, we've just written the last row, so we need
1942 to flush the compressor */
1943 do
1944 {
1945 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001946 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001947 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001948 if (ret == Z_OK)
1949 {
1950 /* check to see if we need more room */
1951 if (!(png_ptr->zstream.avail_out))
1952 {
1953 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1954 png_ptr->zstream.next_out = png_ptr->zbuf;
1955 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1956 }
1957 }
1958 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001959 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001960 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001961 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001962 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001963 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001964 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001965 } while (ret != Z_STREAM_END);
1966
1967 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001968 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001969 {
1970 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001971 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001972 }
1973
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001974 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -06001975 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat0d580581995-07-20 02:43:20 -05001976}
1977
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001978#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001979/* Pick out the correct pixels for the interlace pass.
1980 * The basic idea here is to go through the row with a source
1981 * pointer and a destination pointer (sp and dp), and copy the
1982 * correct pixels for the pass. As the row gets compacted,
1983 * sp will always be >= dp, so we should never overwrite anything.
1984 * See the default: case for the easiest code to understand.
1985 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001986void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001987png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001988{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001989#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001990 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001991
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001992 /* start of interlace block */
1993 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001994
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001995 /* offset to next interlace block */
1996 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001997#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001998
Andreas Dilger47a0c421997-05-16 02:46:07 -05001999 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05002000 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002001 if (pass < 6)
Guy Schalnat0d580581995-07-20 02:43:20 -05002002 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002003 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05002004 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002005 {
Guy Schalnat0d580581995-07-20 02:43:20 -05002006 case 1:
2007 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002008 png_bytep sp;
2009 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05002010 int shift;
2011 int d;
2012 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002013 png_uint_32 i;
2014 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002015
2016 dp = row;
2017 d = 0;
2018 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002019 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002020 i += png_pass_inc[pass])
2021 {
2022 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002023 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05002024 d |= (value << shift);
2025
2026 if (shift == 0)
2027 {
2028 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002029 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002030 d = 0;
2031 }
2032 else
2033 shift--;
2034
2035 }
2036 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002037 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002038 break;
2039 }
2040 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002041 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002042 png_bytep sp;
2043 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05002044 int shift;
2045 int d;
2046 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002047 png_uint_32 i;
2048 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002049
2050 dp = row;
2051 shift = 6;
2052 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002053 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002054 i += png_pass_inc[pass])
2055 {
2056 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002057 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05002058 d |= (value << shift);
2059
2060 if (shift == 0)
2061 {
2062 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002063 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002064 d = 0;
2065 }
2066 else
2067 shift -= 2;
2068 }
2069 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002070 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002071 break;
2072 }
2073 case 4:
2074 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002075 png_bytep sp;
2076 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002077 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05002078 int d;
2079 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002080 png_uint_32 i;
2081 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002082
2083 dp = row;
2084 shift = 4;
2085 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002086 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002087 i += png_pass_inc[pass])
2088 {
2089 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002090 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05002091 d |= (value << shift);
2092
2093 if (shift == 0)
2094 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002095 shift = 4;
2096 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002097 d = 0;
2098 }
2099 else
2100 shift -= 4;
2101 }
2102 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002103 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002104 break;
2105 }
2106 default:
2107 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002108 png_bytep sp;
2109 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002110 png_uint_32 i;
2111 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002112 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002113
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002114 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002115 dp = row;
2116 /* find out how many bytes each pixel takes up */
2117 pixel_bytes = (row_info->pixel_depth >> 3);
2118 /* loop through the row, only looking at the pixels that
2119 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002120 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002121 i += png_pass_inc[pass])
2122 {
2123 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002124 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002125 /* move the pixel */
2126 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002127 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05002128 /* next pixel */
2129 dp += pixel_bytes;
2130 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002131 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002132 }
2133 }
2134 /* set new row width */
2135 row_info->width = (row_info->width +
2136 png_pass_inc[pass] - 1 -
2137 png_pass_start[pass]) /
2138 png_pass_inc[pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002139 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2140 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002141 }
2142}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002143#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002144
Andreas Dilger47a0c421997-05-16 02:46:07 -05002145/* This filters the row, chooses which filter to use, if it has not already
2146 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002147 * chosen filter.
2148 */
Glenn Randers-Pehrson78067772004-11-02 21:49:39 -06002149#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002150#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002151#define PNG_LOMASK ((png_uint_32)0xffffL)
2152#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002153void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002154png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002155{
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002156 png_bytep best_row;
2157#ifndef PNG_NO_WRITE_FILTER
2158 png_bytep prev_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002159 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002160 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002161 png_uint_32 row_bytes = row_info->rowbytes;
2162#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2163 int num_p_filters = (int)png_ptr->num_prev_filters;
2164#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002165
Andreas Dilger47a0c421997-05-16 02:46:07 -05002166 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05002167 /* find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002168 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002169
2170 prev_row = png_ptr->prev_row;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002171#endif
2172 best_row = png_ptr->row_buf;
2173#ifndef PNG_NO_WRITE_FILTER
2174 row_buf = best_row;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002175 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002176
Andreas Dilger47a0c421997-05-16 02:46:07 -05002177 /* The prediction method we use is to find which method provides the
2178 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002179 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002180 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002181 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002182 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002183 * predictive" method (not implemented yet), which does test compressions
2184 * of lines using different filter methods, and then chooses the
2185 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002186 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002187 *
2188 * GRR 980525: consider also
2189 * (1) minimum sum of absolute differences from running average (i.e.,
2190 * keep running sum of non-absolute differences & count of bytes)
2191 * [track dispersion, too? restart average if dispersion too large?]
2192 * (1b) minimum sum of absolute differences from sliding average, probably
2193 * with window size <= deflate window (usually 32K)
2194 * (2) minimum sum of squared differences from zero or running average
2195 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002196 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002197
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002198
Guy Schalnate5a37791996-06-05 15:50:50 -05002199 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002200 * that has been chosen, as it doesn't actually do anything to the data.
2201 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002202 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002203 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002204 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002205 png_bytep rp;
2206 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002207 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002208 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002209
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002210 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002211 {
2212 v = *rp;
2213 sum += (v < 128) ? v : 256 - v;
2214 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002215
2216#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2217 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2218 {
2219 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002220 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002221 sumlo = sum & PNG_LOMASK;
2222 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2223
2224 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002225 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002226 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002227 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002228 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002229 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002230 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002231 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002232 PNG_WEIGHT_SHIFT;
2233 }
2234 }
2235
2236 /* Factor in the cost of this filter (this is here for completeness,
2237 * but it makes no sense to have a "cost" for the NONE filter, as
2238 * it has the minimum possible computational cost - none).
2239 */
2240 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2241 PNG_COST_SHIFT;
2242 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2243 PNG_COST_SHIFT;
2244
2245 if (sumhi > PNG_HIMASK)
2246 sum = PNG_MAXSUM;
2247 else
2248 sum = (sumhi << PNG_HISHIFT) + sumlo;
2249 }
2250#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002251 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002252 }
2253
Guy Schalnate5a37791996-06-05 15:50:50 -05002254 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002255 if (filter_to_do == PNG_FILTER_SUB)
2256 /* it's the only filter so no testing is needed */
2257 {
2258 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002259 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002260 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2261 i++, rp++, dp++)
2262 {
2263 *dp = *rp;
2264 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002265 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002266 i++, rp++, lp++, dp++)
2267 {
2268 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2269 }
2270 best_row = png_ptr->sub_row;
2271 }
2272
2273 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002274 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002275 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002276 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002277 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002278 int v;
2279
2280#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002281 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002282 * would reduce the sum of this filter, so that we can do the
2283 * early exit comparison without scaling the sum each time.
2284 */
2285 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2286 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002287 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002288 png_uint_32 lmhi, lmlo;
2289 lmlo = lmins & PNG_LOMASK;
2290 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2291
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002292 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002293 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002294 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002295 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002296 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002297 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002298 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002299 PNG_WEIGHT_SHIFT;
2300 }
2301 }
2302
2303 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2304 PNG_COST_SHIFT;
2305 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2306 PNG_COST_SHIFT;
2307
2308 if (lmhi > PNG_HIMASK)
2309 lmins = PNG_MAXSUM;
2310 else
2311 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2312 }
2313#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002314
Guy Schalnate5a37791996-06-05 15:50:50 -05002315 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2316 i++, rp++, dp++)
2317 {
2318 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002319
Guy Schalnate5a37791996-06-05 15:50:50 -05002320 sum += (v < 128) ? v : 256 - v;
2321 }
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002322 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002323 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002324 {
2325 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002326
Guy Schalnate5a37791996-06-05 15:50:50 -05002327 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002328
2329 if (sum > lmins) /* We are already worse, don't continue. */
2330 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002331 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002332
2333#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2334 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2335 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002336 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002337 png_uint_32 sumhi, sumlo;
2338 sumlo = sum & PNG_LOMASK;
2339 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2340
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002341 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002342 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002343 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002344 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002345 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002346 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002347 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002348 PNG_WEIGHT_SHIFT;
2349 }
2350 }
2351
2352 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2353 PNG_COST_SHIFT;
2354 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2355 PNG_COST_SHIFT;
2356
2357 if (sumhi > PNG_HIMASK)
2358 sum = PNG_MAXSUM;
2359 else
2360 sum = (sumhi << PNG_HISHIFT) + sumlo;
2361 }
2362#endif
2363
Guy Schalnate5a37791996-06-05 15:50:50 -05002364 if (sum < mins)
2365 {
2366 mins = sum;
2367 best_row = png_ptr->sub_row;
2368 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002369 }
2370
Guy Schalnate5a37791996-06-05 15:50:50 -05002371 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002372 if (filter_to_do == PNG_FILTER_UP)
2373 {
2374 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002375 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002376
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002377 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002378 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002379 i++, rp++, pp++, dp++)
2380 {
2381 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2382 }
2383 best_row = png_ptr->up_row;
2384 }
2385
2386 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002387 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002388 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002389 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002390 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002391 int v;
2392
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002393
Andreas Dilger47a0c421997-05-16 02:46:07 -05002394#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2395 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2396 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002397 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002398 png_uint_32 lmhi, lmlo;
2399 lmlo = lmins & PNG_LOMASK;
2400 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2401
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002402 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002403 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002404 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002405 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002406 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002407 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002408 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002409 PNG_WEIGHT_SHIFT;
2410 }
2411 }
2412
2413 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2414 PNG_COST_SHIFT;
2415 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2416 PNG_COST_SHIFT;
2417
2418 if (lmhi > PNG_HIMASK)
2419 lmins = PNG_MAXSUM;
2420 else
2421 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2422 }
2423#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002424
2425 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002426 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002427 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002428 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002429
2430 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002431
2432 if (sum > lmins) /* We are already worse, don't continue. */
2433 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002434 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002435
2436#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2437 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2438 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002439 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002440 png_uint_32 sumhi, sumlo;
2441 sumlo = sum & PNG_LOMASK;
2442 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2443
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002444 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002445 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002446 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002447 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002448 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002449 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002450 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002451 PNG_WEIGHT_SHIFT;
2452 }
2453 }
2454
2455 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2456 PNG_COST_SHIFT;
2457 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2458 PNG_COST_SHIFT;
2459
2460 if (sumhi > PNG_HIMASK)
2461 sum = PNG_MAXSUM;
2462 else
2463 sum = (sumhi << PNG_HISHIFT) + sumlo;
2464 }
2465#endif
2466
Guy Schalnate5a37791996-06-05 15:50:50 -05002467 if (sum < mins)
2468 {
2469 mins = sum;
2470 best_row = png_ptr->up_row;
2471 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002472 }
2473
Guy Schalnate5a37791996-06-05 15:50:50 -05002474 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002475 if (filter_to_do == PNG_FILTER_AVG)
2476 {
2477 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002478 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002479 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002480 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002481 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002482 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002483 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002484 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002485 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002486 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2487 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002488 }
2489 best_row = png_ptr->avg_row;
2490 }
2491
2492 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002493 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002494 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002495 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002496 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002497 int v;
2498
2499#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2500 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2501 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002502 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002503 png_uint_32 lmhi, lmlo;
2504 lmlo = lmins & PNG_LOMASK;
2505 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2506
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002507 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002508 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002509 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002510 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002511 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002512 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002513 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002514 PNG_WEIGHT_SHIFT;
2515 }
2516 }
2517
2518 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2519 PNG_COST_SHIFT;
2520 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2521 PNG_COST_SHIFT;
2522
2523 if (lmhi > PNG_HIMASK)
2524 lmins = PNG_MAXSUM;
2525 else
2526 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2527 }
2528#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002529
2530 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002531 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002532 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002533 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002534
2535 sum += (v < 128) ? v : 256 - v;
2536 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002537 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002538 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002539 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002540 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002541
2542 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002543
2544 if (sum > lmins) /* We are already worse, don't continue. */
2545 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002546 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002547
2548#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2549 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2550 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002551 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002552 png_uint_32 sumhi, sumlo;
2553 sumlo = sum & PNG_LOMASK;
2554 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2555
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002556 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002557 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002558 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002559 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002560 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002561 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002562 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002563 PNG_WEIGHT_SHIFT;
2564 }
2565 }
2566
2567 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2568 PNG_COST_SHIFT;
2569 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2570 PNG_COST_SHIFT;
2571
2572 if (sumhi > PNG_HIMASK)
2573 sum = PNG_MAXSUM;
2574 else
2575 sum = (sumhi << PNG_HISHIFT) + sumlo;
2576 }
2577#endif
2578
Guy Schalnate5a37791996-06-05 15:50:50 -05002579 if (sum < mins)
2580 {
2581 mins = sum;
2582 best_row = png_ptr->avg_row;
2583 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002584 }
2585
Andreas Dilger47a0c421997-05-16 02:46:07 -05002586 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002587 if (filter_to_do == PNG_FILTER_PAETH)
2588 {
2589 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002590 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002591 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002592 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002593 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002594 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002595 }
2596
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002597 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002598 {
2599 int a, b, c, pa, pb, pc, p;
2600
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002601 b = *pp++;
2602 c = *cp++;
2603 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002604
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002605 p = b - c;
2606 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002607
2608#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002609 pa = abs(p);
2610 pb = abs(pc);
2611 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002612#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002613 pa = p < 0 ? -p : p;
2614 pb = pc < 0 ? -pc : pc;
2615 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002616#endif
2617
2618 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2619
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002620 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002621 }
2622 best_row = png_ptr->paeth_row;
2623 }
2624
2625 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002626 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002627 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002628 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002629 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002630 int v;
2631
2632#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2633 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2634 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002635 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002636 png_uint_32 lmhi, lmlo;
2637 lmlo = lmins & PNG_LOMASK;
2638 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2639
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002640 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002641 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002642 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002643 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002644 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002645 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002646 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002647 PNG_WEIGHT_SHIFT;
2648 }
2649 }
2650
2651 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2652 PNG_COST_SHIFT;
2653 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2654 PNG_COST_SHIFT;
2655
2656 if (lmhi > PNG_HIMASK)
2657 lmins = PNG_MAXSUM;
2658 else
2659 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2660 }
2661#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002662
2663 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002664 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002665 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002666 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002667
2668 sum += (v < 128) ? v : 256 - v;
2669 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002670
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002671 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002672 {
2673 int a, b, c, pa, pb, pc, p;
2674
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002675 b = *pp++;
2676 c = *cp++;
2677 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002678
2679#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002680 p = b - c;
2681 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002682#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002683 pa = abs(p);
2684 pb = abs(pc);
2685 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002686#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002687 pa = p < 0 ? -p : p;
2688 pb = pc < 0 ? -pc : pc;
2689 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002690#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002691 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2692#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002693 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002694 pa = abs(p - a);
2695 pb = abs(p - b);
2696 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002697 if (pa <= pb && pa <= pc)
2698 p = a;
2699 else if (pb <= pc)
2700 p = b;
2701 else
2702 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002703#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002704
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002705 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002706
2707 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002708
2709 if (sum > lmins) /* We are already worse, don't continue. */
2710 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002711 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002712
2713#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2714 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2715 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002716 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002717 png_uint_32 sumhi, sumlo;
2718 sumlo = sum & PNG_LOMASK;
2719 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2720
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002721 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002722 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002723 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002724 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002725 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002726 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002727 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002728 PNG_WEIGHT_SHIFT;
2729 }
2730 }
2731
2732 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2733 PNG_COST_SHIFT;
2734 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2735 PNG_COST_SHIFT;
2736
2737 if (sumhi > PNG_HIMASK)
2738 sum = PNG_MAXSUM;
2739 else
2740 sum = (sumhi << PNG_HISHIFT) + sumlo;
2741 }
2742#endif
2743
Guy Schalnate5a37791996-06-05 15:50:50 -05002744 if (sum < mins)
2745 {
2746 best_row = png_ptr->paeth_row;
2747 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002748 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002749#endif /* PNG_NO_WRITE_FILTER */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002750 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002751
Guy Schalnate5a37791996-06-05 15:50:50 -05002752 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002753
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002754#ifndef PNG_NO_WRITE_FILTER
Andreas Dilger47a0c421997-05-16 02:46:07 -05002755#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2756 /* Save the type of filter we picked this time for future calculations */
2757 if (png_ptr->num_prev_filters > 0)
2758 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002759 int j;
2760 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002761 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002762 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002763 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002764 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002765 }
2766#endif
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002767#endif /* PNG_NO_WRITE_FILTER */
Guy Schalnate5a37791996-06-05 15:50:50 -05002768}
2769
2770
Andreas Dilger47a0c421997-05-16 02:46:07 -05002771/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002772void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002773png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2774{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002775 png_debug(1, "in png_write_filtered_row\n");
2776 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002777 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002778
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002779 png_ptr->zstream.next_in = filtered_row;
2780 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002781 /* repeat until we have compressed all the data */
2782 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002783 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002784 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002785
Guy Schalnate5a37791996-06-05 15:50:50 -05002786 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002787 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002788 /* check for compression errors */
2789 if (ret != Z_OK)
2790 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002791 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002792 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002793 else
2794 png_error(png_ptr, "zlib error");
2795 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002796
Guy Schalnate5a37791996-06-05 15:50:50 -05002797 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002798 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002799 {
2800 /* write the IDAT and reset the zlib output buffer */
2801 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002802 png_ptr->zstream.next_out = png_ptr->zbuf;
2803 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002804 }
2805 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002806 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002807
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002808 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002809 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002810 {
2811 png_bytep tptr;
2812
2813 tptr = png_ptr->prev_row;
2814 png_ptr->prev_row = png_ptr->row_buf;
2815 png_ptr->row_buf = tptr;
2816 }
2817
Guy Schalnate5a37791996-06-05 15:50:50 -05002818 /* finish row - updates counters and flushes zlib if last row */
2819 png_write_finish_row(png_ptr);
2820
2821#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2822 png_ptr->flush_rows++;
2823
2824 if (png_ptr->flush_dist > 0 &&
2825 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002826 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002827 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002828 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002829#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002830}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05002831#endif /* PNG_WRITE_SUPPORTED */