blob: 10eac105e1f60471061f275912a81e471ba323e5 [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-Pehrsonbeb572e2006-08-19 13:59:24 -05004 * Last changed in libpng 1.4.0 July 20, 2006
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06006 * Copyright (c) 1998-2006 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-Pehrson6bc53be2006-06-16 07:52:03 -050073 if(png_ptr->sig_bytes < 3)
74 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{
Andreas Dilger47a0c421997-05-16 02:46:07 -050090 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060091 png_write_chunk_data(png_ptr, data, length);
92 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050093}
94
Andreas Dilger47a0c421997-05-16 02:46:07 -050095/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060096 * The total_length is the sum of the lengths of all the data you will be
97 * passing in png_write_chunk_data().
98 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050099void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600100png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
101 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500102{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500103 png_byte buf[8];
104 png_debug2(0, "Writing %s chunk, length = %lu\n", chunk_name,
105 (unsigned long)length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500106
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500107#ifdef PNG_IO_STATE_SUPPORTED
108 /* Inform the I/O callback that the chunk header is being written.
109 * PNG_IO_CHUNK_HDR requires a single I/O call.
110 */
111 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR;
112#endif
113
114 /* write the length and the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500115 png_save_uint_32(buf, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500116 png_memcpy(buf + 4, chunk_name, 4);
117 png_write_data(png_ptr, buf, 8);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500118
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500119 /* put the chunk name into png_ptr->chunk_name */
120 png_memcpy(png_ptr->chunk_name, chunk_name, 4);
121
Guy Schalnat0d580581995-07-20 02:43:20 -0500122 /* reset the crc and run it over the chunk name */
123 png_reset_crc(png_ptr);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500124 png_calculate_crc(png_ptr, chunk_name, 4);
125
126#ifdef PNG_IO_STATE_SUPPORTED
127 /* Inform the I/O callback that chunk data will (possibly) be written.
128 * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.
129 */
130 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA;
131#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500132}
133
Andreas Dilger47a0c421997-05-16 02:46:07 -0500134/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600135 * Note that multiple calls to this function are allowed, and that the
136 * sum of the lengths from these calls *must* add up to the total_length
137 * given to png_write_chunk_start().
138 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500139void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500140png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500141{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500142 /* write the data, and run the CRC over it */
143 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500144 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600145 png_write_data(png_ptr, data, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500146 /* update the CRC after writing the data,
147 * in case that the user I/O routine alters it.
148 */
149 png_calculate_crc(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500150 }
151}
152
Andreas Dilger47a0c421997-05-16 02:46:07 -0500153/* Finish a chunk started with png_write_chunk_start(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500154void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600155png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500156{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500157 png_byte buf[4];
158
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500159#ifdef PNG_IO_STATE_SUPPORTED
160 /* Inform the I/O callback that the chunk CRC is being written.
161 * PNG_IO_CHUNK_CRC requires a single I/O function call.
162 */
163 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC;
164#endif
165
166 /* write the crc in a single operation */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500167 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500168
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500169 png_write_data(png_ptr, buf, 4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500170}
171
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600172#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600173/*
174 * This pair of functions encapsulates the operation of (a) compressing a
175 * text string, and (b) issuing it later as a series of chunk data writes.
176 * The compression_state structure is shared context for these functions
177 * set up by the caller in order to make the whole mess thread-safe.
178 */
179
180typedef struct
181{
182 char *input; /* the uncompressed input data */
183 int input_len; /* its length */
184 int num_output_ptr; /* number of output pointers used */
185 int max_output_ptr; /* size of output_ptr */
186 png_charpp output_ptr; /* array of pointers to output */
187} compression_state;
188
189/* compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500190static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600191png_text_compress(png_structp png_ptr,
192 png_charp text, png_size_t text_len, int compression,
193 compression_state *comp)
194{
195 int ret;
196
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600197 comp->num_output_ptr = 0;
198 comp->max_output_ptr = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600199 comp->output_ptr = NULL;
200 comp->input = NULL;
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600201 comp->input_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600202
203 /* we may just want to pass the text right through */
204 if (compression == PNG_TEXT_COMPRESSION_NONE)
205 {
206 comp->input = text;
207 comp->input_len = text_len;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500208 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600209 }
210
211 if (compression >= PNG_TEXT_COMPRESSION_LAST)
212 {
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -0500213#ifndef PNG_NO_STDIO
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600214 char msg[50];
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -0500215 png_sprintf(msg, "Unknown compression type %d", compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600216 png_warning(png_ptr, msg);
217#else
218 png_warning(png_ptr, "Unknown compression type");
219#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600220 }
221
222 /* We can't write the chunk until we find out how much data we have,
223 * which means we need to run the compressor first and save the
224 * output. This shouldn't be a problem, as the vast majority of
225 * comments should be reasonable, but we will set up an array of
226 * malloc'd pointers to be sure.
227 *
228 * If we knew the application was well behaved, we could simplify this
229 * greatly by assuming we can always malloc an output buffer large
230 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
231 * and malloc this directly. The only time this would be a bad idea is
232 * if we can't malloc more than 64K and we have 64K of random input
233 * data, or if the input string is incredibly large (although this
234 * wouldn't cause a failure, just a slowdown due to swapping).
235 */
236
237 /* set up the compression buffers */
238 png_ptr->zstream.avail_in = (uInt)text_len;
239 png_ptr->zstream.next_in = (Bytef *)text;
240 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
241 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
242
243 /* this is the same compression loop as in png_write_row() */
244 do
245 {
246 /* compress the data */
247 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
248 if (ret != Z_OK)
249 {
250 /* error */
251 if (png_ptr->zstream.msg != NULL)
252 png_error(png_ptr, png_ptr->zstream.msg);
253 else
254 png_error(png_ptr, "zlib error");
255 }
256 /* check to see if we need more room */
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600257 if (!(png_ptr->zstream.avail_out))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600258 {
259 /* make sure the output array has room */
260 if (comp->num_output_ptr >= comp->max_output_ptr)
261 {
262 int old_max;
263
264 old_max = comp->max_output_ptr;
265 comp->max_output_ptr = comp->num_output_ptr + 4;
266 if (comp->output_ptr != NULL)
267 {
268 png_charpp old_ptr;
269
270 old_ptr = comp->output_ptr;
271 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500272 (png_size_t)(comp->max_output_ptr * sizeof(png_charpp)));
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500273 png_memcpy(comp->output_ptr, old_ptr, old_max
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500274 * sizeof(png_charp));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600275 png_free(png_ptr, old_ptr);
276 }
277 else
278 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500279 (png_size_t)(comp->max_output_ptr * sizeof(png_charp)));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600280 }
281
282 /* save the data */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500283 comp->output_ptr[comp->num_output_ptr] =
284 (png_charp)png_malloc(png_ptr, png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500285 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
286 png_ptr->zbuf_size);
287 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600288
289 /* and reset the buffer */
290 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
291 png_ptr->zstream.next_out = png_ptr->zbuf;
292 }
293 /* continue until we don't have any more to compress */
294 } while (png_ptr->zstream.avail_in);
295
296 /* finish the compression */
297 do
298 {
299 /* tell zlib we are finished */
300 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500301
302 if (ret == Z_OK)
303 {
304 /* check to see if we need more room */
305 if (!(png_ptr->zstream.avail_out))
306 {
307 /* check to make sure our output array has room */
308 if (comp->num_output_ptr >= comp->max_output_ptr)
309 {
310 int old_max;
311
312 old_max = comp->max_output_ptr;
313 comp->max_output_ptr = comp->num_output_ptr + 4;
314 if (comp->output_ptr != NULL)
315 {
316 png_charpp old_ptr;
317
318 old_ptr = comp->output_ptr;
319 /* This could be optimized to realloc() */
320 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500321 (png_size_t)(comp->max_output_ptr * sizeof(png_charpp)));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500322 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500323 old_max * sizeof(png_charp));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500324 png_free(png_ptr, old_ptr);
325 }
326 else
327 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500328 (png_size_t)(comp->max_output_ptr * sizeof(png_charp)));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500329 }
330
331 /* save off the data */
332 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500333 (png_charp)png_malloc(png_ptr, png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500334 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
335 png_ptr->zbuf_size);
336 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500337
338 /* and reset the buffer pointers */
339 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
340 png_ptr->zstream.next_out = png_ptr->zbuf;
341 }
342 }
343 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600344 {
345 /* we got an error */
346 if (png_ptr->zstream.msg != NULL)
347 png_error(png_ptr, png_ptr->zstream.msg);
348 else
349 png_error(png_ptr, "zlib error");
350 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600351 } while (ret != Z_STREAM_END);
352
353 /* text length is number of buffers plus last buffer */
354 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
355 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
356 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
357
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500358 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600359}
360
361/* ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500362static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600363png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
364{
365 int i;
366
367 /* handle the no-compression case */
368 if (comp->input)
369 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500370 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
371 (png_size_t)comp->input_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600372 return;
373 }
374
375 /* write saved output buffers, if any */
376 for (i = 0; i < comp->num_output_ptr; i++)
377 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600378 png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
379 png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600380 png_free(png_ptr, comp->output_ptr[i]);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500381 comp->output_ptr[i]=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600382 }
383 if (comp->max_output_ptr != 0)
384 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500385 comp->output_ptr=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600386 /* write anything left in zbuf */
387 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
388 png_write_chunk_data(png_ptr, png_ptr->zbuf,
389 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
390
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600391 /* reset zlib for another zTXt/iTXt or image data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600392 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600393 png_ptr->zstream.data_type = Z_BINARY;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600394}
395#endif
396
Guy Schalnat0d580581995-07-20 02:43:20 -0500397/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600398 * information. Note that the rest of this code depends upon this
399 * information being correct.
400 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500401void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600402png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500403 int bit_depth, int color_type, int compression_type, int filter_type,
404 int interlace_type)
405{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600406#ifdef PNG_USE_LOCAL_ARRAYS
407 PNG_IHDR;
408#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500409 png_byte buf[13]; /* buffer to store the IHDR info */
410
Andreas Dilger47a0c421997-05-16 02:46:07 -0500411 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500412 /* Check that we have valid input data from the application info */
413 switch (color_type)
414 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500415 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500416 switch (bit_depth)
417 {
418 case 1:
419 case 2:
420 case 4:
421 case 8:
422 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500423 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500424 }
425 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500426 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500427 if (bit_depth != 8 && bit_depth != 16)
428 png_error(png_ptr, "Invalid bit depth for RGB image");
429 png_ptr->channels = 3;
430 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500431 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500432 switch (bit_depth)
433 {
434 case 1:
435 case 2:
436 case 4:
437 case 8: png_ptr->channels = 1; break;
438 default: png_error(png_ptr, "Invalid bit depth for paletted image");
439 }
440 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500441 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500442 if (bit_depth != 8 && bit_depth != 16)
443 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
444 png_ptr->channels = 2;
445 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500446 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500447 if (bit_depth != 8 && bit_depth != 16)
448 png_error(png_ptr, "Invalid bit depth for RGBA image");
449 png_ptr->channels = 4;
450 break;
451 default:
452 png_error(png_ptr, "Invalid image color type specified");
453 }
454
Andreas Dilger47a0c421997-05-16 02:46:07 -0500455 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500456 {
457 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500458 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500459 }
460
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600461 /* Write filter_method 64 (intrapixel differencing) only if
462 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
463 * 2. Libpng did not write a PNG signature (this filter_method is only
464 * used in PNG datastreams that are embedded in MNG datastreams) and
465 * 3. The application called png_permit_mng_features with a mask that
466 * included PNG_FLAG_MNG_FILTER_64 and
467 * 4. The filter_method is 64 and
468 * 5. The color_type is RGB or RGBA
469 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600470 if (
471#if defined(PNG_MNG_FEATURES_SUPPORTED)
472 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600473 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500474 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600475 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600476 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
477#endif
478 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500479 {
480 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500481 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500482 }
483
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600484#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500485 if (interlace_type != PNG_INTERLACE_NONE &&
486 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500487 {
488 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500489 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500490 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600491#else
492 interlace_type=PNG_INTERLACE_NONE;
493#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500494
495 /* save off the relevent information */
496 png_ptr->bit_depth = (png_byte)bit_depth;
497 png_ptr->color_type = (png_byte)color_type;
498 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500499#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600500 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500501#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500502 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500503 png_ptr->width = width;
504 png_ptr->height = height;
505
506 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500507 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
Guy Schalnate5a37791996-06-05 15:50:50 -0500508 /* set the usr info, so any transformations can modify it */
509 png_ptr->usr_width = png_ptr->width;
510 png_ptr->usr_bit_depth = png_ptr->bit_depth;
511 png_ptr->usr_channels = png_ptr->channels;
512
Guy Schalnat0d580581995-07-20 02:43:20 -0500513 /* pack the header information into the buffer */
514 png_save_uint_32(buf, width);
515 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600516 buf[8] = (png_byte)bit_depth;
517 buf[9] = (png_byte)color_type;
518 buf[10] = (png_byte)compression_type;
519 buf[11] = (png_byte)filter_type;
520 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500521
522 /* write the chunk */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500523 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, 13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500524
Andreas Dilger47a0c421997-05-16 02:46:07 -0500525 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600526 png_ptr->zstream.zalloc = png_zalloc;
527 png_ptr->zstream.zfree = png_zfree;
528 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500529 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500530 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500531 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
532 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500533 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500534 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500535 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500536 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500537 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500538 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500539 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500540 png_ptr->zlib_strategy = Z_FILTERED;
541 else
542 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
543 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500544 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500545 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500546 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500547 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500548 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500549 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500550 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500551 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600552 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500553 png_ptr->zlib_method, png_ptr->zlib_window_bits,
554 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600555 png_ptr->zstream.next_out = png_ptr->zbuf;
556 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600557 /* libpng is not interested in zstream.data_type */
558 /* set it to a predefined value, to avoid its evaluation inside zlib */
559 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500560
Guy Schalnate5a37791996-06-05 15:50:50 -0500561 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500562}
563
564/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500565 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600566 * structure.
567 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500568void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500569png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500570{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600571#ifdef PNG_USE_LOCAL_ARRAYS
572 PNG_PLTE;
573#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500574 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600575 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500576 png_byte buf[3];
577
Andreas Dilger47a0c421997-05-16 02:46:07 -0500578 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500579 if ((
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500580#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600581 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500582#endif
583 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500584 {
585 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500586 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500587 png_error(png_ptr, "Invalid number of colors in palette");
588 }
589 else
590 {
591 png_warning(png_ptr, "Invalid number of colors in palette");
592 return;
593 }
594 }
595
596 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
597 {
598 png_warning(png_ptr,
599 "Ignoring request to write a PLTE chunk in grayscale PNG");
600 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500601 }
602
Andreas Dilger47a0c421997-05-16 02:46:07 -0500603 png_ptr->num_palette = (png_uint_16)num_pal;
604 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500605
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600606 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500607#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500608 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500609 {
610 buf[0] = pal_ptr->red;
611 buf[1] = pal_ptr->green;
612 buf[2] = pal_ptr->blue;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500613 png_write_chunk_data(png_ptr, buf, 3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500614 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500615#else
616 /* This is a little slower but some buggy compilers need to do this instead */
617 pal_ptr=palette;
618 for (i = 0; i < num_pal; i++)
619 {
620 buf[0] = pal_ptr[i].red;
621 buf[1] = pal_ptr[i].green;
622 buf[2] = pal_ptr[i].blue;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500623 png_write_chunk_data(png_ptr, buf, 3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500624 }
625#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500626 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500627 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500628}
629
630/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500631void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500632png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500633{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600634#ifdef PNG_USE_LOCAL_ARRAYS
635 PNG_IDAT;
636#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500637 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500638
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500639 /* Optimize the CMF field in the zlib stream. */
640 /* This hack of the zlib stream is compliant to the stream specification. */
641 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
642 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
643 {
644 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
645 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
646 {
647 /* Avoid memory underflows and multiplication overflows. */
648 /* The conditions below are practically always satisfied;
649 however, they still must be checked. */
650 if (length >= 2 &&
651 png_ptr->height < 16384 && png_ptr->width < 16384)
652 {
653 png_uint_32 uncompressed_idat_size = png_ptr->height *
654 ((png_ptr->width *
655 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
656 unsigned int z_cinfo = z_cmf >> 4;
657 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
658 while (uncompressed_idat_size <= half_z_window_size &&
659 half_z_window_size >= 256)
660 {
661 z_cinfo--;
662 half_z_window_size >>= 1;
663 }
664 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
665 if (data[0] != (png_byte)z_cmf)
666 {
667 data[0] = (png_byte)z_cmf;
668 data[1] &= 0xe0;
669 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
670 }
671 }
672 }
673 else
674 png_error(png_ptr,
675 "Invalid zlib compression method or flags in IDAT");
676 }
677
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600678 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500679 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500680}
681
682/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500683void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600684png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500685{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600686#ifdef PNG_USE_LOCAL_ARRAYS
687 PNG_IEND;
688#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500689 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500690 png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL, 0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600691 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500692}
693
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500694#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500695/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600696#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500697void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500698png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500699{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600700#ifdef PNG_USE_LOCAL_ARRAYS
701 PNG_gAMA;
702#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500703 png_uint_32 igamma;
704 png_byte buf[4];
705
Andreas Dilger47a0c421997-05-16 02:46:07 -0500706 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600707 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600708 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500709 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500710 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, 4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500711}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500712#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600713#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500714void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600715png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600716{
717#ifdef PNG_USE_LOCAL_ARRAYS
718 PNG_gAMA;
719#endif
720 png_byte buf[4];
721
722 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600723 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500724 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500725 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, 4);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600726}
727#endif
728#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500729
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600730#if defined(PNG_WRITE_sRGB_SUPPORTED)
731/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500732void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600733png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600734{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600735#ifdef PNG_USE_LOCAL_ARRAYS
736 PNG_sRGB;
737#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600738 png_byte buf[1];
739
740 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600741 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600742 png_warning(png_ptr,
743 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600744 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500745 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, 1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600746}
747#endif
748
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600749#if defined(PNG_WRITE_iCCP_SUPPORTED)
750/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500751void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600752png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
753 png_charp profile, int profile_len)
754{
755#ifdef PNG_USE_LOCAL_ARRAYS
756 PNG_iCCP;
757#endif
758 png_size_t name_len;
759 png_charp new_name;
760 compression_state comp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500761 int embedded_profile_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600762
763 png_debug(1, "in png_write_iCCP\n");
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600764
765 comp.num_output_ptr = 0;
766 comp.max_output_ptr = 0;
767 comp.output_ptr = NULL;
768 comp.input = NULL;
769 comp.input_len = 0;
770
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600771 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
772 &new_name)) == 0)
773 {
774 png_warning(png_ptr, "Empty keyword in iCCP chunk");
775 return;
776 }
777
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600778 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600779 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600780
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500781 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600782 profile_len = 0;
783
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500784 if (profile_len > 3)
785 embedded_profile_len = ((*(profile ))<<24) | ((*(profile+1))<<16) |
786 ((*(profile+2))<< 8) | ((*(profile+3)) );
787
788 if (profile_len < embedded_profile_len)
789 {
790 png_warning(png_ptr,
791 "Embedded profile length too large in iCCP chunk");
792 return;
793 }
794
795 if (profile_len > embedded_profile_len)
796 {
797 png_warning(png_ptr,
798 "Truncating profile to actual length in iCCP chunk");
799 profile_len = embedded_profile_len;
800 }
801
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600802 if (profile_len)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500803 profile_len = png_text_compress(png_ptr, profile,
804 (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600805
806 /* make sure we include the NULL after the name and the compression type */
807 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
808 (png_uint_32)name_len+profile_len+2);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600809 new_name[name_len+1]=0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600810 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
811
812 if (profile_len)
813 png_write_compressed_data_out(png_ptr, &comp);
814
815 png_write_chunk_end(png_ptr);
816 png_free(png_ptr, new_name);
817}
818#endif
819
820#if defined(PNG_WRITE_sPLT_SUPPORTED)
821/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500822void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600823png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600824{
825#ifdef PNG_USE_LOCAL_ARRAYS
826 PNG_sPLT;
827#endif
828 png_size_t name_len;
829 png_charp new_name;
830 png_byte entrybuf[10];
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500831 png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
832 png_size_t palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600833 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500834#ifdef PNG_NO_POINTER_INDEXING
835 int i;
836#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600837
838 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600839 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
840 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600841 {
842 png_warning(png_ptr, "Empty keyword in sPLT chunk");
843 return;
844 }
845
846 /* make sure we include the NULL after the name */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500847 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500848 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600849 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600850 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600851
852 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500853#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600854 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
855 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500856 if (spalette->depth == 8)
857 {
858 entrybuf[0] = (png_byte)ep->red;
859 entrybuf[1] = (png_byte)ep->green;
860 entrybuf[2] = (png_byte)ep->blue;
861 entrybuf[3] = (png_byte)ep->alpha;
862 png_save_uint_16(entrybuf + 4, ep->frequency);
863 }
864 else
865 {
866 png_save_uint_16(entrybuf + 0, ep->red);
867 png_save_uint_16(entrybuf + 2, ep->green);
868 png_save_uint_16(entrybuf + 4, ep->blue);
869 png_save_uint_16(entrybuf + 6, ep->alpha);
870 png_save_uint_16(entrybuf + 8, ep->frequency);
871 }
872 png_write_chunk_data(png_ptr, entrybuf, entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600873 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500874#else
875 ep=spalette->entries;
876 for (i=0; i>spalette->nentries; i++)
877 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500878 if (spalette->depth == 8)
879 {
880 entrybuf[0] = (png_byte)ep[i].red;
881 entrybuf[1] = (png_byte)ep[i].green;
882 entrybuf[2] = (png_byte)ep[i].blue;
883 entrybuf[3] = (png_byte)ep[i].alpha;
884 png_save_uint_16(entrybuf + 4, ep[i].frequency);
885 }
886 else
887 {
888 png_save_uint_16(entrybuf + 0, ep[i].red);
889 png_save_uint_16(entrybuf + 2, ep[i].green);
890 png_save_uint_16(entrybuf + 4, ep[i].blue);
891 png_save_uint_16(entrybuf + 6, ep[i].alpha);
892 png_save_uint_16(entrybuf + 8, ep[i].frequency);
893 }
894 png_write_chunk_data(png_ptr, entrybuf, entry_size);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500895 }
896#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600897
898 png_write_chunk_end(png_ptr);
899 png_free(png_ptr, new_name);
900}
901#endif
902
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500903#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500904/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500905void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600906png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500907{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600908#ifdef PNG_USE_LOCAL_ARRAYS
909 PNG_sBIT;
910#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500911 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500912 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500913
Andreas Dilger47a0c421997-05-16 02:46:07 -0500914 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600915 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500916 if (color_type & PNG_COLOR_MASK_COLOR)
917 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600918 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500919
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500920 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
921 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600922 if (sbit->red == 0 || sbit->red > maxbits ||
923 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500924 sbit->blue == 0 || sbit->blue > maxbits)
925 {
926 png_warning(png_ptr, "Invalid sBIT depth specified");
927 return;
928 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500929 buf[0] = sbit->red;
930 buf[1] = sbit->green;
931 buf[2] = sbit->blue;
932 size = 3;
933 }
934 else
935 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500936 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
937 {
938 png_warning(png_ptr, "Invalid sBIT depth specified");
939 return;
940 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500941 buf[0] = sbit->gray;
942 size = 1;
943 }
944
945 if (color_type & PNG_COLOR_MASK_ALPHA)
946 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500947 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
948 {
949 png_warning(png_ptr, "Invalid sBIT depth specified");
950 return;
951 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500952 buf[size++] = sbit->alpha;
953 }
954
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600955 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500956}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500957#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500958
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500959#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500960/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600961#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500962void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500963png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600964 double red_x, double red_y, double green_x, double green_y,
965 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500966{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600967#ifdef PNG_USE_LOCAL_ARRAYS
968 PNG_cHRM;
969#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500970 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600971 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500972
Andreas Dilger47a0c421997-05-16 02:46:07 -0500973 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600974 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500975 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
976 white_x + white_y > 1.0)
977 {
978 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500979#if !defined(PNG_NO_CONSOLE_IO)
980 fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600981#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500982 return;
983 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600984 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500985 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600986 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500987 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500988
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -0600989 if (red_x < 0 || red_y < 0 || red_x + red_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -0500990 {
991 png_warning(png_ptr, "Invalid cHRM red point specified");
992 return;
993 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600994 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500995 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600996 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500997 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500998
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -0600999 if (green_x < 0 || green_y < 0 || green_x + green_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001000 {
1001 png_warning(png_ptr, "Invalid cHRM green point specified");
1002 return;
1003 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001004 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001005 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001006 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001007 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -05001008
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001009 if (blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001010 {
1011 png_warning(png_ptr, "Invalid cHRM blue point specified");
1012 return;
1013 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001014 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001015 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001016 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001017 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -05001018
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001019 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, 32);
Guy Schalnat0d580581995-07-20 02:43:20 -05001020}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001021#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001022#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001023void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001024png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
1025 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
1026 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
1027 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001028{
1029#ifdef PNG_USE_LOCAL_ARRAYS
1030 PNG_cHRM;
1031#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001032 png_byte buf[32];
1033
1034 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001035 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001036 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
1037 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001038 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001039#if !defined(PNG_NO_CONSOLE_IO)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001040 fprintf(stderr,"white_x=%ld, white_y=%ld\n",(unsigned long)white_x,
1041 (unsigned long)white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001042#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001043 return;
1044 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001045 png_save_uint_32(buf, (png_uint_32)white_x);
1046 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001047
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001048 if (red_x + red_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001049 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001050 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001051 return;
1052 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001053 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1054 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001055
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001056 if (green_x + green_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001057 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001058 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001059 return;
1060 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001061 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1062 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001063
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001064 if (blue_x + blue_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001065 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001066 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001067 return;
1068 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001069 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1070 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001071
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001072 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, 32);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001073}
1074#endif
1075#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001076
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001077#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001078/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001079void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001080png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -05001081 int num_trans, int color_type)
1082{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001083#ifdef PNG_USE_LOCAL_ARRAYS
1084 PNG_tRNS;
1085#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001086 png_byte buf[6];
1087
Andreas Dilger47a0c421997-05-16 02:46:07 -05001088 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001089 if (color_type == PNG_COLOR_TYPE_PALETTE)
1090 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001091 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001092 {
1093 png_warning(png_ptr,"Invalid number of transparent colors specified");
1094 return;
1095 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001096 /* write the chunk out as it is */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001097 png_write_chunk(png_ptr, (png_bytep)png_tRNS,
1098 trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001099 }
1100 else if (color_type == PNG_COLOR_TYPE_GRAY)
1101 {
1102 /* one 16 bit value */
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001103 if(tran->gray >= (1 << png_ptr->bit_depth))
1104 {
1105 png_warning(png_ptr,
1106 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1107 return;
1108 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001109 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001110 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, 2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001111 }
1112 else if (color_type == PNG_COLOR_TYPE_RGB)
1113 {
1114 /* three 16 bit values */
1115 png_save_uint_16(buf, tran->red);
1116 png_save_uint_16(buf + 2, tran->green);
1117 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001118 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001119 {
1120 png_warning(png_ptr,
1121 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1122 return;
1123 }
1124 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, 6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001125 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001126 else
1127 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001128 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001129 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001130}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001131#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001132
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001133#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001134/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001135void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001136png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001137{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001138#ifdef PNG_USE_LOCAL_ARRAYS
1139 PNG_bKGD;
1140#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001141 png_byte buf[6];
1142
Andreas Dilger47a0c421997-05-16 02:46:07 -05001143 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001144 if (color_type == PNG_COLOR_TYPE_PALETTE)
1145 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001146 if (
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001147#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001148 (png_ptr->num_palette ||
1149 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001150#endif
1151 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001152 {
1153 png_warning(png_ptr, "Invalid background palette index");
1154 return;
1155 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001156 buf[0] = back->index;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001157 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001158 }
1159 else if (color_type & PNG_COLOR_MASK_COLOR)
1160 {
1161 png_save_uint_16(buf, back->red);
1162 png_save_uint_16(buf + 2, back->green);
1163 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001164 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001165 {
1166 png_warning(png_ptr,
1167 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1168 return;
1169 }
1170 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, 6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001171 }
1172 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001173 {
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001174 if(back->gray >= (1 << png_ptr->bit_depth))
1175 {
1176 png_warning(png_ptr,
1177 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1178 return;
1179 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001180 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001181 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, 2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001182 }
1183}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001184#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001185
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001186#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001187/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001188void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001189png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001190{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001191#ifdef PNG_USE_LOCAL_ARRAYS
1192 PNG_hIST;
1193#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001194 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001195 png_byte buf[3];
1196
Andreas Dilger47a0c421997-05-16 02:46:07 -05001197 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001198 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001199 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001200 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1201 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001202 png_warning(png_ptr, "Invalid number of histogram entries specified");
1203 return;
1204 }
1205
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001206 png_write_chunk_start(png_ptr, (png_bytep)png_hIST,
1207 (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001208 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001209 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001210 png_save_uint_16(buf, hist[i]);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001211 png_write_chunk_data(png_ptr, buf, 2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001212 }
1213 png_write_chunk_end(png_ptr);
1214}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001215#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001216
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001217#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1218 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001219/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1220 * and if invalid, correct the keyword rather than discarding the entire
1221 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1222 * length, forbids leading or trailing whitespace, multiple internal spaces,
1223 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001224 *
1225 * The new_key is allocated to hold the corrected keyword and must be freed
1226 * by the calling routine. This avoids problems with trying to write to
1227 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001228 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001229png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001230png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001231{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001232 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001233 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001234 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001235 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001236
Andreas Dilger47a0c421997-05-16 02:46:07 -05001237 png_debug(1, "in png_check_keyword\n");
1238 *new_key = NULL;
1239
1240 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001241 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001242 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001243 return 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001244 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001245
Andreas Dilger47a0c421997-05-16 02:46:07 -05001246 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1247
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001248 *new_key = (png_charp)png_malloc_warn(png_ptr, key_len + 2);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001249 if (*new_key == NULL)
1250 {
1251 png_warning(png_ptr, "Out of memory while procesing keyword");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001252 return 0;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001253 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001254
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001255 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001256 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001257 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001258 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001259 {
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -05001260#ifndef PNG_NO_STDIO
Andreas Dilger47a0c421997-05-16 02:46:07 -05001261 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001262
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -05001263 png_sprintf(msg, "invalid keyword character 0x%02X", *kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001264 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001265#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001266 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001267#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001268 *dp = ' ';
1269 }
1270 else
1271 {
1272 *dp = *kp;
1273 }
1274 }
1275 *dp = '\0';
1276
1277 /* Remove any trailing white space. */
1278 kp = *new_key + key_len - 1;
1279 if (*kp == ' ')
1280 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001281 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001282
1283 while (*kp == ' ')
1284 {
1285 *(kp--) = '\0';
1286 key_len--;
1287 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001288 }
1289
1290 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001291 kp = *new_key;
1292 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001293 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001294 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001295
1296 while (*kp == ' ')
1297 {
1298 kp++;
1299 key_len--;
1300 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001301 }
1302
Andreas Dilger47a0c421997-05-16 02:46:07 -05001303 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001304
Andreas Dilger47a0c421997-05-16 02:46:07 -05001305 /* Remove multiple internal spaces. */
1306 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001307 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001308 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001309 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001310 *(dp++) = *kp;
1311 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001312 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001313 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001314 {
1315 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001316 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001317 }
1318 else
1319 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001320 *(dp++) = *kp;
1321 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001322 }
1323 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001324 *dp = '\0';
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001325 if(kwarn)
1326 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001327
1328 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001329 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001330 png_free(png_ptr, *new_key);
1331 *new_key=NULL;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001332 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001333 }
1334
1335 if (key_len > 79)
1336 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001337 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001338 new_key[79] = '\0';
1339 key_len = 79;
1340 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001341
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001342 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001343}
1344#endif
1345
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001346#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001347/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001348void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001349png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001350 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001351{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001352#ifdef PNG_USE_LOCAL_ARRAYS
1353 PNG_tEXt;
1354#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001355 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001356 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001357
Andreas Dilger47a0c421997-05-16 02:46:07 -05001358 png_debug(1, "in png_write_tEXt\n");
1359 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1360 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001361 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001362 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001363 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001364
Andreas Dilger47a0c421997-05-16 02:46:07 -05001365 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001366 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001367 else
1368 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001369
1370 /* make sure we include the 0 after the key */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001371 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt,
1372 (png_uint_32)key_len+text_len+1);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001373 /*
1374 * We leave it to the application to meet PNG-1.0 requirements on the
1375 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1376 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001377 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001378 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001379 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001380 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001381 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001382
Guy Schalnat0d580581995-07-20 02:43:20 -05001383 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001384 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001385}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001386#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001387
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001388#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001389/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001390void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001391png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001392 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001393{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001394#ifdef PNG_USE_LOCAL_ARRAYS
1395 PNG_zTXt;
1396#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001397 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001398 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001399 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001400 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001401
Andreas Dilger47a0c421997-05-16 02:46:07 -05001402 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001403
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001404 comp.num_output_ptr = 0;
1405 comp.max_output_ptr = 0;
1406 comp.output_ptr = NULL;
1407 comp.input = NULL;
1408 comp.input_len = 0;
1409
Andreas Dilger47a0c421997-05-16 02:46:07 -05001410 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001411 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001412 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001413 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001414 }
1415
Andreas Dilger47a0c421997-05-16 02:46:07 -05001416 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1417 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001418 png_write_tEXt(png_ptr, new_key, text, 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001419 png_free(png_ptr, new_key);
1420 return;
1421 }
1422
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001423 text_len = png_strlen(text);
1424
Andreas Dilger47a0c421997-05-16 02:46:07 -05001425 png_free(png_ptr, new_key);
1426
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001427 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001428 text_len = png_text_compress(png_ptr, text, text_len, compression,
1429 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001430
1431 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001432 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1433 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001434 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001435 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001436 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001437 /* write compression */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001438 png_write_chunk_data(png_ptr, (png_bytep)buf, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001439 /* write the compressed data */
1440 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001441
Guy Schalnat0d580581995-07-20 02:43:20 -05001442 /* close the chunk */
1443 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001444}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001445#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001446
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001447#if defined(PNG_WRITE_iTXt_SUPPORTED)
1448/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001449void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001450png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001451 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001452{
1453#ifdef PNG_USE_LOCAL_ARRAYS
1454 PNG_iTXt;
1455#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001456 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001457 png_charp new_lang, new_key;
1458 png_byte cbuf[2];
1459 compression_state comp;
1460
1461 png_debug(1, "in png_write_iTXt\n");
1462
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001463 comp.num_output_ptr = 0;
1464 comp.max_output_ptr = 0;
1465 comp.output_ptr = NULL;
1466 comp.input = NULL;
1467
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001468 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1469 {
1470 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1471 return;
1472 }
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001473 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001474 {
1475 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001476 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001477 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001478 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001479
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001480 if (lang_key == NULL)
1481 lang_key_len = 0;
1482 else
1483 lang_key_len = png_strlen(lang_key);
1484
1485 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001486 text_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001487 else
1488 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001489
1490 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001491 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1492 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001493
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001494
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001495 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001496 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001497
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001498 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1499 (png_uint_32)(
1500 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1501 + key_len
1502 + lang_len
1503 + lang_key_len
1504 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001505
1506 /*
1507 * We leave it to the application to meet PNG-1.0 requirements on the
1508 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1509 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1510 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1511 */
1512 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001513
1514 /* set the compression flag */
1515 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1516 compression == PNG_TEXT_COMPRESSION_NONE)
1517 cbuf[0] = 0;
1518 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1519 cbuf[0] = 1;
1520 /* set the compression method */
1521 cbuf[1] = 0;
1522 png_write_chunk_data(png_ptr, cbuf, 2);
1523
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001524 cbuf[0] = 0;
1525 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf), lang_len + 1);
1526 png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf), lang_key_len + 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001527 png_write_compressed_data_out(png_ptr, &comp);
1528
1529 png_write_chunk_end(png_ptr);
1530 png_free(png_ptr, new_key);
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001531 if (new_lang)
1532 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001533}
1534#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001535
1536#if defined(PNG_WRITE_oFFs_SUPPORTED)
1537/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001538void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001539png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001540 int unit_type)
1541{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001542#ifdef PNG_USE_LOCAL_ARRAYS
1543 PNG_oFFs;
1544#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001545 png_byte buf[9];
1546
1547 png_debug(1, "in png_write_oFFs\n");
1548 if (unit_type >= PNG_OFFSET_LAST)
1549 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1550
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001551 png_save_int_32(buf, x_offset);
1552 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001553 buf[8] = (png_byte)unit_type;
1554
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001555 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, 9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001556}
1557#endif
1558
1559#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001560/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001561void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001562png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1563 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1564{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001565#ifdef PNG_USE_LOCAL_ARRAYS
1566 PNG_pCAL;
1567#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001568 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001569 png_uint_32p params_len;
1570 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001571 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001572 int i;
1573
1574 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1575 if (type >= PNG_EQUATION_LAST)
1576 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1577
1578 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001579 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001580 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001581 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001582 total_len = purpose_len + units_len + 10;
1583
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001584 params_len = (png_uint_32p)png_malloc(png_ptr,
1585 (png_size_t)(nparams * sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001586
1587 /* Find the length of each parameter, making sure we don't count the
1588 null terminator for the last parameter. */
1589 for (i = 0; i < nparams; i++)
1590 {
1591 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001592 png_debug2(3, "pCAL parameter %d length = %lu\n", i,
1593 (unsigned long) params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001594 total_len += (png_size_t)params_len[i];
1595 }
1596
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001597 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001598 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001599 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001600 png_save_int_32(buf, X0);
1601 png_save_int_32(buf + 4, X1);
1602 buf[8] = (png_byte)type;
1603 buf[9] = (png_byte)nparams;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001604 png_write_chunk_data(png_ptr, buf, 10);
1605 png_write_chunk_data(png_ptr, (png_bytep)units, units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001606
1607 png_free(png_ptr, new_purpose);
1608
1609 for (i = 0; i < nparams; i++)
1610 {
1611 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1612 (png_size_t)params_len[i]);
1613 }
1614
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001615 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001616 png_write_chunk_end(png_ptr);
1617}
1618#endif
1619
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001620#if defined(PNG_WRITE_sCAL_SUPPORTED)
1621/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001622#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001623void /* PRIVATE */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001624png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001625{
1626#ifdef PNG_USE_LOCAL_ARRAYS
1627 PNG_sCAL;
1628#endif
1629 png_size_t total_len;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001630 char buf[64];
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001631
1632 png_debug(1, "in png_write_sCAL\n");
1633
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001634 buf[0] = (char)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001635
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001636 png_sprintf(buf + 1, "%12.12e", width);
1637 total_len = 1 + png_strlen(buf + 1) + 1;
1638 png_sprintf(buf + total_len, "%12.12e", height);
1639 total_len += png_strlen(buf + total_len);
1640 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
1641 png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001642}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001643#else
1644#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001645void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001646png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001647 png_charp height)
1648{
1649#ifdef PNG_USE_LOCAL_ARRAYS
1650 PNG_sCAL;
1651#endif
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001652 png_byte buf[64];
1653 png_size_t wlen, hlen, total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001654
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001655 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001656
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001657 wlen = png_strlen(width);
1658 hlen = png_strlen(height);
1659 total_len = wlen + hlen + 2;
1660 if (total_len > 64)
1661 {
1662 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
1663 return;
1664 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001665
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001666 buf[0] = (png_byte)unit;
1667 png_memcpy(buf + 1, width, wlen + 1); /* append the '\0' here */
1668 png_memcpy(buf + wlen + 2, height, hlen); /* do NOT append the '\0' here */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001669
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001670 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
1671 png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001672}
1673#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001674#endif
1675#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001676
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001677#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001678/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001679void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001680png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001681 png_uint_32 y_pixels_per_unit,
1682 int unit_type)
1683{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001684#ifdef PNG_USE_LOCAL_ARRAYS
1685 PNG_pHYs;
1686#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001687 png_byte buf[9];
1688
Andreas Dilger47a0c421997-05-16 02:46:07 -05001689 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001690 if (unit_type >= PNG_RESOLUTION_LAST)
1691 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1692
Guy Schalnat0d580581995-07-20 02:43:20 -05001693 png_save_uint_32(buf, x_pixels_per_unit);
1694 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001695 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001696
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001697 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, 9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001698}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001699#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001700
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001701#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001702/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1703 * or png_convert_from_time_t(), or fill in the structure yourself.
1704 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001705void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001706png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001707{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001708#ifdef PNG_USE_LOCAL_ARRAYS
1709 PNG_tIME;
1710#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001711 png_byte buf[7];
1712
Andreas Dilger47a0c421997-05-16 02:46:07 -05001713 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001714 if (mod_time->month > 12 || mod_time->month < 1 ||
1715 mod_time->day > 31 || mod_time->day < 1 ||
1716 mod_time->hour > 23 || mod_time->second > 60)
1717 {
1718 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1719 return;
1720 }
1721
Guy Schalnat0d580581995-07-20 02:43:20 -05001722 png_save_uint_16(buf, mod_time->year);
1723 buf[2] = mod_time->month;
1724 buf[3] = mod_time->day;
1725 buf[4] = mod_time->hour;
1726 buf[5] = mod_time->minute;
1727 buf[6] = mod_time->second;
1728
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001729 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, 7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001730}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001731#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001732
1733/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001734void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001735png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001736{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001737#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001738 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001739
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001740 /* start of interlace block */
1741 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001742
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001743 /* offset to next interlace block */
1744 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001745
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001746 /* start of interlace block in the y direction */
1747 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001748
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001749 /* offset to next interlace block in the y direction */
1750 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001751#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001752
Andreas Dilger47a0c421997-05-16 02:46:07 -05001753 png_size_t buf_size;
1754
1755 png_debug(1, "in png_write_start_row\n");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001756 buf_size = (PNG_ROWBYTES(
1757 png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001758
Guy Schalnat0d580581995-07-20 02:43:20 -05001759 /* set up row buffer */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001760 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001761 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001762
1763 /* set up filtering buffer, if using this filter */
1764 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001765 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001766 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001767 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001768 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001769 }
1770
Andreas Dilger47a0c421997-05-16 02:46:07 -05001771 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001772 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1773 {
1774 /* set up previous row buffer */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001775 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001776 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001777
1778 if (png_ptr->do_filter & PNG_FILTER_UP)
1779 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001780 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001781 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001782 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001783 }
1784
1785 if (png_ptr->do_filter & PNG_FILTER_AVG)
1786 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001787 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1788 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001789 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001790 }
1791
1792 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1793 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001794 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001795 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001796 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001797 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001798 }
1799
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001800#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001801 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001802 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001803 {
1804 if (!(png_ptr->transformations & PNG_INTERLACE))
1805 {
1806 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1807 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001808 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1809 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001810 }
1811 else
1812 {
1813 png_ptr->num_rows = png_ptr->height;
1814 png_ptr->usr_width = png_ptr->width;
1815 }
1816 }
1817 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001818#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001819 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001820 png_ptr->num_rows = png_ptr->height;
1821 png_ptr->usr_width = png_ptr->width;
1822 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001823 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1824 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001825}
1826
Andreas Dilger47a0c421997-05-16 02:46:07 -05001827/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001828void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001829png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001830{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001831#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001832 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001833
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001834 /* start of interlace block */
1835 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001836
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001837 /* offset to next interlace block */
1838 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001839
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001840 /* start of interlace block in the y direction */
1841 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001842
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001843 /* offset to next interlace block in the y direction */
1844 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001845#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001846
Guy Schalnat0d580581995-07-20 02:43:20 -05001847 int ret;
1848
Andreas Dilger47a0c421997-05-16 02:46:07 -05001849 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001850 /* next row */
1851 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001852
Guy Schalnat0d580581995-07-20 02:43:20 -05001853 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001854 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001855 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001856
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001857#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001858 /* if interlaced, go to next pass */
1859 if (png_ptr->interlaced)
1860 {
1861 png_ptr->row_number = 0;
1862 if (png_ptr->transformations & PNG_INTERLACE)
1863 {
1864 png_ptr->pass++;
1865 }
1866 else
1867 {
1868 /* loop until we find a non-zero width or height pass */
1869 do
1870 {
1871 png_ptr->pass++;
1872 if (png_ptr->pass >= 7)
1873 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001874 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001875 png_pass_inc[png_ptr->pass] - 1 -
1876 png_pass_start[png_ptr->pass]) /
1877 png_pass_inc[png_ptr->pass];
1878 png_ptr->num_rows = (png_ptr->height +
1879 png_pass_yinc[png_ptr->pass] - 1 -
1880 png_pass_ystart[png_ptr->pass]) /
1881 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001882 if (png_ptr->transformations & PNG_INTERLACE)
1883 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001884 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1885
1886 }
1887
Guy Schalnate5a37791996-06-05 15:50:50 -05001888 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001889 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001890 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001891 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001892 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001893 (PNG_ROWBYTES(png_ptr->usr_channels*
1894 png_ptr->usr_bit_depth, png_ptr->width)) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001895 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001896 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001897 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001898#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001899
1900 /* if we get here, we've just written the last row, so we need
1901 to flush the compressor */
1902 do
1903 {
1904 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001905 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001906 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001907 if (ret == Z_OK)
1908 {
1909 /* check to see if we need more room */
1910 if (!(png_ptr->zstream.avail_out))
1911 {
1912 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1913 png_ptr->zstream.next_out = png_ptr->zbuf;
1914 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1915 }
1916 }
1917 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001918 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001919 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001920 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001921 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001922 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001923 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001924 } while (ret != Z_STREAM_END);
1925
1926 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001927 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001928 {
1929 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001930 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001931 }
1932
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001933 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -06001934 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat0d580581995-07-20 02:43:20 -05001935}
1936
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001937#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001938/* Pick out the correct pixels for the interlace pass.
1939 * The basic idea here is to go through the row with a source
1940 * pointer and a destination pointer (sp and dp), and copy the
1941 * correct pixels for the pass. As the row gets compacted,
1942 * sp will always be >= dp, so we should never overwrite anything.
1943 * See the default: case for the easiest code to understand.
1944 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001945void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001946png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001947{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001948#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001949 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001950
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001951 /* start of interlace block */
1952 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001953
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001954 /* offset to next interlace block */
1955 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001956#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001957
Andreas Dilger47a0c421997-05-16 02:46:07 -05001958 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001959 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001960#if defined(PNG_USELESS_TESTS_SUPPORTED)
1961 if (row != NULL && row_info != NULL && pass < 6)
1962#else
1963 if (pass < 6)
1964#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001965 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001966 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001967 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001968 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001969 case 1:
1970 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001971 png_bytep sp;
1972 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001973 int shift;
1974 int d;
1975 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001976 png_uint_32 i;
1977 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001978
1979 dp = row;
1980 d = 0;
1981 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001982 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001983 i += png_pass_inc[pass])
1984 {
1985 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001986 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001987 d |= (value << shift);
1988
1989 if (shift == 0)
1990 {
1991 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001992 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001993 d = 0;
1994 }
1995 else
1996 shift--;
1997
1998 }
1999 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002000 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002001 break;
2002 }
2003 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002004 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002005 png_bytep sp;
2006 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05002007 int shift;
2008 int d;
2009 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002010 png_uint_32 i;
2011 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002012
2013 dp = row;
2014 shift = 6;
2015 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002016 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002017 i += png_pass_inc[pass])
2018 {
2019 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002020 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05002021 d |= (value << shift);
2022
2023 if (shift == 0)
2024 {
2025 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002026 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002027 d = 0;
2028 }
2029 else
2030 shift -= 2;
2031 }
2032 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002033 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002034 break;
2035 }
2036 case 4:
2037 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002038 png_bytep sp;
2039 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002040 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05002041 int d;
2042 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002043 png_uint_32 i;
2044 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002045
2046 dp = row;
2047 shift = 4;
2048 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002049 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002050 i += png_pass_inc[pass])
2051 {
2052 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002053 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05002054 d |= (value << shift);
2055
2056 if (shift == 0)
2057 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002058 shift = 4;
2059 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002060 d = 0;
2061 }
2062 else
2063 shift -= 4;
2064 }
2065 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002066 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002067 break;
2068 }
2069 default:
2070 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002071 png_bytep sp;
2072 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002073 png_uint_32 i;
2074 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002075 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002076
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002077 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002078 dp = row;
2079 /* find out how many bytes each pixel takes up */
2080 pixel_bytes = (row_info->pixel_depth >> 3);
2081 /* loop through the row, only looking at the pixels that
2082 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002083 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002084 i += png_pass_inc[pass])
2085 {
2086 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002087 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002088 /* move the pixel */
2089 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002090 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05002091 /* next pixel */
2092 dp += pixel_bytes;
2093 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002094 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002095 }
2096 }
2097 /* set new row width */
2098 row_info->width = (row_info->width +
2099 png_pass_inc[pass] - 1 -
2100 png_pass_start[pass]) /
2101 png_pass_inc[pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002102 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2103 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002104 }
2105}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002106#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002107
Andreas Dilger47a0c421997-05-16 02:46:07 -05002108/* This filters the row, chooses which filter to use, if it has not already
2109 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002110 * chosen filter.
2111 */
Glenn Randers-Pehrson78067772004-11-02 21:49:39 -06002112#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002113#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002114#define PNG_LOMASK ((png_uint_32)0xffffL)
2115#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002116void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002117png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002118{
Guy Schalnate5a37791996-06-05 15:50:50 -05002119 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002120 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002121 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002122 png_uint_32 row_bytes = row_info->rowbytes;
2123#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2124 int num_p_filters = (int)png_ptr->num_prev_filters;
2125#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002126
Andreas Dilger47a0c421997-05-16 02:46:07 -05002127 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05002128 /* find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002129 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002130
2131 prev_row = png_ptr->prev_row;
2132 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002133 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002134
Andreas Dilger47a0c421997-05-16 02:46:07 -05002135 /* The prediction method we use is to find which method provides the
2136 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002137 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002138 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002139 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002140 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002141 * predictive" method (not implemented yet), which does test compressions
2142 * of lines using different filter methods, and then chooses the
2143 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002144 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002145 *
2146 * GRR 980525: consider also
2147 * (1) minimum sum of absolute differences from running average (i.e.,
2148 * keep running sum of non-absolute differences & count of bytes)
2149 * [track dispersion, too? restart average if dispersion too large?]
2150 * (1b) minimum sum of absolute differences from sliding average, probably
2151 * with window size <= deflate window (usually 32K)
2152 * (2) minimum sum of squared differences from zero or running average
2153 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002154 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002155
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002156
Guy Schalnate5a37791996-06-05 15:50:50 -05002157 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002158 * that has been chosen, as it doesn't actually do anything to the data.
2159 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002160 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002161 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002162 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002163 png_bytep rp;
2164 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002165 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002166 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002167
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002168 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002169 {
2170 v = *rp;
2171 sum += (v < 128) ? v : 256 - v;
2172 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002173
2174#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2175 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2176 {
2177 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002178 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002179 sumlo = sum & PNG_LOMASK;
2180 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2181
2182 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002183 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002184 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002185 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002186 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002187 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002188 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002189 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002190 PNG_WEIGHT_SHIFT;
2191 }
2192 }
2193
2194 /* Factor in the cost of this filter (this is here for completeness,
2195 * but it makes no sense to have a "cost" for the NONE filter, as
2196 * it has the minimum possible computational cost - none).
2197 */
2198 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2199 PNG_COST_SHIFT;
2200 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2201 PNG_COST_SHIFT;
2202
2203 if (sumhi > PNG_HIMASK)
2204 sum = PNG_MAXSUM;
2205 else
2206 sum = (sumhi << PNG_HISHIFT) + sumlo;
2207 }
2208#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002209 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002210 }
2211
Guy Schalnate5a37791996-06-05 15:50:50 -05002212 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002213 if (filter_to_do == PNG_FILTER_SUB)
2214 /* it's the only filter so no testing is needed */
2215 {
2216 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002217 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002218 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2219 i++, rp++, dp++)
2220 {
2221 *dp = *rp;
2222 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002223 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002224 i++, rp++, lp++, dp++)
2225 {
2226 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2227 }
2228 best_row = png_ptr->sub_row;
2229 }
2230
2231 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002232 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002233 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002234 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002235 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002236 int v;
2237
2238#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002239 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002240 * would reduce the sum of this filter, so that we can do the
2241 * early exit comparison without scaling the sum each time.
2242 */
2243 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2244 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002245 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002246 png_uint_32 lmhi, lmlo;
2247 lmlo = lmins & PNG_LOMASK;
2248 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2249
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002250 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002251 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002252 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002253 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002254 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002255 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002256 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002257 PNG_WEIGHT_SHIFT;
2258 }
2259 }
2260
2261 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2262 PNG_COST_SHIFT;
2263 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2264 PNG_COST_SHIFT;
2265
2266 if (lmhi > PNG_HIMASK)
2267 lmins = PNG_MAXSUM;
2268 else
2269 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2270 }
2271#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002272
Guy Schalnate5a37791996-06-05 15:50:50 -05002273 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2274 i++, rp++, dp++)
2275 {
2276 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002277
Guy Schalnate5a37791996-06-05 15:50:50 -05002278 sum += (v < 128) ? v : 256 - v;
2279 }
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002280 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002281 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002282 {
2283 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002284
Guy Schalnate5a37791996-06-05 15:50:50 -05002285 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002286
2287 if (sum > lmins) /* We are already worse, don't continue. */
2288 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002289 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002290
2291#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2292 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2293 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002294 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002295 png_uint_32 sumhi, sumlo;
2296 sumlo = sum & PNG_LOMASK;
2297 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2298
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002299 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002300 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002301 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002302 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002303 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002304 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002305 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002306 PNG_WEIGHT_SHIFT;
2307 }
2308 }
2309
2310 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2311 PNG_COST_SHIFT;
2312 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2313 PNG_COST_SHIFT;
2314
2315 if (sumhi > PNG_HIMASK)
2316 sum = PNG_MAXSUM;
2317 else
2318 sum = (sumhi << PNG_HISHIFT) + sumlo;
2319 }
2320#endif
2321
Guy Schalnate5a37791996-06-05 15:50:50 -05002322 if (sum < mins)
2323 {
2324 mins = sum;
2325 best_row = png_ptr->sub_row;
2326 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002327 }
2328
Guy Schalnate5a37791996-06-05 15:50:50 -05002329 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002330 if (filter_to_do == PNG_FILTER_UP)
2331 {
2332 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002333 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002334
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002335 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002336 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002337 i++, rp++, pp++, dp++)
2338 {
2339 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2340 }
2341 best_row = png_ptr->up_row;
2342 }
2343
2344 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002345 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002346 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002347 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002348 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002349 int v;
2350
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002351
Andreas Dilger47a0c421997-05-16 02:46:07 -05002352#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2353 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2354 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002355 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002356 png_uint_32 lmhi, lmlo;
2357 lmlo = lmins & PNG_LOMASK;
2358 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2359
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002360 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002361 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002362 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002363 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002364 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002365 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002366 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002367 PNG_WEIGHT_SHIFT;
2368 }
2369 }
2370
2371 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2372 PNG_COST_SHIFT;
2373 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2374 PNG_COST_SHIFT;
2375
2376 if (lmhi > PNG_HIMASK)
2377 lmins = PNG_MAXSUM;
2378 else
2379 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2380 }
2381#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002382
2383 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002384 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002385 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002386 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002387
2388 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002389
2390 if (sum > lmins) /* We are already worse, don't continue. */
2391 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002392 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002393
2394#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 sumhi, sumlo;
2399 sumlo = sum & PNG_LOMASK;
2400 sumhi = (sum >> 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 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002407 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002408 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002409 PNG_WEIGHT_SHIFT;
2410 }
2411 }
2412
2413 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2414 PNG_COST_SHIFT;
2415 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2416 PNG_COST_SHIFT;
2417
2418 if (sumhi > PNG_HIMASK)
2419 sum = PNG_MAXSUM;
2420 else
2421 sum = (sumhi << PNG_HISHIFT) + sumlo;
2422 }
2423#endif
2424
Guy Schalnate5a37791996-06-05 15:50:50 -05002425 if (sum < mins)
2426 {
2427 mins = sum;
2428 best_row = png_ptr->up_row;
2429 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002430 }
2431
Guy Schalnate5a37791996-06-05 15:50:50 -05002432 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002433 if (filter_to_do == PNG_FILTER_AVG)
2434 {
2435 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002436 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002437 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002438 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002439 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002440 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002441 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002442 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002443 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002444 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2445 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002446 }
2447 best_row = png_ptr->avg_row;
2448 }
2449
2450 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002451 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002452 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002453 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002454 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002455 int v;
2456
2457#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2458 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2459 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002460 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002461 png_uint_32 lmhi, lmlo;
2462 lmlo = lmins & PNG_LOMASK;
2463 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2464
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002465 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002466 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002467 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002468 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002469 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002470 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002471 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002472 PNG_WEIGHT_SHIFT;
2473 }
2474 }
2475
2476 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2477 PNG_COST_SHIFT;
2478 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2479 PNG_COST_SHIFT;
2480
2481 if (lmhi > PNG_HIMASK)
2482 lmins = PNG_MAXSUM;
2483 else
2484 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2485 }
2486#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002487
2488 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002489 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002490 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002491 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002492
2493 sum += (v < 128) ? v : 256 - v;
2494 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002495 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002496 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002497 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002498 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002499
2500 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002501
2502 if (sum > lmins) /* We are already worse, don't continue. */
2503 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002504 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002505
2506#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2507 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2508 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002509 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002510 png_uint_32 sumhi, sumlo;
2511 sumlo = sum & PNG_LOMASK;
2512 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2513
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002514 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002515 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002516 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002517 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002518 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002519 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002520 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002521 PNG_WEIGHT_SHIFT;
2522 }
2523 }
2524
2525 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2526 PNG_COST_SHIFT;
2527 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2528 PNG_COST_SHIFT;
2529
2530 if (sumhi > PNG_HIMASK)
2531 sum = PNG_MAXSUM;
2532 else
2533 sum = (sumhi << PNG_HISHIFT) + sumlo;
2534 }
2535#endif
2536
Guy Schalnate5a37791996-06-05 15:50:50 -05002537 if (sum < mins)
2538 {
2539 mins = sum;
2540 best_row = png_ptr->avg_row;
2541 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002542 }
2543
Andreas Dilger47a0c421997-05-16 02:46:07 -05002544 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002545 if (filter_to_do == PNG_FILTER_PAETH)
2546 {
2547 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002548 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002549 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002550 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002551 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002552 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002553 }
2554
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002555 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002556 {
2557 int a, b, c, pa, pb, pc, p;
2558
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002559 b = *pp++;
2560 c = *cp++;
2561 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002562
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002563 p = b - c;
2564 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002565
2566#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002567 pa = abs(p);
2568 pb = abs(pc);
2569 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002570#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002571 pa = p < 0 ? -p : p;
2572 pb = pc < 0 ? -pc : pc;
2573 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002574#endif
2575
2576 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2577
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002578 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002579 }
2580 best_row = png_ptr->paeth_row;
2581 }
2582
2583 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002584 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002585 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002586 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002587 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002588 int v;
2589
2590#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2591 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2592 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002593 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002594 png_uint_32 lmhi, lmlo;
2595 lmlo = lmins & PNG_LOMASK;
2596 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2597
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002598 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002599 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002600 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002601 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002602 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002603 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002604 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002605 PNG_WEIGHT_SHIFT;
2606 }
2607 }
2608
2609 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2610 PNG_COST_SHIFT;
2611 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2612 PNG_COST_SHIFT;
2613
2614 if (lmhi > PNG_HIMASK)
2615 lmins = PNG_MAXSUM;
2616 else
2617 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2618 }
2619#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002620
2621 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002622 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002623 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002624 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002625
2626 sum += (v < 128) ? v : 256 - v;
2627 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002628
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002629 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002630 {
2631 int a, b, c, pa, pb, pc, p;
2632
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002633 b = *pp++;
2634 c = *cp++;
2635 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002636
2637#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002638 p = b - c;
2639 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002640#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002641 pa = abs(p);
2642 pb = abs(pc);
2643 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002644#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002645 pa = p < 0 ? -p : p;
2646 pb = pc < 0 ? -pc : pc;
2647 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002648#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002649 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2650#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002651 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002652 pa = abs(p - a);
2653 pb = abs(p - b);
2654 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002655 if (pa <= pb && pa <= pc)
2656 p = a;
2657 else if (pb <= pc)
2658 p = b;
2659 else
2660 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002661#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002662
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002663 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002664
2665 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002666
2667 if (sum > lmins) /* We are already worse, don't continue. */
2668 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002669 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002670
2671#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2672 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2673 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002674 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002675 png_uint_32 sumhi, sumlo;
2676 sumlo = sum & PNG_LOMASK;
2677 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2678
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002679 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002680 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002681 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002682 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002683 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002684 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002685 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002686 PNG_WEIGHT_SHIFT;
2687 }
2688 }
2689
2690 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2691 PNG_COST_SHIFT;
2692 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2693 PNG_COST_SHIFT;
2694
2695 if (sumhi > PNG_HIMASK)
2696 sum = PNG_MAXSUM;
2697 else
2698 sum = (sumhi << PNG_HISHIFT) + sumlo;
2699 }
2700#endif
2701
Guy Schalnate5a37791996-06-05 15:50:50 -05002702 if (sum < mins)
2703 {
2704 best_row = png_ptr->paeth_row;
2705 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002706 }
2707
Andreas Dilger47a0c421997-05-16 02:46:07 -05002708 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002709
Guy Schalnate5a37791996-06-05 15:50:50 -05002710 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002711
2712#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2713 /* Save the type of filter we picked this time for future calculations */
2714 if (png_ptr->num_prev_filters > 0)
2715 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002716 int j;
2717 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002718 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002719 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002720 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002721 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002722 }
2723#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002724}
2725
2726
Andreas Dilger47a0c421997-05-16 02:46:07 -05002727/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002728void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002729png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2730{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002731 png_debug(1, "in png_write_filtered_row\n");
2732 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002733 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002734
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002735 png_ptr->zstream.next_in = filtered_row;
2736 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002737 /* repeat until we have compressed all the data */
2738 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002739 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002740 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002741
Guy Schalnate5a37791996-06-05 15:50:50 -05002742 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002743 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002744 /* check for compression errors */
2745 if (ret != Z_OK)
2746 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002747 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002748 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002749 else
2750 png_error(png_ptr, "zlib error");
2751 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002752
Guy Schalnate5a37791996-06-05 15:50:50 -05002753 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002754 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002755 {
2756 /* write the IDAT and reset the zlib output buffer */
2757 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002758 png_ptr->zstream.next_out = png_ptr->zbuf;
2759 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002760 }
2761 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002762 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002763
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002764 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002765 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002766 {
2767 png_bytep tptr;
2768
2769 tptr = png_ptr->prev_row;
2770 png_ptr->prev_row = png_ptr->row_buf;
2771 png_ptr->row_buf = tptr;
2772 }
2773
Guy Schalnate5a37791996-06-05 15:50:50 -05002774 /* finish row - updates counters and flushes zlib if last row */
2775 png_write_finish_row(png_ptr);
2776
2777#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2778 png_ptr->flush_rows++;
2779
2780 if (png_ptr->flush_dist > 0 &&
2781 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002782 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002783 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002784 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002785#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002786}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05002787#endif /* PNG_WRITE_SUPPORTED */