Bug 1874684 - Part 28: Return DateDuration from DifferenceISODateTime. r=mgaudet
[gecko.git] / media / libpng / pngwutil.c
blobee354070ccfb84087f582d875f64a0b578397ce5
2 /* pngwutil.c - utilities to write a PNG file
4 * Copyright (c) 2018-2024 Cosmin Truta
5 * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
6 * Copyright (c) 1996-1997 Andreas Dilger
7 * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
14 #include "pngpriv.h"
16 #ifdef PNG_WRITE_SUPPORTED
18 #ifdef PNG_WRITE_INT_FUNCTIONS_SUPPORTED
19 /* Place a 32-bit number into a buffer in PNG byte order. We work
20 * with unsigned numbers for convenience, although one supported
21 * ancillary chunk uses signed (two's complement) numbers.
23 void PNGAPI
24 png_save_uint_32(png_bytep buf, png_uint_32 i)
26 buf[0] = (png_byte)((i >> 24) & 0xffU);
27 buf[1] = (png_byte)((i >> 16) & 0xffU);
28 buf[2] = (png_byte)((i >> 8) & 0xffU);
29 buf[3] = (png_byte)( i & 0xffU);
32 /* Place a 16-bit number into a buffer in PNG byte order.
33 * The parameter is declared unsigned int, not png_uint_16,
34 * just to avoid potential problems on pre-ANSI C compilers.
36 void PNGAPI
37 png_save_uint_16(png_bytep buf, unsigned int i)
39 buf[0] = (png_byte)((i >> 8) & 0xffU);
40 buf[1] = (png_byte)( i & 0xffU);
42 #endif
44 /* Simple function to write the signature. If we have already written
45 * the magic bytes of the signature, or more likely, the PNG stream is
46 * being embedded into another stream and doesn't need its own signature,
47 * we should call png_set_sig_bytes() to tell libpng how many of the
48 * bytes have already been written.
50 void PNGAPI
51 png_write_sig(png_structrp png_ptr)
53 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
55 #ifdef PNG_IO_STATE_SUPPORTED
56 /* Inform the I/O callback that the signature is being written */
57 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_SIGNATURE;
58 #endif
60 /* Write the rest of the 8 byte signature */
61 png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
62 (size_t)(8 - png_ptr->sig_bytes));
64 if (png_ptr->sig_bytes < 3)
65 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
68 /* Write the start of a PNG chunk. The type is the chunk type.
69 * The total_length is the sum of the lengths of all the data you will be
70 * passing in png_write_chunk_data().
72 static void
73 png_write_chunk_header(png_structrp png_ptr, png_uint_32 chunk_name,
74 png_uint_32 length)
76 png_byte buf[8];
78 #if defined(PNG_DEBUG) && (PNG_DEBUG > 0)
79 PNG_CSTRING_FROM_CHUNK(buf, chunk_name);
80 png_debug2(0, "Writing %s chunk, length = %lu", buf, (unsigned long)length);
81 #endif
83 if (png_ptr == NULL)
84 return;
86 #ifdef PNG_IO_STATE_SUPPORTED
87 /* Inform the I/O callback that the chunk header is being written.
88 * PNG_IO_CHUNK_HDR requires a single I/O call.
90 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR;
91 #endif
93 /* Write the length and the chunk name */
94 png_save_uint_32(buf, length);
95 png_save_uint_32(buf + 4, chunk_name);
96 png_write_data(png_ptr, buf, 8);
98 /* Put the chunk name into png_ptr->chunk_name */
99 png_ptr->chunk_name = chunk_name;
101 /* Reset the crc and run it over the chunk name */
102 png_reset_crc(png_ptr);
104 png_calculate_crc(png_ptr, buf + 4, 4);
106 #ifdef PNG_IO_STATE_SUPPORTED
107 /* Inform the I/O callback that chunk data will (possibly) be written.
108 * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.
110 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA;
111 #endif
114 void PNGAPI
115 png_write_chunk_start(png_structrp png_ptr, png_const_bytep chunk_string,
116 png_uint_32 length)
118 png_write_chunk_header(png_ptr, PNG_CHUNK_FROM_STRING(chunk_string), length);
121 /* Write the data of a PNG chunk started with png_write_chunk_header().
122 * Note that multiple calls to this function are allowed, and that the
123 * sum of the lengths from these calls *must* add up to the total_length
124 * given to png_write_chunk_header().
126 void PNGAPI
127 png_write_chunk_data(png_structrp png_ptr, png_const_bytep data, size_t length)
129 /* Write the data, and run the CRC over it */
130 if (png_ptr == NULL)
131 return;
133 if (data != NULL && length > 0)
135 png_write_data(png_ptr, data, length);
137 /* Update the CRC after writing the data,
138 * in case the user I/O routine alters it.
140 png_calculate_crc(png_ptr, data, length);
144 /* Finish a chunk started with png_write_chunk_header(). */
145 void PNGAPI
146 png_write_chunk_end(png_structrp png_ptr)
148 png_byte buf[4];
150 if (png_ptr == NULL) return;
152 #ifdef PNG_IO_STATE_SUPPORTED
153 /* Inform the I/O callback that the chunk CRC is being written.
154 * PNG_IO_CHUNK_CRC requires a single I/O function call.
156 png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC;
157 #endif
159 /* Write the crc in a single operation */
160 png_save_uint_32(buf, png_ptr->crc);
162 png_write_data(png_ptr, buf, 4);
165 /* Write a PNG chunk all at once. The type is an array of ASCII characters
166 * representing the chunk name. The array must be at least 4 bytes in
167 * length, and does not need to be null terminated. To be safe, pass the
168 * pre-defined chunk names here, and if you need a new one, define it
169 * where the others are defined. The length is the length of the data.
170 * All the data must be present. If that is not possible, use the
171 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
172 * functions instead.
174 static void
175 png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name,
176 png_const_bytep data, size_t length)
178 if (png_ptr == NULL)
179 return;
181 /* On 64-bit architectures 'length' may not fit in a png_uint_32. */
182 if (length > PNG_UINT_31_MAX)
183 png_error(png_ptr, "length exceeds PNG maximum");
185 png_write_chunk_header(png_ptr, chunk_name, (png_uint_32)length);
186 png_write_chunk_data(png_ptr, data, length);
187 png_write_chunk_end(png_ptr);
190 /* This is the API that calls the internal function above. */
191 void PNGAPI
192 png_write_chunk(png_structrp png_ptr, png_const_bytep chunk_string,
193 png_const_bytep data, size_t length)
195 png_write_complete_chunk(png_ptr, PNG_CHUNK_FROM_STRING(chunk_string), data,
196 length);
199 /* This is used below to find the size of an image to pass to png_deflate_claim,
200 * so it only needs to be accurate if the size is less than 16384 bytes (the
201 * point at which a lower LZ window size can be used.)
203 static png_alloc_size_t
204 png_image_size(png_structrp png_ptr)
206 /* Only return sizes up to the maximum of a png_uint_32; do this by limiting
207 * the width and height used to 15 bits.
209 png_uint_32 h = png_ptr->height;
211 if (png_ptr->rowbytes < 32768 && h < 32768)
213 if (png_ptr->interlaced != 0)
215 /* Interlacing makes the image larger because of the replication of
216 * both the filter byte and the padding to a byte boundary.
218 png_uint_32 w = png_ptr->width;
219 unsigned int pd = png_ptr->pixel_depth;
220 png_alloc_size_t cb_base;
221 int pass;
223 for (cb_base=0, pass=0; pass<=6; ++pass)
225 png_uint_32 pw = PNG_PASS_COLS(w, pass);
227 if (pw > 0)
228 cb_base += (PNG_ROWBYTES(pd, pw)+1) * PNG_PASS_ROWS(h, pass);
231 return cb_base;
234 else
235 return (png_ptr->rowbytes+1) * h;
238 else
239 return 0xffffffffU;
242 #ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
243 /* This is the code to hack the first two bytes of the deflate stream (the
244 * deflate header) to correct the windowBits value to match the actual data
245 * size. Note that the second argument is the *uncompressed* size but the
246 * first argument is the *compressed* data (and it must be deflate
247 * compressed.)
249 static void
250 optimize_cmf(png_bytep data, png_alloc_size_t data_size)
252 /* Optimize the CMF field in the zlib stream. The resultant zlib stream is
253 * still compliant to the stream specification.
255 if (data_size <= 16384) /* else windowBits must be 15 */
257 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
259 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
261 unsigned int z_cinfo;
262 unsigned int half_z_window_size;
264 z_cinfo = z_cmf >> 4;
265 half_z_window_size = 1U << (z_cinfo + 7);
267 if (data_size <= half_z_window_size) /* else no change */
269 unsigned int tmp;
273 half_z_window_size >>= 1;
274 --z_cinfo;
276 while (z_cinfo > 0 && data_size <= half_z_window_size);
278 z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
280 data[0] = (png_byte)z_cmf;
281 tmp = data[1] & 0xe0;
282 tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f;
283 data[1] = (png_byte)tmp;
288 #endif /* WRITE_OPTIMIZE_CMF */
290 /* Initialize the compressor for the appropriate type of compression. */
291 static int
292 png_deflate_claim(png_structrp png_ptr, png_uint_32 owner,
293 png_alloc_size_t data_size)
295 if (png_ptr->zowner != 0)
297 #if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED)
298 char msg[64];
300 PNG_STRING_FROM_CHUNK(msg, owner);
301 msg[4] = ':';
302 msg[5] = ' ';
303 PNG_STRING_FROM_CHUNK(msg+6, png_ptr->zowner);
304 /* So the message that results is "<chunk> using zstream"; this is an
305 * internal error, but is very useful for debugging. i18n requirements
306 * are minimal.
308 (void)png_safecat(msg, (sizeof msg), 10, " using zstream");
309 #endif
310 #if PNG_RELEASE_BUILD
311 png_warning(png_ptr, msg);
313 /* Attempt sane error recovery */
314 if (png_ptr->zowner == png_IDAT) /* don't steal from IDAT */
316 png_ptr->zstream.msg = PNGZ_MSG_CAST("in use by IDAT");
317 return Z_STREAM_ERROR;
320 png_ptr->zowner = 0;
321 #else
322 png_error(png_ptr, msg);
323 #endif
327 int level = png_ptr->zlib_level;
328 int method = png_ptr->zlib_method;
329 int windowBits = png_ptr->zlib_window_bits;
330 int memLevel = png_ptr->zlib_mem_level;
331 int strategy; /* set below */
332 int ret; /* zlib return code */
334 if (owner == png_IDAT)
336 if ((png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY) != 0)
337 strategy = png_ptr->zlib_strategy;
339 #ifdef PNG_WRITE_FILTER_SUPPORTED
340 else if (png_ptr->do_filter != PNG_FILTER_NONE)
341 strategy = PNG_Z_DEFAULT_STRATEGY;
342 #endif
344 else
345 strategy = PNG_Z_DEFAULT_NOFILTER_STRATEGY;
348 else
350 #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
351 level = png_ptr->zlib_text_level;
352 method = png_ptr->zlib_text_method;
353 windowBits = png_ptr->zlib_text_window_bits;
354 memLevel = png_ptr->zlib_text_mem_level;
355 strategy = png_ptr->zlib_text_strategy;
356 #else
357 /* If customization is not supported the values all come from the
358 * IDAT values except for the strategy, which is fixed to the
359 * default. (This is the pre-1.6.0 behavior too, although it was
360 * implemented in a very different way.)
362 strategy = Z_DEFAULT_STRATEGY;
363 #endif
366 /* Adjust 'windowBits' down if larger than 'data_size'; to stop this
367 * happening just pass 32768 as the data_size parameter. Notice that zlib
368 * requires an extra 262 bytes in the window in addition to the data to be
369 * able to see the whole of the data, so if data_size+262 takes us to the
370 * next windowBits size we need to fix up the value later. (Because even
371 * though deflate needs the extra window, inflate does not!)
373 if (data_size <= 16384)
375 /* IMPLEMENTATION NOTE: this 'half_window_size' stuff is only here to
376 * work round a Microsoft Visual C misbehavior which, contrary to C-90,
377 * widens the result of the following shift to 64-bits if (and,
378 * apparently, only if) it is used in a test.
380 unsigned int half_window_size = 1U << (windowBits-1);
382 while (data_size + 262 <= half_window_size)
384 half_window_size >>= 1;
385 --windowBits;
389 /* Check against the previous initialized values, if any. */
390 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0 &&
391 (png_ptr->zlib_set_level != level ||
392 png_ptr->zlib_set_method != method ||
393 png_ptr->zlib_set_window_bits != windowBits ||
394 png_ptr->zlib_set_mem_level != memLevel ||
395 png_ptr->zlib_set_strategy != strategy))
397 if (deflateEnd(&png_ptr->zstream) != Z_OK)
398 png_warning(png_ptr, "deflateEnd failed (ignored)");
400 png_ptr->flags &= ~PNG_FLAG_ZSTREAM_INITIALIZED;
403 /* For safety clear out the input and output pointers (currently zlib
404 * doesn't use them on Init, but it might in the future).
406 png_ptr->zstream.next_in = NULL;
407 png_ptr->zstream.avail_in = 0;
408 png_ptr->zstream.next_out = NULL;
409 png_ptr->zstream.avail_out = 0;
411 /* Now initialize if required, setting the new parameters, otherwise just
412 * do a simple reset to the previous parameters.
414 if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
415 ret = deflateReset(&png_ptr->zstream);
417 else
419 ret = deflateInit2(&png_ptr->zstream, level, method, windowBits,
420 memLevel, strategy);
422 if (ret == Z_OK)
423 png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED;
426 /* The return code is from either deflateReset or deflateInit2; they have
427 * pretty much the same set of error codes.
429 if (ret == Z_OK)
430 png_ptr->zowner = owner;
432 else
433 png_zstream_error(png_ptr, ret);
435 return ret;
439 /* Clean up (or trim) a linked list of compression buffers. */
440 void /* PRIVATE */
441 png_free_buffer_list(png_structrp png_ptr, png_compression_bufferp *listp)
443 png_compression_bufferp list = *listp;
445 if (list != NULL)
447 *listp = NULL;
451 png_compression_bufferp next = list->next;
453 png_free(png_ptr, list);
454 list = next;
456 while (list != NULL);
460 #ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
461 /* This pair of functions encapsulates the operation of (a) compressing a
462 * text string, and (b) issuing it later as a series of chunk data writes.
463 * The compression_state structure is shared context for these functions
464 * set up by the caller to allow access to the relevant local variables.
466 * compression_buffer (new in 1.6.0) is just a linked list of zbuffer_size
467 * temporary buffers. From 1.6.0 it is retained in png_struct so that it will
468 * be correctly freed in the event of a write error (previous implementations
469 * just leaked memory.)
471 typedef struct
473 png_const_bytep input; /* The uncompressed input data */
474 png_alloc_size_t input_len; /* Its length */
475 png_uint_32 output_len; /* Final compressed length */
476 png_byte output[1024]; /* First block of output */
477 } compression_state;
479 static void
480 png_text_compress_init(compression_state *comp, png_const_bytep input,
481 png_alloc_size_t input_len)
483 comp->input = input;
484 comp->input_len = input_len;
485 comp->output_len = 0;
488 /* Compress the data in the compression state input */
489 static int
490 png_text_compress(png_structrp png_ptr, png_uint_32 chunk_name,
491 compression_state *comp, png_uint_32 prefix_len)
493 int ret;
495 /* To find the length of the output it is necessary to first compress the
496 * input. The result is buffered rather than using the two-pass algorithm
497 * that is used on the inflate side; deflate is assumed to be slower and a
498 * PNG writer is assumed to have more memory available than a PNG reader.
500 * IMPLEMENTATION NOTE: the zlib API deflateBound() can be used to find an
501 * upper limit on the output size, but it is always bigger than the input
502 * size so it is likely to be more efficient to use this linked-list
503 * approach.
505 ret = png_deflate_claim(png_ptr, chunk_name, comp->input_len);
507 if (ret != Z_OK)
508 return ret;
510 /* Set up the compression buffers, we need a loop here to avoid overflowing a
511 * uInt. Use ZLIB_IO_MAX to limit the input. The output is always limited
512 * by the output buffer size, so there is no need to check that. Since this
513 * is ANSI-C we know that an 'int', hence a uInt, is always at least 16 bits
514 * in size.
517 png_compression_bufferp *end = &png_ptr->zbuffer_list;
518 png_alloc_size_t input_len = comp->input_len; /* may be zero! */
519 png_uint_32 output_len;
521 /* zlib updates these for us: */
522 png_ptr->zstream.next_in = PNGZ_INPUT_CAST(comp->input);
523 png_ptr->zstream.avail_in = 0; /* Set below */
524 png_ptr->zstream.next_out = comp->output;
525 png_ptr->zstream.avail_out = (sizeof comp->output);
527 output_len = png_ptr->zstream.avail_out;
531 uInt avail_in = ZLIB_IO_MAX;
533 if (avail_in > input_len)
534 avail_in = (uInt)input_len;
536 input_len -= avail_in;
538 png_ptr->zstream.avail_in = avail_in;
540 if (png_ptr->zstream.avail_out == 0)
542 png_compression_buffer *next;
544 /* Chunk data is limited to 2^31 bytes in length, so the prefix
545 * length must be counted here.
547 if (output_len + prefix_len > PNG_UINT_31_MAX)
549 ret = Z_MEM_ERROR;
550 break;
553 /* Need a new (malloc'ed) buffer, but there may be one present
554 * already.
556 next = *end;
557 if (next == NULL)
559 next = png_voidcast(png_compression_bufferp, png_malloc_base
560 (png_ptr, PNG_COMPRESSION_BUFFER_SIZE(png_ptr)));
562 if (next == NULL)
564 ret = Z_MEM_ERROR;
565 break;
568 /* Link in this buffer (so that it will be freed later) */
569 next->next = NULL;
570 *end = next;
573 png_ptr->zstream.next_out = next->output;
574 png_ptr->zstream.avail_out = png_ptr->zbuffer_size;
575 output_len += png_ptr->zstream.avail_out;
577 /* Move 'end' to the next buffer pointer. */
578 end = &next->next;
581 /* Compress the data */
582 ret = deflate(&png_ptr->zstream,
583 input_len > 0 ? Z_NO_FLUSH : Z_FINISH);
585 /* Claw back input data that was not consumed (because avail_in is
586 * reset above every time round the loop).
588 input_len += png_ptr->zstream.avail_in;
589 png_ptr->zstream.avail_in = 0; /* safety */
591 while (ret == Z_OK);
593 /* There may be some space left in the last output buffer. This needs to
594 * be subtracted from output_len.
596 output_len -= png_ptr->zstream.avail_out;
597 png_ptr->zstream.avail_out = 0; /* safety */
598 comp->output_len = output_len;
600 /* Now double check the output length, put in a custom message if it is
601 * too long. Otherwise ensure the z_stream::msg pointer is set to
602 * something.
604 if (output_len + prefix_len >= PNG_UINT_31_MAX)
606 png_ptr->zstream.msg = PNGZ_MSG_CAST("compressed data too long");
607 ret = Z_MEM_ERROR;
610 else
611 png_zstream_error(png_ptr, ret);
613 /* Reset zlib for another zTXt/iTXt or image data */
614 png_ptr->zowner = 0;
616 /* The only success case is Z_STREAM_END, input_len must be 0; if not this
617 * is an internal error.
619 if (ret == Z_STREAM_END && input_len == 0)
621 #ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
622 /* Fix up the deflate header, if required */
623 optimize_cmf(comp->output, comp->input_len);
624 #endif
625 /* But Z_OK is returned, not Z_STREAM_END; this allows the claim
626 * function above to return Z_STREAM_END on an error (though it never
627 * does in the current versions of zlib.)
629 return Z_OK;
632 else
633 return ret;
637 /* Ship the compressed text out via chunk writes */
638 static void
639 png_write_compressed_data_out(png_structrp png_ptr, compression_state *comp)
641 png_uint_32 output_len = comp->output_len;
642 png_const_bytep output = comp->output;
643 png_uint_32 avail = (sizeof comp->output);
644 png_compression_buffer *next = png_ptr->zbuffer_list;
646 for (;;)
648 if (avail > output_len)
649 avail = output_len;
651 png_write_chunk_data(png_ptr, output, avail);
653 output_len -= avail;
655 if (output_len == 0 || next == NULL)
656 break;
658 avail = png_ptr->zbuffer_size;
659 output = next->output;
660 next = next->next;
663 /* This is an internal error; 'next' must have been NULL! */
664 if (output_len > 0)
665 png_error(png_ptr, "error writing ancillary chunked compressed data");
667 #endif /* WRITE_COMPRESSED_TEXT */
669 /* Write the IHDR chunk, and update the png_struct with the necessary
670 * information. Note that the rest of this code depends upon this
671 * information being correct.
673 void /* PRIVATE */
674 png_write_IHDR(png_structrp png_ptr, png_uint_32 width, png_uint_32 height,
675 int bit_depth, int color_type, int compression_type, int filter_type,
676 int interlace_type)
678 png_byte buf[13]; /* Buffer to store the IHDR info */
679 int is_invalid_depth;
681 png_debug(1, "in png_write_IHDR");
683 /* Check that we have valid input data from the application info */
684 switch (color_type)
686 case PNG_COLOR_TYPE_GRAY:
687 switch (bit_depth)
689 case 1:
690 case 2:
691 case 4:
692 case 8:
693 #ifdef PNG_WRITE_16BIT_SUPPORTED
694 case 16:
695 #endif
696 png_ptr->channels = 1; break;
698 default:
699 png_error(png_ptr,
700 "Invalid bit depth for grayscale image");
702 break;
704 case PNG_COLOR_TYPE_RGB:
705 is_invalid_depth = (bit_depth != 8);
706 #ifdef PNG_WRITE_16BIT_SUPPORTED
707 is_invalid_depth = (is_invalid_depth && bit_depth != 16);
708 #endif
709 if (is_invalid_depth)
710 png_error(png_ptr, "Invalid bit depth for RGB image");
712 png_ptr->channels = 3;
713 break;
715 case PNG_COLOR_TYPE_PALETTE:
716 switch (bit_depth)
718 case 1:
719 case 2:
720 case 4:
721 case 8:
722 png_ptr->channels = 1;
723 break;
725 default:
726 png_error(png_ptr, "Invalid bit depth for paletted image");
728 break;
730 case PNG_COLOR_TYPE_GRAY_ALPHA:
731 is_invalid_depth = (bit_depth != 8);
732 #ifdef PNG_WRITE_16BIT_SUPPORTED
733 is_invalid_depth = (is_invalid_depth && bit_depth != 16);
734 #endif
735 if (is_invalid_depth)
736 png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
738 png_ptr->channels = 2;
739 break;
741 case PNG_COLOR_TYPE_RGB_ALPHA:
742 is_invalid_depth = (bit_depth != 8);
743 #ifdef PNG_WRITE_16BIT_SUPPORTED
744 is_invalid_depth = (is_invalid_depth && bit_depth != 16);
745 #endif
746 if (is_invalid_depth)
747 png_error(png_ptr, "Invalid bit depth for RGBA image");
749 png_ptr->channels = 4;
750 break;
752 default:
753 png_error(png_ptr, "Invalid image color type specified");
756 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
758 png_warning(png_ptr, "Invalid compression type specified");
759 compression_type = PNG_COMPRESSION_TYPE_BASE;
762 /* Write filter_method 64 (intrapixel differencing) only if
763 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
764 * 2. Libpng did not write a PNG signature (this filter_method is only
765 * used in PNG datastreams that are embedded in MNG datastreams) and
766 * 3. The application called png_permit_mng_features with a mask that
767 * included PNG_FLAG_MNG_FILTER_64 and
768 * 4. The filter_method is 64 and
769 * 5. The color_type is RGB or RGBA
771 if (
772 #ifdef PNG_MNG_FEATURES_SUPPORTED
773 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
774 ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) &&
775 (color_type == PNG_COLOR_TYPE_RGB ||
776 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
777 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
778 #endif
779 filter_type != PNG_FILTER_TYPE_BASE)
781 png_warning(png_ptr, "Invalid filter type specified");
782 filter_type = PNG_FILTER_TYPE_BASE;
785 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
786 if (interlace_type != PNG_INTERLACE_NONE &&
787 interlace_type != PNG_INTERLACE_ADAM7)
789 png_warning(png_ptr, "Invalid interlace type specified");
790 interlace_type = PNG_INTERLACE_ADAM7;
792 #else
793 interlace_type=PNG_INTERLACE_NONE;
794 #endif
796 /* Save the relevant information */
797 png_ptr->bit_depth = (png_byte)bit_depth;
798 png_ptr->color_type = (png_byte)color_type;
799 png_ptr->interlaced = (png_byte)interlace_type;
800 #ifdef PNG_MNG_FEATURES_SUPPORTED
801 png_ptr->filter_type = (png_byte)filter_type;
802 #endif
803 png_ptr->compression_type = (png_byte)compression_type;
804 png_ptr->width = width;
805 png_ptr->height = height;
807 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
808 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
809 /* Set the usr info, so any transformations can modify it */
810 png_ptr->usr_width = png_ptr->width;
811 png_ptr->usr_bit_depth = png_ptr->bit_depth;
812 png_ptr->usr_channels = png_ptr->channels;
814 /* Pack the header information into the buffer */
815 png_save_uint_32(buf, width);
816 png_save_uint_32(buf + 4, height);
817 buf[8] = (png_byte)bit_depth;
818 buf[9] = (png_byte)color_type;
819 buf[10] = (png_byte)compression_type;
820 buf[11] = (png_byte)filter_type;
821 buf[12] = (png_byte)interlace_type;
823 /* Write the chunk */
824 png_write_complete_chunk(png_ptr, png_IHDR, buf, 13);
826 #ifdef PNG_WRITE_APNG_SUPPORTED
827 png_ptr->first_frame_width = width;
828 png_ptr->first_frame_height = height;
829 #endif
831 if ((png_ptr->do_filter) == PNG_NO_FILTERS)
833 #ifdef PNG_WRITE_FILTER_SUPPORTED
834 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
835 png_ptr->bit_depth < 8)
836 png_ptr->do_filter = PNG_FILTER_NONE;
838 else
839 png_ptr->do_filter = PNG_ALL_FILTERS;
840 #else
841 png_ptr->do_filter = PNG_FILTER_NONE;
842 #endif
845 png_ptr->mode = PNG_HAVE_IHDR; /* not READY_FOR_ZTXT */
848 /* Write the palette. We are careful not to trust png_color to be in the
849 * correct order for PNG, so people can redefine it to any convenient
850 * structure.
852 void /* PRIVATE */
853 png_write_PLTE(png_structrp png_ptr, png_const_colorp palette,
854 png_uint_32 num_pal)
856 png_uint_32 max_palette_length, i;
857 png_const_colorp pal_ptr;
858 png_byte buf[3];
860 png_debug(1, "in png_write_PLTE");
862 max_palette_length = (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ?
863 (1 << png_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH;
865 if ((
866 #ifdef PNG_MNG_FEATURES_SUPPORTED
867 (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0 &&
868 #endif
869 num_pal == 0) || num_pal > max_palette_length)
871 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
873 png_error(png_ptr, "Invalid number of colors in palette");
876 else
878 png_warning(png_ptr, "Invalid number of colors in palette");
879 return;
883 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
885 png_warning(png_ptr,
886 "Ignoring request to write a PLTE chunk in grayscale PNG");
888 return;
891 png_ptr->num_palette = (png_uint_16)num_pal;
892 png_debug1(3, "num_palette = %d", png_ptr->num_palette);
894 png_write_chunk_header(png_ptr, png_PLTE, (png_uint_32)(num_pal * 3));
895 #ifdef PNG_POINTER_INDEXING_SUPPORTED
897 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
899 buf[0] = pal_ptr->red;
900 buf[1] = pal_ptr->green;
901 buf[2] = pal_ptr->blue;
902 png_write_chunk_data(png_ptr, buf, 3);
905 #else
906 /* This is a little slower but some buggy compilers need to do this
907 * instead
909 pal_ptr=palette;
911 for (i = 0; i < num_pal; i++)
913 buf[0] = pal_ptr[i].red;
914 buf[1] = pal_ptr[i].green;
915 buf[2] = pal_ptr[i].blue;
916 png_write_chunk_data(png_ptr, buf, 3);
919 #endif
920 png_write_chunk_end(png_ptr);
921 png_ptr->mode |= PNG_HAVE_PLTE;
924 /* This is similar to png_text_compress, above, except that it does not require
925 * all of the data at once and, instead of buffering the compressed result,
926 * writes it as IDAT chunks. Unlike png_text_compress it *can* png_error out
927 * because it calls the write interface. As a result it does its own error
928 * reporting and does not return an error code. In the event of error it will
929 * just call png_error. The input data length may exceed 32-bits. The 'flush'
930 * parameter is exactly the same as that to deflate, with the following
931 * meanings:
933 * Z_NO_FLUSH: normal incremental output of compressed data
934 * Z_SYNC_FLUSH: do a SYNC_FLUSH, used by png_write_flush
935 * Z_FINISH: this is the end of the input, do a Z_FINISH and clean up
937 * The routine manages the acquire and release of the png_ptr->zstream by
938 * checking and (at the end) clearing png_ptr->zowner; it does some sanity
939 * checks on the 'mode' flags while doing this.
941 void /* PRIVATE */
942 png_compress_IDAT(png_structrp png_ptr, png_const_bytep input,
943 png_alloc_size_t input_len, int flush)
945 if (png_ptr->zowner != png_IDAT)
947 /* First time. Ensure we have a temporary buffer for compression and
948 * trim the buffer list if it has more than one entry to free memory.
949 * If 'WRITE_COMPRESSED_TEXT' is not set the list will never have been
950 * created at this point, but the check here is quick and safe.
952 if (png_ptr->zbuffer_list == NULL)
954 png_ptr->zbuffer_list = png_voidcast(png_compression_bufferp,
955 png_malloc(png_ptr, PNG_COMPRESSION_BUFFER_SIZE(png_ptr)));
956 png_ptr->zbuffer_list->next = NULL;
959 else
960 png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list->next);
962 /* It is a terminal error if we can't claim the zstream. */
963 if (png_deflate_claim(png_ptr, png_IDAT, png_image_size(png_ptr)) != Z_OK)
964 png_error(png_ptr, png_ptr->zstream.msg);
966 /* The output state is maintained in png_ptr->zstream, so it must be
967 * initialized here after the claim.
969 png_ptr->zstream.next_out = png_ptr->zbuffer_list->output;
970 png_ptr->zstream.avail_out = png_ptr->zbuffer_size;
973 /* Now loop reading and writing until all the input is consumed or an error
974 * terminates the operation. The _out values are maintained across calls to
975 * this function, but the input must be reset each time.
977 png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
978 png_ptr->zstream.avail_in = 0; /* set below */
979 for (;;)
981 int ret;
983 /* INPUT: from the row data */
984 uInt avail = ZLIB_IO_MAX;
986 if (avail > input_len)
987 avail = (uInt)input_len; /* safe because of the check */
989 png_ptr->zstream.avail_in = avail;
990 input_len -= avail;
992 ret = deflate(&png_ptr->zstream, input_len > 0 ? Z_NO_FLUSH : flush);
994 /* Include as-yet unconsumed input */
995 input_len += png_ptr->zstream.avail_in;
996 png_ptr->zstream.avail_in = 0;
998 /* OUTPUT: write complete IDAT chunks when avail_out drops to zero. Note
999 * that these two zstream fields are preserved across the calls, therefore
1000 * there is no need to set these up on entry to the loop.
1002 if (png_ptr->zstream.avail_out == 0)
1004 png_bytep data = png_ptr->zbuffer_list->output;
1005 uInt size = png_ptr->zbuffer_size;
1007 /* Write an IDAT containing the data then reset the buffer. The
1008 * first IDAT may need deflate header optimization.
1010 #ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
1011 if ((png_ptr->mode & PNG_HAVE_IDAT) == 0 &&
1012 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
1013 optimize_cmf(data, png_image_size(png_ptr));
1014 #endif
1016 if (size > 0)
1017 #ifdef PNG_WRITE_APNG_SUPPORTED
1019 if (png_ptr->num_frames_written == 0)
1020 #endif
1021 png_write_complete_chunk(png_ptr, png_IDAT, data, size);
1022 #ifdef PNG_WRITE_APNG_SUPPORTED
1023 else
1024 png_write_fdAT(png_ptr, data, size);
1026 #endif /* WRITE_APNG */
1028 png_ptr->mode |= PNG_HAVE_IDAT;
1030 png_ptr->zstream.next_out = data;
1031 png_ptr->zstream.avail_out = size;
1033 /* For SYNC_FLUSH or FINISH it is essential to keep calling zlib with
1034 * the same flush parameter until it has finished output, for NO_FLUSH
1035 * it doesn't matter.
1037 if (ret == Z_OK && flush != Z_NO_FLUSH)
1038 continue;
1041 /* The order of these checks doesn't matter much; it just affects which
1042 * possible error might be detected if multiple things go wrong at once.
1044 if (ret == Z_OK) /* most likely return code! */
1046 /* If all the input has been consumed then just return. If Z_FINISH
1047 * was used as the flush parameter something has gone wrong if we get
1048 * here.
1050 if (input_len == 0)
1052 if (flush == Z_FINISH)
1053 png_error(png_ptr, "Z_OK on Z_FINISH with output space");
1055 return;
1059 else if (ret == Z_STREAM_END && flush == Z_FINISH)
1061 /* This is the end of the IDAT data; any pending output must be
1062 * flushed. For small PNG files we may still be at the beginning.
1064 png_bytep data = png_ptr->zbuffer_list->output;
1065 uInt size = png_ptr->zbuffer_size - png_ptr->zstream.avail_out;
1067 #ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
1068 if ((png_ptr->mode & PNG_HAVE_IDAT) == 0 &&
1069 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
1070 optimize_cmf(data, png_image_size(png_ptr));
1071 #endif
1073 if (size > 0)
1074 #ifdef PNG_WRITE_APNG_SUPPORTED
1076 if (png_ptr->num_frames_written == 0)
1077 #endif
1078 png_write_complete_chunk(png_ptr, png_IDAT, data, size);
1079 #ifdef PNG_WRITE_APNG_SUPPORTED
1080 else
1081 png_write_fdAT(png_ptr, data, size);
1083 #endif /* WRITE_APNG */
1085 png_ptr->zstream.avail_out = 0;
1086 png_ptr->zstream.next_out = NULL;
1087 png_ptr->mode |= PNG_HAVE_IDAT | PNG_AFTER_IDAT;
1089 png_ptr->zowner = 0; /* Release the stream */
1090 return;
1093 else
1095 /* This is an error condition. */
1096 png_zstream_error(png_ptr, ret);
1097 png_error(png_ptr, png_ptr->zstream.msg);
1102 /* Write an IEND chunk */
1103 void /* PRIVATE */
1104 png_write_IEND(png_structrp png_ptr)
1106 png_debug(1, "in png_write_IEND");
1108 png_write_complete_chunk(png_ptr, png_IEND, NULL, 0);
1109 png_ptr->mode |= PNG_HAVE_IEND;
1112 #ifdef PNG_WRITE_gAMA_SUPPORTED
1113 /* Write a gAMA chunk */
1114 void /* PRIVATE */
1115 png_write_gAMA_fixed(png_structrp png_ptr, png_fixed_point file_gamma)
1117 png_byte buf[4];
1119 png_debug(1, "in png_write_gAMA");
1121 /* file_gamma is saved in 1/100,000ths */
1122 png_save_uint_32(buf, (png_uint_32)file_gamma);
1123 png_write_complete_chunk(png_ptr, png_gAMA, buf, 4);
1125 #endif
1127 #ifdef PNG_WRITE_sRGB_SUPPORTED
1128 /* Write a sRGB chunk */
1129 void /* PRIVATE */
1130 png_write_sRGB(png_structrp png_ptr, int srgb_intent)
1132 png_byte buf[1];
1134 png_debug(1, "in png_write_sRGB");
1136 if (srgb_intent >= PNG_sRGB_INTENT_LAST)
1137 png_warning(png_ptr,
1138 "Invalid sRGB rendering intent specified");
1140 buf[0]=(png_byte)srgb_intent;
1141 png_write_complete_chunk(png_ptr, png_sRGB, buf, 1);
1143 #endif
1145 #ifdef PNG_WRITE_iCCP_SUPPORTED
1146 /* Write an iCCP chunk */
1147 void /* PRIVATE */
1148 png_write_iCCP(png_structrp png_ptr, png_const_charp name,
1149 png_const_bytep profile)
1151 png_uint_32 name_len;
1152 png_uint_32 profile_len;
1153 png_byte new_name[81]; /* 1 byte for the compression byte */
1154 compression_state comp;
1155 png_uint_32 temp;
1157 png_debug(1, "in png_write_iCCP");
1159 /* These are all internal problems: the profile should have been checked
1160 * before when it was stored.
1162 if (profile == NULL)
1163 png_error(png_ptr, "No profile for iCCP chunk"); /* internal error */
1165 profile_len = png_get_uint_32(profile);
1167 if (profile_len < 132)
1168 png_error(png_ptr, "ICC profile too short");
1170 temp = (png_uint_32) (*(profile+8));
1171 if (temp > 3 && (profile_len & 0x03))
1172 png_error(png_ptr, "ICC profile length invalid (not a multiple of 4)");
1175 png_uint_32 embedded_profile_len = png_get_uint_32(profile);
1177 if (profile_len != embedded_profile_len)
1178 png_error(png_ptr, "Profile length does not match profile");
1181 name_len = png_check_keyword(png_ptr, name, new_name);
1183 if (name_len == 0)
1184 png_error(png_ptr, "iCCP: invalid keyword");
1186 new_name[++name_len] = PNG_COMPRESSION_TYPE_BASE;
1188 /* Make sure we include the NULL after the name and the compression type */
1189 ++name_len;
1191 png_text_compress_init(&comp, profile, profile_len);
1193 /* Allow for keyword terminator and compression byte */
1194 if (png_text_compress(png_ptr, png_iCCP, &comp, name_len) != Z_OK)
1195 png_error(png_ptr, png_ptr->zstream.msg);
1197 png_write_chunk_header(png_ptr, png_iCCP, name_len + comp.output_len);
1199 png_write_chunk_data(png_ptr, new_name, name_len);
1201 png_write_compressed_data_out(png_ptr, &comp);
1203 png_write_chunk_end(png_ptr);
1205 #endif
1207 #ifdef PNG_WRITE_sPLT_SUPPORTED
1208 /* Write a sPLT chunk */
1209 void /* PRIVATE */
1210 png_write_sPLT(png_structrp png_ptr, png_const_sPLT_tp spalette)
1212 png_uint_32 name_len;
1213 png_byte new_name[80];
1214 png_byte entrybuf[10];
1215 size_t entry_size = (spalette->depth == 8 ? 6 : 10);
1216 size_t palette_size = entry_size * (size_t)spalette->nentries;
1217 png_sPLT_entryp ep;
1218 #ifndef PNG_POINTER_INDEXING_SUPPORTED
1219 int i;
1220 #endif
1222 png_debug(1, "in png_write_sPLT");
1224 name_len = png_check_keyword(png_ptr, spalette->name, new_name);
1226 if (name_len == 0)
1227 png_error(png_ptr, "sPLT: invalid keyword");
1229 /* Make sure we include the NULL after the name */
1230 png_write_chunk_header(png_ptr, png_sPLT,
1231 (png_uint_32)(name_len + 2 + palette_size));
1233 png_write_chunk_data(png_ptr, (png_bytep)new_name, (size_t)(name_len + 1));
1235 png_write_chunk_data(png_ptr, &spalette->depth, 1);
1237 /* Loop through each palette entry, writing appropriately */
1238 #ifdef PNG_POINTER_INDEXING_SUPPORTED
1239 for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
1241 if (spalette->depth == 8)
1243 entrybuf[0] = (png_byte)ep->red;
1244 entrybuf[1] = (png_byte)ep->green;
1245 entrybuf[2] = (png_byte)ep->blue;
1246 entrybuf[3] = (png_byte)ep->alpha;
1247 png_save_uint_16(entrybuf + 4, ep->frequency);
1250 else
1252 png_save_uint_16(entrybuf + 0, ep->red);
1253 png_save_uint_16(entrybuf + 2, ep->green);
1254 png_save_uint_16(entrybuf + 4, ep->blue);
1255 png_save_uint_16(entrybuf + 6, ep->alpha);
1256 png_save_uint_16(entrybuf + 8, ep->frequency);
1259 png_write_chunk_data(png_ptr, entrybuf, entry_size);
1261 #else
1262 ep=spalette->entries;
1263 for (i = 0; i>spalette->nentries; i++)
1265 if (spalette->depth == 8)
1267 entrybuf[0] = (png_byte)ep[i].red;
1268 entrybuf[1] = (png_byte)ep[i].green;
1269 entrybuf[2] = (png_byte)ep[i].blue;
1270 entrybuf[3] = (png_byte)ep[i].alpha;
1271 png_save_uint_16(entrybuf + 4, ep[i].frequency);
1274 else
1276 png_save_uint_16(entrybuf + 0, ep[i].red);
1277 png_save_uint_16(entrybuf + 2, ep[i].green);
1278 png_save_uint_16(entrybuf + 4, ep[i].blue);
1279 png_save_uint_16(entrybuf + 6, ep[i].alpha);
1280 png_save_uint_16(entrybuf + 8, ep[i].frequency);
1283 png_write_chunk_data(png_ptr, entrybuf, entry_size);
1285 #endif
1287 png_write_chunk_end(png_ptr);
1289 #endif
1291 #ifdef PNG_WRITE_sBIT_SUPPORTED
1292 /* Write the sBIT chunk */
1293 void /* PRIVATE */
1294 png_write_sBIT(png_structrp png_ptr, png_const_color_8p sbit, int color_type)
1296 png_byte buf[4];
1297 size_t size;
1299 png_debug(1, "in png_write_sBIT");
1301 /* Make sure we don't depend upon the order of PNG_COLOR_8 */
1302 if ((color_type & PNG_COLOR_MASK_COLOR) != 0)
1304 png_byte maxbits;
1306 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
1307 png_ptr->usr_bit_depth);
1309 if (sbit->red == 0 || sbit->red > maxbits ||
1310 sbit->green == 0 || sbit->green > maxbits ||
1311 sbit->blue == 0 || sbit->blue > maxbits)
1313 png_warning(png_ptr, "Invalid sBIT depth specified");
1314 return;
1317 buf[0] = sbit->red;
1318 buf[1] = sbit->green;
1319 buf[2] = sbit->blue;
1320 size = 3;
1323 else
1325 if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
1327 png_warning(png_ptr, "Invalid sBIT depth specified");
1328 return;
1331 buf[0] = sbit->gray;
1332 size = 1;
1335 if ((color_type & PNG_COLOR_MASK_ALPHA) != 0)
1337 if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
1339 png_warning(png_ptr, "Invalid sBIT depth specified");
1340 return;
1343 buf[size++] = sbit->alpha;
1346 png_write_complete_chunk(png_ptr, png_sBIT, buf, size);
1348 #endif
1350 #ifdef PNG_WRITE_cHRM_SUPPORTED
1351 /* Write the cHRM chunk */
1352 void /* PRIVATE */
1353 png_write_cHRM_fixed(png_structrp png_ptr, const png_xy *xy)
1355 png_byte buf[32];
1357 png_debug(1, "in png_write_cHRM");
1359 /* Each value is saved in 1/100,000ths */
1360 png_save_int_32(buf, xy->whitex);
1361 png_save_int_32(buf + 4, xy->whitey);
1363 png_save_int_32(buf + 8, xy->redx);
1364 png_save_int_32(buf + 12, xy->redy);
1366 png_save_int_32(buf + 16, xy->greenx);
1367 png_save_int_32(buf + 20, xy->greeny);
1369 png_save_int_32(buf + 24, xy->bluex);
1370 png_save_int_32(buf + 28, xy->bluey);
1372 png_write_complete_chunk(png_ptr, png_cHRM, buf, 32);
1374 #endif
1376 #ifdef PNG_WRITE_tRNS_SUPPORTED
1377 /* Write the tRNS chunk */
1378 void /* PRIVATE */
1379 png_write_tRNS(png_structrp png_ptr, png_const_bytep trans_alpha,
1380 png_const_color_16p tran, int num_trans, int color_type)
1382 png_byte buf[6];
1384 png_debug(1, "in png_write_tRNS");
1386 if (color_type == PNG_COLOR_TYPE_PALETTE)
1388 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
1390 png_app_warning(png_ptr,
1391 "Invalid number of transparent colors specified");
1392 return;
1395 /* Write the chunk out as it is */
1396 png_write_complete_chunk(png_ptr, png_tRNS, trans_alpha,
1397 (size_t)num_trans);
1400 else if (color_type == PNG_COLOR_TYPE_GRAY)
1402 /* One 16-bit value */
1403 if (tran->gray >= (1 << png_ptr->bit_depth))
1405 png_app_warning(png_ptr,
1406 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1408 return;
1411 png_save_uint_16(buf, tran->gray);
1412 png_write_complete_chunk(png_ptr, png_tRNS, buf, 2);
1415 else if (color_type == PNG_COLOR_TYPE_RGB)
1417 /* Three 16-bit values */
1418 png_save_uint_16(buf, tran->red);
1419 png_save_uint_16(buf + 2, tran->green);
1420 png_save_uint_16(buf + 4, tran->blue);
1421 #ifdef PNG_WRITE_16BIT_SUPPORTED
1422 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]) != 0)
1423 #else
1424 if ((buf[0] | buf[2] | buf[4]) != 0)
1425 #endif
1427 png_app_warning(png_ptr,
1428 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1429 return;
1432 png_write_complete_chunk(png_ptr, png_tRNS, buf, 6);
1435 else
1437 png_app_warning(png_ptr, "Can't write tRNS with an alpha channel");
1440 #endif
1442 #ifdef PNG_WRITE_bKGD_SUPPORTED
1443 /* Write the background chunk */
1444 void /* PRIVATE */
1445 png_write_bKGD(png_structrp png_ptr, png_const_color_16p back, int color_type)
1447 png_byte buf[6];
1449 png_debug(1, "in png_write_bKGD");
1451 if (color_type == PNG_COLOR_TYPE_PALETTE)
1453 if (
1454 #ifdef PNG_MNG_FEATURES_SUPPORTED
1455 (png_ptr->num_palette != 0 ||
1456 (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0) &&
1457 #endif
1458 back->index >= png_ptr->num_palette)
1460 png_warning(png_ptr, "Invalid background palette index");
1461 return;
1464 buf[0] = back->index;
1465 png_write_complete_chunk(png_ptr, png_bKGD, buf, 1);
1468 else if ((color_type & PNG_COLOR_MASK_COLOR) != 0)
1470 png_save_uint_16(buf, back->red);
1471 png_save_uint_16(buf + 2, back->green);
1472 png_save_uint_16(buf + 4, back->blue);
1473 #ifdef PNG_WRITE_16BIT_SUPPORTED
1474 if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]) != 0)
1475 #else
1476 if ((buf[0] | buf[2] | buf[4]) != 0)
1477 #endif
1479 png_warning(png_ptr,
1480 "Ignoring attempt to write 16-bit bKGD chunk "
1481 "when bit_depth is 8");
1483 return;
1486 png_write_complete_chunk(png_ptr, png_bKGD, buf, 6);
1489 else
1491 if (back->gray >= (1 << png_ptr->bit_depth))
1493 png_warning(png_ptr,
1494 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1496 return;
1499 png_save_uint_16(buf, back->gray);
1500 png_write_complete_chunk(png_ptr, png_bKGD, buf, 2);
1503 #endif
1505 #ifdef PNG_WRITE_eXIf_SUPPORTED
1506 /* Write the Exif data */
1507 void /* PRIVATE */
1508 png_write_eXIf(png_structrp png_ptr, png_bytep exif, int num_exif)
1510 int i;
1511 png_byte buf[1];
1513 png_debug(1, "in png_write_eXIf");
1515 png_write_chunk_header(png_ptr, png_eXIf, (png_uint_32)(num_exif));
1517 for (i = 0; i < num_exif; i++)
1519 buf[0] = exif[i];
1520 png_write_chunk_data(png_ptr, buf, 1);
1523 png_write_chunk_end(png_ptr);
1525 #endif
1527 #ifdef PNG_WRITE_hIST_SUPPORTED
1528 /* Write the histogram */
1529 void /* PRIVATE */
1530 png_write_hIST(png_structrp png_ptr, png_const_uint_16p hist, int num_hist)
1532 int i;
1533 png_byte buf[3];
1535 png_debug(1, "in png_write_hIST");
1537 if (num_hist > (int)png_ptr->num_palette)
1539 png_debug2(3, "num_hist = %d, num_palette = %d", num_hist,
1540 png_ptr->num_palette);
1542 png_warning(png_ptr, "Invalid number of histogram entries specified");
1543 return;
1546 png_write_chunk_header(png_ptr, png_hIST, (png_uint_32)(num_hist * 2));
1548 for (i = 0; i < num_hist; i++)
1550 png_save_uint_16(buf, hist[i]);
1551 png_write_chunk_data(png_ptr, buf, 2);
1554 png_write_chunk_end(png_ptr);
1556 #endif
1558 #ifdef PNG_WRITE_tEXt_SUPPORTED
1559 /* Write a tEXt chunk */
1560 void /* PRIVATE */
1561 png_write_tEXt(png_structrp png_ptr, png_const_charp key, png_const_charp text,
1562 size_t text_len)
1564 png_uint_32 key_len;
1565 png_byte new_key[80];
1567 png_debug(1, "in png_write_tEXt");
1569 key_len = png_check_keyword(png_ptr, key, new_key);
1571 if (key_len == 0)
1572 png_error(png_ptr, "tEXt: invalid keyword");
1574 if (text == NULL || *text == '\0')
1575 text_len = 0;
1577 else
1578 text_len = strlen(text);
1580 if (text_len > PNG_UINT_31_MAX - (key_len+1))
1581 png_error(png_ptr, "tEXt: text too long");
1583 /* Make sure we include the 0 after the key */
1584 png_write_chunk_header(png_ptr, png_tEXt,
1585 (png_uint_32)/*checked above*/(key_len + text_len + 1));
1587 * We leave it to the application to meet PNG-1.0 requirements on the
1588 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1589 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1590 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1592 png_write_chunk_data(png_ptr, new_key, key_len + 1);
1594 if (text_len != 0)
1595 png_write_chunk_data(png_ptr, (png_const_bytep)text, text_len);
1597 png_write_chunk_end(png_ptr);
1599 #endif
1601 #ifdef PNG_WRITE_zTXt_SUPPORTED
1602 /* Write a compressed text chunk */
1603 void /* PRIVATE */
1604 png_write_zTXt(png_structrp png_ptr, png_const_charp key, png_const_charp text,
1605 int compression)
1607 png_uint_32 key_len;
1608 png_byte new_key[81];
1609 compression_state comp;
1611 png_debug(1, "in png_write_zTXt");
1613 if (compression == PNG_TEXT_COMPRESSION_NONE)
1615 png_write_tEXt(png_ptr, key, text, 0);
1616 return;
1619 if (compression != PNG_TEXT_COMPRESSION_zTXt)
1620 png_error(png_ptr, "zTXt: invalid compression type");
1622 key_len = png_check_keyword(png_ptr, key, new_key);
1624 if (key_len == 0)
1625 png_error(png_ptr, "zTXt: invalid keyword");
1627 /* Add the compression method and 1 for the keyword separator. */
1628 new_key[++key_len] = PNG_COMPRESSION_TYPE_BASE;
1629 ++key_len;
1631 /* Compute the compressed data; do it now for the length */
1632 png_text_compress_init(&comp, (png_const_bytep)text,
1633 text == NULL ? 0 : strlen(text));
1635 if (png_text_compress(png_ptr, png_zTXt, &comp, key_len) != Z_OK)
1636 png_error(png_ptr, png_ptr->zstream.msg);
1638 /* Write start of chunk */
1639 png_write_chunk_header(png_ptr, png_zTXt, key_len + comp.output_len);
1641 /* Write key */
1642 png_write_chunk_data(png_ptr, new_key, key_len);
1644 /* Write the compressed data */
1645 png_write_compressed_data_out(png_ptr, &comp);
1647 /* Close the chunk */
1648 png_write_chunk_end(png_ptr);
1650 #endif
1652 #ifdef PNG_WRITE_iTXt_SUPPORTED
1653 /* Write an iTXt chunk */
1654 void /* PRIVATE */
1655 png_write_iTXt(png_structrp png_ptr, int compression, png_const_charp key,
1656 png_const_charp lang, png_const_charp lang_key, png_const_charp text)
1658 png_uint_32 key_len, prefix_len;
1659 size_t lang_len, lang_key_len;
1660 png_byte new_key[82];
1661 compression_state comp;
1663 png_debug(1, "in png_write_iTXt");
1665 key_len = png_check_keyword(png_ptr, key, new_key);
1667 if (key_len == 0)
1668 png_error(png_ptr, "iTXt: invalid keyword");
1670 /* Set the compression flag */
1671 switch (compression)
1673 case PNG_ITXT_COMPRESSION_NONE:
1674 case PNG_TEXT_COMPRESSION_NONE:
1675 compression = new_key[++key_len] = 0; /* no compression */
1676 break;
1678 case PNG_TEXT_COMPRESSION_zTXt:
1679 case PNG_ITXT_COMPRESSION_zTXt:
1680 compression = new_key[++key_len] = 1; /* compressed */
1681 break;
1683 default:
1684 png_error(png_ptr, "iTXt: invalid compression");
1687 new_key[++key_len] = PNG_COMPRESSION_TYPE_BASE;
1688 ++key_len; /* for the keywod separator */
1690 /* We leave it to the application to meet PNG-1.0 requirements on the
1691 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1692 * any non-Latin-1 characters except for NEWLINE. ISO PNG, however,
1693 * specifies that the text is UTF-8 and this really doesn't require any
1694 * checking.
1696 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1698 * TODO: validate the language tag correctly (see the spec.)
1700 if (lang == NULL) lang = ""; /* empty language is valid */
1701 lang_len = strlen(lang)+1;
1702 if (lang_key == NULL) lang_key = ""; /* may be empty */
1703 lang_key_len = strlen(lang_key)+1;
1704 if (text == NULL) text = ""; /* may be empty */
1706 prefix_len = key_len;
1707 if (lang_len > PNG_UINT_31_MAX-prefix_len)
1708 prefix_len = PNG_UINT_31_MAX;
1709 else
1710 prefix_len = (png_uint_32)(prefix_len + lang_len);
1712 if (lang_key_len > PNG_UINT_31_MAX-prefix_len)
1713 prefix_len = PNG_UINT_31_MAX;
1714 else
1715 prefix_len = (png_uint_32)(prefix_len + lang_key_len);
1717 png_text_compress_init(&comp, (png_const_bytep)text, strlen(text));
1719 if (compression != 0)
1721 if (png_text_compress(png_ptr, png_iTXt, &comp, prefix_len) != Z_OK)
1722 png_error(png_ptr, png_ptr->zstream.msg);
1725 else
1727 if (comp.input_len > PNG_UINT_31_MAX-prefix_len)
1728 png_error(png_ptr, "iTXt: uncompressed text too long");
1730 /* So the string will fit in a chunk: */
1731 comp.output_len = (png_uint_32)/*SAFE*/comp.input_len;
1734 png_write_chunk_header(png_ptr, png_iTXt, comp.output_len + prefix_len);
1736 png_write_chunk_data(png_ptr, new_key, key_len);
1738 png_write_chunk_data(png_ptr, (png_const_bytep)lang, lang_len);
1740 png_write_chunk_data(png_ptr, (png_const_bytep)lang_key, lang_key_len);
1742 if (compression != 0)
1743 png_write_compressed_data_out(png_ptr, &comp);
1745 else
1746 png_write_chunk_data(png_ptr, (png_const_bytep)text, comp.output_len);
1748 png_write_chunk_end(png_ptr);
1750 #endif
1752 #ifdef PNG_WRITE_oFFs_SUPPORTED
1753 /* Write the oFFs chunk */
1754 void /* PRIVATE */
1755 png_write_oFFs(png_structrp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
1756 int unit_type)
1758 png_byte buf[9];
1760 png_debug(1, "in png_write_oFFs");
1762 if (unit_type >= PNG_OFFSET_LAST)
1763 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1765 png_save_int_32(buf, x_offset);
1766 png_save_int_32(buf + 4, y_offset);
1767 buf[8] = (png_byte)unit_type;
1769 png_write_complete_chunk(png_ptr, png_oFFs, buf, 9);
1771 #endif
1772 #ifdef PNG_WRITE_pCAL_SUPPORTED
1773 /* Write the pCAL chunk (described in the PNG extensions document) */
1774 void /* PRIVATE */
1775 png_write_pCAL(png_structrp png_ptr, png_charp purpose, png_int_32 X0,
1776 png_int_32 X1, int type, int nparams, png_const_charp units,
1777 png_charpp params)
1779 png_uint_32 purpose_len;
1780 size_t units_len, total_len;
1781 size_t *params_len;
1782 png_byte buf[10];
1783 png_byte new_purpose[80];
1784 int i;
1786 png_debug1(1, "in png_write_pCAL (%d parameters)", nparams);
1788 if (type >= PNG_EQUATION_LAST)
1789 png_error(png_ptr, "Unrecognized equation type for pCAL chunk");
1791 purpose_len = png_check_keyword(png_ptr, purpose, new_purpose);
1793 if (purpose_len == 0)
1794 png_error(png_ptr, "pCAL: invalid keyword");
1796 ++purpose_len; /* terminator */
1798 png_debug1(3, "pCAL purpose length = %d", (int)purpose_len);
1799 units_len = strlen(units) + (nparams == 0 ? 0 : 1);
1800 png_debug1(3, "pCAL units length = %d", (int)units_len);
1801 total_len = purpose_len + units_len + 10;
1803 params_len = (size_t *)png_malloc(png_ptr,
1804 (png_alloc_size_t)((png_alloc_size_t)nparams * (sizeof (size_t))));
1806 /* Find the length of each parameter, making sure we don't count the
1807 * null terminator for the last parameter.
1809 for (i = 0; i < nparams; i++)
1811 params_len[i] = strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
1812 png_debug2(3, "pCAL parameter %d length = %lu", i,
1813 (unsigned long)params_len[i]);
1814 total_len += params_len[i];
1817 png_debug1(3, "pCAL total length = %d", (int)total_len);
1818 png_write_chunk_header(png_ptr, png_pCAL, (png_uint_32)total_len);
1819 png_write_chunk_data(png_ptr, new_purpose, purpose_len);
1820 png_save_int_32(buf, X0);
1821 png_save_int_32(buf + 4, X1);
1822 buf[8] = (png_byte)type;
1823 buf[9] = (png_byte)nparams;
1824 png_write_chunk_data(png_ptr, buf, 10);
1825 png_write_chunk_data(png_ptr, (png_const_bytep)units, (size_t)units_len);
1827 for (i = 0; i < nparams; i++)
1829 png_write_chunk_data(png_ptr, (png_const_bytep)params[i], params_len[i]);
1832 png_free(png_ptr, params_len);
1833 png_write_chunk_end(png_ptr);
1835 #endif
1837 #ifdef PNG_WRITE_sCAL_SUPPORTED
1838 /* Write the sCAL chunk */
1839 void /* PRIVATE */
1840 png_write_sCAL_s(png_structrp png_ptr, int unit, png_const_charp width,
1841 png_const_charp height)
1843 png_byte buf[64];
1844 size_t wlen, hlen, total_len;
1846 png_debug(1, "in png_write_sCAL_s");
1848 wlen = strlen(width);
1849 hlen = strlen(height);
1850 total_len = wlen + hlen + 2;
1852 if (total_len > 64)
1854 png_warning(png_ptr, "Can't write sCAL (buffer too small)");
1855 return;
1858 buf[0] = (png_byte)unit;
1859 memcpy(buf + 1, width, wlen + 1); /* Append the '\0' here */
1860 memcpy(buf + wlen + 2, height, hlen); /* Do NOT append the '\0' here */
1862 png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
1863 png_write_complete_chunk(png_ptr, png_sCAL, buf, total_len);
1865 #endif
1867 #ifdef PNG_WRITE_pHYs_SUPPORTED
1868 /* Write the pHYs chunk */
1869 void /* PRIVATE */
1870 png_write_pHYs(png_structrp png_ptr, png_uint_32 x_pixels_per_unit,
1871 png_uint_32 y_pixels_per_unit,
1872 int unit_type)
1874 png_byte buf[9];
1876 png_debug(1, "in png_write_pHYs");
1878 if (unit_type >= PNG_RESOLUTION_LAST)
1879 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1881 png_save_uint_32(buf, x_pixels_per_unit);
1882 png_save_uint_32(buf + 4, y_pixels_per_unit);
1883 buf[8] = (png_byte)unit_type;
1885 png_write_complete_chunk(png_ptr, png_pHYs, buf, 9);
1887 #endif
1889 #ifdef PNG_WRITE_tIME_SUPPORTED
1890 /* Write the tIME chunk. Use either png_convert_from_struct_tm()
1891 * or png_convert_from_time_t(), or fill in the structure yourself.
1893 void /* PRIVATE */
1894 png_write_tIME(png_structrp png_ptr, png_const_timep mod_time)
1896 png_byte buf[7];
1898 png_debug(1, "in png_write_tIME");
1900 if (mod_time->month > 12 || mod_time->month < 1 ||
1901 mod_time->day > 31 || mod_time->day < 1 ||
1902 mod_time->hour > 23 || mod_time->second > 60)
1904 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1905 return;
1908 png_save_uint_16(buf, mod_time->year);
1909 buf[2] = mod_time->month;
1910 buf[3] = mod_time->day;
1911 buf[4] = mod_time->hour;
1912 buf[5] = mod_time->minute;
1913 buf[6] = mod_time->second;
1915 png_write_complete_chunk(png_ptr, png_tIME, buf, 7);
1917 #endif
1919 #ifdef PNG_WRITE_APNG_SUPPORTED
1920 void /* PRIVATE */
1921 png_write_acTL(png_structp png_ptr,
1922 png_uint_32 num_frames, png_uint_32 num_plays)
1924 png_byte buf[8];
1926 png_debug(1, "in png_write_acTL");
1928 png_ptr->num_frames_to_write = num_frames;
1930 if ((png_ptr->apng_flags & PNG_FIRST_FRAME_HIDDEN) != 0)
1931 num_frames--;
1933 png_save_uint_32(buf, num_frames);
1934 png_save_uint_32(buf + 4, num_plays);
1936 png_write_complete_chunk(png_ptr, png_acTL, buf, (png_size_t)8);
1939 void /* PRIVATE */
1940 png_write_fcTL(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
1941 png_uint_32 x_offset, png_uint_32 y_offset,
1942 png_uint_16 delay_num, png_uint_16 delay_den, png_byte dispose_op,
1943 png_byte blend_op)
1945 png_byte buf[26];
1947 png_debug(1, "in png_write_fcTL");
1949 if (png_ptr->num_frames_written == 0 && (x_offset != 0 || y_offset != 0))
1950 png_error(png_ptr, "x and/or y offset for the first frame aren't 0");
1951 if (png_ptr->num_frames_written == 0 &&
1952 (width != png_ptr->first_frame_width ||
1953 height != png_ptr->first_frame_height))
1954 png_error(png_ptr, "width and/or height in the first frame's fcTL "
1955 "don't match the ones in IHDR");
1957 /* more error checking */
1958 png_ensure_fcTL_is_valid(png_ptr, width, height, x_offset, y_offset,
1959 delay_num, delay_den, dispose_op, blend_op);
1961 png_save_uint_32(buf, png_ptr->next_seq_num);
1962 png_save_uint_32(buf + 4, width);
1963 png_save_uint_32(buf + 8, height);
1964 png_save_uint_32(buf + 12, x_offset);
1965 png_save_uint_32(buf + 16, y_offset);
1966 png_save_uint_16(buf + 20, delay_num);
1967 png_save_uint_16(buf + 22, delay_den);
1968 buf[24] = dispose_op;
1969 buf[25] = blend_op;
1971 png_write_complete_chunk(png_ptr, png_fcTL, buf, (png_size_t)26);
1973 png_ptr->next_seq_num++;
1976 void /* PRIVATE */
1977 png_write_fdAT(png_structp png_ptr,
1978 png_const_bytep data, png_size_t length)
1980 png_byte buf[4];
1982 png_write_chunk_header(png_ptr, png_fdAT, (png_uint_32)(4 + length));
1984 png_save_uint_32(buf, png_ptr->next_seq_num);
1985 png_write_chunk_data(png_ptr, buf, 4);
1987 png_write_chunk_data(png_ptr, data, length);
1989 png_write_chunk_end(png_ptr);
1991 png_ptr->next_seq_num++;
1993 #endif /* WRITE_APNG */
1995 /* Initializes the row writing capability of libpng */
1996 void /* PRIVATE */
1997 png_write_start_row(png_structrp png_ptr)
1999 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
2000 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
2002 /* Start of interlace block */
2003 static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
2005 /* Offset to next interlace block */
2006 static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
2008 /* Start of interlace block in the y direction */
2009 static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
2011 /* Offset to next interlace block in the y direction */
2012 static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
2013 #endif
2015 png_alloc_size_t buf_size;
2016 int usr_pixel_depth;
2018 #ifdef PNG_WRITE_FILTER_SUPPORTED
2019 png_byte filters;
2020 #endif
2022 png_debug(1, "in png_write_start_row");
2024 usr_pixel_depth = png_ptr->usr_channels * png_ptr->usr_bit_depth;
2025 buf_size = PNG_ROWBYTES(usr_pixel_depth, png_ptr->width) + 1;
2027 /* 1.5.6: added to allow checking in the row write code. */
2028 png_ptr->transformed_pixel_depth = png_ptr->pixel_depth;
2029 png_ptr->maximum_pixel_depth = (png_byte)usr_pixel_depth;
2031 /* Set up row buffer */
2032 png_ptr->row_buf = png_voidcast(png_bytep, png_malloc(png_ptr, buf_size));
2034 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
2036 #ifdef PNG_WRITE_FILTER_SUPPORTED
2037 filters = png_ptr->do_filter;
2039 if (png_ptr->height == 1)
2040 filters &= 0xff & ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH);
2042 if (png_ptr->width == 1)
2043 filters &= 0xff & ~(PNG_FILTER_SUB|PNG_FILTER_AVG|PNG_FILTER_PAETH);
2045 if (filters == 0)
2046 filters = PNG_FILTER_NONE;
2048 png_ptr->do_filter = filters;
2050 if (((filters & (PNG_FILTER_SUB | PNG_FILTER_UP | PNG_FILTER_AVG |
2051 PNG_FILTER_PAETH)) != 0) && png_ptr->try_row == NULL)
2053 int num_filters = 0;
2055 png_ptr->try_row = png_voidcast(png_bytep, png_malloc(png_ptr, buf_size));
2057 if (filters & PNG_FILTER_SUB)
2058 num_filters++;
2060 if (filters & PNG_FILTER_UP)
2061 num_filters++;
2063 if (filters & PNG_FILTER_AVG)
2064 num_filters++;
2066 if (filters & PNG_FILTER_PAETH)
2067 num_filters++;
2069 if (num_filters > 1)
2070 png_ptr->tst_row = png_voidcast(png_bytep, png_malloc(png_ptr,
2071 buf_size));
2074 /* We only need to keep the previous row if we are using one of the following
2075 * filters.
2077 if ((filters & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH)) != 0)
2078 png_ptr->prev_row = png_voidcast(png_bytep,
2079 png_calloc(png_ptr, buf_size));
2080 #endif /* WRITE_FILTER */
2082 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
2083 /* If interlaced, we need to set up width and height of pass */
2084 if (png_ptr->interlaced != 0)
2086 if ((png_ptr->transformations & PNG_INTERLACE) == 0)
2088 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
2089 png_pass_ystart[0]) / png_pass_yinc[0];
2091 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
2092 png_pass_start[0]) / png_pass_inc[0];
2095 else
2097 png_ptr->num_rows = png_ptr->height;
2098 png_ptr->usr_width = png_ptr->width;
2102 else
2103 #endif
2105 png_ptr->num_rows = png_ptr->height;
2106 png_ptr->usr_width = png_ptr->width;
2110 /* Internal use only. Called when finished processing a row of data. */
2111 void /* PRIVATE */
2112 png_write_finish_row(png_structrp png_ptr)
2114 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
2115 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
2117 /* Start of interlace block */
2118 static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
2120 /* Offset to next interlace block */
2121 static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
2123 /* Start of interlace block in the y direction */
2124 static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
2126 /* Offset to next interlace block in the y direction */
2127 static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
2128 #endif
2130 png_debug(1, "in png_write_finish_row");
2132 /* Next row */
2133 png_ptr->row_number++;
2135 /* See if we are done */
2136 if (png_ptr->row_number < png_ptr->num_rows)
2137 return;
2139 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
2140 /* If interlaced, go to next pass */
2141 if (png_ptr->interlaced != 0)
2143 png_ptr->row_number = 0;
2144 if ((png_ptr->transformations & PNG_INTERLACE) != 0)
2146 png_ptr->pass++;
2149 else
2151 /* Loop until we find a non-zero width or height pass */
2154 png_ptr->pass++;
2156 if (png_ptr->pass >= 7)
2157 break;
2159 png_ptr->usr_width = (png_ptr->width +
2160 png_pass_inc[png_ptr->pass] - 1 -
2161 png_pass_start[png_ptr->pass]) /
2162 png_pass_inc[png_ptr->pass];
2164 png_ptr->num_rows = (png_ptr->height +
2165 png_pass_yinc[png_ptr->pass] - 1 -
2166 png_pass_ystart[png_ptr->pass]) /
2167 png_pass_yinc[png_ptr->pass];
2169 if ((png_ptr->transformations & PNG_INTERLACE) != 0)
2170 break;
2172 } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
2176 /* Reset the row above the image for the next pass */
2177 if (png_ptr->pass < 7)
2179 if (png_ptr->prev_row != NULL)
2180 memset(png_ptr->prev_row, 0,
2181 PNG_ROWBYTES(png_ptr->usr_channels *
2182 png_ptr->usr_bit_depth, png_ptr->width) + 1);
2184 return;
2187 #endif
2189 /* If we get here, we've just written the last row, so we need
2190 to flush the compressor */
2191 png_compress_IDAT(png_ptr, NULL, 0, Z_FINISH);
2194 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
2195 /* Pick out the correct pixels for the interlace pass.
2196 * The basic idea here is to go through the row with a source
2197 * pointer and a destination pointer (sp and dp), and copy the
2198 * correct pixels for the pass. As the row gets compacted,
2199 * sp will always be >= dp, so we should never overwrite anything.
2200 * See the default: case for the easiest code to understand.
2202 void /* PRIVATE */
2203 png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
2205 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
2207 /* Start of interlace block */
2208 static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
2210 /* Offset to next interlace block */
2211 static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
2213 png_debug(1, "in png_do_write_interlace");
2215 /* We don't have to do anything on the last pass (6) */
2216 if (pass < 6)
2218 /* Each pixel depth is handled separately */
2219 switch (row_info->pixel_depth)
2221 case 1:
2223 png_bytep sp;
2224 png_bytep dp;
2225 unsigned int shift;
2226 int d;
2227 int value;
2228 png_uint_32 i;
2229 png_uint_32 row_width = row_info->width;
2231 dp = row;
2232 d = 0;
2233 shift = 7;
2235 for (i = png_pass_start[pass]; i < row_width;
2236 i += png_pass_inc[pass])
2238 sp = row + (size_t)(i >> 3);
2239 value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
2240 d |= (value << shift);
2242 if (shift == 0)
2244 shift = 7;
2245 *dp++ = (png_byte)d;
2246 d = 0;
2249 else
2250 shift--;
2253 if (shift != 7)
2254 *dp = (png_byte)d;
2256 break;
2259 case 2:
2261 png_bytep sp;
2262 png_bytep dp;
2263 unsigned int shift;
2264 int d;
2265 int value;
2266 png_uint_32 i;
2267 png_uint_32 row_width = row_info->width;
2269 dp = row;
2270 shift = 6;
2271 d = 0;
2273 for (i = png_pass_start[pass]; i < row_width;
2274 i += png_pass_inc[pass])
2276 sp = row + (size_t)(i >> 2);
2277 value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
2278 d |= (value << shift);
2280 if (shift == 0)
2282 shift = 6;
2283 *dp++ = (png_byte)d;
2284 d = 0;
2287 else
2288 shift -= 2;
2290 if (shift != 6)
2291 *dp = (png_byte)d;
2293 break;
2296 case 4:
2298 png_bytep sp;
2299 png_bytep dp;
2300 unsigned int shift;
2301 int d;
2302 int value;
2303 png_uint_32 i;
2304 png_uint_32 row_width = row_info->width;
2306 dp = row;
2307 shift = 4;
2308 d = 0;
2309 for (i = png_pass_start[pass]; i < row_width;
2310 i += png_pass_inc[pass])
2312 sp = row + (size_t)(i >> 1);
2313 value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
2314 d |= (value << shift);
2316 if (shift == 0)
2318 shift = 4;
2319 *dp++ = (png_byte)d;
2320 d = 0;
2323 else
2324 shift -= 4;
2326 if (shift != 4)
2327 *dp = (png_byte)d;
2329 break;
2332 default:
2334 png_bytep sp;
2335 png_bytep dp;
2336 png_uint_32 i;
2337 png_uint_32 row_width = row_info->width;
2338 size_t pixel_bytes;
2340 /* Start at the beginning */
2341 dp = row;
2343 /* Find out how many bytes each pixel takes up */
2344 pixel_bytes = (row_info->pixel_depth >> 3);
2346 /* Loop through the row, only looking at the pixels that matter */
2347 for (i = png_pass_start[pass]; i < row_width;
2348 i += png_pass_inc[pass])
2350 /* Find out where the original pixel is */
2351 sp = row + (size_t)i * pixel_bytes;
2353 /* Move the pixel */
2354 if (dp != sp)
2355 memcpy(dp, sp, pixel_bytes);
2357 /* Next pixel */
2358 dp += pixel_bytes;
2360 break;
2363 /* Set new row width */
2364 row_info->width = (row_info->width +
2365 png_pass_inc[pass] - 1 -
2366 png_pass_start[pass]) /
2367 png_pass_inc[pass];
2369 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2370 row_info->width);
2373 #endif
2376 /* This filters the row, chooses which filter to use, if it has not already
2377 * been specified by the application, and then writes the row out with the
2378 * chosen filter.
2380 static void /* PRIVATE */
2381 png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row,
2382 size_t row_bytes);
2384 #ifdef PNG_WRITE_FILTER_SUPPORTED
2385 static size_t /* PRIVATE */
2386 png_setup_sub_row(png_structrp png_ptr, png_uint_32 bpp,
2387 size_t row_bytes, size_t lmins)
2389 png_bytep rp, dp, lp;
2390 size_t i;
2391 size_t sum = 0;
2392 unsigned int v;
2394 png_ptr->try_row[0] = PNG_FILTER_VALUE_SUB;
2396 for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1; i < bpp;
2397 i++, rp++, dp++)
2399 v = *dp = *rp;
2400 #ifdef PNG_USE_ABS
2401 sum += 128 - abs((int)v - 128);
2402 #else
2403 sum += (v < 128) ? v : 256 - v;
2404 #endif
2407 for (lp = png_ptr->row_buf + 1; i < row_bytes;
2408 i++, rp++, lp++, dp++)
2410 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2411 #ifdef PNG_USE_ABS
2412 sum += 128 - abs((int)v - 128);
2413 #else
2414 sum += (v < 128) ? v : 256 - v;
2415 #endif
2417 if (sum > lmins) /* We are already worse, don't continue. */
2418 break;
2421 return sum;
2424 static void /* PRIVATE */
2425 png_setup_sub_row_only(png_structrp png_ptr, png_uint_32 bpp,
2426 size_t row_bytes)
2428 png_bytep rp, dp, lp;
2429 size_t i;
2431 png_ptr->try_row[0] = PNG_FILTER_VALUE_SUB;
2433 for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1; i < bpp;
2434 i++, rp++, dp++)
2436 *dp = *rp;
2439 for (lp = png_ptr->row_buf + 1; i < row_bytes;
2440 i++, rp++, lp++, dp++)
2442 *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2446 static size_t /* PRIVATE */
2447 png_setup_up_row(png_structrp png_ptr, size_t row_bytes, size_t lmins)
2449 png_bytep rp, dp, pp;
2450 size_t i;
2451 size_t sum = 0;
2452 unsigned int v;
2454 png_ptr->try_row[0] = PNG_FILTER_VALUE_UP;
2456 for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1,
2457 pp = png_ptr->prev_row + 1; i < row_bytes;
2458 i++, rp++, pp++, dp++)
2460 v = *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2461 #ifdef PNG_USE_ABS
2462 sum += 128 - abs((int)v - 128);
2463 #else
2464 sum += (v < 128) ? v : 256 - v;
2465 #endif
2467 if (sum > lmins) /* We are already worse, don't continue. */
2468 break;
2471 return sum;
2473 static void /* PRIVATE */
2474 png_setup_up_row_only(png_structrp png_ptr, size_t row_bytes)
2476 png_bytep rp, dp, pp;
2477 size_t i;
2479 png_ptr->try_row[0] = PNG_FILTER_VALUE_UP;
2481 for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1,
2482 pp = png_ptr->prev_row + 1; i < row_bytes;
2483 i++, rp++, pp++, dp++)
2485 *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2489 static size_t /* PRIVATE */
2490 png_setup_avg_row(png_structrp png_ptr, png_uint_32 bpp,
2491 size_t row_bytes, size_t lmins)
2493 png_bytep rp, dp, pp, lp;
2494 png_uint_32 i;
2495 size_t sum = 0;
2496 unsigned int v;
2498 png_ptr->try_row[0] = PNG_FILTER_VALUE_AVG;
2500 for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1,
2501 pp = png_ptr->prev_row + 1; i < bpp; i++)
2503 v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
2505 #ifdef PNG_USE_ABS
2506 sum += 128 - abs((int)v - 128);
2507 #else
2508 sum += (v < 128) ? v : 256 - v;
2509 #endif
2512 for (lp = png_ptr->row_buf + 1; i < row_bytes; i++)
2514 v = *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2515 & 0xff);
2517 #ifdef PNG_USE_ABS
2518 sum += 128 - abs((int)v - 128);
2519 #else
2520 sum += (v < 128) ? v : 256 - v;
2521 #endif
2523 if (sum > lmins) /* We are already worse, don't continue. */
2524 break;
2527 return sum;
2529 static void /* PRIVATE */
2530 png_setup_avg_row_only(png_structrp png_ptr, png_uint_32 bpp,
2531 size_t row_bytes)
2533 png_bytep rp, dp, pp, lp;
2534 png_uint_32 i;
2536 png_ptr->try_row[0] = PNG_FILTER_VALUE_AVG;
2538 for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1,
2539 pp = png_ptr->prev_row + 1; i < bpp; i++)
2541 *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
2544 for (lp = png_ptr->row_buf + 1; i < row_bytes; i++)
2546 *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2547 & 0xff);
2551 static size_t /* PRIVATE */
2552 png_setup_paeth_row(png_structrp png_ptr, png_uint_32 bpp,
2553 size_t row_bytes, size_t lmins)
2555 png_bytep rp, dp, pp, cp, lp;
2556 size_t i;
2557 size_t sum = 0;
2558 unsigned int v;
2560 png_ptr->try_row[0] = PNG_FILTER_VALUE_PAETH;
2562 for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1,
2563 pp = png_ptr->prev_row + 1; i < bpp; i++)
2565 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2567 #ifdef PNG_USE_ABS
2568 sum += 128 - abs((int)v - 128);
2569 #else
2570 sum += (v < 128) ? v : 256 - v;
2571 #endif
2574 for (lp = png_ptr->row_buf + 1, cp = png_ptr->prev_row + 1; i < row_bytes;
2575 i++)
2577 int a, b, c, pa, pb, pc, p;
2579 b = *pp++;
2580 c = *cp++;
2581 a = *lp++;
2583 p = b - c;
2584 pc = a - c;
2586 #ifdef PNG_USE_ABS
2587 pa = abs(p);
2588 pb = abs(pc);
2589 pc = abs(p + pc);
2590 #else
2591 pa = p < 0 ? -p : p;
2592 pb = pc < 0 ? -pc : pc;
2593 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
2594 #endif
2596 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2598 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
2600 #ifdef PNG_USE_ABS
2601 sum += 128 - abs((int)v - 128);
2602 #else
2603 sum += (v < 128) ? v : 256 - v;
2604 #endif
2606 if (sum > lmins) /* We are already worse, don't continue. */
2607 break;
2610 return sum;
2612 static void /* PRIVATE */
2613 png_setup_paeth_row_only(png_structrp png_ptr, png_uint_32 bpp,
2614 size_t row_bytes)
2616 png_bytep rp, dp, pp, cp, lp;
2617 size_t i;
2619 png_ptr->try_row[0] = PNG_FILTER_VALUE_PAETH;
2621 for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1,
2622 pp = png_ptr->prev_row + 1; i < bpp; i++)
2624 *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2627 for (lp = png_ptr->row_buf + 1, cp = png_ptr->prev_row + 1; i < row_bytes;
2628 i++)
2630 int a, b, c, pa, pb, pc, p;
2632 b = *pp++;
2633 c = *cp++;
2634 a = *lp++;
2636 p = b - c;
2637 pc = a - c;
2639 #ifdef PNG_USE_ABS
2640 pa = abs(p);
2641 pb = abs(pc);
2642 pc = abs(p + pc);
2643 #else
2644 pa = p < 0 ? -p : p;
2645 pb = pc < 0 ? -pc : pc;
2646 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
2647 #endif
2649 p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2651 *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
2654 #endif /* WRITE_FILTER */
2656 void /* PRIVATE */
2657 png_write_find_filter(png_structrp png_ptr, png_row_infop row_info)
2659 #ifndef PNG_WRITE_FILTER_SUPPORTED
2660 png_write_filtered_row(png_ptr, png_ptr->row_buf, row_info->rowbytes+1);
2661 #else
2662 unsigned int filter_to_do = png_ptr->do_filter;
2663 png_bytep row_buf;
2664 png_bytep best_row;
2665 png_uint_32 bpp;
2666 size_t mins;
2667 size_t row_bytes = row_info->rowbytes;
2669 png_debug(1, "in png_write_find_filter");
2671 /* Find out how many bytes offset each pixel is */
2672 bpp = (row_info->pixel_depth + 7) >> 3;
2674 row_buf = png_ptr->row_buf;
2675 mins = PNG_SIZE_MAX - 256/* so we can detect potential overflow of the
2676 running sum */;
2678 /* The prediction method we use is to find which method provides the
2679 * smallest value when summing the absolute values of the distances
2680 * from zero, using anything >= 128 as negative numbers. This is known
2681 * as the "minimum sum of absolute differences" heuristic. Other
2682 * heuristics are the "weighted minimum sum of absolute differences"
2683 * (experimental and can in theory improve compression), and the "zlib
2684 * predictive" method (not implemented yet), which does test compressions
2685 * of lines using different filter methods, and then chooses the
2686 * (series of) filter(s) that give minimum compressed data size (VERY
2687 * computationally expensive).
2689 * GRR 980525: consider also
2691 * (1) minimum sum of absolute differences from running average (i.e.,
2692 * keep running sum of non-absolute differences & count of bytes)
2693 * [track dispersion, too? restart average if dispersion too large?]
2695 * (1b) minimum sum of absolute differences from sliding average, probably
2696 * with window size <= deflate window (usually 32K)
2698 * (2) minimum sum of squared differences from zero or running average
2699 * (i.e., ~ root-mean-square approach)
2703 /* We don't need to test the 'no filter' case if this is the only filter
2704 * that has been chosen, as it doesn't actually do anything to the data.
2706 best_row = png_ptr->row_buf;
2708 if (PNG_SIZE_MAX/128 <= row_bytes)
2710 /* Overflow can occur in the calculation, just select the lowest set
2711 * filter.
2713 filter_to_do &= 0U-filter_to_do;
2715 else if ((filter_to_do & PNG_FILTER_NONE) != 0 &&
2716 filter_to_do != PNG_FILTER_NONE)
2718 /* Overflow not possible and multiple filters in the list, including the
2719 * 'none' filter.
2721 png_bytep rp;
2722 size_t sum = 0;
2723 size_t i;
2724 unsigned int v;
2727 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
2729 v = *rp;
2730 #ifdef PNG_USE_ABS
2731 sum += 128 - abs((int)v - 128);
2732 #else
2733 sum += (v < 128) ? v : 256 - v;
2734 #endif
2738 mins = sum;
2741 /* Sub filter */
2742 if (filter_to_do == PNG_FILTER_SUB)
2743 /* It's the only filter so no testing is needed */
2745 png_setup_sub_row_only(png_ptr, bpp, row_bytes);
2746 best_row = png_ptr->try_row;
2749 else if ((filter_to_do & PNG_FILTER_SUB) != 0)
2751 size_t sum;
2752 size_t lmins = mins;
2754 sum = png_setup_sub_row(png_ptr, bpp, row_bytes, lmins);
2756 if (sum < mins)
2758 mins = sum;
2759 best_row = png_ptr->try_row;
2760 if (png_ptr->tst_row != NULL)
2762 png_ptr->try_row = png_ptr->tst_row;
2763 png_ptr->tst_row = best_row;
2768 /* Up filter */
2769 if (filter_to_do == PNG_FILTER_UP)
2771 png_setup_up_row_only(png_ptr, row_bytes);
2772 best_row = png_ptr->try_row;
2775 else if ((filter_to_do & PNG_FILTER_UP) != 0)
2777 size_t sum;
2778 size_t lmins = mins;
2780 sum = png_setup_up_row(png_ptr, row_bytes, lmins);
2782 if (sum < mins)
2784 mins = sum;
2785 best_row = png_ptr->try_row;
2786 if (png_ptr->tst_row != NULL)
2788 png_ptr->try_row = png_ptr->tst_row;
2789 png_ptr->tst_row = best_row;
2794 /* Avg filter */
2795 if (filter_to_do == PNG_FILTER_AVG)
2797 png_setup_avg_row_only(png_ptr, bpp, row_bytes);
2798 best_row = png_ptr->try_row;
2801 else if ((filter_to_do & PNG_FILTER_AVG) != 0)
2803 size_t sum;
2804 size_t lmins = mins;
2806 sum= png_setup_avg_row(png_ptr, bpp, row_bytes, lmins);
2808 if (sum < mins)
2810 mins = sum;
2811 best_row = png_ptr->try_row;
2812 if (png_ptr->tst_row != NULL)
2814 png_ptr->try_row = png_ptr->tst_row;
2815 png_ptr->tst_row = best_row;
2820 /* Paeth filter */
2821 if (filter_to_do == PNG_FILTER_PAETH)
2823 png_setup_paeth_row_only(png_ptr, bpp, row_bytes);
2824 best_row = png_ptr->try_row;
2827 else if ((filter_to_do & PNG_FILTER_PAETH) != 0)
2829 size_t sum;
2830 size_t lmins = mins;
2832 sum = png_setup_paeth_row(png_ptr, bpp, row_bytes, lmins);
2834 if (sum < mins)
2836 best_row = png_ptr->try_row;
2837 if (png_ptr->tst_row != NULL)
2839 png_ptr->try_row = png_ptr->tst_row;
2840 png_ptr->tst_row = best_row;
2845 /* Do the actual writing of the filtered row data from the chosen filter. */
2846 png_write_filtered_row(png_ptr, best_row, row_info->rowbytes+1);
2848 #endif /* WRITE_FILTER */
2852 /* Do the actual writing of a previously filtered row. */
2853 static void
2854 png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row,
2855 size_t full_row_length/*includes filter byte*/)
2857 png_debug(1, "in png_write_filtered_row");
2859 png_debug1(2, "filter = %d", filtered_row[0]);
2861 png_compress_IDAT(png_ptr, filtered_row, full_row_length, Z_NO_FLUSH);
2863 #ifdef PNG_WRITE_FILTER_SUPPORTED
2864 /* Swap the current and previous rows */
2865 if (png_ptr->prev_row != NULL)
2867 png_bytep tptr;
2869 tptr = png_ptr->prev_row;
2870 png_ptr->prev_row = png_ptr->row_buf;
2871 png_ptr->row_buf = tptr;
2873 #endif /* WRITE_FILTER */
2875 /* Finish row - updates counters and flushes zlib if last row */
2876 png_write_finish_row(png_ptr);
2878 #ifdef PNG_WRITE_FLUSH_SUPPORTED
2879 png_ptr->flush_rows++;
2881 if (png_ptr->flush_dist > 0 &&
2882 png_ptr->flush_rows >= png_ptr->flush_dist)
2884 png_write_flush(png_ptr);
2886 #endif /* WRITE_FLUSH */
2889 #ifdef PNG_WRITE_APNG_SUPPORTED
2890 void /* PRIVATE */
2891 png_write_reset(png_structp png_ptr)
2893 png_ptr->row_number = 0;
2894 png_ptr->pass = 0;
2895 png_ptr->mode &= ~PNG_HAVE_IDAT;
2898 void /* PRIVATE */
2899 png_write_reinit(png_structp png_ptr, png_infop info_ptr,
2900 png_uint_32 width, png_uint_32 height)
2902 if (png_ptr->num_frames_written == 0 &&
2903 (width != png_ptr->first_frame_width ||
2904 height != png_ptr->first_frame_height))
2905 png_error(png_ptr, "width and/or height in the first frame's fcTL "
2906 "don't match the ones in IHDR");
2907 if (width > png_ptr->first_frame_width ||
2908 height > png_ptr->first_frame_height)
2909 png_error(png_ptr, "width and/or height for a frame greater than "
2910 "the ones in IHDR");
2912 png_set_IHDR(png_ptr, info_ptr, width, height,
2913 info_ptr->bit_depth, info_ptr->color_type,
2914 info_ptr->interlace_type, info_ptr->compression_type,
2915 info_ptr->filter_type);
2917 png_ptr->width = width;
2918 png_ptr->height = height;
2919 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
2920 png_ptr->usr_width = png_ptr->width;
2922 #endif /* WRITE_APNG */
2923 #endif /* WRITE */