2 /* pngwutil.c - utilities to write a PNG file
4 * Last changed in libpng 1.4.1 [February 25, 2010]
5 * Copyright (c) 1998-2010 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 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 #define PNG_NO_PEDANTIC_WARNINGS
16 #ifdef PNG_WRITE_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.
24 png_save_uint_32(png_bytep buf
, png_uint_32 i
)
26 buf
[0] = (png_byte
)((i
>> 24) & 0xff);
27 buf
[1] = (png_byte
)((i
>> 16) & 0xff);
28 buf
[2] = (png_byte
)((i
>> 8) & 0xff);
29 buf
[3] = (png_byte
)(i
& 0xff);
32 #ifdef PNG_SAVE_INT_32_SUPPORTED
33 /* The png_save_int_32 function assumes integers are stored in two's
34 * complement format. If this isn't the case, then this routine needs to
35 * be modified to write data in two's complement format.
38 png_save_int_32(png_bytep buf
, png_int_32 i
)
40 buf
[0] = (png_byte
)((i
>> 24) & 0xff);
41 buf
[1] = (png_byte
)((i
>> 16) & 0xff);
42 buf
[2] = (png_byte
)((i
>> 8) & 0xff);
43 buf
[3] = (png_byte
)(i
& 0xff);
47 /* Place a 16-bit number into a buffer in PNG byte order.
48 * The parameter is declared unsigned int, not png_uint_16,
49 * just to avoid potential problems on pre-ANSI C compilers.
52 png_save_uint_16(png_bytep buf
, unsigned int i
)
54 buf
[0] = (png_byte
)((i
>> 8) & 0xff);
55 buf
[1] = (png_byte
)(i
& 0xff);
58 /* Simple function to write the signature. If we have already written
59 * the magic bytes of the signature, or more likely, the PNG stream is
60 * being embedded into another stream and doesn't need its own signature,
61 * we should call png_set_sig_bytes() to tell libpng how many of the
62 * bytes have already been written.
65 png_write_sig(png_structp png_ptr
)
67 png_byte png_signature
[8] = {137, 80, 78, 71, 13, 10, 26, 10};
69 #ifdef PNG_IO_STATE_SUPPORTED
70 /* Inform the I/O callback that the signature is being written */
71 png_ptr
->io_state
= PNG_IO_WRITING
| PNG_IO_SIGNATURE
;
74 /* Write the rest of the 8 byte signature */
75 png_write_data(png_ptr
, &png_signature
[png_ptr
->sig_bytes
],
76 (png_size_t
)(8 - png_ptr
->sig_bytes
));
77 if (png_ptr
->sig_bytes
< 3)
78 png_ptr
->mode
|= PNG_HAVE_PNG_SIGNATURE
;
81 /* Write a PNG chunk all at once. The type is an array of ASCII characters
82 * representing the chunk name. The array must be at least 4 bytes in
83 * length, and does not need to be null terminated. To be safe, pass the
84 * pre-defined chunk names here, and if you need a new one, define it
85 * where the others are defined. The length is the length of the data.
86 * All the data must be present. If that is not possible, use the
87 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
91 png_write_chunk(png_structp png_ptr
, png_bytep chunk_name
,
92 png_bytep data
, png_size_t length
)
96 png_write_chunk_start(png_ptr
, chunk_name
, (png_uint_32
)length
);
97 png_write_chunk_data(png_ptr
, data
, (png_size_t
)length
);
98 png_write_chunk_end(png_ptr
);
101 /* Write the start of a PNG chunk. The type is the chunk type.
102 * The total_length is the sum of the lengths of all the data you will be
103 * passing in png_write_chunk_data().
106 png_write_chunk_start(png_structp png_ptr
, png_bytep chunk_name
,
111 png_debug2(0, "Writing %s chunk, length = %lu", chunk_name
,
112 (unsigned long)length
);
117 #ifdef PNG_IO_STATE_SUPPORTED
118 /* Inform the I/O callback that the chunk header is being written.
119 * PNG_IO_CHUNK_HDR requires a single I/O call.
121 png_ptr
->io_state
= PNG_IO_WRITING
| PNG_IO_CHUNK_HDR
;
124 /* Write the length and the chunk name */
125 png_save_uint_32(buf
, length
);
126 png_memcpy(buf
+ 4, chunk_name
, 4);
127 png_write_data(png_ptr
, buf
, (png_size_t
)8);
128 /* Put the chunk name into png_ptr->chunk_name */
129 png_memcpy(png_ptr
->chunk_name
, chunk_name
, 4);
130 /* Reset the crc and run it over the chunk name */
131 png_reset_crc(png_ptr
);
132 png_calculate_crc(png_ptr
, chunk_name
, 4);
134 #ifdef PNG_IO_STATE_SUPPORTED
135 /* Inform the I/O callback that chunk data will (possibly) be written.
136 * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls.
138 png_ptr
->io_state
= PNG_IO_WRITING
| PNG_IO_CHUNK_DATA
;
142 /* Write the data of a PNG chunk started with png_write_chunk_start().
143 * Note that multiple calls to this function are allowed, and that the
144 * sum of the lengths from these calls *must* add up to the total_length
145 * given to png_write_chunk_start().
148 png_write_chunk_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
150 /* Write the data, and run the CRC over it */
153 if (data
!= NULL
&& length
> 0)
155 png_write_data(png_ptr
, data
, length
);
156 /* Update the CRC after writing the data,
157 * in case that the user I/O routine alters it.
159 png_calculate_crc(png_ptr
, data
, length
);
163 /* Finish a chunk started with png_write_chunk_start(). */
165 png_write_chunk_end(png_structp png_ptr
)
169 if (png_ptr
== NULL
) return;
171 #ifdef PNG_IO_STATE_SUPPORTED
172 /* Inform the I/O callback that the chunk CRC is being written.
173 * PNG_IO_CHUNK_CRC requires a single I/O function call.
175 png_ptr
->io_state
= PNG_IO_WRITING
| PNG_IO_CHUNK_CRC
;
178 /* Write the crc in a single operation */
179 png_save_uint_32(buf
, png_ptr
->crc
);
181 png_write_data(png_ptr
, buf
, (png_size_t
)4);
184 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
185 /* This pair of functions encapsulates the operation of (a) compressing a
186 * text string, and (b) issuing it later as a series of chunk data writes.
187 * The compression_state structure is shared context for these functions
188 * set up by the caller in order to make the whole mess thread-safe.
193 char *input
; /* The uncompressed input data */
194 int input_len
; /* Its length */
195 int num_output_ptr
; /* Number of output pointers used */
196 int max_output_ptr
; /* Size of output_ptr */
197 png_charpp output_ptr
; /* Array of pointers to output */
200 /* Compress given text into storage in the png_ptr structure */
201 static int /* PRIVATE */
202 png_text_compress(png_structp png_ptr
,
203 png_charp text
, png_size_t text_len
, int compression
,
204 compression_state
*comp
)
208 comp
->num_output_ptr
= 0;
209 comp
->max_output_ptr
= 0;
210 comp
->output_ptr
= NULL
;
214 /* We may just want to pass the text right through */
215 if (compression
== PNG_TEXT_COMPRESSION_NONE
)
218 comp
->input_len
= text_len
;
219 return((int)text_len
);
222 if (compression
>= PNG_TEXT_COMPRESSION_LAST
)
224 #ifdef PNG_STDIO_SUPPORTED
226 png_snprintf(msg
, 50, "Unknown compression type %d", compression
);
227 png_warning(png_ptr
, msg
);
229 png_warning(png_ptr
, "Unknown compression type");
233 /* We can't write the chunk until we find out how much data we have,
234 * which means we need to run the compressor first and save the
235 * output. This shouldn't be a problem, as the vast majority of
236 * comments should be reasonable, but we will set up an array of
237 * malloc'd pointers to be sure.
239 * If we knew the application was well behaved, we could simplify this
240 * greatly by assuming we can always malloc an output buffer large
241 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
242 * and malloc this directly. The only time this would be a bad idea is
243 * if we can't malloc more than 64K and we have 64K of random input
244 * data, or if the input string is incredibly large (although this
245 * wouldn't cause a failure, just a slowdown due to swapping).
248 /* Set up the compression buffers */
249 png_ptr
->zstream
.avail_in
= (uInt
)text_len
;
250 png_ptr
->zstream
.next_in
= (Bytef
*)text
;
251 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
252 png_ptr
->zstream
.next_out
= (Bytef
*)png_ptr
->zbuf
;
254 /* This is the same compression loop as in png_write_row() */
257 /* Compress the data */
258 ret
= deflate(&png_ptr
->zstream
, Z_NO_FLUSH
);
262 if (png_ptr
->zstream
.msg
!= NULL
)
263 png_error(png_ptr
, png_ptr
->zstream
.msg
);
265 png_error(png_ptr
, "zlib error");
267 /* Check to see if we need more room */
268 if (!(png_ptr
->zstream
.avail_out
))
270 /* Make sure the output array has room */
271 if (comp
->num_output_ptr
>= comp
->max_output_ptr
)
275 old_max
= comp
->max_output_ptr
;
276 comp
->max_output_ptr
= comp
->num_output_ptr
+ 4;
277 if (comp
->output_ptr
!= NULL
)
281 old_ptr
= comp
->output_ptr
;
282 comp
->output_ptr
= (png_charpp
)png_malloc(png_ptr
,
284 (comp
->max_output_ptr
* png_sizeof(png_charpp
)));
285 png_memcpy(comp
->output_ptr
, old_ptr
, old_max
286 * png_sizeof(png_charp
));
287 png_free(png_ptr
, old_ptr
);
290 comp
->output_ptr
= (png_charpp
)png_malloc(png_ptr
,
292 (comp
->max_output_ptr
* png_sizeof(png_charp
)));
296 comp
->output_ptr
[comp
->num_output_ptr
] =
297 (png_charp
)png_malloc(png_ptr
,
298 (png_alloc_size_t
)png_ptr
->zbuf_size
);
299 png_memcpy(comp
->output_ptr
[comp
->num_output_ptr
], png_ptr
->zbuf
,
301 comp
->num_output_ptr
++;
303 /* and reset the buffer */
304 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
305 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
307 /* Continue until we don't have any more to compress */
308 } while (png_ptr
->zstream
.avail_in
);
310 /* Finish the compression */
313 /* Tell zlib we are finished */
314 ret
= deflate(&png_ptr
->zstream
, Z_FINISH
);
318 /* Check to see if we need more room */
319 if (!(png_ptr
->zstream
.avail_out
))
321 /* Check to make sure our output array has room */
322 if (comp
->num_output_ptr
>= comp
->max_output_ptr
)
326 old_max
= comp
->max_output_ptr
;
327 comp
->max_output_ptr
= comp
->num_output_ptr
+ 4;
328 if (comp
->output_ptr
!= NULL
)
332 old_ptr
= comp
->output_ptr
;
333 /* This could be optimized to realloc() */
334 comp
->output_ptr
= (png_charpp
)png_malloc(png_ptr
,
335 (png_alloc_size_t
)(comp
->max_output_ptr
*
336 png_sizeof(png_charp
)));
337 png_memcpy(comp
->output_ptr
, old_ptr
,
338 old_max
* png_sizeof(png_charp
));
339 png_free(png_ptr
, old_ptr
);
342 comp
->output_ptr
= (png_charpp
)png_malloc(png_ptr
,
343 (png_alloc_size_t
)(comp
->max_output_ptr
*
344 png_sizeof(png_charp
)));
348 comp
->output_ptr
[comp
->num_output_ptr
] =
349 (png_charp
)png_malloc(png_ptr
,
350 (png_alloc_size_t
)png_ptr
->zbuf_size
);
351 png_memcpy(comp
->output_ptr
[comp
->num_output_ptr
], png_ptr
->zbuf
,
353 comp
->num_output_ptr
++;
355 /* and reset the buffer pointers */
356 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
357 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
360 else if (ret
!= Z_STREAM_END
)
362 /* We got an error */
363 if (png_ptr
->zstream
.msg
!= NULL
)
364 png_error(png_ptr
, png_ptr
->zstream
.msg
);
366 png_error(png_ptr
, "zlib error");
368 } while (ret
!= Z_STREAM_END
);
370 /* Text length is number of buffers plus last buffer */
371 text_len
= png_ptr
->zbuf_size
* comp
->num_output_ptr
;
372 if (png_ptr
->zstream
.avail_out
< png_ptr
->zbuf_size
)
373 text_len
+= png_ptr
->zbuf_size
- (png_size_t
)png_ptr
->zstream
.avail_out
;
375 return((int)text_len
);
378 /* Ship the compressed text out via chunk writes */
379 static void /* PRIVATE */
380 png_write_compressed_data_out(png_structp png_ptr
, compression_state
*comp
)
384 /* Handle the no-compression case */
387 png_write_chunk_data(png_ptr
, (png_bytep
)comp
->input
,
388 (png_size_t
)comp
->input_len
);
392 /* Write saved output buffers, if any */
393 for (i
= 0; i
< comp
->num_output_ptr
; i
++)
395 png_write_chunk_data(png_ptr
, (png_bytep
)comp
->output_ptr
[i
],
396 (png_size_t
)png_ptr
->zbuf_size
);
397 png_free(png_ptr
, comp
->output_ptr
[i
]);
399 if (comp
->max_output_ptr
!= 0)
400 png_free(png_ptr
, comp
->output_ptr
);
401 /* Write anything left in zbuf */
402 if (png_ptr
->zstream
.avail_out
< (png_uint_32
)png_ptr
->zbuf_size
)
403 png_write_chunk_data(png_ptr
, png_ptr
->zbuf
,
404 (png_size_t
)(png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
));
406 /* Reset zlib for another zTXt/iTXt or image data */
407 deflateReset(&png_ptr
->zstream
);
408 png_ptr
->zstream
.data_type
= Z_BINARY
;
412 /* Write the IHDR chunk, and update the png_struct with the necessary
413 * information. Note that the rest of this code depends upon this
414 * information being correct.
417 png_write_IHDR(png_structp png_ptr
, png_uint_32 width
, png_uint_32 height
,
418 int bit_depth
, int color_type
, int compression_type
, int filter_type
,
424 png_byte buf
[13]; /* Buffer to store the IHDR info */
426 png_debug(1, "in png_write_IHDR");
428 /* Check that we have valid input data from the application info */
431 case PNG_COLOR_TYPE_GRAY
:
438 case 16: png_ptr
->channels
= 1; break;
439 default: png_error(png_ptr
,
440 "Invalid bit depth for grayscale image");
443 case PNG_COLOR_TYPE_RGB
:
444 if (bit_depth
!= 8 && bit_depth
!= 16)
445 png_error(png_ptr
, "Invalid bit depth for RGB image");
446 png_ptr
->channels
= 3;
448 case PNG_COLOR_TYPE_PALETTE
:
454 case 8: png_ptr
->channels
= 1; break;
455 default: png_error(png_ptr
, "Invalid bit depth for paletted image");
458 case PNG_COLOR_TYPE_GRAY_ALPHA
:
459 if (bit_depth
!= 8 && bit_depth
!= 16)
460 png_error(png_ptr
, "Invalid bit depth for grayscale+alpha image");
461 png_ptr
->channels
= 2;
463 case PNG_COLOR_TYPE_RGB_ALPHA
:
464 if (bit_depth
!= 8 && bit_depth
!= 16)
465 png_error(png_ptr
, "Invalid bit depth for RGBA image");
466 png_ptr
->channels
= 4;
469 png_error(png_ptr
, "Invalid image color type specified");
472 if (compression_type
!= PNG_COMPRESSION_TYPE_BASE
)
474 png_warning(png_ptr
, "Invalid compression type specified");
475 compression_type
= PNG_COMPRESSION_TYPE_BASE
;
478 /* Write filter_method 64 (intrapixel differencing) only if
479 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
480 * 2. Libpng did not write a PNG signature (this filter_method is only
481 * used in PNG datastreams that are embedded in MNG datastreams) and
482 * 3. The application called png_permit_mng_features with a mask that
483 * included PNG_FLAG_MNG_FILTER_64 and
484 * 4. The filter_method is 64 and
485 * 5. The color_type is RGB or RGBA
488 #ifdef PNG_MNG_FEATURES_SUPPORTED
489 !((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) &&
490 ((png_ptr
->mode
&PNG_HAVE_PNG_SIGNATURE
) == 0) &&
491 (color_type
== PNG_COLOR_TYPE_RGB
||
492 color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) &&
493 (filter_type
== PNG_INTRAPIXEL_DIFFERENCING
)) &&
495 filter_type
!= PNG_FILTER_TYPE_BASE
)
497 png_warning(png_ptr
, "Invalid filter type specified");
498 filter_type
= PNG_FILTER_TYPE_BASE
;
501 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
502 if (interlace_type
!= PNG_INTERLACE_NONE
&&
503 interlace_type
!= PNG_INTERLACE_ADAM7
)
505 png_warning(png_ptr
, "Invalid interlace type specified");
506 interlace_type
= PNG_INTERLACE_ADAM7
;
509 interlace_type
=PNG_INTERLACE_NONE
;
512 /* Save the relevent information */
513 png_ptr
->bit_depth
= (png_byte
)bit_depth
;
514 png_ptr
->color_type
= (png_byte
)color_type
;
515 png_ptr
->interlaced
= (png_byte
)interlace_type
;
516 #ifdef PNG_MNG_FEATURES_SUPPORTED
517 png_ptr
->filter_type
= (png_byte
)filter_type
;
519 png_ptr
->compression_type
= (png_byte
)compression_type
;
520 png_ptr
->width
= width
;
521 png_ptr
->height
= height
;
523 png_ptr
->pixel_depth
= (png_byte
)(bit_depth
* png_ptr
->channels
);
524 png_ptr
->rowbytes
= PNG_ROWBYTES(png_ptr
->pixel_depth
, width
);
525 /* Set the usr info, so any transformations can modify it */
526 png_ptr
->usr_width
= png_ptr
->width
;
527 png_ptr
->usr_bit_depth
= png_ptr
->bit_depth
;
528 png_ptr
->usr_channels
= png_ptr
->channels
;
530 /* Pack the header information into the buffer */
531 png_save_uint_32(buf
, width
);
532 png_save_uint_32(buf
+ 4, height
);
533 buf
[8] = (png_byte
)bit_depth
;
534 buf
[9] = (png_byte
)color_type
;
535 buf
[10] = (png_byte
)compression_type
;
536 buf
[11] = (png_byte
)filter_type
;
537 buf
[12] = (png_byte
)interlace_type
;
539 /* Write the chunk */
540 png_write_chunk(png_ptr
, (png_bytep
)png_IHDR
, buf
, (png_size_t
)13);
542 /* Initialize zlib with PNG info */
543 png_ptr
->zstream
.zalloc
= png_zalloc
;
544 png_ptr
->zstream
.zfree
= png_zfree
;
545 png_ptr
->zstream
.opaque
= (voidpf
)png_ptr
;
546 if (!(png_ptr
->do_filter
))
548 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
||
549 png_ptr
->bit_depth
< 8)
550 png_ptr
->do_filter
= PNG_FILTER_NONE
;
552 png_ptr
->do_filter
= PNG_ALL_FILTERS
;
554 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_CUSTOM_STRATEGY
))
556 if (png_ptr
->do_filter
!= PNG_FILTER_NONE
)
557 png_ptr
->zlib_strategy
= Z_FILTERED
;
559 png_ptr
->zlib_strategy
= Z_DEFAULT_STRATEGY
;
561 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_CUSTOM_LEVEL
))
562 png_ptr
->zlib_level
= Z_DEFAULT_COMPRESSION
;
563 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL
))
564 png_ptr
->zlib_mem_level
= 8;
565 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS
))
566 png_ptr
->zlib_window_bits
= 15;
567 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_CUSTOM_METHOD
))
568 png_ptr
->zlib_method
= 8;
569 ret
= deflateInit2(&png_ptr
->zstream
, png_ptr
->zlib_level
,
570 png_ptr
->zlib_method
, png_ptr
->zlib_window_bits
,
571 png_ptr
->zlib_mem_level
, png_ptr
->zlib_strategy
);
574 if (ret
== Z_VERSION_ERROR
) png_error(png_ptr
,
575 "zlib failed to initialize compressor -- version error");
576 if (ret
== Z_STREAM_ERROR
) png_error(png_ptr
,
577 "zlib failed to initialize compressor -- stream error");
578 if (ret
== Z_MEM_ERROR
) png_error(png_ptr
,
579 "zlib failed to initialize compressor -- mem error");
580 png_error(png_ptr
, "zlib failed to initialize compressor");
582 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
583 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
584 /* libpng is not interested in zstream.data_type */
585 /* Set it to a predefined value, to avoid its evaluation inside zlib */
586 png_ptr
->zstream
.data_type
= Z_BINARY
;
588 png_ptr
->mode
= PNG_HAVE_IHDR
;
591 /* Write the palette. We are careful not to trust png_color to be in the
592 * correct order for PNG, so people can redefine it to any convenient
596 png_write_PLTE(png_structp png_ptr
, png_colorp palette
, png_uint_32 num_pal
)
603 png_debug(1, "in png_write_PLTE");
606 #ifdef PNG_MNG_FEATURES_SUPPORTED
607 !(png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_EMPTY_PLTE
) &&
609 num_pal
== 0) || num_pal
> 256)
611 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
613 png_error(png_ptr
, "Invalid number of colors in palette");
617 png_warning(png_ptr
, "Invalid number of colors in palette");
622 if (!(png_ptr
->color_type
&PNG_COLOR_MASK_COLOR
))
625 "Ignoring request to write a PLTE chunk in grayscale PNG");
629 png_ptr
->num_palette
= (png_uint_16
)num_pal
;
630 png_debug1(3, "num_palette = %d", png_ptr
->num_palette
);
632 png_write_chunk_start(png_ptr
, (png_bytep
)png_PLTE
,
633 (png_uint_32
)(num_pal
* 3));
634 #ifdef PNG_POINTER_INDEXING_SUPPORTED
635 for (i
= 0, pal_ptr
= palette
; i
< num_pal
; i
++, pal_ptr
++)
637 buf
[0] = pal_ptr
->red
;
638 buf
[1] = pal_ptr
->green
;
639 buf
[2] = pal_ptr
->blue
;
640 png_write_chunk_data(png_ptr
, buf
, (png_size_t
)3);
643 /* This is a little slower but some buggy compilers need to do this
647 for (i
= 0; i
< num_pal
; i
++)
649 buf
[0] = pal_ptr
[i
].red
;
650 buf
[1] = pal_ptr
[i
].green
;
651 buf
[2] = pal_ptr
[i
].blue
;
652 png_write_chunk_data(png_ptr
, buf
, (png_size_t
)3);
655 png_write_chunk_end(png_ptr
);
656 png_ptr
->mode
|= PNG_HAVE_PLTE
;
659 /* Write an IDAT chunk */
661 png_write_IDAT(png_structp png_ptr
, png_bytep data
, png_size_t length
)
665 png_debug(1, "in png_write_IDAT");
667 /* Optimize the CMF field in the zlib stream. */
668 /* This hack of the zlib stream is compliant to the stream specification. */
669 if (!(png_ptr
->mode
& PNG_HAVE_IDAT
) &&
670 png_ptr
->compression_type
== PNG_COMPRESSION_TYPE_BASE
)
672 unsigned int z_cmf
= data
[0]; /* zlib compression method and flags */
673 if ((z_cmf
& 0x0f) == 8 && (z_cmf
& 0xf0) <= 0x70)
675 /* Avoid memory underflows and multiplication overflows.
677 * The conditions below are practically always satisfied;
678 * however, they still must be checked.
681 png_ptr
->height
< 16384 && png_ptr
->width
< 16384)
683 png_uint_32 uncompressed_idat_size
= png_ptr
->height
*
685 png_ptr
->channels
* png_ptr
->bit_depth
+ 15) >> 3);
686 unsigned int z_cinfo
= z_cmf
>> 4;
687 unsigned int half_z_window_size
= 1 << (z_cinfo
+ 7);
688 while (uncompressed_idat_size
<= half_z_window_size
&&
689 half_z_window_size
>= 256)
692 half_z_window_size
>>= 1;
694 z_cmf
= (z_cmf
& 0x0f) | (z_cinfo
<< 4);
695 if (data
[0] != (png_byte
)z_cmf
)
697 data
[0] = (png_byte
)z_cmf
;
699 data
[1] += (png_byte
)(0x1f - ((z_cmf
<< 8) + data
[1]) % 0x1f);
705 "Invalid zlib compression method or flags in IDAT");
708 png_write_chunk(png_ptr
, (png_bytep
)png_IDAT
, data
, length
);
709 png_ptr
->mode
|= PNG_HAVE_IDAT
;
712 /* Write an IEND chunk */
714 png_write_IEND(png_structp png_ptr
)
718 png_debug(1, "in png_write_IEND");
720 png_write_chunk(png_ptr
, (png_bytep
)png_IEND
, NULL
,
722 png_ptr
->mode
|= PNG_HAVE_IEND
;
725 #ifdef PNG_WRITE_gAMA_SUPPORTED
726 /* Write a gAMA chunk */
727 #ifdef PNG_FLOATING_POINT_SUPPORTED
729 png_write_gAMA(png_structp png_ptr
, double file_gamma
)
735 png_debug(1, "in png_write_gAMA");
737 /* file_gamma is saved in 1/100,000ths */
738 igamma
= (png_uint_32
)(file_gamma
* 100000.0 + 0.5);
739 png_save_uint_32(buf
, igamma
);
740 png_write_chunk(png_ptr
, (png_bytep
)png_gAMA
, buf
, (png_size_t
)4);
743 #ifdef PNG_FIXED_POINT_SUPPORTED
745 png_write_gAMA_fixed(png_structp png_ptr
, png_fixed_point file_gamma
)
750 png_debug(1, "in png_write_gAMA");
752 /* file_gamma is saved in 1/100,000ths */
753 png_save_uint_32(buf
, (png_uint_32
)file_gamma
);
754 png_write_chunk(png_ptr
, (png_bytep
)png_gAMA
, buf
, (png_size_t
)4);
759 #ifdef PNG_WRITE_sRGB_SUPPORTED
760 /* Write a sRGB chunk */
762 png_write_sRGB(png_structp png_ptr
, int srgb_intent
)
767 png_debug(1, "in png_write_sRGB");
769 if (srgb_intent
>= PNG_sRGB_INTENT_LAST
)
771 "Invalid sRGB rendering intent specified");
772 buf
[0]=(png_byte
)srgb_intent
;
773 png_write_chunk(png_ptr
, (png_bytep
)png_sRGB
, buf
, (png_size_t
)1);
777 #ifdef PNG_WRITE_iCCP_SUPPORTED
778 /* Write an iCCP chunk */
780 png_write_iCCP(png_structp png_ptr
, png_charp name
, int compression_type
,
781 png_charp profile
, int profile_len
)
786 compression_state comp
;
787 int embedded_profile_len
= 0;
789 png_debug(1, "in png_write_iCCP");
791 comp
.num_output_ptr
= 0;
792 comp
.max_output_ptr
= 0;
793 comp
.output_ptr
= NULL
;
797 if ((name_len
= png_check_keyword(png_ptr
, name
,
801 if (compression_type
!= PNG_COMPRESSION_TYPE_BASE
)
802 png_warning(png_ptr
, "Unknown compression type in iCCP chunk");
808 embedded_profile_len
=
809 ((*( (png_bytep
)profile
))<<24) |
810 ((*( (png_bytep
)profile
+ 1))<<16) |
811 ((*( (png_bytep
)profile
+ 2))<< 8) |
812 ((*( (png_bytep
)profile
+ 3)) );
814 if (embedded_profile_len
< 0)
817 "Embedded profile length in iCCP chunk is negative");
818 png_free(png_ptr
, new_name
);
822 if (profile_len
< embedded_profile_len
)
825 "Embedded profile length too large in iCCP chunk");
826 png_free(png_ptr
, new_name
);
830 if (profile_len
> embedded_profile_len
)
833 "Truncating profile to actual length in iCCP chunk");
834 profile_len
= embedded_profile_len
;
838 profile_len
= png_text_compress(png_ptr
, profile
,
839 (png_size_t
)profile_len
, PNG_COMPRESSION_TYPE_BASE
, &comp
);
841 /* Make sure we include the NULL after the name and the compression type */
842 png_write_chunk_start(png_ptr
, (png_bytep
)png_iCCP
,
843 (png_uint_32
)(name_len
+ profile_len
+ 2));
844 new_name
[name_len
+ 1] = 0x00;
845 png_write_chunk_data(png_ptr
, (png_bytep
)new_name
,
846 (png_size_t
)(name_len
+ 2));
849 png_write_compressed_data_out(png_ptr
, &comp
);
851 png_write_chunk_end(png_ptr
);
852 png_free(png_ptr
, new_name
);
856 #ifdef PNG_WRITE_sPLT_SUPPORTED
857 /* Write a sPLT chunk */
859 png_write_sPLT(png_structp png_ptr
, png_sPLT_tp spalette
)
864 png_byte entrybuf
[10];
865 png_size_t entry_size
= (spalette
->depth
== 8 ? 6 : 10);
866 png_size_t palette_size
= entry_size
* spalette
->nentries
;
868 #ifndef PNG_POINTER_INDEXING_SUPPORTED
872 png_debug(1, "in png_write_sPLT");
874 if ((name_len
= png_check_keyword(png_ptr
,spalette
->name
, &new_name
))==0)
877 /* Make sure we include the NULL after the name */
878 png_write_chunk_start(png_ptr
, (png_bytep
)png_sPLT
,
879 (png_uint_32
)(name_len
+ 2 + palette_size
));
880 png_write_chunk_data(png_ptr
, (png_bytep
)new_name
,
881 (png_size_t
)(name_len
+ 1));
882 png_write_chunk_data(png_ptr
, (png_bytep
)&spalette
->depth
, (png_size_t
)1);
884 /* Loop through each palette entry, writing appropriately */
885 #ifdef PNG_POINTER_INDEXING_SUPPORTED
886 for (ep
= spalette
->entries
; ep
<spalette
->entries
+ spalette
->nentries
; ep
++)
888 if (spalette
->depth
== 8)
890 entrybuf
[0] = (png_byte
)ep
->red
;
891 entrybuf
[1] = (png_byte
)ep
->green
;
892 entrybuf
[2] = (png_byte
)ep
->blue
;
893 entrybuf
[3] = (png_byte
)ep
->alpha
;
894 png_save_uint_16(entrybuf
+ 4, ep
->frequency
);
898 png_save_uint_16(entrybuf
+ 0, ep
->red
);
899 png_save_uint_16(entrybuf
+ 2, ep
->green
);
900 png_save_uint_16(entrybuf
+ 4, ep
->blue
);
901 png_save_uint_16(entrybuf
+ 6, ep
->alpha
);
902 png_save_uint_16(entrybuf
+ 8, ep
->frequency
);
904 png_write_chunk_data(png_ptr
, entrybuf
, (png_size_t
)entry_size
);
907 ep
=spalette
->entries
;
908 for (i
=0; i
>spalette
->nentries
; i
++)
910 if (spalette
->depth
== 8)
912 entrybuf
[0] = (png_byte
)ep
[i
].red
;
913 entrybuf
[1] = (png_byte
)ep
[i
].green
;
914 entrybuf
[2] = (png_byte
)ep
[i
].blue
;
915 entrybuf
[3] = (png_byte
)ep
[i
].alpha
;
916 png_save_uint_16(entrybuf
+ 4, ep
[i
].frequency
);
920 png_save_uint_16(entrybuf
+ 0, ep
[i
].red
);
921 png_save_uint_16(entrybuf
+ 2, ep
[i
].green
);
922 png_save_uint_16(entrybuf
+ 4, ep
[i
].blue
);
923 png_save_uint_16(entrybuf
+ 6, ep
[i
].alpha
);
924 png_save_uint_16(entrybuf
+ 8, ep
[i
].frequency
);
926 png_write_chunk_data(png_ptr
, entrybuf
, (png_size_t
)entry_size
);
930 png_write_chunk_end(png_ptr
);
931 png_free(png_ptr
, new_name
);
935 #ifdef PNG_WRITE_sBIT_SUPPORTED
936 /* Write the sBIT chunk */
938 png_write_sBIT(png_structp png_ptr
, png_color_8p sbit
, int color_type
)
944 png_debug(1, "in png_write_sBIT");
946 /* Make sure we don't depend upon the order of PNG_COLOR_8 */
947 if (color_type
& PNG_COLOR_MASK_COLOR
)
951 maxbits
= (png_byte
)(color_type
==PNG_COLOR_TYPE_PALETTE
? 8 :
952 png_ptr
->usr_bit_depth
);
953 if (sbit
->red
== 0 || sbit
->red
> maxbits
||
954 sbit
->green
== 0 || sbit
->green
> maxbits
||
955 sbit
->blue
== 0 || sbit
->blue
> maxbits
)
957 png_warning(png_ptr
, "Invalid sBIT depth specified");
961 buf
[1] = sbit
->green
;
967 if (sbit
->gray
== 0 || sbit
->gray
> png_ptr
->usr_bit_depth
)
969 png_warning(png_ptr
, "Invalid sBIT depth specified");
976 if (color_type
& PNG_COLOR_MASK_ALPHA
)
978 if (sbit
->alpha
== 0 || sbit
->alpha
> png_ptr
->usr_bit_depth
)
980 png_warning(png_ptr
, "Invalid sBIT depth specified");
983 buf
[size
++] = sbit
->alpha
;
986 png_write_chunk(png_ptr
, (png_bytep
)png_sBIT
, buf
, size
);
990 #ifdef PNG_WRITE_cHRM_SUPPORTED
991 /* Write the cHRM chunk */
992 #ifdef PNG_FLOATING_POINT_SUPPORTED
994 png_write_cHRM(png_structp png_ptr
, double white_x
, double white_y
,
995 double red_x
, double red_y
, double green_x
, double green_y
,
996 double blue_x
, double blue_y
)
1001 png_fixed_point int_white_x
, int_white_y
, int_red_x
, int_red_y
,
1002 int_green_x
, int_green_y
, int_blue_x
, int_blue_y
;
1004 png_debug(1, "in png_write_cHRM");
1006 int_white_x
= (png_uint_32
)(white_x
* 100000.0 + 0.5);
1007 int_white_y
= (png_uint_32
)(white_y
* 100000.0 + 0.5);
1008 int_red_x
= (png_uint_32
)(red_x
* 100000.0 + 0.5);
1009 int_red_y
= (png_uint_32
)(red_y
* 100000.0 + 0.5);
1010 int_green_x
= (png_uint_32
)(green_x
* 100000.0 + 0.5);
1011 int_green_y
= (png_uint_32
)(green_y
* 100000.0 + 0.5);
1012 int_blue_x
= (png_uint_32
)(blue_x
* 100000.0 + 0.5);
1013 int_blue_y
= (png_uint_32
)(blue_y
* 100000.0 + 0.5);
1015 #ifdef PNG_CHECK_cHRM_SUPPORTED
1016 if (png_check_cHRM_fixed(png_ptr
, int_white_x
, int_white_y
,
1017 int_red_x
, int_red_y
, int_green_x
, int_green_y
, int_blue_x
, int_blue_y
))
1020 /* Each value is saved in 1/100,000ths */
1022 png_save_uint_32(buf
, int_white_x
);
1023 png_save_uint_32(buf
+ 4, int_white_y
);
1025 png_save_uint_32(buf
+ 8, int_red_x
);
1026 png_save_uint_32(buf
+ 12, int_red_y
);
1028 png_save_uint_32(buf
+ 16, int_green_x
);
1029 png_save_uint_32(buf
+ 20, int_green_y
);
1031 png_save_uint_32(buf
+ 24, int_blue_x
);
1032 png_save_uint_32(buf
+ 28, int_blue_y
);
1034 png_write_chunk(png_ptr
, (png_bytep
)png_cHRM
, buf
, (png_size_t
)32);
1038 #ifdef PNG_FIXED_POINT_SUPPORTED
1040 png_write_cHRM_fixed(png_structp png_ptr
, png_fixed_point white_x
,
1041 png_fixed_point white_y
, png_fixed_point red_x
, png_fixed_point red_y
,
1042 png_fixed_point green_x
, png_fixed_point green_y
, png_fixed_point blue_x
,
1043 png_fixed_point blue_y
)
1048 png_debug(1, "in png_write_cHRM");
1050 /* Each value is saved in 1/100,000ths */
1051 #ifdef PNG_CHECK_cHRM_SUPPORTED
1052 if (png_check_cHRM_fixed(png_ptr
, white_x
, white_y
, red_x
, red_y
,
1053 green_x
, green_y
, blue_x
, blue_y
))
1056 png_save_uint_32(buf
, (png_uint_32
)white_x
);
1057 png_save_uint_32(buf
+ 4, (png_uint_32
)white_y
);
1059 png_save_uint_32(buf
+ 8, (png_uint_32
)red_x
);
1060 png_save_uint_32(buf
+ 12, (png_uint_32
)red_y
);
1062 png_save_uint_32(buf
+ 16, (png_uint_32
)green_x
);
1063 png_save_uint_32(buf
+ 20, (png_uint_32
)green_y
);
1065 png_save_uint_32(buf
+ 24, (png_uint_32
)blue_x
);
1066 png_save_uint_32(buf
+ 28, (png_uint_32
)blue_y
);
1068 png_write_chunk(png_ptr
, (png_bytep
)png_cHRM
, buf
, (png_size_t
)32);
1074 #ifdef PNG_WRITE_tRNS_SUPPORTED
1075 /* Write the tRNS chunk */
1077 png_write_tRNS(png_structp png_ptr
, png_bytep trans_alpha
, png_color_16p tran
,
1078 int num_trans
, int color_type
)
1083 png_debug(1, "in png_write_tRNS");
1085 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
1087 if (num_trans
<= 0 || num_trans
> (int)png_ptr
->num_palette
)
1089 png_warning(png_ptr
, "Invalid number of transparent colors specified");
1092 /* Write the chunk out as it is */
1093 png_write_chunk(png_ptr
, (png_bytep
)png_tRNS
, trans_alpha
,
1094 (png_size_t
)num_trans
);
1096 else if (color_type
== PNG_COLOR_TYPE_GRAY
)
1098 /* One 16 bit value */
1099 if (tran
->gray
>= (1 << png_ptr
->bit_depth
))
1101 png_warning(png_ptr
,
1102 "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1105 png_save_uint_16(buf
, tran
->gray
);
1106 png_write_chunk(png_ptr
, (png_bytep
)png_tRNS
, buf
, (png_size_t
)2);
1108 else if (color_type
== PNG_COLOR_TYPE_RGB
)
1110 /* Three 16 bit values */
1111 png_save_uint_16(buf
, tran
->red
);
1112 png_save_uint_16(buf
+ 2, tran
->green
);
1113 png_save_uint_16(buf
+ 4, tran
->blue
);
1114 if (png_ptr
->bit_depth
== 8 && (buf
[0] | buf
[2] | buf
[4]))
1116 png_warning(png_ptr
,
1117 "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1120 png_write_chunk(png_ptr
, (png_bytep
)png_tRNS
, buf
, (png_size_t
)6);
1124 png_warning(png_ptr
, "Can't write tRNS with an alpha channel");
1129 #ifdef PNG_WRITE_bKGD_SUPPORTED
1130 /* Write the background chunk */
1132 png_write_bKGD(png_structp png_ptr
, png_color_16p back
, int color_type
)
1137 png_debug(1, "in png_write_bKGD");
1139 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
1142 #ifdef PNG_MNG_FEATURES_SUPPORTED
1143 (png_ptr
->num_palette
||
1144 (!(png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_EMPTY_PLTE
))) &&
1146 back
->index
>= png_ptr
->num_palette
)
1148 png_warning(png_ptr
, "Invalid background palette index");
1151 buf
[0] = back
->index
;
1152 png_write_chunk(png_ptr
, (png_bytep
)png_bKGD
, buf
, (png_size_t
)1);
1154 else if (color_type
& PNG_COLOR_MASK_COLOR
)
1156 png_save_uint_16(buf
, back
->red
);
1157 png_save_uint_16(buf
+ 2, back
->green
);
1158 png_save_uint_16(buf
+ 4, back
->blue
);
1159 if (png_ptr
->bit_depth
== 8 && (buf
[0] | buf
[2] | buf
[4]))
1161 png_warning(png_ptr
,
1162 "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1165 png_write_chunk(png_ptr
, (png_bytep
)png_bKGD
, buf
, (png_size_t
)6);
1169 if (back
->gray
>= (1 << png_ptr
->bit_depth
))
1171 png_warning(png_ptr
,
1172 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1175 png_save_uint_16(buf
, back
->gray
);
1176 png_write_chunk(png_ptr
, (png_bytep
)png_bKGD
, buf
, (png_size_t
)2);
1181 #ifdef PNG_WRITE_hIST_SUPPORTED
1182 /* Write the histogram */
1184 png_write_hIST(png_structp png_ptr
, png_uint_16p hist
, int num_hist
)
1190 png_debug(1, "in png_write_hIST");
1192 if (num_hist
> (int)png_ptr
->num_palette
)
1194 png_debug2(3, "num_hist = %d, num_palette = %d", num_hist
,
1195 png_ptr
->num_palette
);
1196 png_warning(png_ptr
, "Invalid number of histogram entries specified");
1200 png_write_chunk_start(png_ptr
, (png_bytep
)png_hIST
,
1201 (png_uint_32
)(num_hist
* 2));
1202 for (i
= 0; i
< num_hist
; i
++)
1204 png_save_uint_16(buf
, hist
[i
]);
1205 png_write_chunk_data(png_ptr
, buf
, (png_size_t
)2);
1207 png_write_chunk_end(png_ptr
);
1211 #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
1212 defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
1213 /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
1214 * and if invalid, correct the keyword rather than discarding the entire
1215 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
1216 * length, forbids leading or trailing whitespace, multiple internal spaces,
1217 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
1219 * The new_key is allocated to hold the corrected keyword and must be freed
1220 * by the calling routine. This avoids problems with trying to write to
1221 * static keywords without having to have duplicate copies of the strings.
1223 png_size_t
/* PRIVATE */
1224 png_check_keyword(png_structp png_ptr
, png_charp key
, png_charpp new_key
)
1231 png_debug(1, "in png_check_keyword");
1235 if (key
== NULL
|| (key_len
= png_strlen(key
)) == 0)
1237 png_warning(png_ptr
, "zero length keyword");
1238 return ((png_size_t
)0);
1241 png_debug1(2, "Keyword to be checked is '%s'", key
);
1243 *new_key
= (png_charp
)png_malloc_warn(png_ptr
, (png_uint_32
)(key_len
+ 2));
1244 if (*new_key
== NULL
)
1246 png_warning(png_ptr
, "Out of memory while procesing keyword");
1247 return ((png_size_t
)0);
1250 /* Replace non-printing characters with a blank and print a warning */
1251 for (kp
= key
, dp
= *new_key
; *kp
!= '\0'; kp
++, dp
++)
1253 if ((png_byte
)*kp
< 0x20 ||
1254 ((png_byte
)*kp
> 0x7E && (png_byte
)*kp
< 0xA1))
1256 #ifdef PNG_STDIO_SUPPORTED
1259 png_snprintf(msg
, 40,
1260 "invalid keyword character 0x%02X", (png_byte
)*kp
);
1261 png_warning(png_ptr
, msg
);
1263 png_warning(png_ptr
, "invalid character in keyword");
1274 /* Remove any trailing white space. */
1275 kp
= *new_key
+ key_len
- 1;
1278 png_warning(png_ptr
, "trailing spaces removed from keyword");
1287 /* Remove any leading white space. */
1291 png_warning(png_ptr
, "leading spaces removed from keyword");
1300 png_debug1(2, "Checking for multiple internal spaces in '%s'", kp
);
1302 /* Remove multiple internal spaces. */
1303 for (kflag
= 0, dp
= *new_key
; *kp
!= '\0'; kp
++)
1305 if (*kp
== ' ' && kflag
== 0)
1310 else if (*kp
== ' ')
1323 png_warning(png_ptr
, "extra interior spaces removed from keyword");
1327 png_free(png_ptr
, *new_key
);
1328 png_warning(png_ptr
, "Zero length keyword");
1333 png_warning(png_ptr
, "keyword length must be 1 - 79 characters");
1334 (*new_key
)[79] = '\0';
1342 #ifdef PNG_WRITE_tEXt_SUPPORTED
1343 /* Write a tEXt chunk */
1345 png_write_tEXt(png_structp png_ptr
, png_charp key
, png_charp text
,
1346 png_size_t text_len
)
1352 png_debug(1, "in png_write_tEXt");
1354 if ((key_len
= png_check_keyword(png_ptr
, key
, &new_key
))==0)
1357 if (text
== NULL
|| *text
== '\0')
1360 text_len
= png_strlen(text
);
1362 /* Make sure we include the 0 after the key */
1363 png_write_chunk_start(png_ptr
, (png_bytep
)png_tEXt
,
1364 (png_uint_32
)(key_len
+ text_len
+ 1));
1366 * We leave it to the application to meet PNG-1.0 requirements on the
1367 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1368 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1369 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1371 png_write_chunk_data(png_ptr
, (png_bytep
)new_key
,
1372 (png_size_t
)(key_len
+ 1));
1374 png_write_chunk_data(png_ptr
, (png_bytep
)text
, (png_size_t
)text_len
);
1376 png_write_chunk_end(png_ptr
);
1377 png_free(png_ptr
, new_key
);
1381 #ifdef PNG_WRITE_zTXt_SUPPORTED
1382 /* Write a compressed text chunk */
1384 png_write_zTXt(png_structp png_ptr
, png_charp key
, png_charp text
,
1385 png_size_t text_len
, int compression
)
1391 compression_state comp
;
1393 png_debug(1, "in png_write_zTXt");
1395 comp
.num_output_ptr
= 0;
1396 comp
.max_output_ptr
= 0;
1397 comp
.output_ptr
= NULL
;
1401 if ((key_len
= png_check_keyword(png_ptr
, key
, &new_key
))==0)
1403 png_free(png_ptr
, new_key
);
1407 if (text
== NULL
|| *text
== '\0' || compression
==PNG_TEXT_COMPRESSION_NONE
)
1409 png_write_tEXt(png_ptr
, new_key
, text
, (png_size_t
)0);
1410 png_free(png_ptr
, new_key
);
1414 text_len
= png_strlen(text
);
1416 /* Compute the compressed data; do it now for the length */
1417 text_len
= png_text_compress(png_ptr
, text
, text_len
, compression
,
1420 /* Write start of chunk */
1421 png_write_chunk_start(png_ptr
, (png_bytep
)png_zTXt
,
1422 (png_uint_32
)(key_len
+text_len
+ 2));
1424 png_write_chunk_data(png_ptr
, (png_bytep
)new_key
,
1425 (png_size_t
)(key_len
+ 1));
1426 png_free(png_ptr
, new_key
);
1428 buf
[0] = (png_byte
)compression
;
1429 /* Write compression */
1430 png_write_chunk_data(png_ptr
, (png_bytep
)buf
, (png_size_t
)1);
1431 /* Write the compressed data */
1432 png_write_compressed_data_out(png_ptr
, &comp
);
1434 /* Close the chunk */
1435 png_write_chunk_end(png_ptr
);
1439 #ifdef PNG_WRITE_iTXt_SUPPORTED
1440 /* Write an iTXt chunk */
1442 png_write_iTXt(png_structp png_ptr
, int compression
, png_charp key
,
1443 png_charp lang
, png_charp lang_key
, png_charp text
)
1446 png_size_t lang_len
, key_len
, lang_key_len
, text_len
;
1448 png_charp new_key
= NULL
;
1450 compression_state comp
;
1452 png_debug(1, "in png_write_iTXt");
1454 comp
.num_output_ptr
= 0;
1455 comp
.max_output_ptr
= 0;
1456 comp
.output_ptr
= NULL
;
1459 if ((key_len
= png_check_keyword(png_ptr
, key
, &new_key
))==0)
1462 if ((lang_len
= png_check_keyword(png_ptr
, lang
, &new_lang
))==0)
1464 png_warning(png_ptr
, "Empty language field in iTXt chunk");
1469 if (lang_key
== NULL
)
1472 lang_key_len
= png_strlen(lang_key
);
1477 text_len
= png_strlen(text
);
1479 /* Compute the compressed data; do it now for the length */
1480 text_len
= png_text_compress(png_ptr
, text
, text_len
, compression
-2,
1484 /* Make sure we include the compression flag, the compression byte,
1485 * and the NULs after the key, lang, and lang_key parts */
1487 png_write_chunk_start(png_ptr
, (png_bytep
)png_iTXt
,
1489 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1495 /* We leave it to the application to meet PNG-1.0 requirements on the
1496 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1497 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1498 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1500 png_write_chunk_data(png_ptr
, (png_bytep
)new_key
,
1501 (png_size_t
)(key_len
+ 1));
1503 /* Set the compression flag */
1504 if (compression
== PNG_ITXT_COMPRESSION_NONE
|| \
1505 compression
== PNG_TEXT_COMPRESSION_NONE
)
1507 else /* compression == PNG_ITXT_COMPRESSION_zTXt */
1509 /* Set the compression method */
1511 png_write_chunk_data(png_ptr
, cbuf
, (png_size_t
)2);
1514 png_write_chunk_data(png_ptr
, (new_lang
? (png_bytep
)new_lang
: cbuf
),
1515 (png_size_t
)(lang_len
+ 1));
1516 png_write_chunk_data(png_ptr
, (lang_key
? (png_bytep
)lang_key
: cbuf
),
1517 (png_size_t
)(lang_key_len
+ 1));
1518 png_write_compressed_data_out(png_ptr
, &comp
);
1520 png_write_chunk_end(png_ptr
);
1521 png_free(png_ptr
, new_key
);
1522 png_free(png_ptr
, new_lang
);
1526 #ifdef PNG_WRITE_oFFs_SUPPORTED
1527 /* Write the oFFs chunk */
1529 png_write_oFFs(png_structp png_ptr
, png_int_32 x_offset
, png_int_32 y_offset
,
1535 png_debug(1, "in png_write_oFFs");
1537 if (unit_type
>= PNG_OFFSET_LAST
)
1538 png_warning(png_ptr
, "Unrecognized unit type for oFFs chunk");
1540 png_save_int_32(buf
, x_offset
);
1541 png_save_int_32(buf
+ 4, y_offset
);
1542 buf
[8] = (png_byte
)unit_type
;
1544 png_write_chunk(png_ptr
, (png_bytep
)png_oFFs
, buf
, (png_size_t
)9);
1547 #ifdef PNG_WRITE_pCAL_SUPPORTED
1548 /* Write the pCAL chunk (described in the PNG extensions document) */
1550 png_write_pCAL(png_structp png_ptr
, png_charp purpose
, png_int_32 X0
,
1551 png_int_32 X1
, int type
, int nparams
, png_charp units
, png_charpp params
)
1554 png_size_t purpose_len
, units_len
, total_len
;
1555 png_uint_32p params_len
;
1557 png_charp new_purpose
;
1560 png_debug1(1, "in png_write_pCAL (%d parameters)", nparams
);
1562 if (type
>= PNG_EQUATION_LAST
)
1563 png_warning(png_ptr
, "Unrecognized equation type for pCAL chunk");
1565 purpose_len
= png_check_keyword(png_ptr
, purpose
, &new_purpose
) + 1;
1566 png_debug1(3, "pCAL purpose length = %d", (int)purpose_len
);
1567 units_len
= png_strlen(units
) + (nparams
== 0 ? 0 : 1);
1568 png_debug1(3, "pCAL units length = %d", (int)units_len
);
1569 total_len
= purpose_len
+ units_len
+ 10;
1571 params_len
= (png_uint_32p
)png_malloc(png_ptr
,
1572 (png_alloc_size_t
)(nparams
* png_sizeof(png_uint_32
)));
1574 /* Find the length of each parameter, making sure we don't count the
1575 null terminator for the last parameter. */
1576 for (i
= 0; i
< nparams
; i
++)
1578 params_len
[i
] = png_strlen(params
[i
]) + (i
== nparams
- 1 ? 0 : 1);
1579 png_debug2(3, "pCAL parameter %d length = %lu", i
,
1580 (unsigned long) params_len
[i
]);
1581 total_len
+= (png_size_t
)params_len
[i
];
1584 png_debug1(3, "pCAL total length = %d", (int)total_len
);
1585 png_write_chunk_start(png_ptr
, (png_bytep
)png_pCAL
, (png_uint_32
)total_len
);
1586 png_write_chunk_data(png_ptr
, (png_bytep
)new_purpose
,
1587 (png_size_t
)purpose_len
);
1588 png_save_int_32(buf
, X0
);
1589 png_save_int_32(buf
+ 4, X1
);
1590 buf
[8] = (png_byte
)type
;
1591 buf
[9] = (png_byte
)nparams
;
1592 png_write_chunk_data(png_ptr
, buf
, (png_size_t
)10);
1593 png_write_chunk_data(png_ptr
, (png_bytep
)units
, (png_size_t
)units_len
);
1595 png_free(png_ptr
, new_purpose
);
1597 for (i
= 0; i
< nparams
; i
++)
1599 png_write_chunk_data(png_ptr
, (png_bytep
)params
[i
],
1600 (png_size_t
)params_len
[i
]);
1603 png_free(png_ptr
, params_len
);
1604 png_write_chunk_end(png_ptr
);
1608 #ifdef PNG_WRITE_sCAL_SUPPORTED
1609 /* Write the sCAL chunk */
1610 #if defined(PNG_FLOATING_POINT_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
1612 png_write_sCAL(png_structp png_ptr
, int unit
, double width
, double height
)
1616 png_size_t total_len
;
1618 png_debug(1, "in png_write_sCAL");
1620 buf
[0] = (char)unit
;
1621 png_snprintf(buf
+ 1, 63, "%12.12e", width
);
1622 total_len
= 1 + png_strlen(buf
+ 1) + 1;
1623 png_snprintf(buf
+ total_len
, 64-total_len
, "%12.12e", height
);
1624 total_len
+= png_strlen(buf
+ total_len
);
1626 png_debug1(3, "sCAL total length = %u", (unsigned int)total_len
);
1627 png_write_chunk(png_ptr
, (png_bytep
)png_sCAL
, (png_bytep
)buf
, total_len
);
1630 #ifdef PNG_FIXED_POINT_SUPPORTED
1632 png_write_sCAL_s(png_structp png_ptr
, int unit
, png_charp width
,
1637 png_size_t wlen
, hlen
, total_len
;
1639 png_debug(1, "in png_write_sCAL_s");
1641 wlen
= png_strlen(width
);
1642 hlen
= png_strlen(height
);
1643 total_len
= wlen
+ hlen
+ 2;
1646 png_warning(png_ptr
, "Can't write sCAL (buffer too small)");
1650 buf
[0] = (png_byte
)unit
;
1651 png_memcpy(buf
+ 1, width
, wlen
+ 1); /* Append the '\0' here */
1652 png_memcpy(buf
+ wlen
+ 2, height
, hlen
); /* Do NOT append the '\0' here */
1654 png_debug1(3, "sCAL total length = %u", (unsigned int)total_len
);
1655 png_write_chunk(png_ptr
, (png_bytep
)png_sCAL
, buf
, total_len
);
1661 #ifdef PNG_WRITE_pHYs_SUPPORTED
1662 /* Write the pHYs chunk */
1664 png_write_pHYs(png_structp png_ptr
, png_uint_32 x_pixels_per_unit
,
1665 png_uint_32 y_pixels_per_unit
,
1671 png_debug(1, "in png_write_pHYs");
1673 if (unit_type
>= PNG_RESOLUTION_LAST
)
1674 png_warning(png_ptr
, "Unrecognized unit type for pHYs chunk");
1676 png_save_uint_32(buf
, x_pixels_per_unit
);
1677 png_save_uint_32(buf
+ 4, y_pixels_per_unit
);
1678 buf
[8] = (png_byte
)unit_type
;
1680 png_write_chunk(png_ptr
, (png_bytep
)png_pHYs
, buf
, (png_size_t
)9);
1684 #ifdef PNG_WRITE_tIME_SUPPORTED
1685 /* Write the tIME chunk. Use either png_convert_from_struct_tm()
1686 * or png_convert_from_time_t(), or fill in the structure yourself.
1689 png_write_tIME(png_structp png_ptr
, png_timep mod_time
)
1694 png_debug(1, "in png_write_tIME");
1696 if (mod_time
->month
> 12 || mod_time
->month
< 1 ||
1697 mod_time
->day
> 31 || mod_time
->day
< 1 ||
1698 mod_time
->hour
> 23 || mod_time
->second
> 60)
1700 png_warning(png_ptr
, "Invalid time specified for tIME chunk");
1704 png_save_uint_16(buf
, mod_time
->year
);
1705 buf
[2] = mod_time
->month
;
1706 buf
[3] = mod_time
->day
;
1707 buf
[4] = mod_time
->hour
;
1708 buf
[5] = mod_time
->minute
;
1709 buf
[6] = mod_time
->second
;
1711 png_write_chunk(png_ptr
, (png_bytep
)png_tIME
, buf
, (png_size_t
)7);
1715 /* Initializes the row writing capability of libpng */
1717 png_write_start_row(png_structp png_ptr
)
1719 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1720 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1722 /* Start of interlace block */
1723 int png_pass_start
[7] = {0, 4, 0, 2, 0, 1, 0};
1725 /* Offset to next interlace block */
1726 int png_pass_inc
[7] = {8, 8, 4, 4, 2, 2, 1};
1728 /* Start of interlace block in the y direction */
1729 int png_pass_ystart
[7] = {0, 0, 4, 0, 2, 0, 1};
1731 /* Offset to next interlace block in the y direction */
1732 int png_pass_yinc
[7] = {8, 8, 8, 4, 4, 2, 2};
1735 png_size_t buf_size
;
1737 png_debug(1, "in png_write_start_row");
1739 buf_size
= (png_size_t
)(PNG_ROWBYTES(
1740 png_ptr
->usr_channels
*png_ptr
->usr_bit_depth
, png_ptr
->width
) + 1);
1742 /* Set up row buffer */
1743 png_ptr
->row_buf
= (png_bytep
)png_malloc(png_ptr
,
1744 (png_alloc_size_t
)buf_size
);
1745 png_ptr
->row_buf
[0] = PNG_FILTER_VALUE_NONE
;
1747 #ifdef PNG_WRITE_FILTER_SUPPORTED
1748 /* Set up filtering buffer, if using this filter */
1749 if (png_ptr
->do_filter
& PNG_FILTER_SUB
)
1751 png_ptr
->sub_row
= (png_bytep
)png_malloc(png_ptr
,
1752 (png_alloc_size_t
)(png_ptr
->rowbytes
+ 1));
1753 png_ptr
->sub_row
[0] = PNG_FILTER_VALUE_SUB
;
1756 /* We only need to keep the previous row if we are using one of these. */
1757 if (png_ptr
->do_filter
& (PNG_FILTER_AVG
| PNG_FILTER_UP
| PNG_FILTER_PAETH
))
1759 /* Set up previous row buffer */
1760 png_ptr
->prev_row
= (png_bytep
)png_calloc(png_ptr
,
1761 (png_alloc_size_t
)buf_size
);
1763 if (png_ptr
->do_filter
& PNG_FILTER_UP
)
1765 png_ptr
->up_row
= (png_bytep
)png_malloc(png_ptr
,
1766 (png_size_t
)(png_ptr
->rowbytes
+ 1));
1767 png_ptr
->up_row
[0] = PNG_FILTER_VALUE_UP
;
1770 if (png_ptr
->do_filter
& PNG_FILTER_AVG
)
1772 png_ptr
->avg_row
= (png_bytep
)png_malloc(png_ptr
,
1773 (png_alloc_size_t
)(png_ptr
->rowbytes
+ 1));
1774 png_ptr
->avg_row
[0] = PNG_FILTER_VALUE_AVG
;
1777 if (png_ptr
->do_filter
& PNG_FILTER_PAETH
)
1779 png_ptr
->paeth_row
= (png_bytep
)png_malloc(png_ptr
,
1780 (png_size_t
)(png_ptr
->rowbytes
+ 1));
1781 png_ptr
->paeth_row
[0] = PNG_FILTER_VALUE_PAETH
;
1784 #endif /* PNG_WRITE_FILTER_SUPPORTED */
1786 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1787 /* If interlaced, we need to set up width and height of pass */
1788 if (png_ptr
->interlaced
)
1790 if (!(png_ptr
->transformations
& PNG_INTERLACE
))
1792 png_ptr
->num_rows
= (png_ptr
->height
+ png_pass_yinc
[0] - 1 -
1793 png_pass_ystart
[0]) / png_pass_yinc
[0];
1794 png_ptr
->usr_width
= (png_ptr
->width
+ png_pass_inc
[0] - 1 -
1795 png_pass_start
[0]) / png_pass_inc
[0];
1799 png_ptr
->num_rows
= png_ptr
->height
;
1800 png_ptr
->usr_width
= png_ptr
->width
;
1806 png_ptr
->num_rows
= png_ptr
->height
;
1807 png_ptr
->usr_width
= png_ptr
->width
;
1809 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
1810 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
1813 /* Internal use only. Called when finished processing a row of data. */
1815 png_write_finish_row(png_structp png_ptr
)
1817 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1818 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1820 /* Start of interlace block */
1821 int png_pass_start
[7] = {0, 4, 0, 2, 0, 1, 0};
1823 /* Offset to next interlace block */
1824 int png_pass_inc
[7] = {8, 8, 4, 4, 2, 2, 1};
1826 /* Start of interlace block in the y direction */
1827 int png_pass_ystart
[7] = {0, 0, 4, 0, 2, 0, 1};
1829 /* Offset to next interlace block in the y direction */
1830 int png_pass_yinc
[7] = {8, 8, 8, 4, 4, 2, 2};
1835 png_debug(1, "in png_write_finish_row");
1838 png_ptr
->row_number
++;
1840 /* See if we are done */
1841 if (png_ptr
->row_number
< png_ptr
->num_rows
)
1844 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1845 /* If interlaced, go to next pass */
1846 if (png_ptr
->interlaced
)
1848 png_ptr
->row_number
= 0;
1849 if (png_ptr
->transformations
& PNG_INTERLACE
)
1855 /* Loop until we find a non-zero width or height pass */
1859 if (png_ptr
->pass
>= 7)
1861 png_ptr
->usr_width
= (png_ptr
->width
+
1862 png_pass_inc
[png_ptr
->pass
] - 1 -
1863 png_pass_start
[png_ptr
->pass
]) /
1864 png_pass_inc
[png_ptr
->pass
];
1865 png_ptr
->num_rows
= (png_ptr
->height
+
1866 png_pass_yinc
[png_ptr
->pass
] - 1 -
1867 png_pass_ystart
[png_ptr
->pass
]) /
1868 png_pass_yinc
[png_ptr
->pass
];
1869 if (png_ptr
->transformations
& PNG_INTERLACE
)
1871 } while (png_ptr
->usr_width
== 0 || png_ptr
->num_rows
== 0);
1875 /* Reset the row above the image for the next pass */
1876 if (png_ptr
->pass
< 7)
1878 if (png_ptr
->prev_row
!= NULL
)
1879 png_memset(png_ptr
->prev_row
, 0,
1880 (png_size_t
)(PNG_ROWBYTES(png_ptr
->usr_channels
*
1881 png_ptr
->usr_bit_depth
, png_ptr
->width
)) + 1);
1887 /* If we get here, we've just written the last row, so we need
1888 to flush the compressor */
1891 /* Tell the compressor we are done */
1892 ret
= deflate(&png_ptr
->zstream
, Z_FINISH
);
1893 /* Check for an error */
1896 /* Check to see if we need more room */
1897 if (!(png_ptr
->zstream
.avail_out
))
1899 png_write_IDAT(png_ptr
, png_ptr
->zbuf
, png_ptr
->zbuf_size
);
1900 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
1901 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
1904 else if (ret
!= Z_STREAM_END
)
1906 if (png_ptr
->zstream
.msg
!= NULL
)
1907 png_error(png_ptr
, png_ptr
->zstream
.msg
);
1909 png_error(png_ptr
, "zlib error");
1911 } while (ret
!= Z_STREAM_END
);
1913 /* Write any extra space */
1914 if (png_ptr
->zstream
.avail_out
< png_ptr
->zbuf_size
)
1916 png_write_IDAT(png_ptr
, png_ptr
->zbuf
, png_ptr
->zbuf_size
-
1917 png_ptr
->zstream
.avail_out
);
1920 deflateReset(&png_ptr
->zstream
);
1921 png_ptr
->zstream
.data_type
= Z_BINARY
;
1924 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1925 /* Pick out the correct pixels for the interlace pass.
1926 * The basic idea here is to go through the row with a source
1927 * pointer and a destination pointer (sp and dp), and copy the
1928 * correct pixels for the pass. As the row gets compacted,
1929 * sp will always be >= dp, so we should never overwrite anything.
1930 * See the default: case for the easiest code to understand.
1933 png_do_write_interlace(png_row_infop row_info
, png_bytep row
, int pass
)
1935 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1937 /* Start of interlace block */
1938 int png_pass_start
[7] = {0, 4, 0, 2, 0, 1, 0};
1940 /* Offset to next interlace block */
1941 int png_pass_inc
[7] = {8, 8, 4, 4, 2, 2, 1};
1943 png_debug(1, "in png_do_write_interlace");
1945 /* We don't have to do anything on the last pass (6) */
1948 /* Each pixel depth is handled separately */
1949 switch (row_info
->pixel_depth
)
1959 png_uint_32 row_width
= row_info
->width
;
1964 for (i
= png_pass_start
[pass
]; i
< row_width
;
1965 i
+= png_pass_inc
[pass
])
1967 sp
= row
+ (png_size_t
)(i
>> 3);
1968 value
= (int)(*sp
>> (7 - (int)(i
& 0x07))) & 0x01;
1969 d
|= (value
<< shift
);
1974 *dp
++ = (png_byte
)d
;
1993 png_uint_32 row_width
= row_info
->width
;
1998 for (i
= png_pass_start
[pass
]; i
< row_width
;
1999 i
+= png_pass_inc
[pass
])
2001 sp
= row
+ (png_size_t
)(i
>> 2);
2002 value
= (*sp
>> ((3 - (int)(i
& 0x03)) << 1)) & 0x03;
2003 d
|= (value
<< shift
);
2008 *dp
++ = (png_byte
)d
;
2026 png_uint_32 row_width
= row_info
->width
;
2031 for (i
= png_pass_start
[pass
]; i
< row_width
;
2032 i
+= png_pass_inc
[pass
])
2034 sp
= row
+ (png_size_t
)(i
>> 1);
2035 value
= (*sp
>> ((1 - (int)(i
& 0x01)) << 2)) & 0x0f;
2036 d
|= (value
<< shift
);
2041 *dp
++ = (png_byte
)d
;
2056 png_uint_32 row_width
= row_info
->width
;
2057 png_size_t pixel_bytes
;
2059 /* Start at the beginning */
2061 /* Find out how many bytes each pixel takes up */
2062 pixel_bytes
= (row_info
->pixel_depth
>> 3);
2063 /* Loop through the row, only looking at the pixels that
2065 for (i
= png_pass_start
[pass
]; i
< row_width
;
2066 i
+= png_pass_inc
[pass
])
2068 /* Find out where the original pixel is */
2069 sp
= row
+ (png_size_t
)i
* pixel_bytes
;
2070 /* Move the pixel */
2072 png_memcpy(dp
, sp
, pixel_bytes
);
2079 /* Set new row width */
2080 row_info
->width
= (row_info
->width
+
2081 png_pass_inc
[pass
] - 1 -
2082 png_pass_start
[pass
]) /
2084 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
,
2090 /* This filters the row, chooses which filter to use, if it has not already
2091 * been specified by the application, and then writes the row out with the
2094 #define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
2095 #define PNG_HISHIFT 10
2096 #define PNG_LOMASK ((png_uint_32)0xffffL)
2097 #define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
2099 png_write_find_filter(png_structp png_ptr
, png_row_infop row_info
)
2102 #ifdef PNG_WRITE_FILTER_SUPPORTED
2103 png_bytep prev_row
, row_buf
;
2104 png_uint_32 mins
, bpp
;
2105 png_byte filter_to_do
= png_ptr
->do_filter
;
2106 png_uint_32 row_bytes
= row_info
->rowbytes
;
2107 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2108 int num_p_filters
= (int)png_ptr
->num_prev_filters
;
2111 png_debug(1, "in png_write_find_filter");
2113 #ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2114 if (png_ptr
->row_number
== 0 && filter_to_do
== PNG_ALL_FILTERS
)
2116 /* These will never be selected so we need not test them. */
2117 filter_to_do
&= ~(PNG_FILTER_UP
| PNG_FILTER_PAETH
);
2121 /* Find out how many bytes offset each pixel is */
2122 bpp
= (row_info
->pixel_depth
+ 7) >> 3;
2124 prev_row
= png_ptr
->prev_row
;
2126 best_row
= png_ptr
->row_buf
;
2127 #ifdef PNG_WRITE_FILTER_SUPPORTED
2131 /* The prediction method we use is to find which method provides the
2132 * smallest value when summing the absolute values of the distances
2133 * from zero, using anything >= 128 as negative numbers. This is known
2134 * as the "minimum sum of absolute differences" heuristic. Other
2135 * heuristics are the "weighted minimum sum of absolute differences"
2136 * (experimental and can in theory improve compression), and the "zlib
2137 * predictive" method (not implemented yet), which does test compressions
2138 * of lines using different filter methods, and then chooses the
2139 * (series of) filter(s) that give minimum compressed data size (VERY
2140 * computationally expensive).
2142 * GRR 980525: consider also
2143 * (1) minimum sum of absolute differences from running average (i.e.,
2144 * keep running sum of non-absolute differences & count of bytes)
2145 * [track dispersion, too? restart average if dispersion too large?]
2146 * (1b) minimum sum of absolute differences from sliding average, probably
2147 * with window size <= deflate window (usually 32K)
2148 * (2) minimum sum of squared differences from zero or running average
2149 * (i.e., ~ root-mean-square approach)
2153 /* We don't need to test the 'no filter' case if this is the only filter
2154 * that has been chosen, as it doesn't actually do anything to the data.
2156 if ((filter_to_do
& PNG_FILTER_NONE
) &&
2157 filter_to_do
!= PNG_FILTER_NONE
)
2160 png_uint_32 sum
= 0;
2164 for (i
= 0, rp
= row_buf
+ 1; i
< row_bytes
; i
++, rp
++)
2167 sum
+= (v
< 128) ? v
: 256 - v
;
2170 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2171 if (png_ptr
->heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
2173 png_uint_32 sumhi
, sumlo
;
2175 sumlo
= sum
& PNG_LOMASK
;
2176 sumhi
= (sum
>> PNG_HISHIFT
) & PNG_HIMASK
; /* Gives us some footroom */
2178 /* Reduce the sum if we match any of the previous rows */
2179 for (j
= 0; j
< num_p_filters
; j
++)
2181 if (png_ptr
->prev_filters
[j
] == PNG_FILTER_VALUE_NONE
)
2183 sumlo
= (sumlo
* png_ptr
->filter_weights
[j
]) >>
2185 sumhi
= (sumhi
* png_ptr
->filter_weights
[j
]) >>
2190 /* Factor in the cost of this filter (this is here for completeness,
2191 * but it makes no sense to have a "cost" for the NONE filter, as
2192 * it has the minimum possible computational cost - none).
2194 sumlo
= (sumlo
* png_ptr
->filter_costs
[PNG_FILTER_VALUE_NONE
]) >>
2196 sumhi
= (sumhi
* png_ptr
->filter_costs
[PNG_FILTER_VALUE_NONE
]) >>
2199 if (sumhi
> PNG_HIMASK
)
2202 sum
= (sumhi
<< PNG_HISHIFT
) + sumlo
;
2209 if (filter_to_do
== PNG_FILTER_SUB
)
2210 /* It's the only filter so no testing is needed */
2212 png_bytep rp
, lp
, dp
;
2214 for (i
= 0, rp
= row_buf
+ 1, dp
= png_ptr
->sub_row
+ 1; i
< bpp
;
2219 for (lp
= row_buf
+ 1; i
< row_bytes
;
2220 i
++, rp
++, lp
++, dp
++)
2222 *dp
= (png_byte
)(((int)*rp
- (int)*lp
) & 0xff);
2224 best_row
= png_ptr
->sub_row
;
2227 else if (filter_to_do
& PNG_FILTER_SUB
)
2229 png_bytep rp
, dp
, lp
;
2230 png_uint_32 sum
= 0, lmins
= mins
;
2234 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2235 /* We temporarily increase the "minimum sum" by the factor we
2236 * would reduce the sum of this filter, so that we can do the
2237 * early exit comparison without scaling the sum each time.
2239 if (png_ptr
->heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
2242 png_uint_32 lmhi
, lmlo
;
2243 lmlo
= lmins
& PNG_LOMASK
;
2244 lmhi
= (lmins
>> PNG_HISHIFT
) & PNG_HIMASK
;
2246 for (j
= 0; j
< num_p_filters
; j
++)
2248 if (png_ptr
->prev_filters
[j
] == PNG_FILTER_VALUE_SUB
)
2250 lmlo
= (lmlo
* png_ptr
->inv_filter_weights
[j
]) >>
2252 lmhi
= (lmhi
* png_ptr
->inv_filter_weights
[j
]) >>
2257 lmlo
= (lmlo
* png_ptr
->inv_filter_costs
[PNG_FILTER_VALUE_SUB
]) >>
2259 lmhi
= (lmhi
* png_ptr
->inv_filter_costs
[PNG_FILTER_VALUE_SUB
]) >>
2262 if (lmhi
> PNG_HIMASK
)
2265 lmins
= (lmhi
<< PNG_HISHIFT
) + lmlo
;
2269 for (i
= 0, rp
= row_buf
+ 1, dp
= png_ptr
->sub_row
+ 1; i
< bpp
;
2274 sum
+= (v
< 128) ? v
: 256 - v
;
2276 for (lp
= row_buf
+ 1; i
< row_bytes
;
2277 i
++, rp
++, lp
++, dp
++)
2279 v
= *dp
= (png_byte
)(((int)*rp
- (int)*lp
) & 0xff);
2281 sum
+= (v
< 128) ? v
: 256 - v
;
2283 if (sum
> lmins
) /* We are already worse, don't continue. */
2287 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2288 if (png_ptr
->heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
2291 png_uint_32 sumhi
, sumlo
;
2292 sumlo
= sum
& PNG_LOMASK
;
2293 sumhi
= (sum
>> PNG_HISHIFT
) & PNG_HIMASK
;
2295 for (j
= 0; j
< num_p_filters
; j
++)
2297 if (png_ptr
->prev_filters
[j
] == PNG_FILTER_VALUE_SUB
)
2299 sumlo
= (sumlo
* png_ptr
->inv_filter_weights
[j
]) >>
2301 sumhi
= (sumhi
* png_ptr
->inv_filter_weights
[j
]) >>
2306 sumlo
= (sumlo
* png_ptr
->inv_filter_costs
[PNG_FILTER_VALUE_SUB
]) >>
2308 sumhi
= (sumhi
* png_ptr
->inv_filter_costs
[PNG_FILTER_VALUE_SUB
]) >>
2311 if (sumhi
> PNG_HIMASK
)
2314 sum
= (sumhi
<< PNG_HISHIFT
) + sumlo
;
2321 best_row
= png_ptr
->sub_row
;
2326 if (filter_to_do
== PNG_FILTER_UP
)
2328 png_bytep rp
, dp
, pp
;
2331 for (i
= 0, rp
= row_buf
+ 1, dp
= png_ptr
->up_row
+ 1,
2332 pp
= prev_row
+ 1; i
< row_bytes
;
2333 i
++, rp
++, pp
++, dp
++)
2335 *dp
= (png_byte
)(((int)*rp
- (int)*pp
) & 0xff);
2337 best_row
= png_ptr
->up_row
;
2340 else if (filter_to_do
& PNG_FILTER_UP
)
2342 png_bytep rp
, dp
, pp
;
2343 png_uint_32 sum
= 0, lmins
= mins
;
2348 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2349 if (png_ptr
->heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
2352 png_uint_32 lmhi
, lmlo
;
2353 lmlo
= lmins
& PNG_LOMASK
;
2354 lmhi
= (lmins
>> PNG_HISHIFT
) & PNG_HIMASK
;
2356 for (j
= 0; j
< num_p_filters
; j
++)
2358 if (png_ptr
->prev_filters
[j
] == PNG_FILTER_VALUE_UP
)
2360 lmlo
= (lmlo
* png_ptr
->inv_filter_weights
[j
]) >>
2362 lmhi
= (lmhi
* png_ptr
->inv_filter_weights
[j
]) >>
2367 lmlo
= (lmlo
* png_ptr
->inv_filter_costs
[PNG_FILTER_VALUE_UP
]) >>
2369 lmhi
= (lmhi
* png_ptr
->inv_filter_costs
[PNG_FILTER_VALUE_UP
]) >>
2372 if (lmhi
> PNG_HIMASK
)
2375 lmins
= (lmhi
<< PNG_HISHIFT
) + lmlo
;
2379 for (i
= 0, rp
= row_buf
+ 1, dp
= png_ptr
->up_row
+ 1,
2380 pp
= prev_row
+ 1; i
< row_bytes
; i
++)
2382 v
= *dp
++ = (png_byte
)(((int)*rp
++ - (int)*pp
++) & 0xff);
2384 sum
+= (v
< 128) ? v
: 256 - v
;
2386 if (sum
> lmins
) /* We are already worse, don't continue. */
2390 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2391 if (png_ptr
->heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
2394 png_uint_32 sumhi
, sumlo
;
2395 sumlo
= sum
& PNG_LOMASK
;
2396 sumhi
= (sum
>> PNG_HISHIFT
) & PNG_HIMASK
;
2398 for (j
= 0; j
< num_p_filters
; j
++)
2400 if (png_ptr
->prev_filters
[j
] == PNG_FILTER_VALUE_UP
)
2402 sumlo
= (sumlo
* png_ptr
->filter_weights
[j
]) >>
2404 sumhi
= (sumhi
* png_ptr
->filter_weights
[j
]) >>
2409 sumlo
= (sumlo
* png_ptr
->filter_costs
[PNG_FILTER_VALUE_UP
]) >>
2411 sumhi
= (sumhi
* png_ptr
->filter_costs
[PNG_FILTER_VALUE_UP
]) >>
2414 if (sumhi
> PNG_HIMASK
)
2417 sum
= (sumhi
<< PNG_HISHIFT
) + sumlo
;
2424 best_row
= png_ptr
->up_row
;
2429 if (filter_to_do
== PNG_FILTER_AVG
)
2431 png_bytep rp
, dp
, pp
, lp
;
2433 for (i
= 0, rp
= row_buf
+ 1, dp
= png_ptr
->avg_row
+ 1,
2434 pp
= prev_row
+ 1; i
< bpp
; i
++)
2436 *dp
++ = (png_byte
)(((int)*rp
++ - ((int)*pp
++ / 2)) & 0xff);
2438 for (lp
= row_buf
+ 1; i
< row_bytes
; i
++)
2440 *dp
++ = (png_byte
)(((int)*rp
++ - (((int)*pp
++ + (int)*lp
++) / 2))
2443 best_row
= png_ptr
->avg_row
;
2446 else if (filter_to_do
& PNG_FILTER_AVG
)
2448 png_bytep rp
, dp
, pp
, lp
;
2449 png_uint_32 sum
= 0, lmins
= mins
;
2453 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2454 if (png_ptr
->heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
2457 png_uint_32 lmhi
, lmlo
;
2458 lmlo
= lmins
& PNG_LOMASK
;
2459 lmhi
= (lmins
>> PNG_HISHIFT
) & PNG_HIMASK
;
2461 for (j
= 0; j
< num_p_filters
; j
++)
2463 if (png_ptr
->prev_filters
[j
] == PNG_FILTER_VALUE_AVG
)
2465 lmlo
= (lmlo
* png_ptr
->inv_filter_weights
[j
]) >>
2467 lmhi
= (lmhi
* png_ptr
->inv_filter_weights
[j
]) >>
2472 lmlo
= (lmlo
* png_ptr
->inv_filter_costs
[PNG_FILTER_VALUE_AVG
]) >>
2474 lmhi
= (lmhi
* png_ptr
->inv_filter_costs
[PNG_FILTER_VALUE_AVG
]) >>
2477 if (lmhi
> PNG_HIMASK
)
2480 lmins
= (lmhi
<< PNG_HISHIFT
) + lmlo
;
2484 for (i
= 0, rp
= row_buf
+ 1, dp
= png_ptr
->avg_row
+ 1,
2485 pp
= prev_row
+ 1; i
< bpp
; i
++)
2487 v
= *dp
++ = (png_byte
)(((int)*rp
++ - ((int)*pp
++ / 2)) & 0xff);
2489 sum
+= (v
< 128) ? v
: 256 - v
;
2491 for (lp
= row_buf
+ 1; i
< row_bytes
; i
++)
2494 (png_byte
)(((int)*rp
++ - (((int)*pp
++ + (int)*lp
++) / 2)) & 0xff);
2496 sum
+= (v
< 128) ? v
: 256 - v
;
2498 if (sum
> lmins
) /* We are already worse, don't continue. */
2502 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2503 if (png_ptr
->heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
2506 png_uint_32 sumhi
, sumlo
;
2507 sumlo
= sum
& PNG_LOMASK
;
2508 sumhi
= (sum
>> PNG_HISHIFT
) & PNG_HIMASK
;
2510 for (j
= 0; j
< num_p_filters
; j
++)
2512 if (png_ptr
->prev_filters
[j
] == PNG_FILTER_VALUE_NONE
)
2514 sumlo
= (sumlo
* png_ptr
->filter_weights
[j
]) >>
2516 sumhi
= (sumhi
* png_ptr
->filter_weights
[j
]) >>
2521 sumlo
= (sumlo
* png_ptr
->filter_costs
[PNG_FILTER_VALUE_AVG
]) >>
2523 sumhi
= (sumhi
* png_ptr
->filter_costs
[PNG_FILTER_VALUE_AVG
]) >>
2526 if (sumhi
> PNG_HIMASK
)
2529 sum
= (sumhi
<< PNG_HISHIFT
) + sumlo
;
2536 best_row
= png_ptr
->avg_row
;
2541 if (filter_to_do
== PNG_FILTER_PAETH
)
2543 png_bytep rp
, dp
, pp
, cp
, lp
;
2545 for (i
= 0, rp
= row_buf
+ 1, dp
= png_ptr
->paeth_row
+ 1,
2546 pp
= prev_row
+ 1; i
< bpp
; i
++)
2548 *dp
++ = (png_byte
)(((int)*rp
++ - (int)*pp
++) & 0xff);
2551 for (lp
= row_buf
+ 1, cp
= prev_row
+ 1; i
< row_bytes
; i
++)
2553 int a
, b
, c
, pa
, pb
, pc
, p
;
2567 pa
= p
< 0 ? -p
: p
;
2568 pb
= pc
< 0 ? -pc
: pc
;
2569 pc
= (p
+ pc
) < 0 ? -(p
+ pc
) : p
+ pc
;
2572 p
= (pa
<= pb
&& pa
<=pc
) ? a
: (pb
<= pc
) ? b
: c
;
2574 *dp
++ = (png_byte
)(((int)*rp
++ - p
) & 0xff);
2576 best_row
= png_ptr
->paeth_row
;
2579 else if (filter_to_do
& PNG_FILTER_PAETH
)
2581 png_bytep rp
, dp
, pp
, cp
, lp
;
2582 png_uint_32 sum
= 0, lmins
= mins
;
2586 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2587 if (png_ptr
->heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
2590 png_uint_32 lmhi
, lmlo
;
2591 lmlo
= lmins
& PNG_LOMASK
;
2592 lmhi
= (lmins
>> PNG_HISHIFT
) & PNG_HIMASK
;
2594 for (j
= 0; j
< num_p_filters
; j
++)
2596 if (png_ptr
->prev_filters
[j
] == PNG_FILTER_VALUE_PAETH
)
2598 lmlo
= (lmlo
* png_ptr
->inv_filter_weights
[j
]) >>
2600 lmhi
= (lmhi
* png_ptr
->inv_filter_weights
[j
]) >>
2605 lmlo
= (lmlo
* png_ptr
->inv_filter_costs
[PNG_FILTER_VALUE_PAETH
]) >>
2607 lmhi
= (lmhi
* png_ptr
->inv_filter_costs
[PNG_FILTER_VALUE_PAETH
]) >>
2610 if (lmhi
> PNG_HIMASK
)
2613 lmins
= (lmhi
<< PNG_HISHIFT
) + lmlo
;
2617 for (i
= 0, rp
= row_buf
+ 1, dp
= png_ptr
->paeth_row
+ 1,
2618 pp
= prev_row
+ 1; i
< bpp
; i
++)
2620 v
= *dp
++ = (png_byte
)(((int)*rp
++ - (int)*pp
++) & 0xff);
2622 sum
+= (v
< 128) ? v
: 256 - v
;
2625 for (lp
= row_buf
+ 1, cp
= prev_row
+ 1; i
< row_bytes
; i
++)
2627 int a
, b
, c
, pa
, pb
, pc
, p
;
2633 #ifndef PNG_SLOW_PAETH
2641 pa
= p
< 0 ? -p
: p
;
2642 pb
= pc
< 0 ? -pc
: pc
;
2643 pc
= (p
+ pc
) < 0 ? -(p
+ pc
) : p
+ pc
;
2645 p
= (pa
<= pb
&& pa
<=pc
) ? a
: (pb
<= pc
) ? b
: c
;
2646 #else /* PNG_SLOW_PAETH */
2651 if (pa
<= pb
&& pa
<= pc
)
2657 #endif /* PNG_SLOW_PAETH */
2659 v
= *dp
++ = (png_byte
)(((int)*rp
++ - p
) & 0xff);
2661 sum
+= (v
< 128) ? v
: 256 - v
;
2663 if (sum
> lmins
) /* We are already worse, don't continue. */
2667 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2668 if (png_ptr
->heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
2671 png_uint_32 sumhi
, sumlo
;
2672 sumlo
= sum
& PNG_LOMASK
;
2673 sumhi
= (sum
>> PNG_HISHIFT
) & PNG_HIMASK
;
2675 for (j
= 0; j
< num_p_filters
; j
++)
2677 if (png_ptr
->prev_filters
[j
] == PNG_FILTER_VALUE_PAETH
)
2679 sumlo
= (sumlo
* png_ptr
->filter_weights
[j
]) >>
2681 sumhi
= (sumhi
* png_ptr
->filter_weights
[j
]) >>
2686 sumlo
= (sumlo
* png_ptr
->filter_costs
[PNG_FILTER_VALUE_PAETH
]) >>
2688 sumhi
= (sumhi
* png_ptr
->filter_costs
[PNG_FILTER_VALUE_PAETH
]) >>
2691 if (sumhi
> PNG_HIMASK
)
2694 sum
= (sumhi
<< PNG_HISHIFT
) + sumlo
;
2700 best_row
= png_ptr
->paeth_row
;
2703 #endif /* PNG_WRITE_FILTER_SUPPORTED */
2704 /* Do the actual writing of the filtered row data from the chosen filter. */
2706 png_write_filtered_row(png_ptr
, best_row
);
2708 #ifdef PNG_WRITE_FILTER_SUPPORTED
2709 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2710 /* Save the type of filter we picked this time for future calculations */
2711 if (png_ptr
->num_prev_filters
> 0)
2714 for (j
= 1; j
< num_p_filters
; j
++)
2716 png_ptr
->prev_filters
[j
] = png_ptr
->prev_filters
[j
- 1];
2718 png_ptr
->prev_filters
[j
] = best_row
[0];
2721 #endif /* PNG_WRITE_FILTER_SUPPORTED */
2725 /* Do the actual writing of a previously filtered row. */
2727 png_write_filtered_row(png_structp png_ptr
, png_bytep filtered_row
)
2729 png_debug(1, "in png_write_filtered_row");
2731 png_debug1(2, "filter = %d", filtered_row
[0]);
2732 /* Set up the zlib input buffer */
2734 png_ptr
->zstream
.next_in
= filtered_row
;
2735 png_ptr
->zstream
.avail_in
= (uInt
)png_ptr
->row_info
.rowbytes
+ 1;
2736 /* Repeat until we have compressed all the data */
2739 int ret
; /* Return of zlib */
2741 /* Compress the data */
2742 ret
= deflate(&png_ptr
->zstream
, Z_NO_FLUSH
);
2743 /* Check for compression errors */
2746 if (png_ptr
->zstream
.msg
!= NULL
)
2747 png_error(png_ptr
, png_ptr
->zstream
.msg
);
2749 png_error(png_ptr
, "zlib error");
2752 /* See if it is time to write another IDAT */
2753 if (!(png_ptr
->zstream
.avail_out
))
2755 /* Write the IDAT and reset the zlib output buffer */
2756 png_write_IDAT(png_ptr
, png_ptr
->zbuf
, png_ptr
->zbuf_size
);
2757 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
2758 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
2760 /* Repeat until all data has been compressed */
2761 } while (png_ptr
->zstream
.avail_in
);
2763 /* Swap the current and previous rows */
2764 if (png_ptr
->prev_row
!= NULL
)
2768 tptr
= png_ptr
->prev_row
;
2769 png_ptr
->prev_row
= png_ptr
->row_buf
;
2770 png_ptr
->row_buf
= tptr
;
2773 /* Finish row - updates counters and flushes zlib if last row */
2774 png_write_finish_row(png_ptr
);
2776 #ifdef PNG_WRITE_FLUSH_SUPPORTED
2777 png_ptr
->flush_rows
++;
2779 if (png_ptr
->flush_dist
> 0 &&
2780 png_ptr
->flush_rows
>= png_ptr
->flush_dist
)
2782 png_write_flush(png_ptr
);
2786 #endif /* PNG_WRITE_SUPPORTED */