blob: 390d1c50735c51c13de296617f7e131c780c15cd [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{
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -060090 if(png_ptr == NULL) return;
Andreas Dilger47a0c421997-05-16 02:46:07 -050091 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060092 png_write_chunk_data(png_ptr, data, length);
93 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050094}
95
Andreas Dilger47a0c421997-05-16 02:46:07 -050096/* Write the start of a PNG chunk. The type is the chunk type.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060097 * The total_length is the sum of the lengths of all the data you will be
98 * passing in png_write_chunk_data().
99 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500100void PNGAPI
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600101png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
102 png_uint_32 length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500103{
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500104 png_byte buf[8];
105 png_debug2(0, "Writing %s chunk, length = %lu\n", chunk_name,
106 (unsigned long)length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500107
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500108#ifdef PNG_IO_STATE_SUPPORTED
109 /* Inform the I/O callback that the chunk header is being written.
110 * PNG_IO_CHUNK_HDR requires a single I/O call.
111 */
112 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR;
113#endif
114
115 /* write the length and the chunk name */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500116 png_save_uint_32(buf, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500117 png_memcpy(buf + 4, chunk_name, 4);
118 png_write_data(png_ptr, buf, 8);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500119
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500120 /* put the chunk name into png_ptr->chunk_name */
121 png_memcpy(png_ptr->chunk_name, chunk_name, 4);
122
Guy Schalnat0d580581995-07-20 02:43:20 -0500123 /* reset the crc and run it over the chunk name */
124 png_reset_crc(png_ptr);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500125 png_calculate_crc(png_ptr, chunk_name, 4);
126
127#ifdef PNG_IO_STATE_SUPPORTED
128 /* Inform the I/O callback that chunk data will (possibly) be written.
129 * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.
130 */
131 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA;
132#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500133}
134
Andreas Dilger47a0c421997-05-16 02:46:07 -0500135/* Write the data of a PNG chunk started with png_write_chunk_start().
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600136 * Note that multiple calls to this function are allowed, and that the
137 * sum of the lengths from these calls *must* add up to the total_length
138 * given to png_write_chunk_start().
139 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500140void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500141png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500142{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500143 /* write the data, and run the CRC over it */
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600144 if(png_ptr == NULL) return;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500145 if (data != NULL && length > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500146 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600147 png_write_data(png_ptr, data, length);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500148 /* update the CRC after writing the data,
149 * in case that the user I/O routine alters it.
150 */
151 png_calculate_crc(png_ptr, data, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500152 }
153}
154
Andreas Dilger47a0c421997-05-16 02:46:07 -0500155/* Finish a chunk started with png_write_chunk_start(). */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500156void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600157png_write_chunk_end(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500158{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500159 png_byte buf[4];
160
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500161#ifdef PNG_IO_STATE_SUPPORTED
162 /* Inform the I/O callback that the chunk CRC is being written.
163 * PNG_IO_CHUNK_CRC requires a single I/O function call.
164 */
165 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC;
166#endif
167
168 /* write the crc in a single operation */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500169 png_save_uint_32(buf, png_ptr->crc);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500170
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500171 png_write_data(png_ptr, buf, 4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500172}
173
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600174#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600175/*
176 * This pair of functions encapsulates the operation of (a) compressing a
177 * text string, and (b) issuing it later as a series of chunk data writes.
178 * The compression_state structure is shared context for these functions
179 * set up by the caller in order to make the whole mess thread-safe.
180 */
181
182typedef struct
183{
184 char *input; /* the uncompressed input data */
185 int input_len; /* its length */
186 int num_output_ptr; /* number of output pointers used */
187 int max_output_ptr; /* size of output_ptr */
188 png_charpp output_ptr; /* array of pointers to output */
189} compression_state;
190
191/* compress given text into storage in the png_ptr structure */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500192static int /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600193png_text_compress(png_structp png_ptr,
194 png_charp text, png_size_t text_len, int compression,
195 compression_state *comp)
196{
197 int ret;
198
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600199 comp->num_output_ptr = 0;
200 comp->max_output_ptr = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600201 comp->output_ptr = NULL;
202 comp->input = NULL;
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600203 comp->input_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600204
205 /* we may just want to pass the text right through */
206 if (compression == PNG_TEXT_COMPRESSION_NONE)
207 {
208 comp->input = text;
209 comp->input_len = text_len;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500210 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600211 }
212
213 if (compression >= PNG_TEXT_COMPRESSION_LAST)
214 {
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -0500215#ifndef PNG_NO_STDIO
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600216 char msg[50];
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -0500217 png_sprintf(msg, "Unknown compression type %d", compression);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600218 png_warning(png_ptr, msg);
219#else
220 png_warning(png_ptr, "Unknown compression type");
221#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600222 }
223
224 /* We can't write the chunk until we find out how much data we have,
225 * which means we need to run the compressor first and save the
226 * output. This shouldn't be a problem, as the vast majority of
227 * comments should be reasonable, but we will set up an array of
228 * malloc'd pointers to be sure.
229 *
230 * If we knew the application was well behaved, we could simplify this
231 * greatly by assuming we can always malloc an output buffer large
232 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
233 * and malloc this directly. The only time this would be a bad idea is
234 * if we can't malloc more than 64K and we have 64K of random input
235 * data, or if the input string is incredibly large (although this
236 * wouldn't cause a failure, just a slowdown due to swapping).
237 */
238
239 /* set up the compression buffers */
240 png_ptr->zstream.avail_in = (uInt)text_len;
241 png_ptr->zstream.next_in = (Bytef *)text;
242 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
243 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
244
245 /* this is the same compression loop as in png_write_row() */
246 do
247 {
248 /* compress the data */
249 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
250 if (ret != Z_OK)
251 {
252 /* error */
253 if (png_ptr->zstream.msg != NULL)
254 png_error(png_ptr, png_ptr->zstream.msg);
255 else
256 png_error(png_ptr, "zlib error");
257 }
258 /* check to see if we need more room */
Glenn Randers-Pehrson16e11662004-11-01 14:13:40 -0600259 if (!(png_ptr->zstream.avail_out))
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600260 {
261 /* make sure the output array has room */
262 if (comp->num_output_ptr >= comp->max_output_ptr)
263 {
264 int old_max;
265
266 old_max = comp->max_output_ptr;
267 comp->max_output_ptr = comp->num_output_ptr + 4;
268 if (comp->output_ptr != NULL)
269 {
270 png_charpp old_ptr;
271
272 old_ptr = comp->output_ptr;
273 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500274 (png_size_t)(comp->max_output_ptr * sizeof(png_charpp)));
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500275 png_memcpy(comp->output_ptr, old_ptr, old_max
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500276 * sizeof(png_charp));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600277 png_free(png_ptr, old_ptr);
278 }
279 else
280 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500281 (png_size_t)(comp->max_output_ptr * sizeof(png_charp)));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600282 }
283
284 /* save the data */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500285 comp->output_ptr[comp->num_output_ptr] =
286 (png_charp)png_malloc(png_ptr, png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500287 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
288 png_ptr->zbuf_size);
289 comp->num_output_ptr++;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600290
291 /* and reset the buffer */
292 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
293 png_ptr->zstream.next_out = png_ptr->zbuf;
294 }
295 /* continue until we don't have any more to compress */
296 } while (png_ptr->zstream.avail_in);
297
298 /* finish the compression */
299 do
300 {
301 /* tell zlib we are finished */
302 ret = deflate(&png_ptr->zstream, Z_FINISH);
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500303
304 if (ret == Z_OK)
305 {
306 /* check to see if we need more room */
307 if (!(png_ptr->zstream.avail_out))
308 {
309 /* check to make sure our output array has room */
310 if (comp->num_output_ptr >= comp->max_output_ptr)
311 {
312 int old_max;
313
314 old_max = comp->max_output_ptr;
315 comp->max_output_ptr = comp->num_output_ptr + 4;
316 if (comp->output_ptr != NULL)
317 {
318 png_charpp old_ptr;
319
320 old_ptr = comp->output_ptr;
321 /* This could be optimized to realloc() */
322 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500323 (png_size_t)(comp->max_output_ptr * sizeof(png_charpp)));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500324 png_memcpy(comp->output_ptr, old_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500325 old_max * sizeof(png_charp));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500326 png_free(png_ptr, old_ptr);
327 }
328 else
329 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500330 (png_size_t)(comp->max_output_ptr * sizeof(png_charp)));
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500331 }
332
333 /* save off the data */
334 comp->output_ptr[comp->num_output_ptr] =
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500335 (png_charp)png_malloc(png_ptr, png_ptr->zbuf_size);
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500336 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
337 png_ptr->zbuf_size);
338 comp->num_output_ptr++;
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -0500339
340 /* and reset the buffer pointers */
341 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
342 png_ptr->zstream.next_out = png_ptr->zbuf;
343 }
344 }
345 else if (ret != Z_STREAM_END)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600346 {
347 /* we got an error */
348 if (png_ptr->zstream.msg != NULL)
349 png_error(png_ptr, png_ptr->zstream.msg);
350 else
351 png_error(png_ptr, "zlib error");
352 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600353 } while (ret != Z_STREAM_END);
354
355 /* text length is number of buffers plus last buffer */
356 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
357 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
358 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
359
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500360 return((int)text_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600361}
362
363/* ship the compressed text out via chunk writes */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500364static void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600365png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
366{
367 int i;
368
369 /* handle the no-compression case */
370 if (comp->input)
371 {
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500372 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
373 (png_size_t)comp->input_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600374 return;
375 }
376
377 /* write saved output buffers, if any */
378 for (i = 0; i < comp->num_output_ptr; i++)
379 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600380 png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
381 png_ptr->zbuf_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600382 png_free(png_ptr, comp->output_ptr[i]);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500383 comp->output_ptr[i]=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600384 }
385 if (comp->max_output_ptr != 0)
386 png_free(png_ptr, comp->output_ptr);
Glenn Randers-Pehrson4766a242000-07-17 06:17:09 -0500387 comp->output_ptr=NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600388 /* write anything left in zbuf */
389 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
390 png_write_chunk_data(png_ptr, png_ptr->zbuf,
391 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
392
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600393 /* reset zlib for another zTXt/iTXt or image data */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600394 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600395 png_ptr->zstream.data_type = Z_BINARY;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600396}
397#endif
398
Guy Schalnat0d580581995-07-20 02:43:20 -0500399/* Write the IHDR chunk, and update the png_struct with the necessary
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600400 * information. Note that the rest of this code depends upon this
401 * information being correct.
402 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500403void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600404png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
Guy Schalnat0d580581995-07-20 02:43:20 -0500405 int bit_depth, int color_type, int compression_type, int filter_type,
406 int interlace_type)
407{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600408#ifdef PNG_USE_LOCAL_ARRAYS
409 PNG_IHDR;
410#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500411 png_byte buf[13]; /* buffer to store the IHDR info */
412
Andreas Dilger47a0c421997-05-16 02:46:07 -0500413 png_debug(1, "in png_write_IHDR\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500414 /* Check that we have valid input data from the application info */
415 switch (color_type)
416 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500417 case PNG_COLOR_TYPE_GRAY:
Guy Schalnate5a37791996-06-05 15:50:50 -0500418 switch (bit_depth)
419 {
420 case 1:
421 case 2:
422 case 4:
423 case 8:
424 case 16: png_ptr->channels = 1; break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500425 default: png_error(png_ptr,"Invalid bit depth for grayscale image");
Guy Schalnate5a37791996-06-05 15:50:50 -0500426 }
427 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500428 case PNG_COLOR_TYPE_RGB:
Guy Schalnate5a37791996-06-05 15:50:50 -0500429 if (bit_depth != 8 && bit_depth != 16)
430 png_error(png_ptr, "Invalid bit depth for RGB image");
431 png_ptr->channels = 3;
432 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500433 case PNG_COLOR_TYPE_PALETTE:
Guy Schalnate5a37791996-06-05 15:50:50 -0500434 switch (bit_depth)
435 {
436 case 1:
437 case 2:
438 case 4:
439 case 8: png_ptr->channels = 1; break;
440 default: png_error(png_ptr, "Invalid bit depth for paletted image");
441 }
442 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500443 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500444 if (bit_depth != 8 && bit_depth != 16)
445 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
446 png_ptr->channels = 2;
447 break;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500448 case PNG_COLOR_TYPE_RGB_ALPHA:
Guy Schalnate5a37791996-06-05 15:50:50 -0500449 if (bit_depth != 8 && bit_depth != 16)
450 png_error(png_ptr, "Invalid bit depth for RGBA image");
451 png_ptr->channels = 4;
452 break;
453 default:
454 png_error(png_ptr, "Invalid image color type specified");
455 }
456
Andreas Dilger47a0c421997-05-16 02:46:07 -0500457 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500458 {
459 png_warning(png_ptr, "Invalid compression type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500460 compression_type = PNG_COMPRESSION_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500461 }
462
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600463 /* Write filter_method 64 (intrapixel differencing) only if
464 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
465 * 2. Libpng did not write a PNG signature (this filter_method is only
466 * used in PNG datastreams that are embedded in MNG datastreams) and
467 * 3. The application called png_permit_mng_features with a mask that
468 * included PNG_FLAG_MNG_FILTER_64 and
469 * 4. The filter_method is 64 and
470 * 5. The color_type is RGB or RGBA
471 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600472 if (
473#if defined(PNG_MNG_FEATURES_SUPPORTED)
474 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600475 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500476 (color_type == PNG_COLOR_TYPE_RGB ||
Glenn Randers-Pehrson408b4212000-12-18 09:33:57 -0600477 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600478 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
479#endif
480 filter_type != PNG_FILTER_TYPE_BASE)
Guy Schalnate5a37791996-06-05 15:50:50 -0500481 {
482 png_warning(png_ptr, "Invalid filter type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500483 filter_type = PNG_FILTER_TYPE_BASE;
Guy Schalnate5a37791996-06-05 15:50:50 -0500484 }
485
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600486#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500487 if (interlace_type != PNG_INTERLACE_NONE &&
488 interlace_type != PNG_INTERLACE_ADAM7)
Guy Schalnate5a37791996-06-05 15:50:50 -0500489 {
490 png_warning(png_ptr, "Invalid interlace type specified");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500491 interlace_type = PNG_INTERLACE_ADAM7;
Guy Schalnate5a37791996-06-05 15:50:50 -0500492 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600493#else
494 interlace_type=PNG_INTERLACE_NONE;
495#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500496
497 /* save off the relevent information */
498 png_ptr->bit_depth = (png_byte)bit_depth;
499 png_ptr->color_type = (png_byte)color_type;
500 png_ptr->interlaced = (png_byte)interlace_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500501#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -0600502 png_ptr->filter_type = (png_byte)filter_type;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500503#endif
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500504 png_ptr->compression_type = (png_byte)compression_type;
Guy Schalnate5a37791996-06-05 15:50:50 -0500505 png_ptr->width = width;
506 png_ptr->height = height;
507
508 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500509 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
Guy Schalnate5a37791996-06-05 15:50:50 -0500510 /* set the usr info, so any transformations can modify it */
511 png_ptr->usr_width = png_ptr->width;
512 png_ptr->usr_bit_depth = png_ptr->bit_depth;
513 png_ptr->usr_channels = png_ptr->channels;
514
Guy Schalnat0d580581995-07-20 02:43:20 -0500515 /* pack the header information into the buffer */
516 png_save_uint_32(buf, width);
517 png_save_uint_32(buf + 4, height);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600518 buf[8] = (png_byte)bit_depth;
519 buf[9] = (png_byte)color_type;
520 buf[10] = (png_byte)compression_type;
521 buf[11] = (png_byte)filter_type;
522 buf[12] = (png_byte)interlace_type;
Guy Schalnat0d580581995-07-20 02:43:20 -0500523
524 /* write the chunk */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500525 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, 13);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500526
Andreas Dilger47a0c421997-05-16 02:46:07 -0500527 /* initialize zlib with PNG info */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600528 png_ptr->zstream.zalloc = png_zalloc;
529 png_ptr->zstream.zfree = png_zfree;
530 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500531 if (!(png_ptr->do_filter))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500532 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500533 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
534 png_ptr->bit_depth < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500535 png_ptr->do_filter = PNG_FILTER_NONE;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500536 else
Guy Schalnate5a37791996-06-05 15:50:50 -0500537 png_ptr->do_filter = PNG_ALL_FILTERS;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500538 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500539 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500540 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500541 if (png_ptr->do_filter != PNG_FILTER_NONE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500542 png_ptr->zlib_strategy = Z_FILTERED;
543 else
544 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
545 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500546 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500547 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
Guy Schalnate5a37791996-06-05 15:50:50 -0500548 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500549 png_ptr->zlib_mem_level = 8;
Guy Schalnate5a37791996-06-05 15:50:50 -0500550 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500551 png_ptr->zlib_window_bits = 15;
Guy Schalnate5a37791996-06-05 15:50:50 -0500552 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500553 png_ptr->zlib_method = 8;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600554 deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
Andreas Dilger47a0c421997-05-16 02:46:07 -0500555 png_ptr->zlib_method, png_ptr->zlib_window_bits,
556 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600557 png_ptr->zstream.next_out = png_ptr->zbuf;
558 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -0600559 /* libpng is not interested in zstream.data_type */
560 /* set it to a predefined value, to avoid its evaluation inside zlib */
561 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500562
Guy Schalnate5a37791996-06-05 15:50:50 -0500563 png_ptr->mode = PNG_HAVE_IHDR;
Guy Schalnat0d580581995-07-20 02:43:20 -0500564}
565
566/* write the palette. We are careful not to trust png_color to be in the
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500567 * correct order for PNG, so people can redefine it to any convenient
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600568 * structure.
569 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500570void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500571png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
Guy Schalnat0d580581995-07-20 02:43:20 -0500572{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600573#ifdef PNG_USE_LOCAL_ARRAYS
574 PNG_PLTE;
575#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500576 png_uint_32 i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600577 png_colorp pal_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500578 png_byte buf[3];
579
Andreas Dilger47a0c421997-05-16 02:46:07 -0500580 png_debug(1, "in png_write_PLTE\n");
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500581 if ((
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -0500582#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600583 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500584#endif
585 num_pal == 0) || num_pal > 256)
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500586 {
587 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500588 {
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500589 png_error(png_ptr, "Invalid number of colors in palette");
590 }
591 else
592 {
593 png_warning(png_ptr, "Invalid number of colors in palette");
594 return;
595 }
596 }
597
598 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
599 {
600 png_warning(png_ptr,
601 "Ignoring request to write a PLTE chunk in grayscale PNG");
602 return;
Guy Schalnate5a37791996-06-05 15:50:50 -0500603 }
604
Andreas Dilger47a0c421997-05-16 02:46:07 -0500605 png_ptr->num_palette = (png_uint_16)num_pal;
606 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -0500607
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600608 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500609#ifndef PNG_NO_POINTER_INDEXING
Andreas Dilger47a0c421997-05-16 02:46:07 -0500610 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500611 {
612 buf[0] = pal_ptr->red;
613 buf[1] = pal_ptr->green;
614 buf[2] = pal_ptr->blue;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500615 png_write_chunk_data(png_ptr, buf, 3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500616 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500617#else
618 /* This is a little slower but some buggy compilers need to do this instead */
619 pal_ptr=palette;
620 for (i = 0; i < num_pal; i++)
621 {
622 buf[0] = pal_ptr[i].red;
623 buf[1] = pal_ptr[i].green;
624 buf[2] = pal_ptr[i].blue;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500625 png_write_chunk_data(png_ptr, buf, 3);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500626 }
627#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500628 png_write_chunk_end(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500629 png_ptr->mode |= PNG_HAVE_PLTE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500630}
631
632/* write an IDAT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500633void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500634png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
Guy Schalnat0d580581995-07-20 02:43:20 -0500635{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600636#ifdef PNG_USE_LOCAL_ARRAYS
637 PNG_IDAT;
638#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500639 png_debug(1, "in png_write_IDAT\n");
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -0500640
Glenn Randers-Pehrson5b779162004-09-04 13:25:08 -0500641 /* Optimize the CMF field in the zlib stream. */
642 /* This hack of the zlib stream is compliant to the stream specification. */
643 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
644 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
645 {
646 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
647 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
648 {
649 /* Avoid memory underflows and multiplication overflows. */
650 /* The conditions below are practically always satisfied;
651 however, they still must be checked. */
652 if (length >= 2 &&
653 png_ptr->height < 16384 && png_ptr->width < 16384)
654 {
655 png_uint_32 uncompressed_idat_size = png_ptr->height *
656 ((png_ptr->width *
657 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
658 unsigned int z_cinfo = z_cmf >> 4;
659 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
660 while (uncompressed_idat_size <= half_z_window_size &&
661 half_z_window_size >= 256)
662 {
663 z_cinfo--;
664 half_z_window_size >>= 1;
665 }
666 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
667 if (data[0] != (png_byte)z_cmf)
668 {
669 data[0] = (png_byte)z_cmf;
670 data[1] &= 0xe0;
671 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
672 }
673 }
674 }
675 else
676 png_error(png_ptr,
677 "Invalid zlib compression method or flags in IDAT");
678 }
679
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600680 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
Guy Schalnate5a37791996-06-05 15:50:50 -0500681 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500682}
683
684/* write an IEND chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500685void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600686png_write_IEND(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500687{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600688#ifdef PNG_USE_LOCAL_ARRAYS
689 PNG_IEND;
690#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500691 png_debug(1, "in png_write_IEND\n");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500692 png_write_chunk(png_ptr, (png_bytep)png_IEND, NULL, 0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600693 png_ptr->mode |= PNG_HAVE_IEND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500694}
695
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500696#if defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500697/* write a gAMA chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600698#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500699void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500700png_write_gAMA(png_structp png_ptr, double file_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500701{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600702#ifdef PNG_USE_LOCAL_ARRAYS
703 PNG_gAMA;
704#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500705 png_uint_32 igamma;
706 png_byte buf[4];
707
Andreas Dilger47a0c421997-05-16 02:46:07 -0500708 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600709 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600710 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500711 png_save_uint_32(buf, igamma);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500712 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, 4);
Guy Schalnat0d580581995-07-20 02:43:20 -0500713}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500714#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600715#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500716void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600717png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600718{
719#ifdef PNG_USE_LOCAL_ARRAYS
720 PNG_gAMA;
721#endif
722 png_byte buf[4];
723
724 png_debug(1, "in png_write_gAMA\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600725 /* file_gamma is saved in 1/100,000ths */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -0500726 png_save_uint_32(buf, (png_uint_32)file_gamma);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500727 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, 4);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600728}
729#endif
730#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500731
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600732#if defined(PNG_WRITE_sRGB_SUPPORTED)
733/* write a sRGB chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500734void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600735png_write_sRGB(png_structp png_ptr, int srgb_intent)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600736{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600737#ifdef PNG_USE_LOCAL_ARRAYS
738 PNG_sRGB;
739#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600740 png_byte buf[1];
741
742 png_debug(1, "in png_write_sRGB\n");
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600743 if(srgb_intent >= PNG_sRGB_INTENT_LAST)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600744 png_warning(png_ptr,
745 "Invalid sRGB rendering intent specified");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600746 buf[0]=(png_byte)srgb_intent;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500747 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, 1);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600748}
749#endif
750
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600751#if defined(PNG_WRITE_iCCP_SUPPORTED)
752/* write an iCCP chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500753void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600754png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
755 png_charp profile, int profile_len)
756{
757#ifdef PNG_USE_LOCAL_ARRAYS
758 PNG_iCCP;
759#endif
760 png_size_t name_len;
761 png_charp new_name;
762 compression_state comp;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500763 int embedded_profile_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600764
765 png_debug(1, "in png_write_iCCP\n");
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600766
767 comp.num_output_ptr = 0;
768 comp.max_output_ptr = 0;
769 comp.output_ptr = NULL;
770 comp.input = NULL;
771 comp.input_len = 0;
772
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600773 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
774 &new_name)) == 0)
775 {
776 png_warning(png_ptr, "Empty keyword in iCCP chunk");
777 return;
778 }
779
Glenn Randers-Pehrson76e5fd62000-12-28 07:50:05 -0600780 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600781 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600782
Glenn Randers-Pehrson13944802000-06-24 07:42:42 -0500783 if (profile == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600784 profile_len = 0;
785
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500786 if (profile_len > 3)
787 embedded_profile_len = ((*(profile ))<<24) | ((*(profile+1))<<16) |
788 ((*(profile+2))<< 8) | ((*(profile+3)) );
789
790 if (profile_len < embedded_profile_len)
791 {
792 png_warning(png_ptr,
793 "Embedded profile length too large in iCCP chunk");
794 return;
795 }
796
797 if (profile_len > embedded_profile_len)
798 {
799 png_warning(png_ptr,
800 "Truncating profile to actual length in iCCP chunk");
801 profile_len = embedded_profile_len;
802 }
803
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600804 if (profile_len)
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500805 profile_len = png_text_compress(png_ptr, profile,
806 (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600807
808 /* make sure we include the NULL after the name and the compression type */
809 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
810 (png_uint_32)name_len+profile_len+2);
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -0600811 new_name[name_len+1]=0x00;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600812 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
813
814 if (profile_len)
815 png_write_compressed_data_out(png_ptr, &comp);
816
817 png_write_chunk_end(png_ptr);
818 png_free(png_ptr, new_name);
819}
820#endif
821
822#if defined(PNG_WRITE_sPLT_SUPPORTED)
823/* write a sPLT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500824void /* PRIVATE */
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600825png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600826{
827#ifdef PNG_USE_LOCAL_ARRAYS
828 PNG_sPLT;
829#endif
830 png_size_t name_len;
831 png_charp new_name;
832 png_byte entrybuf[10];
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500833 png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
834 png_size_t palette_size = entry_size * spalette->nentries;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600835 png_sPLT_entryp ep;
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500836#ifdef PNG_NO_POINTER_INDEXING
837 int i;
838#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600839
840 png_debug(1, "in png_write_sPLT\n");
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600841 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
842 spalette->name, &new_name))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600843 {
844 png_warning(png_ptr, "Empty keyword in sPLT chunk");
845 return;
846 }
847
848 /* make sure we include the NULL after the name */
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -0500849 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500850 (png_uint_32)(name_len + 2 + palette_size));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600851 png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600852 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600853
854 /* loop through each palette entry, writing appropriately */
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500855#ifndef PNG_NO_POINTER_INDEXING
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600856 for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
857 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500858 if (spalette->depth == 8)
859 {
860 entrybuf[0] = (png_byte)ep->red;
861 entrybuf[1] = (png_byte)ep->green;
862 entrybuf[2] = (png_byte)ep->blue;
863 entrybuf[3] = (png_byte)ep->alpha;
864 png_save_uint_16(entrybuf + 4, ep->frequency);
865 }
866 else
867 {
868 png_save_uint_16(entrybuf + 0, ep->red);
869 png_save_uint_16(entrybuf + 2, ep->green);
870 png_save_uint_16(entrybuf + 4, ep->blue);
871 png_save_uint_16(entrybuf + 6, ep->alpha);
872 png_save_uint_16(entrybuf + 8, ep->frequency);
873 }
874 png_write_chunk_data(png_ptr, entrybuf, entry_size);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600875 }
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500876#else
877 ep=spalette->entries;
878 for (i=0; i>spalette->nentries; i++)
879 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500880 if (spalette->depth == 8)
881 {
882 entrybuf[0] = (png_byte)ep[i].red;
883 entrybuf[1] = (png_byte)ep[i].green;
884 entrybuf[2] = (png_byte)ep[i].blue;
885 entrybuf[3] = (png_byte)ep[i].alpha;
886 png_save_uint_16(entrybuf + 4, ep[i].frequency);
887 }
888 else
889 {
890 png_save_uint_16(entrybuf + 0, ep[i].red);
891 png_save_uint_16(entrybuf + 2, ep[i].green);
892 png_save_uint_16(entrybuf + 4, ep[i].blue);
893 png_save_uint_16(entrybuf + 6, ep[i].alpha);
894 png_save_uint_16(entrybuf + 8, ep[i].frequency);
895 }
896 png_write_chunk_data(png_ptr, entrybuf, entry_size);
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -0500897 }
898#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600899
900 png_write_chunk_end(png_ptr);
901 png_free(png_ptr, new_name);
902}
903#endif
904
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500905#if defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500906/* write the sBIT chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500907void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -0600908png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -0500909{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600910#ifdef PNG_USE_LOCAL_ARRAYS
911 PNG_sBIT;
912#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500913 png_byte buf[4];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500914 png_size_t size;
Guy Schalnat0d580581995-07-20 02:43:20 -0500915
Andreas Dilger47a0c421997-05-16 02:46:07 -0500916 png_debug(1, "in png_write_sBIT\n");
Guy Schalnat6d764711995-12-19 03:22:19 -0600917 /* make sure we don't depend upon the order of PNG_COLOR_8 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500918 if (color_type & PNG_COLOR_MASK_COLOR)
919 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600920 png_byte maxbits;
Guy Schalnate5a37791996-06-05 15:50:50 -0500921
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500922 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
923 png_ptr->usr_bit_depth);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600924 if (sbit->red == 0 || sbit->red > maxbits ||
925 sbit->green == 0 || sbit->green > maxbits ||
Guy Schalnate5a37791996-06-05 15:50:50 -0500926 sbit->blue == 0 || sbit->blue > maxbits)
927 {
928 png_warning(png_ptr, "Invalid sBIT depth specified");
929 return;
930 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500931 buf[0] = sbit->red;
932 buf[1] = sbit->green;
933 buf[2] = sbit->blue;
934 size = 3;
935 }
936 else
937 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500938 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
939 {
940 png_warning(png_ptr, "Invalid sBIT depth specified");
941 return;
942 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500943 buf[0] = sbit->gray;
944 size = 1;
945 }
946
947 if (color_type & PNG_COLOR_MASK_ALPHA)
948 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500949 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
950 {
951 png_warning(png_ptr, "Invalid sBIT depth specified");
952 return;
953 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500954 buf[size++] = sbit->alpha;
955 }
956
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600957 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500958}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500959#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500960
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500961#if defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500962/* write the cHRM chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600963#ifdef PNG_FLOATING_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500964void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500965png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600966 double red_x, double red_y, double green_x, double green_y,
967 double blue_x, double blue_y)
Guy Schalnat0d580581995-07-20 02:43:20 -0500968{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600969#ifdef PNG_USE_LOCAL_ARRAYS
970 PNG_cHRM;
971#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500972 png_byte buf[32];
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600973 png_uint_32 itemp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500974
Andreas Dilger47a0c421997-05-16 02:46:07 -0500975 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600976 /* each value is saved in 1/100,000ths */
Guy Schalnate5a37791996-06-05 15:50:50 -0500977 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
978 white_x + white_y > 1.0)
979 {
980 png_warning(png_ptr, "Invalid cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -0500981#if !defined(PNG_NO_CONSOLE_IO)
982 fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -0600983#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500984 return;
985 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600986 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500987 png_save_uint_32(buf, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600988 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500989 png_save_uint_32(buf + 4, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -0500990
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -0600991 if (red_x < 0 || red_y < 0 || red_x + red_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -0500992 {
993 png_warning(png_ptr, "Invalid cHRM red point specified");
994 return;
995 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600996 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500997 png_save_uint_32(buf + 8, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -0600998 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -0500999 png_save_uint_32(buf + 12, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -05001000
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001001 if (green_x < 0 || green_y < 0 || green_x + green_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001002 {
1003 png_warning(png_ptr, "Invalid cHRM green point specified");
1004 return;
1005 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001006 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001007 png_save_uint_32(buf + 16, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001008 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001009 png_save_uint_32(buf + 20, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -05001010
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001011 if (blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001012 {
1013 png_warning(png_ptr, "Invalid cHRM blue point specified");
1014 return;
1015 }
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001016 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001017 png_save_uint_32(buf + 24, itemp);
Glenn Randers-Pehrson397100e1998-03-07 19:45:37 -06001018 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
Guy Schalnat0d580581995-07-20 02:43:20 -05001019 png_save_uint_32(buf + 28, itemp);
Guy Schalnate5a37791996-06-05 15:50:50 -05001020
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001021 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, 32);
Guy Schalnat0d580581995-07-20 02:43:20 -05001022}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001023#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001024#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001025void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001026png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
1027 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
1028 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
1029 png_fixed_point blue_y)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001030{
1031#ifdef PNG_USE_LOCAL_ARRAYS
1032 PNG_cHRM;
1033#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001034 png_byte buf[32];
1035
1036 png_debug(1, "in png_write_cHRM\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001037 /* each value is saved in 1/100,000ths */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001038 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
1039 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001040 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
Glenn Randers-Pehrson316f97a2000-07-08 13:19:41 -05001041#if !defined(PNG_NO_CONSOLE_IO)
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001042 fprintf(stderr,"white_x=%ld, white_y=%ld\n",(unsigned long)white_x,
1043 (unsigned long)white_y);
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001044#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001045 return;
1046 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001047 png_save_uint_32(buf, (png_uint_32)white_x);
1048 png_save_uint_32(buf + 4, (png_uint_32)white_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001049
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001050 if (red_x + red_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001051 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001052 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001053 return;
1054 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001055 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1056 png_save_uint_32(buf + 12, (png_uint_32)red_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001057
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001058 if (green_x + green_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001059 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001060 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001061 return;
1062 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001063 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1064 png_save_uint_32(buf + 20, (png_uint_32)green_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001065
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001066 if (blue_x + blue_y > 100000L)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001067 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001068 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001069 return;
1070 }
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001071 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1072 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001073
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001074 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, 32);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001075}
1076#endif
1077#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001078
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001079#if defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001080/* write the tRNS chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001081void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001082png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
Guy Schalnat0d580581995-07-20 02:43:20 -05001083 int num_trans, int color_type)
1084{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001085#ifdef PNG_USE_LOCAL_ARRAYS
1086 PNG_tRNS;
1087#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001088 png_byte buf[6];
1089
Andreas Dilger47a0c421997-05-16 02:46:07 -05001090 png_debug(1, "in png_write_tRNS\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001091 if (color_type == PNG_COLOR_TYPE_PALETTE)
1092 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001093 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001094 {
1095 png_warning(png_ptr,"Invalid number of transparent colors specified");
1096 return;
1097 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001098 /* write the chunk out as it is */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001099 png_write_chunk(png_ptr, (png_bytep)png_tRNS,
1100 trans, (png_size_t)num_trans);
Guy Schalnat0d580581995-07-20 02:43:20 -05001101 }
1102 else if (color_type == PNG_COLOR_TYPE_GRAY)
1103 {
1104 /* one 16 bit value */
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001105 if(tran->gray >= (1 << png_ptr->bit_depth))
1106 {
1107 png_warning(png_ptr,
1108 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1109 return;
1110 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001111 png_save_uint_16(buf, tran->gray);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001112 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, 2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001113 }
1114 else if (color_type == PNG_COLOR_TYPE_RGB)
1115 {
1116 /* three 16 bit values */
1117 png_save_uint_16(buf, tran->red);
1118 png_save_uint_16(buf + 2, tran->green);
1119 png_save_uint_16(buf + 4, tran->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001120 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001121 {
1122 png_warning(png_ptr,
1123 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1124 return;
1125 }
1126 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, 6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001127 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001128 else
1129 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001130 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
Guy Schalnate5a37791996-06-05 15:50:50 -05001131 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001132}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001133#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001134
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001135#if defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001136/* write the background chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001137void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001138png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05001139{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001140#ifdef PNG_USE_LOCAL_ARRAYS
1141 PNG_bKGD;
1142#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001143 png_byte buf[6];
1144
Andreas Dilger47a0c421997-05-16 02:46:07 -05001145 png_debug(1, "in png_write_bKGD\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001146 if (color_type == PNG_COLOR_TYPE_PALETTE)
1147 {
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001148 if (
Glenn Randers-Pehrson1fd5fb32001-05-06 05:34:26 -05001149#if defined(PNG_MNG_FEATURES_SUPPORTED)
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001150 (png_ptr->num_palette ||
1151 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001152#endif
1153 back->index > png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001154 {
1155 png_warning(png_ptr, "Invalid background palette index");
1156 return;
1157 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001158 buf[0] = back->index;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001159 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001160 }
1161 else if (color_type & PNG_COLOR_MASK_COLOR)
1162 {
1163 png_save_uint_16(buf, back->red);
1164 png_save_uint_16(buf + 2, back->green);
1165 png_save_uint_16(buf + 4, back->blue);
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001166 if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001167 {
1168 png_warning(png_ptr,
1169 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1170 return;
1171 }
1172 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, 6);
Guy Schalnat0d580581995-07-20 02:43:20 -05001173 }
1174 else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001175 {
Glenn Randers-Pehrson1ea0ff32001-08-07 22:25:59 -05001176 if(back->gray >= (1 << png_ptr->bit_depth))
1177 {
1178 png_warning(png_ptr,
1179 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1180 return;
1181 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001182 png_save_uint_16(buf, back->gray);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001183 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, 2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001184 }
1185}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001186#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001187
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001188#if defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001189/* write the histogram */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001190void /* PRIVATE */
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001191png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
Guy Schalnat0d580581995-07-20 02:43:20 -05001192{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001193#ifdef PNG_USE_LOCAL_ARRAYS
1194 PNG_hIST;
1195#endif
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001196 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -05001197 png_byte buf[3];
1198
Andreas Dilger47a0c421997-05-16 02:46:07 -05001199 png_debug(1, "in png_write_hIST\n");
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001200 if (num_hist > (int)png_ptr->num_palette)
Guy Schalnate5a37791996-06-05 15:50:50 -05001201 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001202 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1203 png_ptr->num_palette);
Guy Schalnate5a37791996-06-05 15:50:50 -05001204 png_warning(png_ptr, "Invalid number of histogram entries specified");
1205 return;
1206 }
1207
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001208 png_write_chunk_start(png_ptr, (png_bytep)png_hIST,
1209 (png_uint_32)(num_hist * 2));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001210 for (i = 0; i < num_hist; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001211 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001212 png_save_uint_16(buf, hist[i]);
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001213 png_write_chunk_data(png_ptr, buf, 2);
Guy Schalnat0d580581995-07-20 02:43:20 -05001214 }
1215 png_write_chunk_end(png_ptr);
1216}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001217#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001218
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001219#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1220 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001221/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1222 * and if invalid, correct the keyword rather than discarding the entire
1223 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1224 * length, forbids leading or trailing whitespace, multiple internal spaces,
1225 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
Andreas Dilger47a0c421997-05-16 02:46:07 -05001226 *
1227 * The new_key is allocated to hold the corrected keyword and must be freed
1228 * by the calling routine. This avoids problems with trying to write to
1229 * static keywords without having to have duplicate copies of the strings.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001230 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001231png_size_t /* PRIVATE */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001232png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001233{
Andreas Dilger47a0c421997-05-16 02:46:07 -05001234 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001235 png_charp kp, dp;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001236 int kflag;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001237 int kwarn=0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001238
Andreas Dilger47a0c421997-05-16 02:46:07 -05001239 png_debug(1, "in png_check_keyword\n");
1240 *new_key = NULL;
1241
1242 if (key == NULL || (key_len = png_strlen(key)) == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001243 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001244 png_warning(png_ptr, "zero length keyword");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001245 return 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001246 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001247
Andreas Dilger47a0c421997-05-16 02:46:07 -05001248 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1249
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001250 *new_key = (png_charp)png_malloc_warn(png_ptr, key_len + 2);
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001251 if (*new_key == NULL)
1252 {
1253 png_warning(png_ptr, "Out of memory while procesing keyword");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001254 return 0;
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -05001255 }
Glenn Randers-Pehrsone68f5a32001-05-14 09:20:53 -05001256
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001257 /* Replace non-printing characters with a blank and print a warning */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001258 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001259 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001260 if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
Andreas Dilger47a0c421997-05-16 02:46:07 -05001261 {
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -05001262#ifndef PNG_NO_STDIO
Andreas Dilger47a0c421997-05-16 02:46:07 -05001263 char msg[40];
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001264
Glenn Randers-Pehrson17218292006-04-20 07:20:46 -05001265 png_sprintf(msg, "invalid keyword character 0x%02X", *kp);
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001266 png_warning(png_ptr, msg);
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001267#else
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001268 png_warning(png_ptr, "invalid character in keyword");
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001269#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001270 *dp = ' ';
1271 }
1272 else
1273 {
1274 *dp = *kp;
1275 }
1276 }
1277 *dp = '\0';
1278
1279 /* Remove any trailing white space. */
1280 kp = *new_key + key_len - 1;
1281 if (*kp == ' ')
1282 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001283 png_warning(png_ptr, "trailing spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001284
1285 while (*kp == ' ')
1286 {
1287 *(kp--) = '\0';
1288 key_len--;
1289 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001290 }
1291
1292 /* Remove any leading white space. */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001293 kp = *new_key;
1294 if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001295 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001296 png_warning(png_ptr, "leading spaces removed from keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001297
1298 while (*kp == ' ')
1299 {
1300 kp++;
1301 key_len--;
1302 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001303 }
1304
Andreas Dilger47a0c421997-05-16 02:46:07 -05001305 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001306
Andreas Dilger47a0c421997-05-16 02:46:07 -05001307 /* Remove multiple internal spaces. */
1308 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001309 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001310 if (*kp == ' ' && kflag == 0)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001311 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001312 *(dp++) = *kp;
1313 kflag = 1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001314 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001315 else if (*kp == ' ')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001316 {
1317 key_len--;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001318 kwarn=1;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001319 }
1320 else
1321 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001322 *(dp++) = *kp;
1323 kflag = 0;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001324 }
1325 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001326 *dp = '\0';
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001327 if(kwarn)
1328 png_warning(png_ptr, "extra interior spaces removed from keyword");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001329
1330 if (key_len == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001331 {
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -05001332 png_free(png_ptr, *new_key);
1333 *new_key=NULL;
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001334 png_warning(png_ptr, "Zero length keyword");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001335 }
1336
1337 if (key_len > 79)
1338 {
Glenn Randers-Pehrsonf6b4f452000-12-01 09:23:06 -06001339 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001340 new_key[79] = '\0';
1341 key_len = 79;
1342 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001343
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -06001344 return (key_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001345}
1346#endif
1347
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001348#if defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001349/* write a tEXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001350void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001351png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001352 png_size_t text_len)
Guy Schalnat0d580581995-07-20 02:43:20 -05001353{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001354#ifdef PNG_USE_LOCAL_ARRAYS
1355 PNG_tEXt;
1356#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001357 png_size_t key_len;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001358 png_charp new_key;
Guy Schalnat0d580581995-07-20 02:43:20 -05001359
Andreas Dilger47a0c421997-05-16 02:46:07 -05001360 png_debug(1, "in png_write_tEXt\n");
1361 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1362 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001363 png_warning(png_ptr, "Empty keyword in tEXt chunk");
Guy Schalnate5a37791996-06-05 15:50:50 -05001364 return;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001365 }
Guy Schalnate5a37791996-06-05 15:50:50 -05001366
Andreas Dilger47a0c421997-05-16 02:46:07 -05001367 if (text == NULL || *text == '\0')
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001368 text_len = 0;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001369 else
1370 text_len = png_strlen(text);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001371
1372 /* make sure we include the 0 after the key */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001373 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt,
1374 (png_uint_32)key_len+text_len+1);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001375 /*
1376 * We leave it to the application to meet PNG-1.0 requirements on the
1377 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1378 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001379 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001380 */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001381 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001382 if (text_len)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001383 png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001384
Guy Schalnat0d580581995-07-20 02:43:20 -05001385 png_write_chunk_end(png_ptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001386 png_free(png_ptr, new_key);
Guy Schalnat0d580581995-07-20 02:43:20 -05001387}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001388#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001389
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001390#if defined(PNG_WRITE_zTXt_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001391/* write a compressed text chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001392void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001393png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001394 png_size_t text_len, int compression)
Guy Schalnat0d580581995-07-20 02:43:20 -05001395{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001396#ifdef PNG_USE_LOCAL_ARRAYS
1397 PNG_zTXt;
1398#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001399 png_size_t key_len;
Guy Schalnat0d580581995-07-20 02:43:20 -05001400 char buf[1];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001401 png_charp new_key;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001402 compression_state comp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001403
Andreas Dilger47a0c421997-05-16 02:46:07 -05001404 png_debug(1, "in png_write_zTXt\n");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001405
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001406 comp.num_output_ptr = 0;
1407 comp.max_output_ptr = 0;
1408 comp.output_ptr = NULL;
1409 comp.input = NULL;
1410 comp.input_len = 0;
1411
Andreas Dilger47a0c421997-05-16 02:46:07 -05001412 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001413 {
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06001414 png_warning(png_ptr, "Empty keyword in zTXt chunk");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001415 return;
Guy Schalnate5a37791996-06-05 15:50:50 -05001416 }
1417
Andreas Dilger47a0c421997-05-16 02:46:07 -05001418 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1419 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001420 png_write_tEXt(png_ptr, new_key, text, 0);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001421 png_free(png_ptr, new_key);
1422 return;
1423 }
1424
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001425 text_len = png_strlen(text);
1426
Andreas Dilger47a0c421997-05-16 02:46:07 -05001427 png_free(png_ptr, new_key);
1428
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001429 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001430 text_len = png_text_compress(png_ptr, text, text_len, compression,
1431 &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001432
1433 /* write start of chunk */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001434 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
1435 (key_len+text_len+2));
Guy Schalnat0d580581995-07-20 02:43:20 -05001436 /* write key */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001437 png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001438 buf[0] = (png_byte)compression;
Guy Schalnat0d580581995-07-20 02:43:20 -05001439 /* write compression */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001440 png_write_chunk_data(png_ptr, (png_bytep)buf, 1);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001441 /* write the compressed data */
1442 png_write_compressed_data_out(png_ptr, &comp);
Guy Schalnat0d580581995-07-20 02:43:20 -05001443
Guy Schalnat0d580581995-07-20 02:43:20 -05001444 /* close the chunk */
1445 png_write_chunk_end(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001446}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001447#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001448
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001449#if defined(PNG_WRITE_iTXt_SUPPORTED)
1450/* write an iTXt chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001451void /* PRIVATE */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001452png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001453 png_charp lang, png_charp lang_key, png_charp text)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001454{
1455#ifdef PNG_USE_LOCAL_ARRAYS
1456 PNG_iTXt;
1457#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001458 png_size_t lang_len, key_len, lang_key_len, text_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001459 png_charp new_lang, new_key;
1460 png_byte cbuf[2];
1461 compression_state comp;
1462
1463 png_debug(1, "in png_write_iTXt\n");
1464
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06001465 comp.num_output_ptr = 0;
1466 comp.max_output_ptr = 0;
1467 comp.output_ptr = NULL;
1468 comp.input = NULL;
1469
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001470 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1471 {
1472 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1473 return;
1474 }
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001475 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001476 {
1477 png_warning(png_ptr, "Empty language field in iTXt chunk");
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001478 new_lang = NULL;
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05001479 lang_len = 0;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001480 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001481
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001482 if (lang_key == NULL)
1483 lang_key_len = 0;
1484 else
1485 lang_key_len = png_strlen(lang_key);
1486
1487 if (text == NULL)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001488 text_len = 0;
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001489 else
1490 text_len = png_strlen(text);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001491
1492 /* compute the compressed data; do it now for the length */
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001493 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1494 &comp);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001495
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001496
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001497 /* make sure we include the compression flag, the compression byte,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001498 * and the NULs after the key, lang, and lang_key parts */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001499
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001500 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1501 (png_uint_32)(
1502 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1503 + key_len
1504 + lang_len
1505 + lang_key_len
1506 + text_len));
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001507
1508 /*
1509 * We leave it to the application to meet PNG-1.0 requirements on the
1510 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1511 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1512 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1513 */
1514 png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001515
1516 /* set the compression flag */
1517 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1518 compression == PNG_TEXT_COMPRESSION_NONE)
1519 cbuf[0] = 0;
1520 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1521 cbuf[0] = 1;
1522 /* set the compression method */
1523 cbuf[1] = 0;
1524 png_write_chunk_data(png_ptr, cbuf, 2);
1525
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001526 cbuf[0] = 0;
1527 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf), lang_len + 1);
1528 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 -06001529 png_write_compressed_data_out(png_ptr, &comp);
1530
1531 png_write_chunk_end(png_ptr);
1532 png_free(png_ptr, new_key);
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001533 if (new_lang)
1534 png_free(png_ptr, new_lang);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001535}
1536#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001537
1538#if defined(PNG_WRITE_oFFs_SUPPORTED)
1539/* write the oFFs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001540void /* PRIVATE */
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001541png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
Andreas Dilger47a0c421997-05-16 02:46:07 -05001542 int unit_type)
1543{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001544#ifdef PNG_USE_LOCAL_ARRAYS
1545 PNG_oFFs;
1546#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05001547 png_byte buf[9];
1548
1549 png_debug(1, "in png_write_oFFs\n");
1550 if (unit_type >= PNG_OFFSET_LAST)
1551 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1552
Glenn Randers-Pehrsonb1828932001-06-23 08:03:17 -05001553 png_save_int_32(buf, x_offset);
1554 png_save_int_32(buf + 4, y_offset);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001555 buf[8] = (png_byte)unit_type;
1556
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001557 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, 9);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001558}
1559#endif
1560
1561#if defined(PNG_WRITE_pCAL_SUPPORTED)
Glenn Randers-Pehrsonff9c9472000-07-11 07:12:36 -05001562/* write the pCAL chunk (described in the PNG extensions document) */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001563void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001564png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1565 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1566{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001567#ifdef PNG_USE_LOCAL_ARRAYS
1568 PNG_pCAL;
1569#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001570 png_size_t purpose_len, units_len, total_len;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001571 png_uint_32p params_len;
1572 png_byte buf[10];
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001573 png_charp new_purpose;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001574 int i;
1575
1576 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1577 if (type >= PNG_EQUATION_LAST)
1578 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1579
1580 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001581 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001582 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001583 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001584 total_len = purpose_len + units_len + 10;
1585
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001586 params_len = (png_uint_32p)png_malloc(png_ptr,
1587 (png_size_t)(nparams * sizeof(png_uint_32)));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001588
1589 /* Find the length of each parameter, making sure we don't count the
1590 null terminator for the last parameter. */
1591 for (i = 0; i < nparams; i++)
1592 {
1593 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001594 png_debug2(3, "pCAL parameter %d length = %lu\n", i,
1595 (unsigned long) params_len[i]);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001596 total_len += (png_size_t)params_len[i];
1597 }
1598
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -05001599 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001600 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001601 png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001602 png_save_int_32(buf, X0);
1603 png_save_int_32(buf + 4, X1);
1604 buf[8] = (png_byte)type;
1605 buf[9] = (png_byte)nparams;
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001606 png_write_chunk_data(png_ptr, buf, 10);
1607 png_write_chunk_data(png_ptr, (png_bytep)units, units_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001608
1609 png_free(png_ptr, new_purpose);
1610
1611 for (i = 0; i < nparams; i++)
1612 {
1613 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1614 (png_size_t)params_len[i]);
1615 }
1616
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001617 png_free(png_ptr, params_len);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001618 png_write_chunk_end(png_ptr);
1619}
1620#endif
1621
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001622#if defined(PNG_WRITE_sCAL_SUPPORTED)
1623/* write the sCAL chunk */
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001624#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001625void /* PRIVATE */
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001626png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001627{
1628#ifdef PNG_USE_LOCAL_ARRAYS
1629 PNG_sCAL;
1630#endif
1631 png_size_t total_len;
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001632 char buf[64];
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001633
1634 png_debug(1, "in png_write_sCAL\n");
1635
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001636 buf[0] = (char)unit;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001637
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001638 png_sprintf(buf + 1, "%12.12e", width);
1639 total_len = 1 + png_strlen(buf + 1) + 1;
1640 png_sprintf(buf + total_len, "%12.12e", height);
1641 total_len += png_strlen(buf + total_len);
1642 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
1643 png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001644}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001645#else
1646#ifdef PNG_FIXED_POINT_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001647void /* PRIVATE */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001648png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001649 png_charp height)
1650{
1651#ifdef PNG_USE_LOCAL_ARRAYS
1652 PNG_sCAL;
1653#endif
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001654 png_byte buf[64];
1655 png_size_t wlen, hlen, total_len;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001656
Glenn Randers-Pehrson68ea2432000-04-01 21:10:05 -06001657 png_debug(1, "in png_write_sCAL_s\n");
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001658
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001659 wlen = png_strlen(width);
1660 hlen = png_strlen(height);
1661 total_len = wlen + hlen + 2;
1662 if (total_len > 64)
1663 {
1664 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
1665 return;
1666 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001667
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001668 buf[0] = (png_byte)unit;
1669 png_memcpy(buf + 1, width, wlen + 1); /* append the '\0' here */
1670 png_memcpy(buf + wlen + 2, height, hlen); /* do NOT append the '\0' here */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001671
Glenn Randers-Pehrson6bc53be2006-06-16 07:52:03 -05001672 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
1673 png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001674}
1675#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001676#endif
1677#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001678
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001679#if defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -05001680/* write the pHYs chunk */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001681void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001682png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
Guy Schalnat0d580581995-07-20 02:43:20 -05001683 png_uint_32 y_pixels_per_unit,
1684 int unit_type)
1685{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001686#ifdef PNG_USE_LOCAL_ARRAYS
1687 PNG_pHYs;
1688#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001689 png_byte buf[9];
1690
Andreas Dilger47a0c421997-05-16 02:46:07 -05001691 png_debug(1, "in png_write_pHYs\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001692 if (unit_type >= PNG_RESOLUTION_LAST)
1693 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1694
Guy Schalnat0d580581995-07-20 02:43:20 -05001695 png_save_uint_32(buf, x_pixels_per_unit);
1696 png_save_uint_32(buf + 4, y_pixels_per_unit);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001697 buf[8] = (png_byte)unit_type;
Guy Schalnat0d580581995-07-20 02:43:20 -05001698
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001699 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, 9);
Guy Schalnat0d580581995-07-20 02:43:20 -05001700}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001701#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001702
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001703#if defined(PNG_WRITE_tIME_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001704/* Write the tIME chunk. Use either png_convert_from_struct_tm()
1705 * or png_convert_from_time_t(), or fill in the structure yourself.
1706 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001707void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001708png_write_tIME(png_structp png_ptr, png_timep mod_time)
Guy Schalnat0d580581995-07-20 02:43:20 -05001709{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001710#ifdef PNG_USE_LOCAL_ARRAYS
1711 PNG_tIME;
1712#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001713 png_byte buf[7];
1714
Andreas Dilger47a0c421997-05-16 02:46:07 -05001715 png_debug(1, "in png_write_tIME\n");
Guy Schalnate5a37791996-06-05 15:50:50 -05001716 if (mod_time->month > 12 || mod_time->month < 1 ||
1717 mod_time->day > 31 || mod_time->day < 1 ||
1718 mod_time->hour > 23 || mod_time->second > 60)
1719 {
1720 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1721 return;
1722 }
1723
Guy Schalnat0d580581995-07-20 02:43:20 -05001724 png_save_uint_16(buf, mod_time->year);
1725 buf[2] = mod_time->month;
1726 buf[3] = mod_time->day;
1727 buf[4] = mod_time->hour;
1728 buf[5] = mod_time->minute;
1729 buf[6] = mod_time->second;
1730
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001731 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, 7);
Guy Schalnat0d580581995-07-20 02:43:20 -05001732}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001733#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001734
1735/* initializes the row writing capability of libpng */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001736void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001737png_write_start_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001738{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001739#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001740 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001741
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001742 /* start of interlace block */
1743 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001744
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001745 /* offset to next interlace block */
1746 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001747
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001748 /* start of interlace block in the y direction */
1749 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001750
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001751 /* offset to next interlace block in the y direction */
1752 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001753#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001754
Andreas Dilger47a0c421997-05-16 02:46:07 -05001755 png_size_t buf_size;
1756
1757 png_debug(1, "in png_write_start_row\n");
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001758 buf_size = (PNG_ROWBYTES(
1759 png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001760
Guy Schalnat0d580581995-07-20 02:43:20 -05001761 /* set up row buffer */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001762 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001763 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
Guy Schalnate5a37791996-06-05 15:50:50 -05001764
1765 /* set up filtering buffer, if using this filter */
1766 if (png_ptr->do_filter & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05001767 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001768 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001769 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001770 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
Guy Schalnate5a37791996-06-05 15:50:50 -05001771 }
1772
Andreas Dilger47a0c421997-05-16 02:46:07 -05001773 /* We only need to keep the previous row if we are using one of these. */
Guy Schalnate5a37791996-06-05 15:50:50 -05001774 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1775 {
1776 /* set up previous row buffer */
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001777 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, buf_size);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001778 png_memset(png_ptr->prev_row, 0, buf_size);
Guy Schalnate5a37791996-06-05 15:50:50 -05001779
1780 if (png_ptr->do_filter & PNG_FILTER_UP)
1781 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001782 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001783 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001784 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
Guy Schalnate5a37791996-06-05 15:50:50 -05001785 }
1786
1787 if (png_ptr->do_filter & PNG_FILTER_AVG)
1788 {
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001789 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1790 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001791 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
Guy Schalnate5a37791996-06-05 15:50:50 -05001792 }
1793
1794 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1795 {
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001796 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06001797 (png_ptr->rowbytes + 1));
Andreas Dilger47a0c421997-05-16 02:46:07 -05001798 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
Guy Schalnate5a37791996-06-05 15:50:50 -05001799 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001800 }
1801
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001802#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001803 /* if interlaced, we need to set up width and height of pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001804 if (png_ptr->interlaced)
Guy Schalnat0d580581995-07-20 02:43:20 -05001805 {
1806 if (!(png_ptr->transformations & PNG_INTERLACE))
1807 {
1808 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1809 png_pass_ystart[0]) / png_pass_yinc[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05001810 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1811 png_pass_start[0]) / png_pass_inc[0];
Guy Schalnat0d580581995-07-20 02:43:20 -05001812 }
1813 else
1814 {
1815 png_ptr->num_rows = png_ptr->height;
1816 png_ptr->usr_width = png_ptr->width;
1817 }
1818 }
1819 else
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001820#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001821 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001822 png_ptr->num_rows = png_ptr->height;
1823 png_ptr->usr_width = png_ptr->width;
1824 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001825 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1826 png_ptr->zstream.next_out = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -05001827}
1828
Andreas Dilger47a0c421997-05-16 02:46:07 -05001829/* Internal use only. Called when finished processing a row of data. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001830void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001831png_write_finish_row(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001832{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001833#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001834 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001835
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001836 /* start of interlace block */
1837 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001838
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001839 /* offset to next interlace block */
1840 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001841
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001842 /* start of interlace block in the y direction */
1843 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001844
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001845 /* offset to next interlace block in the y direction */
1846 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001847#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001848
Guy Schalnat0d580581995-07-20 02:43:20 -05001849 int ret;
1850
Andreas Dilger47a0c421997-05-16 02:46:07 -05001851 png_debug(1, "in png_write_finish_row\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001852 /* next row */
1853 png_ptr->row_number++;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001854
Guy Schalnat0d580581995-07-20 02:43:20 -05001855 /* see if we are done */
Guy Schalnat6d764711995-12-19 03:22:19 -06001856 if (png_ptr->row_number < png_ptr->num_rows)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001857 return;
Guy Schalnat0d580581995-07-20 02:43:20 -05001858
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001859#ifdef PNG_WRITE_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001860 /* if interlaced, go to next pass */
1861 if (png_ptr->interlaced)
1862 {
1863 png_ptr->row_number = 0;
1864 if (png_ptr->transformations & PNG_INTERLACE)
1865 {
1866 png_ptr->pass++;
1867 }
1868 else
1869 {
1870 /* loop until we find a non-zero width or height pass */
1871 do
1872 {
1873 png_ptr->pass++;
1874 if (png_ptr->pass >= 7)
1875 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001876 png_ptr->usr_width = (png_ptr->width +
Guy Schalnat0d580581995-07-20 02:43:20 -05001877 png_pass_inc[png_ptr->pass] - 1 -
1878 png_pass_start[png_ptr->pass]) /
1879 png_pass_inc[png_ptr->pass];
1880 png_ptr->num_rows = (png_ptr->height +
1881 png_pass_yinc[png_ptr->pass] - 1 -
1882 png_pass_ystart[png_ptr->pass]) /
1883 png_pass_yinc[png_ptr->pass];
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001884 if (png_ptr->transformations & PNG_INTERLACE)
1885 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05001886 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
1887
1888 }
1889
Guy Schalnate5a37791996-06-05 15:50:50 -05001890 /* reset the row above the image for the next pass */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001891 if (png_ptr->pass < 7)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001892 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001893 if (png_ptr->prev_row != NULL)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001894 png_memset(png_ptr->prev_row, 0,
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -05001895 (PNG_ROWBYTES(png_ptr->usr_channels*
1896 png_ptr->usr_bit_depth, png_ptr->width)) + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -05001897 return;
Guy Schalnatc21f90c1996-06-17 16:24:45 -05001898 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001899 }
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001900#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001901
1902 /* if we get here, we've just written the last row, so we need
1903 to flush the compressor */
1904 do
1905 {
1906 /* tell the compressor we are done */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001907 ret = deflate(&png_ptr->zstream, Z_FINISH);
Guy Schalnat0d580581995-07-20 02:43:20 -05001908 /* check for an error */
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05001909 if (ret == Z_OK)
1910 {
1911 /* check to see if we need more room */
1912 if (!(png_ptr->zstream.avail_out))
1913 {
1914 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
1915 png_ptr->zstream.next_out = png_ptr->zbuf;
1916 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1917 }
1918 }
1919 else if (ret != Z_STREAM_END)
Guy Schalnat0d580581995-07-20 02:43:20 -05001920 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001921 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001922 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnat0d580581995-07-20 02:43:20 -05001923 else
Guy Schalnat6d764711995-12-19 03:22:19 -06001924 png_error(png_ptr, "zlib error");
Guy Schalnat0d580581995-07-20 02:43:20 -05001925 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001926 } while (ret != Z_STREAM_END);
1927
1928 /* write any extra space */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001929 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
Guy Schalnat0d580581995-07-20 02:43:20 -05001930 {
1931 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001932 png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -05001933 }
1934
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001935 deflateReset(&png_ptr->zstream);
Glenn Randers-Pehrson878b31e2004-11-12 22:04:56 -06001936 png_ptr->zstream.data_type = Z_BINARY;
Guy Schalnat0d580581995-07-20 02:43:20 -05001937}
1938
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001939#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001940/* Pick out the correct pixels for the interlace pass.
1941 * The basic idea here is to go through the row with a source
1942 * pointer and a destination pointer (sp and dp), and copy the
1943 * correct pixels for the pass. As the row gets compacted,
1944 * sp will always be >= dp, so we should never overwrite anything.
1945 * See the default: case for the easiest code to understand.
1946 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001947void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001948png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
Guy Schalnat0d580581995-07-20 02:43:20 -05001949{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001950#ifdef PNG_USE_LOCAL_ARRAYS
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001951 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001952
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001953 /* start of interlace block */
1954 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001955
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06001956 /* offset to next interlace block */
1957 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -06001958#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001959
Andreas Dilger47a0c421997-05-16 02:46:07 -05001960 png_debug(1, "in png_do_write_interlace\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05001961 /* we don't have to do anything on the last pass (6) */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001962#if defined(PNG_USELESS_TESTS_SUPPORTED)
1963 if (row != NULL && row_info != NULL && pass < 6)
1964#else
1965 if (pass < 6)
1966#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001967 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001968 /* each pixel depth is handled separately */
Guy Schalnat0d580581995-07-20 02:43:20 -05001969 switch (row_info->pixel_depth)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001970 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001971 case 1:
1972 {
Guy Schalnat6d764711995-12-19 03:22:19 -06001973 png_bytep sp;
1974 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05001975 int shift;
1976 int d;
1977 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001978 png_uint_32 i;
1979 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001980
1981 dp = row;
1982 d = 0;
1983 shift = 7;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001984 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05001985 i += png_pass_inc[pass])
1986 {
1987 sp = row + (png_size_t)(i >> 3);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001988 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
Guy Schalnat0d580581995-07-20 02:43:20 -05001989 d |= (value << shift);
1990
1991 if (shift == 0)
1992 {
1993 shift = 7;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001994 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05001995 d = 0;
1996 }
1997 else
1998 shift--;
1999
2000 }
2001 if (shift != 7)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002002 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002003 break;
2004 }
2005 case 2:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002006 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002007 png_bytep sp;
2008 png_bytep dp;
Guy Schalnat0d580581995-07-20 02:43:20 -05002009 int shift;
2010 int d;
2011 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002012 png_uint_32 i;
2013 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002014
2015 dp = row;
2016 shift = 6;
2017 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002018 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002019 i += png_pass_inc[pass])
2020 {
2021 sp = row + (png_size_t)(i >> 2);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002022 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
Guy Schalnat0d580581995-07-20 02:43:20 -05002023 d |= (value << shift);
2024
2025 if (shift == 0)
2026 {
2027 shift = 6;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002028 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002029 d = 0;
2030 }
2031 else
2032 shift -= 2;
2033 }
2034 if (shift != 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002035 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002036 break;
2037 }
2038 case 4:
2039 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002040 png_bytep sp;
2041 png_bytep dp;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002042 int shift;
Guy Schalnat0d580581995-07-20 02:43:20 -05002043 int d;
2044 int value;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002045 png_uint_32 i;
2046 png_uint_32 row_width = row_info->width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002047
2048 dp = row;
2049 shift = 4;
2050 d = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002051 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002052 i += png_pass_inc[pass])
2053 {
2054 sp = row + (png_size_t)(i >> 1);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002055 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
Guy Schalnat0d580581995-07-20 02:43:20 -05002056 d |= (value << shift);
2057
2058 if (shift == 0)
2059 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002060 shift = 4;
2061 *dp++ = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002062 d = 0;
2063 }
2064 else
2065 shift -= 4;
2066 }
2067 if (shift != 4)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002068 *dp = (png_byte)d;
Guy Schalnat0d580581995-07-20 02:43:20 -05002069 break;
2070 }
2071 default:
2072 {
Guy Schalnat6d764711995-12-19 03:22:19 -06002073 png_bytep sp;
2074 png_bytep dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002075 png_uint_32 i;
2076 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002077 png_size_t pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002078
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002079 /* start at the beginning */
Guy Schalnat0d580581995-07-20 02:43:20 -05002080 dp = row;
2081 /* find out how many bytes each pixel takes up */
2082 pixel_bytes = (row_info->pixel_depth >> 3);
2083 /* loop through the row, only looking at the pixels that
2084 matter */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002085 for (i = png_pass_start[pass]; i < row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05002086 i += png_pass_inc[pass])
2087 {
2088 /* find out where the original pixel is */
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -06002089 sp = row + (png_size_t)i * pixel_bytes;
Guy Schalnat0d580581995-07-20 02:43:20 -05002090 /* move the pixel */
2091 if (dp != sp)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002092 png_memcpy(dp, sp, pixel_bytes);
Guy Schalnat0d580581995-07-20 02:43:20 -05002093 /* next pixel */
2094 dp += pixel_bytes;
2095 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002096 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002097 }
2098 }
2099 /* set new row width */
2100 row_info->width = (row_info->width +
2101 png_pass_inc[pass] - 1 -
2102 png_pass_start[pass]) /
2103 png_pass_inc[pass];
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002104 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2105 row_info->width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002106 }
2107}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002108#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002109
Andreas Dilger47a0c421997-05-16 02:46:07 -05002110/* This filters the row, chooses which filter to use, if it has not already
2111 * been specified by the application, and then writes the row out with the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002112 * chosen filter.
2113 */
Glenn Randers-Pehrson78067772004-11-02 21:49:39 -06002114#define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002115#define PNG_HISHIFT 10
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -06002116#define PNG_LOMASK ((png_uint_32)0xffffL)
2117#define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002118void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002119png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002120{
Guy Schalnate5a37791996-06-05 15:50:50 -05002121 png_bytep prev_row, best_row, row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002122 png_uint_32 mins, bpp;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002123 png_byte filter_to_do = png_ptr->do_filter;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002124 png_uint_32 row_bytes = row_info->rowbytes;
2125#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2126 int num_p_filters = (int)png_ptr->num_prev_filters;
2127#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002128
Andreas Dilger47a0c421997-05-16 02:46:07 -05002129 png_debug(1, "in png_write_find_filter\n");
Guy Schalnat0d580581995-07-20 02:43:20 -05002130 /* find out how many bytes offset each pixel is */
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002131 bpp = (row_info->pixel_depth + 7) >> 3;
Guy Schalnate5a37791996-06-05 15:50:50 -05002132
2133 prev_row = png_ptr->prev_row;
2134 best_row = row_buf = png_ptr->row_buf;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002135 mins = PNG_MAXSUM;
Guy Schalnat0d580581995-07-20 02:43:20 -05002136
Andreas Dilger47a0c421997-05-16 02:46:07 -05002137 /* The prediction method we use is to find which method provides the
2138 * smallest value when summing the absolute values of the distances
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002139 * from zero, using anything >= 128 as negative numbers. This is known
Andreas Dilger47a0c421997-05-16 02:46:07 -05002140 * as the "minimum sum of absolute differences" heuristic. Other
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002141 * heuristics are the "weighted minimum sum of absolute differences"
Andreas Dilger47a0c421997-05-16 02:46:07 -05002142 * (experimental and can in theory improve compression), and the "zlib
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002143 * predictive" method (not implemented yet), which does test compressions
2144 * of lines using different filter methods, and then chooses the
2145 * (series of) filter(s) that give minimum compressed data size (VERY
Andreas Dilger47a0c421997-05-16 02:46:07 -05002146 * computationally expensive).
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002147 *
2148 * GRR 980525: consider also
2149 * (1) minimum sum of absolute differences from running average (i.e.,
2150 * keep running sum of non-absolute differences & count of bytes)
2151 * [track dispersion, too? restart average if dispersion too large?]
2152 * (1b) minimum sum of absolute differences from sliding average, probably
2153 * with window size <= deflate window (usually 32K)
2154 * (2) minimum sum of squared differences from zero or running average
2155 * (i.e., ~ root-mean-square approach)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002156 */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002157
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002158
Guy Schalnate5a37791996-06-05 15:50:50 -05002159 /* We don't need to test the 'no filter' case if this is the only filter
Andreas Dilger47a0c421997-05-16 02:46:07 -05002160 * that has been chosen, as it doesn't actually do anything to the data.
2161 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002162 if ((filter_to_do & PNG_FILTER_NONE) &&
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002163 filter_to_do != PNG_FILTER_NONE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002164 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002165 png_bytep rp;
2166 png_uint_32 sum = 0;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002167 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002168 int v;
Guy Schalnat0d580581995-07-20 02:43:20 -05002169
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002170 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002171 {
2172 v = *rp;
2173 sum += (v < 128) ? v : 256 - v;
2174 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002175
2176#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2177 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2178 {
2179 png_uint_32 sumhi, sumlo;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002180 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002181 sumlo = sum & PNG_LOMASK;
2182 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2183
2184 /* Reduce the sum if we match any of the previous rows */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002185 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002186 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002187 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002188 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002189 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002190 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002191 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002192 PNG_WEIGHT_SHIFT;
2193 }
2194 }
2195
2196 /* Factor in the cost of this filter (this is here for completeness,
2197 * but it makes no sense to have a "cost" for the NONE filter, as
2198 * it has the minimum possible computational cost - none).
2199 */
2200 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2201 PNG_COST_SHIFT;
2202 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2203 PNG_COST_SHIFT;
2204
2205 if (sumhi > PNG_HIMASK)
2206 sum = PNG_MAXSUM;
2207 else
2208 sum = (sumhi << PNG_HISHIFT) + sumlo;
2209 }
2210#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002211 mins = sum;
Guy Schalnat0d580581995-07-20 02:43:20 -05002212 }
2213
Guy Schalnate5a37791996-06-05 15:50:50 -05002214 /* sub filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002215 if (filter_to_do == PNG_FILTER_SUB)
2216 /* it's the only filter so no testing is needed */
2217 {
2218 png_bytep rp, lp, dp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002219 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002220 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2221 i++, rp++, dp++)
2222 {
2223 *dp = *rp;
2224 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002225 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002226 i++, rp++, lp++, dp++)
2227 {
2228 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2229 }
2230 best_row = png_ptr->sub_row;
2231 }
2232
2233 else if (filter_to_do & PNG_FILTER_SUB)
Guy Schalnat0d580581995-07-20 02:43:20 -05002234 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002235 png_bytep rp, dp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002236 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002237 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002238 int v;
2239
2240#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002241 /* We temporarily increase the "minimum sum" by the factor we
Andreas Dilger47a0c421997-05-16 02:46:07 -05002242 * would reduce the sum of this filter, so that we can do the
2243 * early exit comparison without scaling the sum each time.
2244 */
2245 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2246 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002247 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002248 png_uint_32 lmhi, lmlo;
2249 lmlo = lmins & PNG_LOMASK;
2250 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2251
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002252 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002253 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002254 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002255 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002256 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002257 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002258 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002259 PNG_WEIGHT_SHIFT;
2260 }
2261 }
2262
2263 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2264 PNG_COST_SHIFT;
2265 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2266 PNG_COST_SHIFT;
2267
2268 if (lmhi > PNG_HIMASK)
2269 lmins = PNG_MAXSUM;
2270 else
2271 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2272 }
2273#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002274
Guy Schalnate5a37791996-06-05 15:50:50 -05002275 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2276 i++, rp++, dp++)
2277 {
2278 v = *dp = *rp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002279
Guy Schalnate5a37791996-06-05 15:50:50 -05002280 sum += (v < 128) ? v : 256 - v;
2281 }
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002282 for (lp = row_buf + 1; i < row_bytes;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002283 i++, rp++, lp++, dp++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002284 {
2285 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002286
Guy Schalnate5a37791996-06-05 15:50:50 -05002287 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002288
2289 if (sum > lmins) /* We are already worse, don't continue. */
2290 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002291 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002292
2293#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2294 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2295 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002296 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002297 png_uint_32 sumhi, sumlo;
2298 sumlo = sum & PNG_LOMASK;
2299 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2300
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002301 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002302 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002303 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002304 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002305 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002306 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002307 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002308 PNG_WEIGHT_SHIFT;
2309 }
2310 }
2311
2312 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2313 PNG_COST_SHIFT;
2314 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2315 PNG_COST_SHIFT;
2316
2317 if (sumhi > PNG_HIMASK)
2318 sum = PNG_MAXSUM;
2319 else
2320 sum = (sumhi << PNG_HISHIFT) + sumlo;
2321 }
2322#endif
2323
Guy Schalnate5a37791996-06-05 15:50:50 -05002324 if (sum < mins)
2325 {
2326 mins = sum;
2327 best_row = png_ptr->sub_row;
2328 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002329 }
2330
Guy Schalnate5a37791996-06-05 15:50:50 -05002331 /* up filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002332 if (filter_to_do == PNG_FILTER_UP)
2333 {
2334 png_bytep rp, dp, pp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002335 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002336
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002337 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002338 pp = prev_row + 1; i < row_bytes;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002339 i++, rp++, pp++, dp++)
2340 {
2341 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2342 }
2343 best_row = png_ptr->up_row;
2344 }
2345
2346 else if (filter_to_do & PNG_FILTER_UP)
Guy Schalnat0d580581995-07-20 02:43:20 -05002347 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002348 png_bytep rp, dp, pp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002349 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002350 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002351 int v;
2352
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002353
Andreas Dilger47a0c421997-05-16 02:46:07 -05002354#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2355 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2356 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002357 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002358 png_uint_32 lmhi, lmlo;
2359 lmlo = lmins & PNG_LOMASK;
2360 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2361
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002362 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002363 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002364 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002365 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002366 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002367 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002368 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002369 PNG_WEIGHT_SHIFT;
2370 }
2371 }
2372
2373 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2374 PNG_COST_SHIFT;
2375 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2376 PNG_COST_SHIFT;
2377
2378 if (lmhi > PNG_HIMASK)
2379 lmins = PNG_MAXSUM;
2380 else
2381 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2382 }
2383#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002384
2385 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002386 pp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002387 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002388 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002389
2390 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002391
2392 if (sum > lmins) /* We are already worse, don't continue. */
2393 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002394 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002395
2396#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2397 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2398 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002399 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002400 png_uint_32 sumhi, sumlo;
2401 sumlo = sum & PNG_LOMASK;
2402 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2403
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002404 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002405 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002406 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002407 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002408 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002409 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002410 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002411 PNG_WEIGHT_SHIFT;
2412 }
2413 }
2414
2415 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2416 PNG_COST_SHIFT;
2417 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2418 PNG_COST_SHIFT;
2419
2420 if (sumhi > PNG_HIMASK)
2421 sum = PNG_MAXSUM;
2422 else
2423 sum = (sumhi << PNG_HISHIFT) + sumlo;
2424 }
2425#endif
2426
Guy Schalnate5a37791996-06-05 15:50:50 -05002427 if (sum < mins)
2428 {
2429 mins = sum;
2430 best_row = png_ptr->up_row;
2431 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002432 }
2433
Guy Schalnate5a37791996-06-05 15:50:50 -05002434 /* avg filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002435 if (filter_to_do == PNG_FILTER_AVG)
2436 {
2437 png_bytep rp, dp, pp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002438 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002439 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002440 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002441 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002442 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002443 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002444 for (lp = row_buf + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002445 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002446 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2447 & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002448 }
2449 best_row = png_ptr->avg_row;
2450 }
2451
2452 else if (filter_to_do & PNG_FILTER_AVG)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002453 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002454 png_bytep rp, dp, pp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002455 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002456 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002457 int v;
2458
2459#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2460 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2461 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002462 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002463 png_uint_32 lmhi, lmlo;
2464 lmlo = lmins & PNG_LOMASK;
2465 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2466
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002467 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002468 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002469 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002470 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002471 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002472 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002473 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002474 PNG_WEIGHT_SHIFT;
2475 }
2476 }
2477
2478 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2479 PNG_COST_SHIFT;
2480 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2481 PNG_COST_SHIFT;
2482
2483 if (lmhi > PNG_HIMASK)
2484 lmins = PNG_MAXSUM;
2485 else
2486 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2487 }
2488#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002489
2490 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002491 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002492 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002493 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002494
2495 sum += (v < 128) ? v : 256 - v;
2496 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002497 for (lp = row_buf + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002498 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002499 v = *dp++ =
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002500 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002501
2502 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002503
2504 if (sum > lmins) /* We are already worse, don't continue. */
2505 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002506 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002507
2508#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2509 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2510 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002511 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002512 png_uint_32 sumhi, sumlo;
2513 sumlo = sum & PNG_LOMASK;
2514 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2515
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002516 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002517 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002518 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002519 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002520 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002521 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002522 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002523 PNG_WEIGHT_SHIFT;
2524 }
2525 }
2526
2527 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2528 PNG_COST_SHIFT;
2529 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2530 PNG_COST_SHIFT;
2531
2532 if (sumhi > PNG_HIMASK)
2533 sum = PNG_MAXSUM;
2534 else
2535 sum = (sumhi << PNG_HISHIFT) + sumlo;
2536 }
2537#endif
2538
Guy Schalnate5a37791996-06-05 15:50:50 -05002539 if (sum < mins)
2540 {
2541 mins = sum;
2542 best_row = png_ptr->avg_row;
2543 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002544 }
2545
Andreas Dilger47a0c421997-05-16 02:46:07 -05002546 /* Paeth filter */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002547 if (filter_to_do == PNG_FILTER_PAETH)
2548 {
2549 png_bytep rp, dp, pp, cp, lp;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002550 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002551 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002552 pp = prev_row + 1; i < bpp; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002553 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002554 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002555 }
2556
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002557 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002558 {
2559 int a, b, c, pa, pb, pc, p;
2560
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002561 b = *pp++;
2562 c = *cp++;
2563 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002564
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002565 p = b - c;
2566 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002567
2568#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002569 pa = abs(p);
2570 pb = abs(pc);
2571 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002572#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002573 pa = p < 0 ? -p : p;
2574 pb = pc < 0 ? -pc : pc;
2575 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002576#endif
2577
2578 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2579
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002580 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002581 }
2582 best_row = png_ptr->paeth_row;
2583 }
2584
2585 else if (filter_to_do & PNG_FILTER_PAETH)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002586 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002587 png_bytep rp, dp, pp, cp, lp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002588 png_uint_32 sum = 0, lmins = mins;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002589 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002590 int v;
2591
2592#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2593 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2594 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002595 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002596 png_uint_32 lmhi, lmlo;
2597 lmlo = lmins & PNG_LOMASK;
2598 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2599
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002600 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002601 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002602 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002603 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002604 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002605 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002606 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002607 PNG_WEIGHT_SHIFT;
2608 }
2609 }
2610
2611 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2612 PNG_COST_SHIFT;
2613 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2614 PNG_COST_SHIFT;
2615
2616 if (lmhi > PNG_HIMASK)
2617 lmins = PNG_MAXSUM;
2618 else
2619 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2620 }
2621#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002622
2623 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002624 pp = prev_row + 1; i < bpp; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002625 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002626 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002627
2628 sum += (v < 128) ? v : 256 - v;
2629 }
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002630
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002631 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05002632 {
2633 int a, b, c, pa, pb, pc, p;
2634
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002635 b = *pp++;
2636 c = *cp++;
2637 a = *lp++;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002638
2639#ifndef PNG_SLOW_PAETH
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002640 p = b - c;
2641 pc = a - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002642#ifdef PNG_USE_ABS
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002643 pa = abs(p);
2644 pb = abs(pc);
2645 pc = abs(p + pc);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002646#else
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002647 pa = p < 0 ? -p : p;
2648 pb = pc < 0 ? -pc : pc;
2649 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002650#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002651 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2652#else /* PNG_SLOW_PAETH */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002653 p = a + b - c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002654 pa = abs(p - a);
2655 pb = abs(p - b);
2656 pc = abs(p - c);
Guy Schalnate5a37791996-06-05 15:50:50 -05002657 if (pa <= pb && pa <= pc)
2658 p = a;
2659 else if (pb <= pc)
2660 p = b;
2661 else
2662 p = c;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002663#endif /* PNG_SLOW_PAETH */
Guy Schalnate5a37791996-06-05 15:50:50 -05002664
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002665 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
Guy Schalnate5a37791996-06-05 15:50:50 -05002666
2667 sum += (v < 128) ? v : 256 - v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002668
2669 if (sum > lmins) /* We are already worse, don't continue. */
2670 break;
Guy Schalnate5a37791996-06-05 15:50:50 -05002671 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002672
2673#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2674 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2675 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002676 int j;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002677 png_uint_32 sumhi, sumlo;
2678 sumlo = sum & PNG_LOMASK;
2679 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2680
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002681 for (j = 0; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002682 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002683 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002684 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002685 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002686 PNG_WEIGHT_SHIFT;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002687 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
Andreas Dilger47a0c421997-05-16 02:46:07 -05002688 PNG_WEIGHT_SHIFT;
2689 }
2690 }
2691
2692 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2693 PNG_COST_SHIFT;
2694 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2695 PNG_COST_SHIFT;
2696
2697 if (sumhi > PNG_HIMASK)
2698 sum = PNG_MAXSUM;
2699 else
2700 sum = (sumhi << PNG_HISHIFT) + sumlo;
2701 }
2702#endif
2703
Guy Schalnate5a37791996-06-05 15:50:50 -05002704 if (sum < mins)
2705 {
2706 best_row = png_ptr->paeth_row;
2707 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002708 }
2709
Andreas Dilger47a0c421997-05-16 02:46:07 -05002710 /* Do the actual writing of the filtered row data from the chosen filter. */
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002711
Guy Schalnate5a37791996-06-05 15:50:50 -05002712 png_write_filtered_row(png_ptr, best_row);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002713
2714#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2715 /* Save the type of filter we picked this time for future calculations */
2716 if (png_ptr->num_prev_filters > 0)
2717 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002718 int j;
2719 for (j = 1; j < num_p_filters; j++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002720 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002721 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002722 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002723 png_ptr->prev_filters[j] = best_row[0];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002724 }
2725#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05002726}
2727
2728
Andreas Dilger47a0c421997-05-16 02:46:07 -05002729/* Do the actual writing of a previously filtered row. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002730void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -05002731png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2732{
Andreas Dilger47a0c421997-05-16 02:46:07 -05002733 png_debug(1, "in png_write_filtered_row\n");
2734 png_debug1(2, "filter = %d\n", filtered_row[0]);
Guy Schalnate5a37791996-06-05 15:50:50 -05002735 /* set up the zlib input buffer */
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002736
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002737 png_ptr->zstream.next_in = filtered_row;
2738 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
Guy Schalnate5a37791996-06-05 15:50:50 -05002739 /* repeat until we have compressed all the data */
2740 do
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002741 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002742 int ret; /* return of zlib */
Guy Schalnat0d580581995-07-20 02:43:20 -05002743
Guy Schalnate5a37791996-06-05 15:50:50 -05002744 /* compress the data */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002745 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
Guy Schalnate5a37791996-06-05 15:50:50 -05002746 /* check for compression errors */
2747 if (ret != Z_OK)
2748 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05002749 if (png_ptr->zstream.msg != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002750 png_error(png_ptr, png_ptr->zstream.msg);
Guy Schalnate5a37791996-06-05 15:50:50 -05002751 else
2752 png_error(png_ptr, "zlib error");
2753 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002754
Guy Schalnate5a37791996-06-05 15:50:50 -05002755 /* see if it is time to write another IDAT */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002756 if (!(png_ptr->zstream.avail_out))
Guy Schalnate5a37791996-06-05 15:50:50 -05002757 {
2758 /* write the IDAT and reset the zlib output buffer */
2759 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002760 png_ptr->zstream.next_out = png_ptr->zbuf;
2761 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -05002762 }
2763 /* repeat until all data has been compressed */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002764 } while (png_ptr->zstream.avail_in);
Guy Schalnate5a37791996-06-05 15:50:50 -05002765
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002766 /* swap the current and previous rows */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002767 if (png_ptr->prev_row != NULL)
Guy Schalnatc21f90c1996-06-17 16:24:45 -05002768 {
2769 png_bytep tptr;
2770
2771 tptr = png_ptr->prev_row;
2772 png_ptr->prev_row = png_ptr->row_buf;
2773 png_ptr->row_buf = tptr;
2774 }
2775
Guy Schalnate5a37791996-06-05 15:50:50 -05002776 /* finish row - updates counters and flushes zlib if last row */
2777 png_write_finish_row(png_ptr);
2778
2779#if defined(PNG_WRITE_FLUSH_SUPPORTED)
2780 png_ptr->flush_rows++;
2781
2782 if (png_ptr->flush_dist > 0 &&
2783 png_ptr->flush_rows >= png_ptr->flush_dist)
Guy Schalnat0d580581995-07-20 02:43:20 -05002784 {
Guy Schalnate5a37791996-06-05 15:50:50 -05002785 png_write_flush(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05002786 }
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002787#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002788}
Glenn Randers-Pehrson3097f612001-05-07 14:52:45 -05002789#endif /* PNG_WRITE_SUPPORTED */