Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / modules / libimg / png / pngwutil.c
blob86373f571d615965c194bfb87d7335eed2c41c23
2 /* pngwutil.c - utilities to write a PNG file
4 * Last changed in libpng 1.2.30 [August 13, 2008]
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2008 Glenn Randers-Pehrson
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9 */
11 #define PNG_INTERNAL
12 #include "png.h"
13 #ifdef PNG_WRITE_SUPPORTED
15 /* 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.
19 void PNGAPI
20 png_save_uint_32(png_bytep buf, png_uint_32 i)
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);
28 /* The png_save_int_32 function assumes integers are stored in two's
29 * complement format. If this isn't the case, then this routine needs to
30 * be modified to write data in two's complement format.
32 void PNGAPI
33 png_save_int_32(png_bytep buf, png_int_32 i)
35 buf[0] = (png_byte)((i >> 24) & 0xff);
36 buf[1] = (png_byte)((i >> 16) & 0xff);
37 buf[2] = (png_byte)((i >> 8) & 0xff);
38 buf[3] = (png_byte)(i & 0xff);
41 /* Place a 16-bit number into a buffer in PNG byte order.
42 * The parameter is declared unsigned int, not png_uint_16,
43 * just to avoid potential problems on pre-ANSI C compilers.
45 void PNGAPI
46 png_save_uint_16(png_bytep buf, unsigned int i)
48 buf[0] = (png_byte)((i >> 8) & 0xff);
49 buf[1] = (png_byte)(i & 0xff);
52 /* Simple function to write the signature. If we have already written
53 * the magic bytes of the signature, or more likely, the PNG stream is
54 * being embedded into another stream and doesn't need its own signature,
55 * we should call png_set_sig_bytes() to tell libpng how many of the
56 * bytes have already been written.
58 void /* PRIVATE */
59 png_write_sig(png_structp png_ptr)
61 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
63 /* write the rest of the 8 byte signature */
64 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
65 (png_size_t)(8 - png_ptr->sig_bytes));
66 if (png_ptr->sig_bytes < 3)
67 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
70 /* Write a PNG chunk all at once. The type is an array of ASCII characters
71 * representing the chunk name. The array must be at least 4 bytes in
72 * length, and does not need to be null terminated. To be safe, pass the
73 * pre-defined chunk names here, and if you need a new one, define it
74 * where the others are defined. The length is the length of the data.
75 * All the data must be present. If that is not possible, use the
76 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
77 * functions instead.
79 void PNGAPI
80 png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
81 png_bytep data, png_size_t length)
83 if (png_ptr == NULL) return;
84 png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
85 png_write_chunk_data(png_ptr, data, (png_size_t)length);
86 png_write_chunk_end(png_ptr);
89 /* Write the start of a PNG chunk. The type is the chunk type.
90 * The total_length is the sum of the lengths of all the data you will be
91 * passing in png_write_chunk_data().
93 void PNGAPI
94 png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
95 png_uint_32 length)
97 png_byte buf[8];
99 png_debug2(0, "Writing %s chunk, length = %lu\n", chunk_name,
100 (unsigned long)length);
101 if (png_ptr == NULL) return;
103 /* write the length and the chunk name */
104 png_save_uint_32(buf, length);
105 png_memcpy(buf + 4, chunk_name, 4);
106 png_write_data(png_ptr, buf, (png_size_t)8);
107 /* put the chunk name into png_ptr->chunk_name */
108 png_memcpy(png_ptr->chunk_name, chunk_name, 4);
109 /* reset the crc and run it over the chunk name */
110 png_reset_crc(png_ptr);
111 png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
114 /* Write the data of a PNG chunk started with png_write_chunk_start().
115 * Note that multiple calls to this function are allowed, and that the
116 * sum of the lengths from these calls *must* add up to the total_length
117 * given to png_write_chunk_start().
119 void PNGAPI
120 png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
122 /* write the data, and run the CRC over it */
123 if (png_ptr == NULL) return;
124 if (data != NULL && length > 0)
126 png_write_data(png_ptr, data, length);
127 /* update the CRC after writing the data,
128 * in case that the user I/O routine alters it.
130 png_calculate_crc(png_ptr, data, length);
134 /* Finish a chunk started with png_write_chunk_start(). */
135 void PNGAPI
136 png_write_chunk_end(png_structp png_ptr)
138 png_byte buf[4];
140 if (png_ptr == NULL) return;
142 /* write the crc in a single operation */
143 png_save_uint_32(buf, png_ptr->crc);
145 png_write_data(png_ptr, buf, (png_size_t)4);
148 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
150 * This pair of functions encapsulates the operation of (a) compressing a
151 * text string, and (b) issuing it later as a series of chunk data writes.
152 * The compression_state structure is shared context for these functions
153 * set up by the caller in order to make the whole mess thread-safe.
156 typedef struct
158 char *input; /* the uncompressed input data */
159 int input_len; /* its length */
160 int num_output_ptr; /* number of output pointers used */
161 int max_output_ptr; /* size of output_ptr */
162 png_charpp output_ptr; /* array of pointers to output */
163 } compression_state;
165 /* compress given text into storage in the png_ptr structure */
166 static int /* PRIVATE */
167 png_text_compress(png_structp png_ptr,
168 png_charp text, png_size_t text_len, int compression,
169 compression_state *comp)
171 int ret;
173 comp->num_output_ptr = 0;
174 comp->max_output_ptr = 0;
175 comp->output_ptr = NULL;
176 comp->input = NULL;
177 comp->input_len = 0;
179 /* we may just want to pass the text right through */
180 if (compression == PNG_TEXT_COMPRESSION_NONE)
182 comp->input = text;
183 comp->input_len = text_len;
184 return((int)text_len);
187 if (compression >= PNG_TEXT_COMPRESSION_LAST)
189 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
190 char msg[50];
191 png_snprintf(msg, 50, "Unknown compression type %d", compression);
192 png_warning(png_ptr, msg);
193 #else
194 png_warning(png_ptr, "Unknown compression type");
195 #endif
198 /* We can't write the chunk until we find out how much data we have,
199 * which means we need to run the compressor first and save the
200 * output. This shouldn't be a problem, as the vast majority of
201 * comments should be reasonable, but we will set up an array of
202 * malloc'd pointers to be sure.
204 * If we knew the application was well behaved, we could simplify this
205 * greatly by assuming we can always malloc an output buffer large
206 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
207 * and malloc this directly. The only time this would be a bad idea is
208 * if we can't malloc more than 64K and we have 64K of random input
209 * data, or if the input string is incredibly large (although this
210 * wouldn't cause a failure, just a slowdown due to swapping).
213 /* set up the compression buffers */
214 png_ptr->zstream.avail_in = (uInt)text_len;
215 png_ptr->zstream.next_in = (Bytef *)text;
216 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
217 png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
219 /* this is the same compression loop as in png_write_row() */
222 /* compress the data */
223 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
224 if (ret != Z_OK)
226 /* error */
227 if (png_ptr->zstream.msg != NULL)
228 png_error(png_ptr, png_ptr->zstream.msg);
229 else
230 png_error(png_ptr, "zlib error");
232 /* check to see if we need more room */
233 if (!(png_ptr->zstream.avail_out))
235 /* make sure the output array has room */
236 if (comp->num_output_ptr >= comp->max_output_ptr)
238 int old_max;
240 old_max = comp->max_output_ptr;
241 comp->max_output_ptr = comp->num_output_ptr + 4;
242 if (comp->output_ptr != NULL)
244 png_charpp old_ptr;
246 old_ptr = comp->output_ptr;
247 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
248 (png_uint_32)
249 (comp->max_output_ptr * png_sizeof(png_charpp)));
250 png_memcpy(comp->output_ptr, old_ptr, old_max
251 * png_sizeof(png_charp));
252 png_free(png_ptr, old_ptr);
254 else
255 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
256 (png_uint_32)
257 (comp->max_output_ptr * png_sizeof(png_charp)));
260 /* save the data */
261 comp->output_ptr[comp->num_output_ptr] =
262 (png_charp)png_malloc(png_ptr,
263 (png_uint_32)png_ptr->zbuf_size);
264 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
265 png_ptr->zbuf_size);
266 comp->num_output_ptr++;
268 /* and reset the buffer */
269 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
270 png_ptr->zstream.next_out = png_ptr->zbuf;
272 /* continue until we don't have any more to compress */
273 } while (png_ptr->zstream.avail_in);
275 /* finish the compression */
278 /* tell zlib we are finished */
279 ret = deflate(&png_ptr->zstream, Z_FINISH);
281 if (ret == Z_OK)
283 /* check to see if we need more room */
284 if (!(png_ptr->zstream.avail_out))
286 /* check to make sure our output array has room */
287 if (comp->num_output_ptr >= comp->max_output_ptr)
289 int old_max;
291 old_max = comp->max_output_ptr;
292 comp->max_output_ptr = comp->num_output_ptr + 4;
293 if (comp->output_ptr != NULL)
295 png_charpp old_ptr;
297 old_ptr = comp->output_ptr;
298 /* This could be optimized to realloc() */
299 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
300 (png_uint_32)(comp->max_output_ptr *
301 png_sizeof(png_charp)));
302 png_memcpy(comp->output_ptr, old_ptr,
303 old_max * png_sizeof(png_charp));
304 png_free(png_ptr, old_ptr);
306 else
307 comp->output_ptr = (png_charpp)png_malloc(png_ptr,
308 (png_uint_32)(comp->max_output_ptr *
309 png_sizeof(png_charp)));
312 /* save off the data */
313 comp->output_ptr[comp->num_output_ptr] =
314 (png_charp)png_malloc(png_ptr,
315 (png_uint_32)png_ptr->zbuf_size);
316 png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
317 png_ptr->zbuf_size);
318 comp->num_output_ptr++;
320 /* and reset the buffer pointers */
321 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
322 png_ptr->zstream.next_out = png_ptr->zbuf;
325 else if (ret != Z_STREAM_END)
327 /* we got an error */
328 if (png_ptr->zstream.msg != NULL)
329 png_error(png_ptr, png_ptr->zstream.msg);
330 else
331 png_error(png_ptr, "zlib error");
333 } while (ret != Z_STREAM_END);
335 /* text length is number of buffers plus last buffer */
336 text_len = png_ptr->zbuf_size * comp->num_output_ptr;
337 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
338 text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
340 return((int)text_len);
343 /* ship the compressed text out via chunk writes */
344 static void /* PRIVATE */
345 png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
347 int i;
349 /* handle the no-compression case */
350 if (comp->input)
352 png_write_chunk_data(png_ptr, (png_bytep)comp->input,
353 (png_size_t)comp->input_len);
354 return;
357 /* write saved output buffers, if any */
358 for (i = 0; i < comp->num_output_ptr; i++)
360 png_write_chunk_data(png_ptr, (png_bytep)comp->output_ptr[i],
361 (png_size_t)png_ptr->zbuf_size);
362 png_free(png_ptr, comp->output_ptr[i]);
363 comp->output_ptr[i]=NULL;
365 if (comp->max_output_ptr != 0)
366 png_free(png_ptr, comp->output_ptr);
367 comp->output_ptr=NULL;
368 /* write anything left in zbuf */
369 if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
370 png_write_chunk_data(png_ptr, png_ptr->zbuf,
371 (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out));
373 /* reset zlib for another zTXt/iTXt or image data */
374 deflateReset(&png_ptr->zstream);
375 png_ptr->zstream.data_type = Z_BINARY;
377 #endif
379 /* Write the IHDR chunk, and update the png_struct with the necessary
380 * information. Note that the rest of this code depends upon this
381 * information being correct.
383 void /* PRIVATE */
384 png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
385 int bit_depth, int color_type, int compression_type, int filter_type,
386 int interlace_type)
388 #ifdef PNG_USE_LOCAL_ARRAYS
389 PNG_IHDR;
390 #endif
391 int ret;
393 png_byte buf[13]; /* buffer to store the IHDR info */
395 png_debug(1, "in png_write_IHDR\n");
396 /* Check that we have valid input data from the application info */
397 switch (color_type)
399 case PNG_COLOR_TYPE_GRAY:
400 switch (bit_depth)
402 case 1:
403 case 2:
404 case 4:
405 case 8:
406 case 16: png_ptr->channels = 1; break;
407 default: png_error(png_ptr, "Invalid bit depth for grayscale image");
409 break;
410 case PNG_COLOR_TYPE_RGB:
411 if (bit_depth != 8 && bit_depth != 16)
412 png_error(png_ptr, "Invalid bit depth for RGB image");
413 png_ptr->channels = 3;
414 break;
415 case PNG_COLOR_TYPE_PALETTE:
416 switch (bit_depth)
418 case 1:
419 case 2:
420 case 4:
421 case 8: png_ptr->channels = 1; break;
422 default: png_error(png_ptr, "Invalid bit depth for paletted image");
424 break;
425 case PNG_COLOR_TYPE_GRAY_ALPHA:
426 if (bit_depth != 8 && bit_depth != 16)
427 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
428 png_ptr->channels = 2;
429 break;
430 case PNG_COLOR_TYPE_RGB_ALPHA:
431 if (bit_depth != 8 && bit_depth != 16)
432 png_error(png_ptr, "Invalid bit depth for RGBA image");
433 png_ptr->channels = 4;
434 break;
435 default:
436 png_error(png_ptr, "Invalid image color type specified");
439 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
441 png_warning(png_ptr, "Invalid compression type specified");
442 compression_type = PNG_COMPRESSION_TYPE_BASE;
445 /* Write filter_method 64 (intrapixel differencing) only if
446 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
447 * 2. Libpng did not write a PNG signature (this filter_method is only
448 * used in PNG datastreams that are embedded in MNG datastreams) and
449 * 3. The application called png_permit_mng_features with a mask that
450 * included PNG_FLAG_MNG_FILTER_64 and
451 * 4. The filter_method is 64 and
452 * 5. The color_type is RGB or RGBA
454 if (
455 #if defined(PNG_MNG_FEATURES_SUPPORTED)
456 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
457 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
458 (color_type == PNG_COLOR_TYPE_RGB ||
459 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
460 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
461 #endif
462 filter_type != PNG_FILTER_TYPE_BASE)
464 png_warning(png_ptr, "Invalid filter type specified");
465 filter_type = PNG_FILTER_TYPE_BASE;
468 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
469 if (interlace_type != PNG_INTERLACE_NONE &&
470 interlace_type != PNG_INTERLACE_ADAM7)
472 png_warning(png_ptr, "Invalid interlace type specified");
473 interlace_type = PNG_INTERLACE_ADAM7;
475 #else
476 interlace_type=PNG_INTERLACE_NONE;
477 #endif
479 /* save off the relevent information */
480 png_ptr->bit_depth = (png_byte)bit_depth;
481 png_ptr->color_type = (png_byte)color_type;
482 png_ptr->interlaced = (png_byte)interlace_type;
483 #if defined(PNG_MNG_FEATURES_SUPPORTED)
484 png_ptr->filter_type = (png_byte)filter_type;
485 #endif
486 png_ptr->compression_type = (png_byte)compression_type;
487 png_ptr->width = width;
488 png_ptr->height = height;
490 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
491 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
492 /* set the usr info, so any transformations can modify it */
493 png_ptr->usr_width = png_ptr->width;
494 png_ptr->usr_bit_depth = png_ptr->bit_depth;
495 png_ptr->usr_channels = png_ptr->channels;
497 /* pack the header information into the buffer */
498 png_save_uint_32(buf, width);
499 png_save_uint_32(buf + 4, height);
500 buf[8] = (png_byte)bit_depth;
501 buf[9] = (png_byte)color_type;
502 buf[10] = (png_byte)compression_type;
503 buf[11] = (png_byte)filter_type;
504 buf[12] = (png_byte)interlace_type;
506 /* write the chunk */
507 png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
509 #if defined(PNG_WRITE_APNG_SUPPORTED)
510 png_ptr->first_frame_width = width;
511 png_ptr->first_frame_height = height;
512 #endif
514 /* initialize zlib with PNG info */
515 png_ptr->zstream.zalloc = png_zalloc;
516 png_ptr->zstream.zfree = png_zfree;
517 png_ptr->zstream.opaque = (voidpf)png_ptr;
518 if (!(png_ptr->do_filter))
520 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
521 png_ptr->bit_depth < 8)
522 png_ptr->do_filter = PNG_FILTER_NONE;
523 else
524 png_ptr->do_filter = PNG_ALL_FILTERS;
526 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
528 if (png_ptr->do_filter != PNG_FILTER_NONE)
529 png_ptr->zlib_strategy = Z_FILTERED;
530 else
531 png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
533 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
534 png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
535 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
536 png_ptr->zlib_mem_level = 8;
537 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
538 png_ptr->zlib_window_bits = 15;
539 if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
540 png_ptr->zlib_method = 8;
541 ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
542 png_ptr->zlib_method, png_ptr->zlib_window_bits,
543 png_ptr->zlib_mem_level, png_ptr->zlib_strategy);
544 if (ret != Z_OK)
546 if (ret == Z_VERSION_ERROR) png_error(png_ptr,
547 "zlib failed to initialize compressor -- version error");
548 if (ret == Z_STREAM_ERROR) png_error(png_ptr,
549 "zlib failed to initialize compressor -- stream error");
550 if (ret == Z_MEM_ERROR) png_error(png_ptr,
551 "zlib failed to initialize compressor -- mem error");
552 png_error(png_ptr, "zlib failed to initialize compressor");
554 png_ptr->zstream.next_out = png_ptr->zbuf;
555 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
556 /* libpng is not interested in zstream.data_type */
557 /* set it to a predefined value, to avoid its evaluation inside zlib */
558 png_ptr->zstream.data_type = Z_BINARY;
560 png_ptr->mode = PNG_HAVE_IHDR;
563 /* write the palette. We are careful not to trust png_color to be in the
564 * correct order for PNG, so people can redefine it to any convenient
565 * structure.
567 void /* PRIVATE */
568 png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
570 #ifdef PNG_USE_LOCAL_ARRAYS
571 PNG_PLTE;
572 #endif
573 png_uint_32 i;
574 png_colorp pal_ptr;
575 png_byte buf[3];
577 png_debug(1, "in png_write_PLTE\n");
578 if ((
579 #if defined(PNG_MNG_FEATURES_SUPPORTED)
580 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
581 #endif
582 num_pal == 0) || num_pal > 256)
584 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
586 png_error(png_ptr, "Invalid number of colors in palette");
588 else
590 png_warning(png_ptr, "Invalid number of colors in palette");
591 return;
595 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
597 png_warning(png_ptr,
598 "Ignoring request to write a PLTE chunk in grayscale PNG");
599 return;
602 png_ptr->num_palette = (png_uint_16)num_pal;
603 png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
605 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE,
606 (png_uint_32)(num_pal * 3));
607 #ifndef PNG_NO_POINTER_INDEXING
608 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
610 buf[0] = pal_ptr->red;
611 buf[1] = pal_ptr->green;
612 buf[2] = pal_ptr->blue;
613 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
615 #else
616 /* This is a little slower but some buggy compilers need to do this instead */
617 pal_ptr=palette;
618 for (i = 0; i < num_pal; i++)
620 buf[0] = pal_ptr[i].red;
621 buf[1] = pal_ptr[i].green;
622 buf[2] = pal_ptr[i].blue;
623 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
625 #endif
626 png_write_chunk_end(png_ptr);
627 png_ptr->mode |= PNG_HAVE_PLTE;
630 /* write an IDAT chunk */
631 void /* PRIVATE */
632 png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
634 #ifdef PNG_USE_LOCAL_ARRAYS
635 PNG_IDAT;
636 #if defined(PNG_WRITE_APNG_SUPPORTED)
637 PNG_fdAT;
638 #endif
639 #endif
640 png_debug(1, "in png_write_IDAT\n");
642 /* Optimize the CMF field in the zlib stream. */
643 /* This hack of the zlib stream is compliant to the stream specification. */
644 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
645 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
647 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
648 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
650 /* Avoid memory underflows and multiplication overflows. */
651 /* The conditions below are practically always satisfied;
652 however, they still must be checked. */
653 if (length >= 2 &&
654 png_ptr->height < 16384 && png_ptr->width < 16384)
656 png_uint_32 uncompressed_idat_size = png_ptr->height *
657 ((png_ptr->width *
658 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
659 unsigned int z_cinfo = z_cmf >> 4;
660 unsigned int half_z_window_size = 1 << (z_cinfo + 7);
661 while (uncompressed_idat_size <= half_z_window_size &&
662 half_z_window_size >= 256)
664 z_cinfo--;
665 half_z_window_size >>= 1;
667 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
668 if (data[0] != (png_byte)z_cmf)
670 data[0] = (png_byte)z_cmf;
671 data[1] &= 0xe0;
672 data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
676 else
677 png_error(png_ptr,
678 "Invalid zlib compression method or flags in IDAT");
681 #if defined(PNG_WRITE_APNG_SUPPORTED)
682 if(png_ptr->num_frames_written == 0)
683 #endif
684 png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
685 #if defined(PNG_WRITE_APNG_SUPPORTED)
686 else
688 png_byte buf[4];
690 png_write_chunk_start(png_ptr, (png_bytep)png_fdAT, 4 + length);
692 png_save_uint_32(buf, png_ptr->next_seq_num);
693 png_write_chunk_data(png_ptr, buf, 4);
695 png_write_chunk_data(png_ptr, data, length);
697 png_write_chunk_end(png_ptr);
699 png_ptr->next_seq_num++;
701 #endif
703 png_ptr->mode |= PNG_HAVE_IDAT;
706 /* write an IEND chunk */
707 void /* PRIVATE */
708 png_write_IEND(png_structp png_ptr)
710 #ifdef PNG_USE_LOCAL_ARRAYS
711 PNG_IEND;
712 #endif
713 png_debug(1, "in png_write_IEND\n");
714 png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL,
715 (png_size_t)0);
716 png_ptr->mode |= PNG_HAVE_IEND;
719 #if defined(PNG_WRITE_gAMA_SUPPORTED)
720 /* write a gAMA chunk */
721 #ifdef PNG_FLOATING_POINT_SUPPORTED
722 void /* PRIVATE */
723 png_write_gAMA(png_structp png_ptr, double file_gamma)
725 #ifdef PNG_USE_LOCAL_ARRAYS
726 PNG_gAMA;
727 #endif
728 png_uint_32 igamma;
729 png_byte buf[4];
731 png_debug(1, "in png_write_gAMA\n");
732 /* file_gamma is saved in 1/100,000ths */
733 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
734 png_save_uint_32(buf, igamma);
735 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
737 #endif
738 #ifdef PNG_FIXED_POINT_SUPPORTED
739 void /* PRIVATE */
740 png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
742 #ifdef PNG_USE_LOCAL_ARRAYS
743 PNG_gAMA;
744 #endif
745 png_byte buf[4];
747 png_debug(1, "in png_write_gAMA\n");
748 /* file_gamma is saved in 1/100,000ths */
749 png_save_uint_32(buf, (png_uint_32)file_gamma);
750 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
752 #endif
753 #endif
755 #if defined(PNG_WRITE_sRGB_SUPPORTED)
756 /* write a sRGB chunk */
757 void /* PRIVATE */
758 png_write_sRGB(png_structp png_ptr, int srgb_intent)
760 #ifdef PNG_USE_LOCAL_ARRAYS
761 PNG_sRGB;
762 #endif
763 png_byte buf[1];
765 png_debug(1, "in png_write_sRGB\n");
766 if (srgb_intent >= PNG_sRGB_INTENT_LAST)
767 png_warning(png_ptr,
768 "Invalid sRGB rendering intent specified");
769 buf[0]=(png_byte)srgb_intent;
770 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
772 #endif
774 #if defined(PNG_WRITE_iCCP_SUPPORTED)
775 /* write an iCCP chunk */
776 void /* PRIVATE */
777 png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
778 png_charp profile, int profile_len)
780 #ifdef PNG_USE_LOCAL_ARRAYS
781 PNG_iCCP;
782 #endif
783 png_size_t name_len;
784 png_charp new_name;
785 compression_state comp;
786 int embedded_profile_len = 0;
788 png_debug(1, "in png_write_iCCP\n");
790 comp.num_output_ptr = 0;
791 comp.max_output_ptr = 0;
792 comp.output_ptr = NULL;
793 comp.input = NULL;
794 comp.input_len = 0;
796 if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
797 &new_name)) == 0)
799 png_warning(png_ptr, "Empty keyword in iCCP chunk");
800 return;
803 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
804 png_warning(png_ptr, "Unknown compression type in iCCP chunk");
806 if (profile == NULL)
807 profile_len = 0;
809 if (profile_len > 3)
810 embedded_profile_len =
811 ((*( (png_bytep)profile ))<<24) |
812 ((*( (png_bytep)profile + 1))<<16) |
813 ((*( (png_bytep)profile + 2))<< 8) |
814 ((*( (png_bytep)profile + 3)) );
816 if (profile_len < embedded_profile_len)
818 png_warning(png_ptr,
819 "Embedded profile length too large in iCCP chunk");
820 return;
823 if (profile_len > embedded_profile_len)
825 png_warning(png_ptr,
826 "Truncating profile to actual length in iCCP chunk");
827 profile_len = embedded_profile_len;
830 if (profile_len)
831 profile_len = png_text_compress(png_ptr, profile,
832 (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp);
834 /* make sure we include the NULL after the name and the compression type */
835 png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
836 (png_uint_32)(name_len + profile_len + 2));
837 new_name[name_len + 1] = 0x00;
838 png_write_chunk_data(png_ptr, (png_bytep)new_name,
839 (png_size_t)(name_len + 2));
841 if (profile_len)
842 png_write_compressed_data_out(png_ptr, &comp);
844 png_write_chunk_end(png_ptr);
845 png_free(png_ptr, new_name);
847 #endif
849 #if defined(PNG_WRITE_sPLT_SUPPORTED)
850 /* write a sPLT chunk */
851 void /* PRIVATE */
852 png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
854 #ifdef PNG_USE_LOCAL_ARRAYS
855 PNG_sPLT;
856 #endif
857 png_size_t name_len;
858 png_charp new_name;
859 png_byte entrybuf[10];
860 int entry_size = (spalette->depth == 8 ? 6 : 10);
861 int palette_size = entry_size * spalette->nentries;
862 png_sPLT_entryp ep;
863 #ifdef PNG_NO_POINTER_INDEXING
864 int i;
865 #endif
867 png_debug(1, "in png_write_sPLT\n");
868 if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
869 spalette->name, &new_name))==0)
871 png_warning(png_ptr, "Empty keyword in sPLT chunk");
872 return;
875 /* make sure we include the NULL after the name */
876 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
877 (png_uint_32)(name_len + 2 + palette_size));
878 png_write_chunk_data(png_ptr, (png_bytep)new_name,
879 (png_size_t)(name_len + 1));
880 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, (png_size_t)1);
882 /* loop through each palette entry, writing appropriately */
883 #ifndef PNG_NO_POINTER_INDEXING
884 for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
886 if (spalette->depth == 8)
888 entrybuf[0] = (png_byte)ep->red;
889 entrybuf[1] = (png_byte)ep->green;
890 entrybuf[2] = (png_byte)ep->blue;
891 entrybuf[3] = (png_byte)ep->alpha;
892 png_save_uint_16(entrybuf + 4, ep->frequency);
894 else
896 png_save_uint_16(entrybuf + 0, ep->red);
897 png_save_uint_16(entrybuf + 2, ep->green);
898 png_save_uint_16(entrybuf + 4, ep->blue);
899 png_save_uint_16(entrybuf + 6, ep->alpha);
900 png_save_uint_16(entrybuf + 8, ep->frequency);
902 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
904 #else
905 ep=spalette->entries;
906 for (i=0; i>spalette->nentries; i++)
908 if (spalette->depth == 8)
910 entrybuf[0] = (png_byte)ep[i].red;
911 entrybuf[1] = (png_byte)ep[i].green;
912 entrybuf[2] = (png_byte)ep[i].blue;
913 entrybuf[3] = (png_byte)ep[i].alpha;
914 png_save_uint_16(entrybuf + 4, ep[i].frequency);
916 else
918 png_save_uint_16(entrybuf + 0, ep[i].red);
919 png_save_uint_16(entrybuf + 2, ep[i].green);
920 png_save_uint_16(entrybuf + 4, ep[i].blue);
921 png_save_uint_16(entrybuf + 6, ep[i].alpha);
922 png_save_uint_16(entrybuf + 8, ep[i].frequency);
924 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
926 #endif
928 png_write_chunk_end(png_ptr);
929 png_free(png_ptr, new_name);
931 #endif
933 #if defined(PNG_WRITE_sBIT_SUPPORTED)
934 /* write the sBIT chunk */
935 void /* PRIVATE */
936 png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
938 #ifdef PNG_USE_LOCAL_ARRAYS
939 PNG_sBIT;
940 #endif
941 png_byte buf[4];
942 png_size_t size;
944 png_debug(1, "in png_write_sBIT\n");
945 /* make sure we don't depend upon the order of PNG_COLOR_8 */
946 if (color_type & PNG_COLOR_MASK_COLOR)
948 png_byte maxbits;
950 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
951 png_ptr->usr_bit_depth);
952 if (sbit->red == 0 || sbit->red > maxbits ||
953 sbit->green == 0 || sbit->green > maxbits ||
954 sbit->blue == 0 || sbit->blue > maxbits)
956 png_warning(png_ptr, "Invalid sBIT depth specified");
957 return;
959 buf[0] = sbit->red;
960 buf[1] = sbit->green;
961 buf[2] = sbit->blue;
962 size = 3;
964 else
966 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
968 png_warning(png_ptr, "Invalid sBIT depth specified");
969 return;
971 buf[0] = sbit->gray;
972 size = 1;
975 if (color_type & PNG_COLOR_MASK_ALPHA)
977 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
979 png_warning(png_ptr, "Invalid sBIT depth specified");
980 return;
982 buf[size++] = sbit->alpha;
985 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
987 #endif
989 #if defined(PNG_WRITE_cHRM_SUPPORTED)
990 /* write the cHRM chunk */
991 #ifdef PNG_FLOATING_POINT_SUPPORTED
992 void /* PRIVATE */
993 png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
994 double red_x, double red_y, double green_x, double green_y,
995 double blue_x, double blue_y)
997 #ifdef PNG_USE_LOCAL_ARRAYS
998 PNG_cHRM;
999 #endif
1000 png_byte buf[32];
1001 png_uint_32 itemp;
1003 png_debug(1, "in png_write_cHRM\n");
1004 /* each value is saved in 1/100,000ths */
1005 if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
1006 white_x + white_y > 1.0)
1008 png_warning(png_ptr, "Invalid cHRM white point specified");
1009 #if !defined(PNG_NO_CONSOLE_IO)
1010 fprintf(stderr, "white_x=%f, white_y=%f\n", white_x, white_y);
1011 #endif
1012 return;
1014 itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
1015 png_save_uint_32(buf, itemp);
1016 itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
1017 png_save_uint_32(buf + 4, itemp);
1019 if (red_x < 0 || red_y < 0 || red_x + red_y > 1.0)
1021 png_warning(png_ptr, "Invalid cHRM red point specified");
1022 return;
1024 itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
1025 png_save_uint_32(buf + 8, itemp);
1026 itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
1027 png_save_uint_32(buf + 12, itemp);
1029 if (green_x < 0 || green_y < 0 || green_x + green_y > 1.0)
1031 png_warning(png_ptr, "Invalid cHRM green point specified");
1032 return;
1034 itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
1035 png_save_uint_32(buf + 16, itemp);
1036 itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
1037 png_save_uint_32(buf + 20, itemp);
1039 if (blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0)
1041 png_warning(png_ptr, "Invalid cHRM blue point specified");
1042 return;
1044 itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
1045 png_save_uint_32(buf + 24, itemp);
1046 itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
1047 png_save_uint_32(buf + 28, itemp);
1049 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
1051 #endif
1052 #ifdef PNG_FIXED_POINT_SUPPORTED
1053 void /* PRIVATE */
1054 png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
1055 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
1056 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
1057 png_fixed_point blue_y)
1059 #ifdef PNG_USE_LOCAL_ARRAYS
1060 PNG_cHRM;
1061 #endif
1062 png_byte buf[32];
1064 png_debug(1, "in png_write_cHRM\n");
1065 /* each value is saved in 1/100,000ths */
1066 if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
1068 png_warning(png_ptr, "Invalid fixed cHRM white point specified");
1069 #if !defined(PNG_NO_CONSOLE_IO)
1070 fprintf(stderr, "white_x=%ld, white_y=%ld\n", (unsigned long)white_x,
1071 (unsigned long)white_y);
1072 #endif
1073 return;
1075 png_save_uint_32(buf, (png_uint_32)white_x);
1076 png_save_uint_32(buf + 4, (png_uint_32)white_y);
1078 if (red_x + red_y > 100000L)
1080 png_warning(png_ptr, "Invalid cHRM fixed red point specified");
1081 return;
1083 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1084 png_save_uint_32(buf + 12, (png_uint_32)red_y);
1086 if (green_x + green_y > 100000L)
1088 png_warning(png_ptr, "Invalid fixed cHRM green point specified");
1089 return;
1091 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1092 png_save_uint_32(buf + 20, (png_uint_32)green_y);
1094 if (blue_x + blue_y > 100000L)
1096 png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
1097 return;
1099 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1100 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
1102 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
1104 #endif
1105 #endif
1107 #if defined(PNG_WRITE_tRNS_SUPPORTED)
1108 /* write the tRNS chunk */
1109 void /* PRIVATE */
1110 png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
1111 int num_trans, int color_type)
1113 #ifdef PNG_USE_LOCAL_ARRAYS
1114 PNG_tRNS;
1115 #endif
1116 png_byte buf[6];
1118 png_debug(1, "in png_write_tRNS\n");
1119 if (color_type == PNG_COLOR_TYPE_PALETTE)
1121 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
1123 png_warning(png_ptr, "Invalid number of transparent colors specified");
1124 return;
1126 /* write the chunk out as it is */
1127 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans,
1128 (png_size_t)num_trans);
1130 else if (color_type == PNG_COLOR_TYPE_GRAY)
1132 /* one 16 bit value */
1133 if (tran->gray >= (1 << png_ptr->bit_depth))
1135 png_warning(png_ptr,
1136 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1137 return;
1139 png_save_uint_16(buf, tran->gray);
1140 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
1142 else if (color_type == PNG_COLOR_TYPE_RGB)
1144 /* three 16 bit values */
1145 png_save_uint_16(buf, tran->red);
1146 png_save_uint_16(buf + 2, tran->green);
1147 png_save_uint_16(buf + 4, tran->blue);
1148 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1150 png_warning(png_ptr,
1151 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1152 return;
1154 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
1156 else
1158 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
1161 #endif
1163 #if defined(PNG_WRITE_bKGD_SUPPORTED)
1164 /* write the background chunk */
1165 void /* PRIVATE */
1166 png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
1168 #ifdef PNG_USE_LOCAL_ARRAYS
1169 PNG_bKGD;
1170 #endif
1171 png_byte buf[6];
1173 png_debug(1, "in png_write_bKGD\n");
1174 if (color_type == PNG_COLOR_TYPE_PALETTE)
1176 if (
1177 #if defined(PNG_MNG_FEATURES_SUPPORTED)
1178 (png_ptr->num_palette ||
1179 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
1180 #endif
1181 back->index > png_ptr->num_palette)
1183 png_warning(png_ptr, "Invalid background palette index");
1184 return;
1186 buf[0] = back->index;
1187 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
1189 else if (color_type & PNG_COLOR_MASK_COLOR)
1191 png_save_uint_16(buf, back->red);
1192 png_save_uint_16(buf + 2, back->green);
1193 png_save_uint_16(buf + 4, back->blue);
1194 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
1196 png_warning(png_ptr,
1197 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1198 return;
1200 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
1202 else
1204 if (back->gray >= (1 << png_ptr->bit_depth))
1206 png_warning(png_ptr,
1207 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1208 return;
1210 png_save_uint_16(buf, back->gray);
1211 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
1214 #endif
1216 #if defined(PNG_WRITE_hIST_SUPPORTED)
1217 /* write the histogram */
1218 void /* PRIVATE */
1219 png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
1221 #ifdef PNG_USE_LOCAL_ARRAYS
1222 PNG_hIST;
1223 #endif
1224 int i;
1225 png_byte buf[3];
1227 png_debug(1, "in png_write_hIST\n");
1228 if (num_hist > (int)png_ptr->num_palette)
1230 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
1231 png_ptr->num_palette);
1232 png_warning(png_ptr, "Invalid number of histogram entries specified");
1233 return;
1236 png_write_chunk_start(png_ptr, (png_bytep)png_hIST,
1237 (png_uint_32)(num_hist * 2));
1238 for (i = 0; i < num_hist; i++)
1240 png_save_uint_16(buf, hist[i]);
1241 png_write_chunk_data(png_ptr, buf, (png_size_t)2);
1243 png_write_chunk_end(png_ptr);
1245 #endif
1247 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1248 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
1249 /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1250 * and if invalid, correct the keyword rather than discarding the entire
1251 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1252 * length, forbids leading or trailing whitespace, multiple internal spaces,
1253 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
1255 * The new_key is allocated to hold the corrected keyword and must be freed
1256 * by the calling routine. This avoids problems with trying to write to
1257 * static keywords without having to have duplicate copies of the strings.
1259 png_size_t /* PRIVATE */
1260 png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
1262 png_size_t key_len;
1263 png_charp kp, dp;
1264 int kflag;
1265 int kwarn=0;
1267 png_debug(1, "in png_check_keyword\n");
1268 *new_key = NULL;
1270 if (key == NULL || (key_len = png_strlen(key)) == 0)
1272 png_warning(png_ptr, "zero length keyword");
1273 return ((png_size_t)0);
1276 png_debug1(2, "Keyword to be checked is '%s'\n", key);
1278 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
1279 if (*new_key == NULL)
1281 png_warning(png_ptr, "Out of memory while procesing keyword");
1282 return ((png_size_t)0);
1285 /* Replace non-printing characters with a blank and print a warning */
1286 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
1288 if ((png_byte)*kp < 0x20 ||
1289 ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1))
1291 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
1292 char msg[40];
1294 png_snprintf(msg, 40,
1295 "invalid keyword character 0x%02X", (png_byte)*kp);
1296 png_warning(png_ptr, msg);
1297 #else
1298 png_warning(png_ptr, "invalid character in keyword");
1299 #endif
1300 *dp = ' ';
1302 else
1304 *dp = *kp;
1307 *dp = '\0';
1309 /* Remove any trailing white space. */
1310 kp = *new_key + key_len - 1;
1311 if (*kp == ' ')
1313 png_warning(png_ptr, "trailing spaces removed from keyword");
1315 while (*kp == ' ')
1317 *(kp--) = '\0';
1318 key_len--;
1322 /* Remove any leading white space. */
1323 kp = *new_key;
1324 if (*kp == ' ')
1326 png_warning(png_ptr, "leading spaces removed from keyword");
1328 while (*kp == ' ')
1330 kp++;
1331 key_len--;
1335 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
1337 /* Remove multiple internal spaces. */
1338 for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
1340 if (*kp == ' ' && kflag == 0)
1342 *(dp++) = *kp;
1343 kflag = 1;
1345 else if (*kp == ' ')
1347 key_len--;
1348 kwarn=1;
1350 else
1352 *(dp++) = *kp;
1353 kflag = 0;
1356 *dp = '\0';
1357 if (kwarn)
1358 png_warning(png_ptr, "extra interior spaces removed from keyword");
1360 if (key_len == 0)
1362 png_free(png_ptr, *new_key);
1363 *new_key=NULL;
1364 png_warning(png_ptr, "Zero length keyword");
1367 if (key_len > 79)
1369 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
1370 new_key[79] = '\0';
1371 key_len = 79;
1374 return (key_len);
1376 #endif
1378 #if defined(PNG_WRITE_tEXt_SUPPORTED)
1379 /* write a tEXt chunk */
1380 void /* PRIVATE */
1381 png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
1382 png_size_t text_len)
1384 #ifdef PNG_USE_LOCAL_ARRAYS
1385 PNG_tEXt;
1386 #endif
1387 png_size_t key_len;
1388 png_charp new_key;
1390 png_debug(1, "in png_write_tEXt\n");
1391 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1393 png_warning(png_ptr, "Empty keyword in tEXt chunk");
1394 return;
1397 if (text == NULL || *text == '\0')
1398 text_len = 0;
1399 else
1400 text_len = png_strlen(text);
1402 /* make sure we include the 0 after the key */
1403 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt,
1404 (png_uint_32)(key_len + text_len + 1));
1406 * We leave it to the application to meet PNG-1.0 requirements on the
1407 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1408 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1409 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1411 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1412 (png_size_t)(key_len + 1));
1413 if (text_len)
1414 png_write_chunk_data(png_ptr, (png_bytep)text, (png_size_t)text_len);
1416 png_write_chunk_end(png_ptr);
1417 png_free(png_ptr, new_key);
1419 #endif
1421 #if defined(PNG_WRITE_zTXt_SUPPORTED)
1422 /* write a compressed text chunk */
1423 void /* PRIVATE */
1424 png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
1425 png_size_t text_len, int compression)
1427 #ifdef PNG_USE_LOCAL_ARRAYS
1428 PNG_zTXt;
1429 #endif
1430 png_size_t key_len;
1431 char buf[1];
1432 png_charp new_key;
1433 compression_state comp;
1435 png_debug(1, "in png_write_zTXt\n");
1437 comp.num_output_ptr = 0;
1438 comp.max_output_ptr = 0;
1439 comp.output_ptr = NULL;
1440 comp.input = NULL;
1441 comp.input_len = 0;
1443 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1445 png_warning(png_ptr, "Empty keyword in zTXt chunk");
1446 png_free(png_ptr, new_key);
1447 return;
1450 if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
1452 png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
1453 png_free(png_ptr, new_key);
1454 return;
1457 text_len = png_strlen(text);
1459 /* compute the compressed data; do it now for the length */
1460 text_len = png_text_compress(png_ptr, text, text_len, compression,
1461 &comp);
1463 /* write start of chunk */
1464 png_write_chunk_start(png_ptr, (png_bytep)png_zTXt,
1465 (png_uint_32)(key_len+text_len + 2));
1466 /* write key */
1467 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1468 (png_size_t)(key_len + 1));
1469 png_free(png_ptr, new_key);
1471 buf[0] = (png_byte)compression;
1472 /* write compression */
1473 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
1474 /* write the compressed data */
1475 png_write_compressed_data_out(png_ptr, &comp);
1477 /* close the chunk */
1478 png_write_chunk_end(png_ptr);
1480 #endif
1482 #if defined(PNG_WRITE_iTXt_SUPPORTED)
1483 /* write an iTXt chunk */
1484 void /* PRIVATE */
1485 png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
1486 png_charp lang, png_charp lang_key, png_charp text)
1488 #ifdef PNG_USE_LOCAL_ARRAYS
1489 PNG_iTXt;
1490 #endif
1491 png_size_t lang_len, key_len, lang_key_len, text_len;
1492 png_charp new_lang, new_key;
1493 png_byte cbuf[2];
1494 compression_state comp;
1496 png_debug(1, "in png_write_iTXt\n");
1498 comp.num_output_ptr = 0;
1499 comp.max_output_ptr = 0;
1500 comp.output_ptr = NULL;
1501 comp.input = NULL;
1503 if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1505 png_warning(png_ptr, "Empty keyword in iTXt chunk");
1506 return;
1508 if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
1510 png_warning(png_ptr, "Empty language field in iTXt chunk");
1511 new_lang = NULL;
1512 lang_len = 0;
1515 if (lang_key == NULL)
1516 lang_key_len = 0;
1517 else
1518 lang_key_len = png_strlen(lang_key);
1520 if (text == NULL)
1521 text_len = 0;
1522 else
1523 text_len = png_strlen(text);
1525 /* compute the compressed data; do it now for the length */
1526 text_len = png_text_compress(png_ptr, text, text_len, compression-2,
1527 &comp);
1530 /* make sure we include the compression flag, the compression byte,
1531 * and the NULs after the key, lang, and lang_key parts */
1533 png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
1534 (png_uint_32)(
1535 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1536 + key_len
1537 + lang_len
1538 + lang_key_len
1539 + text_len));
1542 * We leave it to the application to meet PNG-1.0 requirements on the
1543 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1544 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1545 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1547 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1548 (png_size_t)(key_len + 1));
1550 /* set the compression flag */
1551 if (compression == PNG_ITXT_COMPRESSION_NONE || \
1552 compression == PNG_TEXT_COMPRESSION_NONE)
1553 cbuf[0] = 0;
1554 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1555 cbuf[0] = 1;
1556 /* set the compression method */
1557 cbuf[1] = 0;
1558 png_write_chunk_data(png_ptr, cbuf, (png_size_t)2);
1560 cbuf[0] = 0;
1561 png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf),
1562 (png_size_t)(lang_len + 1));
1563 png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf),
1564 (png_size_t)(lang_key_len + 1));
1565 png_write_compressed_data_out(png_ptr, &comp);
1567 png_write_chunk_end(png_ptr);
1568 png_free(png_ptr, new_key);
1569 png_free(png_ptr, new_lang);
1571 #endif
1573 #if defined(PNG_WRITE_oFFs_SUPPORTED)
1574 /* write the oFFs chunk */
1575 void /* PRIVATE */
1576 png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
1577 int unit_type)
1579 #ifdef PNG_USE_LOCAL_ARRAYS
1580 PNG_oFFs;
1581 #endif
1582 png_byte buf[9];
1584 png_debug(1, "in png_write_oFFs\n");
1585 if (unit_type >= PNG_OFFSET_LAST)
1586 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1588 png_save_int_32(buf, x_offset);
1589 png_save_int_32(buf + 4, y_offset);
1590 buf[8] = (png_byte)unit_type;
1592 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
1594 #endif
1595 #if defined(PNG_WRITE_pCAL_SUPPORTED)
1596 /* write the pCAL chunk (described in the PNG extensions document) */
1597 void /* PRIVATE */
1598 png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1599 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1601 #ifdef PNG_USE_LOCAL_ARRAYS
1602 PNG_pCAL;
1603 #endif
1604 png_size_t purpose_len, units_len, total_len;
1605 png_uint_32p params_len;
1606 png_byte buf[10];
1607 png_charp new_purpose;
1608 int i;
1610 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
1611 if (type >= PNG_EQUATION_LAST)
1612 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1614 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
1615 png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
1616 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
1617 png_debug1(3, "pCAL units length = %d\n", (int)units_len);
1618 total_len = purpose_len + units_len + 10;
1620 params_len = (png_uint_32p)png_malloc(png_ptr,
1621 (png_uint_32)(nparams * png_sizeof(png_uint_32)));
1623 /* Find the length of each parameter, making sure we don't count the
1624 null terminator for the last parameter. */
1625 for (i = 0; i < nparams; i++)
1627 params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
1628 png_debug2(3, "pCAL parameter %d length = %lu\n", i,
1629 (unsigned long) params_len[i]);
1630 total_len += (png_size_t)params_len[i];
1633 png_debug1(3, "pCAL total length = %d\n", (int)total_len);
1634 png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
1635 png_write_chunk_data(png_ptr, (png_bytep)new_purpose,
1636 (png_size_t)purpose_len);
1637 png_save_int_32(buf, X0);
1638 png_save_int_32(buf + 4, X1);
1639 buf[8] = (png_byte)type;
1640 buf[9] = (png_byte)nparams;
1641 png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1642 png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
1644 png_free(png_ptr, new_purpose);
1646 for (i = 0; i < nparams; i++)
1648 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1649 (png_size_t)params_len[i]);
1652 png_free(png_ptr, params_len);
1653 png_write_chunk_end(png_ptr);
1655 #endif
1657 #if defined(PNG_WRITE_sCAL_SUPPORTED)
1658 /* write the sCAL chunk */
1659 #if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
1660 void /* PRIVATE */
1661 png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
1663 #ifdef PNG_USE_LOCAL_ARRAYS
1664 PNG_sCAL;
1665 #endif
1666 char buf[64];
1667 png_size_t total_len;
1669 png_debug(1, "in png_write_sCAL\n");
1671 buf[0] = (char)unit;
1672 #if defined(_WIN32_WCE)
1673 /* sprintf() function is not supported on WindowsCE */
1675 wchar_t wc_buf[32];
1676 size_t wc_len;
1677 swprintf(wc_buf, TEXT("%12.12e"), width);
1678 wc_len = wcslen(wc_buf);
1679 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + 1, wc_len, NULL, NULL);
1680 total_len = wc_len + 2;
1681 swprintf(wc_buf, TEXT("%12.12e"), height);
1682 wc_len = wcslen(wc_buf);
1683 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + total_len, wc_len,
1684 NULL, NULL);
1685 total_len += wc_len;
1687 #else
1688 png_snprintf(buf + 1, 63, "%12.12e", width);
1689 total_len = 1 + png_strlen(buf + 1) + 1;
1690 png_snprintf(buf + total_len, 64-total_len, "%12.12e", height);
1691 total_len += png_strlen(buf + total_len);
1692 #endif
1694 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
1695 png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len);
1697 #else
1698 #ifdef PNG_FIXED_POINT_SUPPORTED
1699 void /* PRIVATE */
1700 png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
1701 png_charp height)
1703 #ifdef PNG_USE_LOCAL_ARRAYS
1704 PNG_sCAL;
1705 #endif
1706 png_byte buf[64];
1707 png_size_t wlen, hlen, total_len;
1709 png_debug(1, "in png_write_sCAL_s\n");
1711 wlen = png_strlen(width);
1712 hlen = png_strlen(height);
1713 total_len = wlen + hlen + 2;
1714 if (total_len > 64)
1716 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
1717 return;
1720 buf[0] = (png_byte)unit;
1721 png_memcpy(buf + 1, width, wlen + 1); /* append the '\0' here */
1722 png_memcpy(buf + wlen + 2, height, hlen); /* do NOT append the '\0' here */
1724 png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
1725 png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len);
1727 #endif
1728 #endif
1729 #endif
1731 #if defined(PNG_WRITE_pHYs_SUPPORTED)
1732 /* write the pHYs chunk */
1733 void /* PRIVATE */
1734 png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
1735 png_uint_32 y_pixels_per_unit,
1736 int unit_type)
1738 #ifdef PNG_USE_LOCAL_ARRAYS
1739 PNG_pHYs;
1740 #endif
1741 png_byte buf[9];
1743 png_debug(1, "in png_write_pHYs\n");
1744 if (unit_type >= PNG_RESOLUTION_LAST)
1745 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1747 png_save_uint_32(buf, x_pixels_per_unit);
1748 png_save_uint_32(buf + 4, y_pixels_per_unit);
1749 buf[8] = (png_byte)unit_type;
1751 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
1753 #endif
1755 #if defined(PNG_WRITE_tIME_SUPPORTED)
1756 /* Write the tIME chunk. Use either png_convert_from_struct_tm()
1757 * or png_convert_from_time_t(), or fill in the structure yourself.
1759 void /* PRIVATE */
1760 png_write_tIME(png_structp png_ptr, png_timep mod_time)
1762 #ifdef PNG_USE_LOCAL_ARRAYS
1763 PNG_tIME;
1764 #endif
1765 png_byte buf[7];
1767 png_debug(1, "in png_write_tIME\n");
1768 if (mod_time->month > 12 || mod_time->month < 1 ||
1769 mod_time->day > 31 || mod_time->day < 1 ||
1770 mod_time->hour > 23 || mod_time->second > 60)
1772 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1773 return;
1776 png_save_uint_16(buf, mod_time->year);
1777 buf[2] = mod_time->month;
1778 buf[3] = mod_time->day;
1779 buf[4] = mod_time->hour;
1780 buf[5] = mod_time->minute;
1781 buf[6] = mod_time->second;
1783 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
1785 #endif
1787 #if defined(PNG_WRITE_APNG_SUPPORTED)
1788 void /* PRIVATE */
1789 png_write_acTL(png_structp png_ptr,
1790 png_uint_32 num_frames, png_uint_32 num_plays)
1792 #ifdef PNG_USE_LOCAL_ARRAYS
1793 PNG_acTL;
1794 #endif
1795 png_byte data[16];
1797 png_debug(1, "in png_write_acTL\n");
1799 png_ptr->num_frames_to_write = num_frames;
1801 if (png_ptr->apng_flags & PNG_FIRST_FRAME_HIDDEN)
1802 num_frames--;
1804 png_save_uint_32(data, num_frames);
1805 png_save_uint_32(data + 4, num_plays);
1807 png_write_chunk(png_ptr, (png_bytep)png_acTL, data, (png_size_t)8);
1810 void /* PRIVATE */
1811 png_write_fcTL(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
1812 png_uint_32 x_offset, png_uint_32 y_offset,
1813 png_uint_16 delay_num, png_uint_16 delay_den, png_byte dispose_op,
1814 png_byte blend_op)
1816 #ifdef PNG_USE_LOCAL_ARRAYS
1817 PNG_fcTL;
1818 #endif
1819 png_byte data[26];
1821 png_debug(1, "in png_write_fcTL\n");
1823 if (png_ptr->num_frames_written == 0 && (x_offset != 0 || y_offset != 0))
1824 png_error(png_ptr, "x and/or y offset for the first frame aren't 0\n");
1825 if (png_ptr->num_frames_written == 0 &&
1826 (width != png_ptr->first_frame_width ||
1827 height != png_ptr->first_frame_height))
1828 png_error(png_ptr, "width and/or height in the first frame's fcTL "
1829 "don't match the ones in IHDR\n");
1831 /* more error checking */
1832 png_ensure_fcTL_is_valid(png_ptr, width, height, x_offset, y_offset,
1833 delay_num, delay_den, dispose_op, blend_op);
1835 png_save_uint_32(data, png_ptr->next_seq_num);
1836 png_save_uint_32(data + 4, width);
1837 png_save_uint_32(data + 8, height);
1838 png_save_uint_32(data + 12, x_offset);
1839 png_save_uint_32(data + 16, y_offset);
1840 png_save_uint_16(data + 20, delay_num);
1841 png_save_uint_16(data + 22, delay_den);
1842 data[24] = dispose_op;
1843 data[25] = blend_op;
1845 png_write_chunk(png_ptr, (png_bytep)png_fcTL, data, (png_size_t)26);
1847 png_ptr->next_seq_num++;
1849 #endif /* PNG_WRITE_APNG_SUPPORTED */
1851 /* initializes the row writing capability of libpng */
1852 void /* PRIVATE */
1853 png_write_start_row(png_structp png_ptr)
1855 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1856 #ifdef PNG_USE_LOCAL_ARRAYS
1857 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1859 /* start of interlace block */
1860 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1862 /* offset to next interlace block */
1863 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1865 /* start of interlace block in the y direction */
1866 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
1868 /* offset to next interlace block in the y direction */
1869 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
1870 #endif
1871 #endif
1873 png_size_t buf_size;
1875 png_debug(1, "in png_write_start_row\n");
1876 buf_size = (png_size_t)(PNG_ROWBYTES(
1877 png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
1879 /* set up row buffer */
1880 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr,
1881 (png_uint_32)buf_size);
1882 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
1884 #ifndef PNG_NO_WRITE_FILTER
1885 /* set up filtering buffer, if using this filter */
1886 if (png_ptr->do_filter & PNG_FILTER_SUB)
1888 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
1889 (png_uint_32)(png_ptr->rowbytes + 1));
1890 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
1893 /* We only need to keep the previous row if we are using one of these. */
1894 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1896 /* set up previous row buffer */
1897 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr,
1898 (png_uint_32)buf_size);
1899 png_memset(png_ptr->prev_row, 0, buf_size);
1901 if (png_ptr->do_filter & PNG_FILTER_UP)
1903 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
1904 (png_uint_32)(png_ptr->rowbytes + 1));
1905 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
1908 if (png_ptr->do_filter & PNG_FILTER_AVG)
1910 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1911 (png_uint_32)(png_ptr->rowbytes + 1));
1912 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
1915 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1917 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
1918 (png_uint_32)(png_ptr->rowbytes + 1));
1919 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
1922 #endif /* PNG_NO_WRITE_FILTER */
1924 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1925 /* if interlaced, we need to set up width and height of pass */
1926 if (png_ptr->interlaced)
1928 if (!(png_ptr->transformations & PNG_INTERLACE))
1930 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1931 png_pass_ystart[0]) / png_pass_yinc[0];
1932 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
1933 png_pass_start[0]) / png_pass_inc[0];
1935 else
1937 png_ptr->num_rows = png_ptr->height;
1938 png_ptr->usr_width = png_ptr->width;
1941 else
1942 #endif
1944 png_ptr->num_rows = png_ptr->height;
1945 png_ptr->usr_width = png_ptr->width;
1947 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1948 png_ptr->zstream.next_out = png_ptr->zbuf;
1951 /* Internal use only. Called when finished processing a row of data. */
1952 void /* PRIVATE */
1953 png_write_finish_row(png_structp png_ptr)
1955 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1956 #ifdef PNG_USE_LOCAL_ARRAYS
1957 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1959 /* start of interlace block */
1960 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1962 /* offset to next interlace block */
1963 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1965 /* start of interlace block in the y direction */
1966 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
1968 /* offset to next interlace block in the y direction */
1969 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
1970 #endif
1971 #endif
1973 int ret;
1975 png_debug(1, "in png_write_finish_row\n");
1976 /* next row */
1977 png_ptr->row_number++;
1979 /* see if we are done */
1980 if (png_ptr->row_number < png_ptr->num_rows)
1981 return;
1983 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1984 /* if interlaced, go to next pass */
1985 if (png_ptr->interlaced)
1987 png_ptr->row_number = 0;
1988 if (png_ptr->transformations & PNG_INTERLACE)
1990 png_ptr->pass++;
1992 else
1994 /* loop until we find a non-zero width or height pass */
1997 png_ptr->pass++;
1998 if (png_ptr->pass >= 7)
1999 break;
2000 png_ptr->usr_width = (png_ptr->width +
2001 png_pass_inc[png_ptr->pass] - 1 -
2002 png_pass_start[png_ptr->pass]) /
2003 png_pass_inc[png_ptr->pass];
2004 png_ptr->num_rows = (png_ptr->height +
2005 png_pass_yinc[png_ptr->pass] - 1 -
2006 png_pass_ystart[png_ptr->pass]) /
2007 png_pass_yinc[png_ptr->pass];
2008 if (png_ptr->transformations & PNG_INTERLACE)
2009 break;
2010 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
2014 /* reset the row above the image for the next pass */
2015 if (png_ptr->pass < 7)
2017 if (png_ptr->prev_row != NULL)
2018 png_memset(png_ptr->prev_row, 0,
2019 (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
2020 png_ptr->usr_bit_depth, png_ptr->width)) + 1);
2021 return;
2024 #endif
2026 /* if we get here, we've just written the last row, so we need
2027 to flush the compressor */
2030 /* tell the compressor we are done */
2031 ret = deflate(&png_ptr->zstream, Z_FINISH);
2032 /* check for an error */
2033 if (ret == Z_OK)
2035 /* check to see if we need more room */
2036 if (!(png_ptr->zstream.avail_out))
2038 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
2039 png_ptr->zstream.next_out = png_ptr->zbuf;
2040 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
2043 else if (ret != Z_STREAM_END)
2045 if (png_ptr->zstream.msg != NULL)
2046 png_error(png_ptr, png_ptr->zstream.msg);
2047 else
2048 png_error(png_ptr, "zlib error");
2050 } while (ret != Z_STREAM_END);
2052 /* write any extra space */
2053 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
2055 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
2056 png_ptr->zstream.avail_out);
2059 deflateReset(&png_ptr->zstream);
2060 png_ptr->zstream.data_type = Z_BINARY;
2063 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
2064 /* Pick out the correct pixels for the interlace pass.
2065 * The basic idea here is to go through the row with a source
2066 * pointer and a destination pointer (sp and dp), and copy the
2067 * correct pixels for the pass. As the row gets compacted,
2068 * sp will always be >= dp, so we should never overwrite anything.
2069 * See the default: case for the easiest code to understand.
2071 void /* PRIVATE */
2072 png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
2074 #ifdef PNG_USE_LOCAL_ARRAYS
2075 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
2077 /* start of interlace block */
2078 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
2080 /* offset to next interlace block */
2081 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
2082 #endif
2084 png_debug(1, "in png_do_write_interlace\n");
2085 /* we don't have to do anything on the last pass (6) */
2086 #if defined(PNG_USELESS_TESTS_SUPPORTED)
2087 if (row != NULL && row_info != NULL && pass < 6)
2088 #else
2089 if (pass < 6)
2090 #endif
2092 /* each pixel depth is handled separately */
2093 switch (row_info->pixel_depth)
2095 case 1:
2097 png_bytep sp;
2098 png_bytep dp;
2099 int shift;
2100 int d;
2101 int value;
2102 png_uint_32 i;
2103 png_uint_32 row_width = row_info->width;
2105 dp = row;
2106 d = 0;
2107 shift = 7;
2108 for (i = png_pass_start[pass]; i < row_width;
2109 i += png_pass_inc[pass])
2111 sp = row + (png_size_t)(i >> 3);
2112 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
2113 d |= (value << shift);
2115 if (shift == 0)
2117 shift = 7;
2118 *dp++ = (png_byte)d;
2119 d = 0;
2121 else
2122 shift--;
2125 if (shift != 7)
2126 *dp = (png_byte)d;
2127 break;
2129 case 2:
2131 png_bytep sp;
2132 png_bytep dp;
2133 int shift;
2134 int d;
2135 int value;
2136 png_uint_32 i;
2137 png_uint_32 row_width = row_info->width;
2139 dp = row;
2140 shift = 6;
2141 d = 0;
2142 for (i = png_pass_start[pass]; i < row_width;
2143 i += png_pass_inc[pass])
2145 sp = row + (png_size_t)(i >> 2);
2146 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
2147 d |= (value << shift);
2149 if (shift == 0)
2151 shift = 6;
2152 *dp++ = (png_byte)d;
2153 d = 0;
2155 else
2156 shift -= 2;
2158 if (shift != 6)
2159 *dp = (png_byte)d;
2160 break;
2162 case 4:
2164 png_bytep sp;
2165 png_bytep dp;
2166 int shift;
2167 int d;
2168 int value;
2169 png_uint_32 i;
2170 png_uint_32 row_width = row_info->width;
2172 dp = row;
2173 shift = 4;
2174 d = 0;
2175 for (i = png_pass_start[pass]; i < row_width;
2176 i += png_pass_inc[pass])
2178 sp = row + (png_size_t)(i >> 1);
2179 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
2180 d |= (value << shift);
2182 if (shift == 0)
2184 shift = 4;
2185 *dp++ = (png_byte)d;
2186 d = 0;
2188 else
2189 shift -= 4;
2191 if (shift != 4)
2192 *dp = (png_byte)d;
2193 break;
2195 default:
2197 png_bytep sp;
2198 png_bytep dp;
2199 png_uint_32 i;
2200 png_uint_32 row_width = row_info->width;
2201 png_size_t pixel_bytes;
2203 /* start at the beginning */
2204 dp = row;
2205 /* find out how many bytes each pixel takes up */
2206 pixel_bytes = (row_info->pixel_depth >> 3);
2207 /* loop through the row, only looking at the pixels that
2208 matter */
2209 for (i = png_pass_start[pass]; i < row_width;
2210 i += png_pass_inc[pass])
2212 /* find out where the original pixel is */
2213 sp = row + (png_size_t)i * pixel_bytes;
2214 /* move the pixel */
2215 if (dp != sp)
2216 png_memcpy(dp, sp, pixel_bytes);
2217 /* next pixel */
2218 dp += pixel_bytes;
2220 break;
2223 /* set new row width */
2224 row_info->width = (row_info->width +
2225 png_pass_inc[pass] - 1 -
2226 png_pass_start[pass]) /
2227 png_pass_inc[pass];
2228 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2229 row_info->width);
2232 #endif
2234 /* This filters the row, chooses which filter to use, if it has not already
2235 * been specified by the application, and then writes the row out with the
2236 * chosen filter.
2238 #define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
2239 #define PNG_HISHIFT 10
2240 #define PNG_LOMASK ((png_uint_32)0xffffL)
2241 #define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
2242 void /* PRIVATE */
2243 png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
2245 png_bytep best_row;
2246 #ifndef PNG_NO_WRITE_FILTER
2247 png_bytep prev_row, row_buf;
2248 png_uint_32 mins, bpp;
2249 png_byte filter_to_do = png_ptr->do_filter;
2250 png_uint_32 row_bytes = row_info->rowbytes;
2251 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2252 int num_p_filters = (int)png_ptr->num_prev_filters;
2253 #endif
2255 png_debug(1, "in png_write_find_filter\n");
2256 /* find out how many bytes offset each pixel is */
2257 bpp = (row_info->pixel_depth + 7) >> 3;
2259 prev_row = png_ptr->prev_row;
2260 #endif
2261 best_row = png_ptr->row_buf;
2262 #ifndef PNG_NO_WRITE_FILTER
2263 row_buf = best_row;
2264 mins = PNG_MAXSUM;
2266 /* The prediction method we use is to find which method provides the
2267 * smallest value when summing the absolute values of the distances
2268 * from zero, using anything >= 128 as negative numbers. This is known
2269 * as the "minimum sum of absolute differences" heuristic. Other
2270 * heuristics are the "weighted minimum sum of absolute differences"
2271 * (experimental and can in theory improve compression), and the "zlib
2272 * predictive" method (not implemented yet), which does test compressions
2273 * of lines using different filter methods, and then chooses the
2274 * (series of) filter(s) that give minimum compressed data size (VERY
2275 * computationally expensive).
2277 * GRR 980525: consider also
2278 * (1) minimum sum of absolute differences from running average (i.e.,
2279 * keep running sum of non-absolute differences & count of bytes)
2280 * [track dispersion, too? restart average if dispersion too large?]
2281 * (1b) minimum sum of absolute differences from sliding average, probably
2282 * with window size <= deflate window (usually 32K)
2283 * (2) minimum sum of squared differences from zero or running average
2284 * (i.e., ~ root-mean-square approach)
2288 /* We don't need to test the 'no filter' case if this is the only filter
2289 * that has been chosen, as it doesn't actually do anything to the data.
2291 if ((filter_to_do & PNG_FILTER_NONE) &&
2292 filter_to_do != PNG_FILTER_NONE)
2294 png_bytep rp;
2295 png_uint_32 sum = 0;
2296 png_uint_32 i;
2297 int v;
2299 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
2301 v = *rp;
2302 sum += (v < 128) ? v : 256 - v;
2305 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2306 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2308 png_uint_32 sumhi, sumlo;
2309 int j;
2310 sumlo = sum & PNG_LOMASK;
2311 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2313 /* Reduce the sum if we match any of the previous rows */
2314 for (j = 0; j < num_p_filters; j++)
2316 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
2318 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2319 PNG_WEIGHT_SHIFT;
2320 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2321 PNG_WEIGHT_SHIFT;
2325 /* Factor in the cost of this filter (this is here for completeness,
2326 * but it makes no sense to have a "cost" for the NONE filter, as
2327 * it has the minimum possible computational cost - none).
2329 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2330 PNG_COST_SHIFT;
2331 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2332 PNG_COST_SHIFT;
2334 if (sumhi > PNG_HIMASK)
2335 sum = PNG_MAXSUM;
2336 else
2337 sum = (sumhi << PNG_HISHIFT) + sumlo;
2339 #endif
2340 mins = sum;
2343 /* sub filter */
2344 if (filter_to_do == PNG_FILTER_SUB)
2345 /* it's the only filter so no testing is needed */
2347 png_bytep rp, lp, dp;
2348 png_uint_32 i;
2349 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2350 i++, rp++, dp++)
2352 *dp = *rp;
2354 for (lp = row_buf + 1; i < row_bytes;
2355 i++, rp++, lp++, dp++)
2357 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2359 best_row = png_ptr->sub_row;
2362 else if (filter_to_do & PNG_FILTER_SUB)
2364 png_bytep rp, dp, lp;
2365 png_uint_32 sum = 0, lmins = mins;
2366 png_uint_32 i;
2367 int v;
2369 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2370 /* We temporarily increase the "minimum sum" by the factor we
2371 * would reduce the sum of this filter, so that we can do the
2372 * early exit comparison without scaling the sum each time.
2374 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2376 int j;
2377 png_uint_32 lmhi, lmlo;
2378 lmlo = lmins & PNG_LOMASK;
2379 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2381 for (j = 0; j < num_p_filters; j++)
2383 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
2385 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2386 PNG_WEIGHT_SHIFT;
2387 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2388 PNG_WEIGHT_SHIFT;
2392 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2393 PNG_COST_SHIFT;
2394 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2395 PNG_COST_SHIFT;
2397 if (lmhi > PNG_HIMASK)
2398 lmins = PNG_MAXSUM;
2399 else
2400 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2402 #endif
2404 for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2405 i++, rp++, dp++)
2407 v = *dp = *rp;
2409 sum += (v < 128) ? v : 256 - v;
2411 for (lp = row_buf + 1; i < row_bytes;
2412 i++, rp++, lp++, dp++)
2414 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2416 sum += (v < 128) ? v : 256 - v;
2418 if (sum > lmins) /* We are already worse, don't continue. */
2419 break;
2422 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2423 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2425 int j;
2426 png_uint_32 sumhi, sumlo;
2427 sumlo = sum & PNG_LOMASK;
2428 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2430 for (j = 0; j < num_p_filters; j++)
2432 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
2434 sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
2435 PNG_WEIGHT_SHIFT;
2436 sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
2437 PNG_WEIGHT_SHIFT;
2441 sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2442 PNG_COST_SHIFT;
2443 sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2444 PNG_COST_SHIFT;
2446 if (sumhi > PNG_HIMASK)
2447 sum = PNG_MAXSUM;
2448 else
2449 sum = (sumhi << PNG_HISHIFT) + sumlo;
2451 #endif
2453 if (sum < mins)
2455 mins = sum;
2456 best_row = png_ptr->sub_row;
2460 /* up filter */
2461 if (filter_to_do == PNG_FILTER_UP)
2463 png_bytep rp, dp, pp;
2464 png_uint_32 i;
2466 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
2467 pp = prev_row + 1; i < row_bytes;
2468 i++, rp++, pp++, dp++)
2470 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2472 best_row = png_ptr->up_row;
2475 else if (filter_to_do & PNG_FILTER_UP)
2477 png_bytep rp, dp, pp;
2478 png_uint_32 sum = 0, lmins = mins;
2479 png_uint_32 i;
2480 int v;
2483 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2484 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2486 int j;
2487 png_uint_32 lmhi, lmlo;
2488 lmlo = lmins & PNG_LOMASK;
2489 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2491 for (j = 0; j < num_p_filters; j++)
2493 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
2495 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2496 PNG_WEIGHT_SHIFT;
2497 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2498 PNG_WEIGHT_SHIFT;
2502 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2503 PNG_COST_SHIFT;
2504 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2505 PNG_COST_SHIFT;
2507 if (lmhi > PNG_HIMASK)
2508 lmins = PNG_MAXSUM;
2509 else
2510 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2512 #endif
2514 for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
2515 pp = prev_row + 1; i < row_bytes; i++)
2517 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2519 sum += (v < 128) ? v : 256 - v;
2521 if (sum > lmins) /* We are already worse, don't continue. */
2522 break;
2525 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2526 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2528 int j;
2529 png_uint_32 sumhi, sumlo;
2530 sumlo = sum & PNG_LOMASK;
2531 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2533 for (j = 0; j < num_p_filters; j++)
2535 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
2537 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2538 PNG_WEIGHT_SHIFT;
2539 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2540 PNG_WEIGHT_SHIFT;
2544 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2545 PNG_COST_SHIFT;
2546 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2547 PNG_COST_SHIFT;
2549 if (sumhi > PNG_HIMASK)
2550 sum = PNG_MAXSUM;
2551 else
2552 sum = (sumhi << PNG_HISHIFT) + sumlo;
2554 #endif
2556 if (sum < mins)
2558 mins = sum;
2559 best_row = png_ptr->up_row;
2563 /* avg filter */
2564 if (filter_to_do == PNG_FILTER_AVG)
2566 png_bytep rp, dp, pp, lp;
2567 png_uint_32 i;
2568 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
2569 pp = prev_row + 1; i < bpp; i++)
2571 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
2573 for (lp = row_buf + 1; i < row_bytes; i++)
2575 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2576 & 0xff);
2578 best_row = png_ptr->avg_row;
2581 else if (filter_to_do & PNG_FILTER_AVG)
2583 png_bytep rp, dp, pp, lp;
2584 png_uint_32 sum = 0, lmins = mins;
2585 png_uint_32 i;
2586 int v;
2588 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2589 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2591 int j;
2592 png_uint_32 lmhi, lmlo;
2593 lmlo = lmins & PNG_LOMASK;
2594 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2596 for (j = 0; j < num_p_filters; j++)
2598 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
2600 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2601 PNG_WEIGHT_SHIFT;
2602 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2603 PNG_WEIGHT_SHIFT;
2607 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2608 PNG_COST_SHIFT;
2609 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2610 PNG_COST_SHIFT;
2612 if (lmhi > PNG_HIMASK)
2613 lmins = PNG_MAXSUM;
2614 else
2615 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2617 #endif
2619 for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
2620 pp = prev_row + 1; i < bpp; i++)
2622 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
2624 sum += (v < 128) ? v : 256 - v;
2626 for (lp = row_buf + 1; i < row_bytes; i++)
2628 v = *dp++ =
2629 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
2631 sum += (v < 128) ? v : 256 - v;
2633 if (sum > lmins) /* We are already worse, don't continue. */
2634 break;
2637 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2638 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2640 int j;
2641 png_uint_32 sumhi, sumlo;
2642 sumlo = sum & PNG_LOMASK;
2643 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2645 for (j = 0; j < num_p_filters; j++)
2647 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
2649 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2650 PNG_WEIGHT_SHIFT;
2651 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2652 PNG_WEIGHT_SHIFT;
2656 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2657 PNG_COST_SHIFT;
2658 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2659 PNG_COST_SHIFT;
2661 if (sumhi > PNG_HIMASK)
2662 sum = PNG_MAXSUM;
2663 else
2664 sum = (sumhi << PNG_HISHIFT) + sumlo;
2666 #endif
2668 if (sum < mins)
2670 mins = sum;
2671 best_row = png_ptr->avg_row;
2675 /* Paeth filter */
2676 if (filter_to_do == PNG_FILTER_PAETH)
2678 png_bytep rp, dp, pp, cp, lp;
2679 png_uint_32 i;
2680 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
2681 pp = prev_row + 1; i < bpp; i++)
2683 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2686 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
2688 int a, b, c, pa, pb, pc, p;
2690 b = *pp++;
2691 c = *cp++;
2692 a = *lp++;
2694 p = b - c;
2695 pc = a - c;
2697 #ifdef PNG_USE_ABS
2698 pa = abs(p);
2699 pb = abs(pc);
2700 pc = abs(p + pc);
2701 #else
2702 pa = p < 0 ? -p : p;
2703 pb = pc < 0 ? -pc : pc;
2704 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
2705 #endif
2707 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2709 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
2711 best_row = png_ptr->paeth_row;
2714 else if (filter_to_do & PNG_FILTER_PAETH)
2716 png_bytep rp, dp, pp, cp, lp;
2717 png_uint_32 sum = 0, lmins = mins;
2718 png_uint_32 i;
2719 int v;
2721 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2722 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2724 int j;
2725 png_uint_32 lmhi, lmlo;
2726 lmlo = lmins & PNG_LOMASK;
2727 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2729 for (j = 0; j < num_p_filters; j++)
2731 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
2733 lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2734 PNG_WEIGHT_SHIFT;
2735 lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2736 PNG_WEIGHT_SHIFT;
2740 lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2741 PNG_COST_SHIFT;
2742 lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2743 PNG_COST_SHIFT;
2745 if (lmhi > PNG_HIMASK)
2746 lmins = PNG_MAXSUM;
2747 else
2748 lmins = (lmhi << PNG_HISHIFT) + lmlo;
2750 #endif
2752 for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
2753 pp = prev_row + 1; i < bpp; i++)
2755 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2757 sum += (v < 128) ? v : 256 - v;
2760 for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
2762 int a, b, c, pa, pb, pc, p;
2764 b = *pp++;
2765 c = *cp++;
2766 a = *lp++;
2768 #ifndef PNG_SLOW_PAETH
2769 p = b - c;
2770 pc = a - c;
2771 #ifdef PNG_USE_ABS
2772 pa = abs(p);
2773 pb = abs(pc);
2774 pc = abs(p + pc);
2775 #else
2776 pa = p < 0 ? -p : p;
2777 pb = pc < 0 ? -pc : pc;
2778 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
2779 #endif
2780 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2781 #else /* PNG_SLOW_PAETH */
2782 p = a + b - c;
2783 pa = abs(p - a);
2784 pb = abs(p - b);
2785 pc = abs(p - c);
2786 if (pa <= pb && pa <= pc)
2787 p = a;
2788 else if (pb <= pc)
2789 p = b;
2790 else
2791 p = c;
2792 #endif /* PNG_SLOW_PAETH */
2794 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
2796 sum += (v < 128) ? v : 256 - v;
2798 if (sum > lmins) /* We are already worse, don't continue. */
2799 break;
2802 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2803 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2805 int j;
2806 png_uint_32 sumhi, sumlo;
2807 sumlo = sum & PNG_LOMASK;
2808 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2810 for (j = 0; j < num_p_filters; j++)
2812 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
2814 sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2815 PNG_WEIGHT_SHIFT;
2816 sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2817 PNG_WEIGHT_SHIFT;
2821 sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2822 PNG_COST_SHIFT;
2823 sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2824 PNG_COST_SHIFT;
2826 if (sumhi > PNG_HIMASK)
2827 sum = PNG_MAXSUM;
2828 else
2829 sum = (sumhi << PNG_HISHIFT) + sumlo;
2831 #endif
2833 if (sum < mins)
2835 best_row = png_ptr->paeth_row;
2838 #endif /* PNG_NO_WRITE_FILTER */
2839 /* Do the actual writing of the filtered row data from the chosen filter. */
2841 png_write_filtered_row(png_ptr, best_row);
2843 #ifndef PNG_NO_WRITE_FILTER
2844 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2845 /* Save the type of filter we picked this time for future calculations */
2846 if (png_ptr->num_prev_filters > 0)
2848 int j;
2849 for (j = 1; j < num_p_filters; j++)
2851 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
2853 png_ptr->prev_filters[j] = best_row[0];
2855 #endif
2856 #endif /* PNG_NO_WRITE_FILTER */
2860 /* Do the actual writing of a previously filtered row. */
2861 void /* PRIVATE */
2862 png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2864 png_debug(1, "in png_write_filtered_row\n");
2865 png_debug1(2, "filter = %d\n", filtered_row[0]);
2866 /* set up the zlib input buffer */
2868 png_ptr->zstream.next_in = filtered_row;
2869 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
2870 /* repeat until we have compressed all the data */
2873 int ret; /* return of zlib */
2875 /* compress the data */
2876 ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
2877 /* check for compression errors */
2878 if (ret != Z_OK)
2880 if (png_ptr->zstream.msg != NULL)
2881 png_error(png_ptr, png_ptr->zstream.msg);
2882 else
2883 png_error(png_ptr, "zlib error");
2886 /* see if it is time to write another IDAT */
2887 if (!(png_ptr->zstream.avail_out))
2889 /* write the IDAT and reset the zlib output buffer */
2890 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
2891 png_ptr->zstream.next_out = png_ptr->zbuf;
2892 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
2894 /* repeat until all data has been compressed */
2895 } while (png_ptr->zstream.avail_in);
2897 /* swap the current and previous rows */
2898 if (png_ptr->prev_row != NULL)
2900 png_bytep tptr;
2902 tptr = png_ptr->prev_row;
2903 png_ptr->prev_row = png_ptr->row_buf;
2904 png_ptr->row_buf = tptr;
2907 /* finish row - updates counters and flushes zlib if last row */
2908 png_write_finish_row(png_ptr);
2910 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
2911 png_ptr->flush_rows++;
2913 if (png_ptr->flush_dist > 0 &&
2914 png_ptr->flush_rows >= png_ptr->flush_dist)
2916 png_write_flush(png_ptr);
2918 #endif
2921 #if defined(PNG_WRITE_APNG_SUPPORTED)
2922 void /* PRIVATE */
2923 png_write_reset(png_structp png_ptr)
2925 png_ptr->row_number = 0;
2926 png_ptr->pass = 0;
2927 png_ptr->mode &= ~PNG_HAVE_IDAT;
2930 void /* PRIVATE */
2931 png_write_reinit(png_structp png_ptr, png_infop info_ptr,
2932 png_uint_32 width, png_uint_32 height)
2934 if (png_ptr->num_frames_written == 0 &&
2935 (width != png_ptr->first_frame_width ||
2936 height != png_ptr->first_frame_height))
2937 png_error(png_ptr, "width and/or height in the first frame's fcTL "
2938 "don't match the ones in IHDR\n");
2939 if (width > png_ptr->first_frame_width ||
2940 height > png_ptr->first_frame_height)
2941 png_error(png_ptr, "width and/or height for a frame greater than"
2942 "the ones in IHDR");
2944 png_set_IHDR(png_ptr, info_ptr, width, height,
2945 info_ptr->bit_depth, info_ptr->color_type,
2946 info_ptr->interlace_type, info_ptr->compression_type,
2947 info_ptr->filter_type);
2949 png_ptr->width = width;
2950 png_ptr->height = height;
2951 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
2952 png_ptr->usr_width = png_ptr->width;
2954 #endif
2955 #endif /* PNG_WRITE_SUPPORTED */