2 /* pngwutil.c - utilities to write a PNG file
4 * libpng 1.0.3 - January 14, 1999
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
8 * Copyright (c) 1998, 1999 Glenn Randers-Pehrson
14 /* Place a 32-bit number into a buffer in PNG byte order. We work
15 * with unsigned numbers for convenience, although one supported
16 * ancillary chunk uses signed (two's complement) numbers.
19 png_save_uint_32(png_bytep buf
, png_uint_32 i
)
21 buf
[0] = (png_byte
)((i
>> 24) & 0xff);
22 buf
[1] = (png_byte
)((i
>> 16) & 0xff);
23 buf
[2] = (png_byte
)((i
>> 8) & 0xff);
24 buf
[3] = (png_byte
)(i
& 0xff);
27 #if defined(PNG_WRITE_pCAL_SUPPORTED)
28 /* The png_save_int_32 function assumes integers are stored in two's
29 * complement format. If this isn't the case, then this routine needs to
30 * be modified to write data in two's complement format.
33 png_save_int_32(png_bytep buf
, png_int_32 i
)
35 buf
[0] = (png_byte
)((i
>> 24) & 0xff);
36 buf
[1] = (png_byte
)((i
>> 16) & 0xff);
37 buf
[2] = (png_byte
)((i
>> 8) & 0xff);
38 buf
[3] = (png_byte
)(i
& 0xff);
42 /* Place a 16-bit number into a buffer in PNG byte order.
43 * The parameter is declared unsigned int, not png_uint_16,
44 * just to avoid potential problems on pre-ANSI C compilers.
47 png_save_uint_16(png_bytep buf
, unsigned int i
)
49 buf
[0] = (png_byte
)((i
>> 8) & 0xff);
50 buf
[1] = (png_byte
)(i
& 0xff);
53 /* Write a PNG chunk all at once. The type is an array of ASCII characters
54 * representing the chunk name. The array must be at least 4 bytes in
55 * length, and does not need to be null terminated. To be safe, pass the
56 * pre-defined chunk names here, and if you need a new one, define it
57 * where the others are defined. The length is the length of the data.
58 * All the data must be present. If that is not possible, use the
59 * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
63 png_write_chunk(png_structp png_ptr
, png_bytep chunk_name
,
64 png_bytep data
, png_size_t length
)
66 png_write_chunk_start(png_ptr
, chunk_name
, (png_uint_32
)length
);
67 png_write_chunk_data(png_ptr
, data
, length
);
68 png_write_chunk_end(png_ptr
);
71 /* Write the start of a PNG chunk. The type is the chunk type.
72 * The total_length is the sum of the lengths of all the data you will be
73 * passing in png_write_chunk_data().
76 png_write_chunk_start(png_structp png_ptr
, png_bytep chunk_name
,
80 png_debug2(0, "Writing %s chunk (%d bytes)\n", chunk_name
, length
);
82 /* write the length */
83 png_save_uint_32(buf
, length
);
84 png_write_data(png_ptr
, buf
, (png_size_t
)4);
86 /* write the chunk name */
87 png_write_data(png_ptr
, chunk_name
, (png_size_t
)4);
88 /* reset the crc and run it over the chunk name */
89 png_reset_crc(png_ptr
);
90 png_calculate_crc(png_ptr
, chunk_name
, (png_size_t
)4);
93 /* Write the data of a PNG chunk started with png_write_chunk_start().
94 * Note that multiple calls to this function are allowed, and that the
95 * sum of the lengths from these calls *must* add up to the total_length
96 * given to png_write_chunk_start().
99 png_write_chunk_data(png_structp png_ptr
, png_bytep data
, png_size_t length
)
101 /* write the data, and run the CRC over it */
102 if (data
!= NULL
&& length
> 0)
104 png_calculate_crc(png_ptr
, data
, length
);
105 png_write_data(png_ptr
, data
, length
);
109 /* Finish a chunk started with png_write_chunk_start(). */
111 png_write_chunk_end(png_structp png_ptr
)
116 png_save_uint_32(buf
, png_ptr
->crc
);
118 png_write_data(png_ptr
, buf
, (png_size_t
)4);
121 /* Simple function to write the signature. If we have already written
122 * the magic bytes of the signature, or more likely, the PNG stream is
123 * being embedded into another stream and doesn't need its own signature,
124 * we should call png_set_sig_bytes() to tell libpng how many of the
125 * bytes have already been written.
128 png_write_sig(png_structp png_ptr
)
130 /* write the rest of the 8 byte signature */
131 png_write_data(png_ptr
, &png_sig
[png_ptr
->sig_bytes
],
132 (png_size_t
)8 - png_ptr
->sig_bytes
);
135 /* Write the IHDR chunk, and update the png_struct with the necessary
136 * information. Note that the rest of this code depends upon this
137 * information being correct.
140 png_write_IHDR(png_structp png_ptr
, png_uint_32 width
, png_uint_32 height
,
141 int bit_depth
, int color_type
, int compression_type
, int filter_type
,
144 png_byte buf
[13]; /* buffer to store the IHDR info */
146 png_debug(1, "in png_write_IHDR\n");
147 /* Check that we have valid input data from the application info */
150 case PNG_COLOR_TYPE_GRAY
:
157 case 16: png_ptr
->channels
= 1; break;
158 default: png_error(png_ptr
,"Invalid bit depth for grayscale image");
161 case PNG_COLOR_TYPE_RGB
:
162 if (bit_depth
!= 8 && bit_depth
!= 16)
163 png_error(png_ptr
, "Invalid bit depth for RGB image");
164 png_ptr
->channels
= 3;
166 case PNG_COLOR_TYPE_PALETTE
:
172 case 8: png_ptr
->channels
= 1; break;
173 default: png_error(png_ptr
, "Invalid bit depth for paletted image");
176 case PNG_COLOR_TYPE_GRAY_ALPHA
:
177 if (bit_depth
!= 8 && bit_depth
!= 16)
178 png_error(png_ptr
, "Invalid bit depth for grayscale+alpha image");
179 png_ptr
->channels
= 2;
181 case PNG_COLOR_TYPE_RGB_ALPHA
:
182 if (bit_depth
!= 8 && bit_depth
!= 16)
183 png_error(png_ptr
, "Invalid bit depth for RGBA image");
184 png_ptr
->channels
= 4;
187 png_error(png_ptr
, "Invalid image color type specified");
190 if (compression_type
!= PNG_COMPRESSION_TYPE_BASE
)
192 png_warning(png_ptr
, "Invalid compression type specified");
193 compression_type
= PNG_COMPRESSION_TYPE_BASE
;
196 if (filter_type
!= PNG_FILTER_TYPE_BASE
)
198 png_warning(png_ptr
, "Invalid filter type specified");
199 filter_type
= PNG_FILTER_TYPE_BASE
;
202 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
203 if (interlace_type
!= PNG_INTERLACE_NONE
&&
204 interlace_type
!= PNG_INTERLACE_ADAM7
)
206 png_warning(png_ptr
, "Invalid interlace type specified");
207 interlace_type
= PNG_INTERLACE_ADAM7
;
210 interlace_type
=PNG_INTERLACE_NONE
;
213 /* save off the relevent information */
214 png_ptr
->bit_depth
= (png_byte
)bit_depth
;
215 png_ptr
->color_type
= (png_byte
)color_type
;
216 png_ptr
->interlaced
= (png_byte
)interlace_type
;
217 png_ptr
->width
= width
;
218 png_ptr
->height
= height
;
220 png_ptr
->pixel_depth
= (png_byte
)(bit_depth
* png_ptr
->channels
);
221 png_ptr
->rowbytes
= ((width
* (png_size_t
)png_ptr
->pixel_depth
+ 7) >> 3);
222 /* set the usr info, so any transformations can modify it */
223 png_ptr
->usr_width
= png_ptr
->width
;
224 png_ptr
->usr_bit_depth
= png_ptr
->bit_depth
;
225 png_ptr
->usr_channels
= png_ptr
->channels
;
227 /* pack the header information into the buffer */
228 png_save_uint_32(buf
, width
);
229 png_save_uint_32(buf
+ 4, height
);
230 buf
[8] = (png_byte
)bit_depth
;
231 buf
[9] = (png_byte
)color_type
;
232 buf
[10] = (png_byte
)compression_type
;
233 buf
[11] = (png_byte
)filter_type
;
234 buf
[12] = (png_byte
)interlace_type
;
236 /* write the chunk */
237 png_write_chunk(png_ptr
, png_IHDR
, buf
, (png_size_t
)13);
239 /* initialize zlib with PNG info */
240 png_ptr
->zstream
.zalloc
= png_zalloc
;
241 png_ptr
->zstream
.zfree
= png_zfree
;
242 png_ptr
->zstream
.opaque
= (voidpf
)png_ptr
;
243 if (!(png_ptr
->do_filter
))
245 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
||
246 png_ptr
->bit_depth
< 8)
247 png_ptr
->do_filter
= PNG_FILTER_NONE
;
249 png_ptr
->do_filter
= PNG_ALL_FILTERS
;
251 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_CUSTOM_STRATEGY
))
253 if (png_ptr
->do_filter
!= PNG_FILTER_NONE
)
254 png_ptr
->zlib_strategy
= Z_FILTERED
;
256 png_ptr
->zlib_strategy
= Z_DEFAULT_STRATEGY
;
258 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_CUSTOM_LEVEL
))
259 png_ptr
->zlib_level
= Z_DEFAULT_COMPRESSION
;
260 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL
))
261 png_ptr
->zlib_mem_level
= 8;
262 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS
))
263 png_ptr
->zlib_window_bits
= 15;
264 if (!(png_ptr
->flags
& PNG_FLAG_ZLIB_CUSTOM_METHOD
))
265 png_ptr
->zlib_method
= 8;
266 deflateInit2(&png_ptr
->zstream
, png_ptr
->zlib_level
,
267 png_ptr
->zlib_method
, png_ptr
->zlib_window_bits
,
268 png_ptr
->zlib_mem_level
, png_ptr
->zlib_strategy
);
269 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
270 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
272 png_ptr
->mode
= PNG_HAVE_IHDR
;
275 /* write the palette. We are careful not to trust png_color to be in the
276 * correct order for PNG, so people can redefine it to any convenient
280 png_write_PLTE(png_structp png_ptr
, png_colorp palette
, png_uint_32 num_pal
)
286 png_debug(1, "in png_write_PLTE\n");
287 if (num_pal
== 0 || num_pal
> 256)
289 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
291 png_error(png_ptr
, "Invalid number of colors in palette");
295 png_warning(png_ptr
, "Invalid number of colors in palette");
300 png_ptr
->num_palette
= (png_uint_16
)num_pal
;
301 png_debug1(3, "num_palette = %d\n", png_ptr
->num_palette
);
303 png_write_chunk_start(png_ptr
, png_PLTE
, num_pal
* 3);
304 for (i
= 0, pal_ptr
= palette
; i
< num_pal
; i
++, pal_ptr
++)
306 buf
[0] = pal_ptr
->red
;
307 buf
[1] = pal_ptr
->green
;
308 buf
[2] = pal_ptr
->blue
;
309 png_write_chunk_data(png_ptr
, buf
, (png_size_t
)3);
311 png_write_chunk_end(png_ptr
);
312 png_ptr
->mode
|= PNG_HAVE_PLTE
;
315 /* write an IDAT chunk */
317 png_write_IDAT(png_structp png_ptr
, png_bytep data
, png_size_t length
)
319 png_debug(1, "in png_write_IDAT\n");
320 png_write_chunk(png_ptr
, png_IDAT
, data
, length
);
321 png_ptr
->mode
|= PNG_HAVE_IDAT
;
324 /* write an IEND chunk */
326 png_write_IEND(png_structp png_ptr
)
328 png_debug(1, "in png_write_IEND\n");
329 png_write_chunk(png_ptr
, png_IEND
, NULL
, (png_size_t
)0);
330 png_ptr
->mode
|= PNG_HAVE_IEND
;
333 #if defined(PNG_WRITE_gAMA_SUPPORTED)
334 /* write a gAMA chunk */
336 png_write_gAMA(png_structp png_ptr
, double file_gamma
)
341 png_debug(1, "in png_write_gAMA\n");
342 /* file_gamma is saved in 1/1000000ths */
343 igamma
= (png_uint_32
)(file_gamma
* 100000.0 + 0.5);
344 png_save_uint_32(buf
, igamma
);
345 png_write_chunk(png_ptr
, png_gAMA
, buf
, (png_size_t
)4);
349 #if defined(PNG_WRITE_sRGB_SUPPORTED)
350 /* write a sRGB chunk */
352 png_write_sRGB(png_structp png_ptr
, int srgb_intent
)
356 png_debug(1, "in png_write_sRGB\n");
357 if(srgb_intent
>= PNG_sRGB_INTENT_LAST
)
359 "Invalid sRGB rendering intent specified");
360 buf
[0]=(png_byte
)srgb_intent
;
361 png_write_chunk(png_ptr
, png_sRGB
, buf
, (png_size_t
)1);
365 #if defined(PNG_WRITE_sBIT_SUPPORTED)
366 /* write the sBIT chunk */
368 png_write_sBIT(png_structp png_ptr
, png_color_8p sbit
, int color_type
)
373 png_debug(1, "in png_write_sBIT\n");
374 /* make sure we don't depend upon the order of PNG_COLOR_8 */
375 if (color_type
& PNG_COLOR_MASK_COLOR
)
379 maxbits
= color_type
==PNG_COLOR_TYPE_PALETTE
? 8 : png_ptr
->usr_bit_depth
;
380 if (sbit
->red
== 0 || sbit
->red
> maxbits
||
381 sbit
->green
== 0 || sbit
->green
> maxbits
||
382 sbit
->blue
== 0 || sbit
->blue
> maxbits
)
384 png_warning(png_ptr
, "Invalid sBIT depth specified");
388 buf
[1] = sbit
->green
;
394 if (sbit
->gray
== 0 || sbit
->gray
> png_ptr
->usr_bit_depth
)
396 png_warning(png_ptr
, "Invalid sBIT depth specified");
403 if (color_type
& PNG_COLOR_MASK_ALPHA
)
405 if (sbit
->alpha
== 0 || sbit
->alpha
> png_ptr
->usr_bit_depth
)
407 png_warning(png_ptr
, "Invalid sBIT depth specified");
410 buf
[size
++] = sbit
->alpha
;
413 png_write_chunk(png_ptr
, png_sBIT
, buf
, size
);
417 #if defined(PNG_WRITE_cHRM_SUPPORTED)
418 /* write the cHRM chunk */
420 png_write_cHRM(png_structp png_ptr
, double white_x
, double white_y
,
421 double red_x
, double red_y
, double green_x
, double green_y
,
422 double blue_x
, double blue_y
)
427 png_debug(1, "in png_write_cHRM\n");
428 /* each value is saved int 1/1000000ths */
429 if (white_x
< 0 || white_x
> 0.8 || white_y
< 0 || white_y
> 0.8 ||
430 white_x
+ white_y
> 1.0)
432 png_warning(png_ptr
, "Invalid cHRM white point specified");
435 itemp
= (png_uint_32
)(white_x
* 100000.0 + 0.5);
436 png_save_uint_32(buf
, itemp
);
437 itemp
= (png_uint_32
)(white_y
* 100000.0 + 0.5);
438 png_save_uint_32(buf
+ 4, itemp
);
440 if (red_x
< 0 || red_x
> 0.8 || red_y
< 0 || red_y
> 0.8 ||
443 png_warning(png_ptr
, "Invalid cHRM red point specified");
446 itemp
= (png_uint_32
)(red_x
* 100000.0 + 0.5);
447 png_save_uint_32(buf
+ 8, itemp
);
448 itemp
= (png_uint_32
)(red_y
* 100000.0 + 0.5);
449 png_save_uint_32(buf
+ 12, itemp
);
451 if (green_x
< 0 || green_x
> 0.8 || green_y
< 0 || green_y
> 0.8 ||
452 green_x
+ green_y
> 1.0)
454 png_warning(png_ptr
, "Invalid cHRM green point specified");
457 itemp
= (png_uint_32
)(green_x
* 100000.0 + 0.5);
458 png_save_uint_32(buf
+ 16, itemp
);
459 itemp
= (png_uint_32
)(green_y
* 100000.0 + 0.5);
460 png_save_uint_32(buf
+ 20, itemp
);
462 if (blue_x
< 0 || blue_x
> 0.8 || blue_y
< 0 || blue_y
> 0.8 ||
463 blue_x
+ blue_y
> 1.0)
465 png_warning(png_ptr
, "Invalid cHRM blue point specified");
468 itemp
= (png_uint_32
)(blue_x
* 100000.0 + 0.5);
469 png_save_uint_32(buf
+ 24, itemp
);
470 itemp
= (png_uint_32
)(blue_y
* 100000.0 + 0.5);
471 png_save_uint_32(buf
+ 28, itemp
);
473 png_write_chunk(png_ptr
, png_cHRM
, buf
, (png_size_t
)32);
477 #if defined(PNG_WRITE_tRNS_SUPPORTED)
478 /* write the tRNS chunk */
480 png_write_tRNS(png_structp png_ptr
, png_bytep trans
, png_color_16p tran
,
481 int num_trans
, int color_type
)
485 png_debug(1, "in png_write_tRNS\n");
486 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
488 if (num_trans
<= 0 || num_trans
> (int)png_ptr
->num_palette
)
490 png_warning(png_ptr
,"Invalid number of transparent colors specified");
493 /* write the chunk out as it is */
494 png_write_chunk(png_ptr
, png_tRNS
, trans
, (png_size_t
)num_trans
);
496 else if (color_type
== PNG_COLOR_TYPE_GRAY
)
498 /* one 16 bit value */
499 png_save_uint_16(buf
, tran
->gray
);
500 png_write_chunk(png_ptr
, png_tRNS
, buf
, (png_size_t
)2);
502 else if (color_type
== PNG_COLOR_TYPE_RGB
)
504 /* three 16 bit values */
505 png_save_uint_16(buf
, tran
->red
);
506 png_save_uint_16(buf
+ 2, tran
->green
);
507 png_save_uint_16(buf
+ 4, tran
->blue
);
508 png_write_chunk(png_ptr
, png_tRNS
, buf
, (png_size_t
)6);
512 png_warning(png_ptr
, "Can't write tRNS with an alpha channel");
517 #if defined(PNG_WRITE_bKGD_SUPPORTED)
518 /* write the background chunk */
520 png_write_bKGD(png_structp png_ptr
, png_color_16p back
, int color_type
)
524 png_debug(1, "in png_write_bKGD\n");
525 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
527 if (back
->index
> png_ptr
->num_palette
)
529 png_warning(png_ptr
, "Invalid background palette index");
532 buf
[0] = back
->index
;
533 png_write_chunk(png_ptr
, png_bKGD
, buf
, (png_size_t
)1);
535 else if (color_type
& PNG_COLOR_MASK_COLOR
)
537 png_save_uint_16(buf
, back
->red
);
538 png_save_uint_16(buf
+ 2, back
->green
);
539 png_save_uint_16(buf
+ 4, back
->blue
);
540 png_write_chunk(png_ptr
, png_bKGD
, buf
, (png_size_t
)6);
544 png_save_uint_16(buf
, back
->gray
);
545 png_write_chunk(png_ptr
, png_bKGD
, buf
, (png_size_t
)2);
550 #if defined(PNG_WRITE_hIST_SUPPORTED)
551 /* write the histogram */
553 png_write_hIST(png_structp png_ptr
, png_uint_16p hist
, int num_hist
)
558 png_debug(1, "in png_write_hIST\n");
559 if (num_hist
> (int)png_ptr
->num_palette
)
561 png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist
,
562 png_ptr
->num_palette
);
563 png_warning(png_ptr
, "Invalid number of histogram entries specified");
567 png_write_chunk_start(png_ptr
, png_hIST
, (png_uint_32
)(num_hist
* 2));
568 for (i
= 0; i
< num_hist
; i
++)
570 png_save_uint_16(buf
, hist
[i
]);
571 png_write_chunk_data(png_ptr
, buf
, (png_size_t
)2);
573 png_write_chunk_end(png_ptr
);
577 #if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED) || \
578 defined(PNG_WRITE_pCAL_SUPPORTED)
579 /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
580 * and if invalid, correct the keyword rather than discarding the entire
581 * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
582 * length, forbids leading or trailing whitespace, multiple internal spaces,
583 * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
585 * The new_key is allocated to hold the corrected keyword and must be freed
586 * by the calling routine. This avoids problems with trying to write to
587 * static keywords without having to have duplicate copies of the strings.
590 png_check_keyword(png_structp png_ptr
, png_charp key
, png_charpp new_key
)
596 png_debug(1, "in png_check_keyword\n");
599 if (key
== NULL
|| (key_len
= png_strlen(key
)) == 0)
601 png_chunk_warning(png_ptr
, "zero length keyword");
602 return ((png_size_t
)0);
605 png_debug1(2, "Keyword to be checked is '%s'\n", key
);
607 *new_key
= (png_charp
)png_malloc(png_ptr
, (png_uint_32
)(key_len
+ 1));
609 /* Replace non-printing characters with a blank and print a warning */
610 for (kp
= key
, dp
= *new_key
; *kp
!= '\0'; kp
++, dp
++)
612 if (*kp
< 0x20 || (*kp
> 0x7E && (png_byte
)*kp
< 0xA1))
614 #if !defined(PNG_NO_STDIO)
617 sprintf(msg
, "invalid keyword character 0x%02X", *kp
);
618 png_chunk_warning(png_ptr
, msg
);
620 png_chunk_warning(png_ptr
, "invalid character in keyword");
631 /* Remove any trailing white space. */
632 kp
= *new_key
+ key_len
- 1;
635 png_chunk_warning(png_ptr
, "trailing spaces removed from keyword");
644 /* Remove any leading white space. */
648 png_chunk_warning(png_ptr
, "leading spaces removed from keyword");
657 png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp
);
659 /* Remove multiple internal spaces. */
660 for (kflag
= 0, dp
= *new_key
; *kp
!= '\0'; kp
++)
662 if (*kp
== ' ' && kflag
== 0)
681 png_chunk_warning(png_ptr
, "zero length keyword");
686 png_chunk_warning(png_ptr
, "keyword length must be 1 - 79 characters");
695 #if defined(PNG_WRITE_tEXt_SUPPORTED)
696 /* write a tEXt chunk */
698 png_write_tEXt(png_structp png_ptr
, png_charp key
, png_charp text
,
704 png_debug(1, "in png_write_tEXt\n");
705 if (key
== NULL
|| (key_len
= png_check_keyword(png_ptr
, key
, &new_key
))==0)
707 png_warning(png_ptr
, "Empty keyword in tEXt chunk");
711 if (text
== NULL
|| *text
== '\0')
714 /* make sure we include the 0 after the key */
715 png_write_chunk_start(png_ptr
, png_tEXt
, (png_uint_32
)key_len
+text_len
+1);
716 png_write_chunk_data(png_ptr
, (png_bytep
)new_key
, key_len
+ 1);
718 png_write_chunk_data(png_ptr
, (png_bytep
)text
, text_len
);
720 png_write_chunk_end(png_ptr
);
721 png_free(png_ptr
, new_key
);
725 #if defined(PNG_WRITE_zTXt_SUPPORTED)
726 /* write a compressed text chunk */
728 png_write_zTXt(png_structp png_ptr
, png_charp key
, png_charp text
,
729 png_size_t text_len
, int compression
)
735 png_charpp output_ptr
= NULL
; /* array of pointers to output */
736 int num_output_ptr
= 0; /* number of output pointers used */
737 int max_output_ptr
= 0; /* size of output_ptr */
739 png_debug(1, "in png_write_zTXt\n");
741 if (key
== NULL
|| (key_len
= png_check_keyword(png_ptr
, key
, &new_key
))==0)
743 png_warning(png_ptr
, "Empty keyword in zTXt chunk");
747 if (text
== NULL
|| *text
== '\0' || compression
==PNG_TEXT_COMPRESSION_NONE
)
749 png_write_tEXt(png_ptr
, new_key
, text
, (png_size_t
)0);
750 png_free(png_ptr
, new_key
);
754 png_free(png_ptr
, new_key
);
756 if (compression
>= PNG_TEXT_COMPRESSION_LAST
)
758 #if !defined(PNG_NO_STDIO)
760 sprintf(msg
, "Unknown zTXt compression type %d", compression
);
761 png_warning(png_ptr
, msg
);
763 png_warning(png_ptr
, "Unknown zTXt compression type");
765 compression
= PNG_TEXT_COMPRESSION_zTXt
;
768 /* We can't write the chunk until we find out how much data we have,
769 * which means we need to run the compressor first and save the
770 * output. This shouldn't be a problem, as the vast majority of
771 * comments should be reasonable, but we will set up an array of
772 * malloc'd pointers to be sure.
774 * If we knew the application was well behaved, we could simplify this
775 * greatly by assuming we can always malloc an output buffer large
776 * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
777 * and malloc this directly. The only time this would be a bad idea is
778 * if we can't malloc more than 64K and we have 64K of random input
779 * data, or if the input string is incredibly large (although this
780 * wouldn't cause a failure, just a slowdown due to swapping).
783 /* set up the compression buffers */
784 png_ptr
->zstream
.avail_in
= (uInt
)text_len
;
785 png_ptr
->zstream
.next_in
= (Bytef
*)text
;
786 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
787 png_ptr
->zstream
.next_out
= (Bytef
*)png_ptr
->zbuf
;
789 /* this is the same compression loop as in png_write_row() */
792 /* compress the data */
793 ret
= deflate(&png_ptr
->zstream
, Z_NO_FLUSH
);
797 if (png_ptr
->zstream
.msg
!= NULL
)
798 png_error(png_ptr
, png_ptr
->zstream
.msg
);
800 png_error(png_ptr
, "zlib error");
802 /* check to see if we need more room */
803 if (!png_ptr
->zstream
.avail_out
&& png_ptr
->zstream
.avail_in
)
805 /* make sure the output array has room */
806 if (num_output_ptr
>= max_output_ptr
)
810 old_max
= max_output_ptr
;
811 max_output_ptr
= num_output_ptr
+ 4;
812 if (output_ptr
!= NULL
)
816 old_ptr
= output_ptr
;
817 output_ptr
= (png_charpp
)png_malloc(png_ptr
,
818 (png_uint_32
)(max_output_ptr
* sizeof (png_charpp
)));
819 png_memcpy(output_ptr
, old_ptr
, old_max
* sizeof (png_charp
));
820 png_free(png_ptr
, old_ptr
);
823 output_ptr
= (png_charpp
)png_malloc(png_ptr
,
824 (png_uint_32
)(max_output_ptr
* sizeof (png_charp
)));
828 output_ptr
[num_output_ptr
] = (png_charp
)png_malloc(png_ptr
,
829 (png_uint_32
)png_ptr
->zbuf_size
);
830 png_memcpy(output_ptr
[num_output_ptr
], png_ptr
->zbuf
,
834 /* and reset the buffer */
835 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
836 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
838 /* continue until we don't have any more to compress */
839 } while (png_ptr
->zstream
.avail_in
);
841 /* finish the compression */
844 /* tell zlib we are finished */
845 ret
= deflate(&png_ptr
->zstream
, Z_FINISH
);
846 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
848 /* we got an error */
849 if (png_ptr
->zstream
.msg
!= NULL
)
850 png_error(png_ptr
, png_ptr
->zstream
.msg
);
852 png_error(png_ptr
, "zlib error");
855 /* check to see if we need more room */
856 if (!(png_ptr
->zstream
.avail_out
) && ret
== Z_OK
)
858 /* check to make sure our output array has room */
859 if (num_output_ptr
>= max_output_ptr
)
863 old_max
= max_output_ptr
;
864 max_output_ptr
= num_output_ptr
+ 4;
865 if (output_ptr
!= NULL
)
869 old_ptr
= output_ptr
;
870 /* This could be optimized to realloc() */
871 output_ptr
= (png_charpp
)png_malloc(png_ptr
,
872 (png_uint_32
)(max_output_ptr
* sizeof (png_charpp
)));
873 png_memcpy(output_ptr
, old_ptr
, old_max
* sizeof (png_charp
));
874 png_free(png_ptr
, old_ptr
);
877 output_ptr
= (png_charpp
)png_malloc(png_ptr
,
878 (png_uint_32
)(max_output_ptr
* sizeof (png_charp
)));
881 /* save off the data */
882 output_ptr
[num_output_ptr
] = (png_charp
)png_malloc(png_ptr
,
883 (png_uint_32
)png_ptr
->zbuf_size
);
884 png_memcpy(output_ptr
[num_output_ptr
], png_ptr
->zbuf
,
888 /* and reset the buffer pointers */
889 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
890 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
892 } while (ret
!= Z_STREAM_END
);
894 /* text length is number of buffers plus last buffer */
895 text_len
= png_ptr
->zbuf_size
* num_output_ptr
;
896 if (png_ptr
->zstream
.avail_out
< png_ptr
->zbuf_size
)
897 text_len
+= png_ptr
->zbuf_size
- (png_size_t
)png_ptr
->zstream
.avail_out
;
899 /* write start of chunk */
900 png_write_chunk_start(png_ptr
, png_zTXt
, (png_uint_32
)(key_len
+text_len
+2));
902 png_write_chunk_data(png_ptr
, (png_bytep
)key
, key_len
+ 1);
903 buf
[0] = (png_byte
)compression
;
904 /* write compression */
905 png_write_chunk_data(png_ptr
, (png_bytep
)buf
, (png_size_t
)1);
907 /* write saved output buffers, if any */
908 for (i
= 0; i
< num_output_ptr
; i
++)
910 png_write_chunk_data(png_ptr
,(png_bytep
)output_ptr
[i
],png_ptr
->zbuf_size
);
911 png_free(png_ptr
, output_ptr
[i
]);
913 if (max_output_ptr
!= 0)
914 png_free(png_ptr
, output_ptr
);
915 /* write anything left in zbuf */
916 if (png_ptr
->zstream
.avail_out
< (png_uint_32
)png_ptr
->zbuf_size
)
917 png_write_chunk_data(png_ptr
, png_ptr
->zbuf
,
918 png_ptr
->zbuf_size
- png_ptr
->zstream
.avail_out
);
919 /* close the chunk */
920 png_write_chunk_end(png_ptr
);
922 /* reset zlib for another zTXt or the image data */
923 deflateReset(&png_ptr
->zstream
);
928 #if defined(PNG_WRITE_oFFs_SUPPORTED)
929 /* write the oFFs chunk */
931 png_write_oFFs(png_structp png_ptr
, png_uint_32 x_offset
,
932 png_uint_32 y_offset
,
937 png_debug(1, "in png_write_oFFs\n");
938 if (unit_type
>= PNG_OFFSET_LAST
)
939 png_warning(png_ptr
, "Unrecognized unit type for oFFs chunk");
941 png_save_uint_32(buf
, x_offset
);
942 png_save_uint_32(buf
+ 4, y_offset
);
943 buf
[8] = (png_byte
)unit_type
;
945 png_write_chunk(png_ptr
, png_oFFs
, buf
, (png_size_t
)9);
949 #if defined(PNG_WRITE_pCAL_SUPPORTED)
950 /* write the pCAL chunk (png-scivis-19970203) */
952 png_write_pCAL(png_structp png_ptr
, png_charp purpose
, png_int_32 X0
,
953 png_int_32 X1
, int type
, int nparams
, png_charp units
, png_charpp params
)
955 png_size_t purpose_len
, units_len
, total_len
;
956 png_uint_32p params_len
;
958 png_charp new_purpose
;
961 png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams
);
962 if (type
>= PNG_EQUATION_LAST
)
963 png_warning(png_ptr
, "Unrecognized equation type for pCAL chunk");
965 purpose_len
= png_check_keyword(png_ptr
, purpose
, &new_purpose
) + 1;
966 png_debug1(3, "pCAL purpose length = %d\n", purpose_len
);
967 units_len
= png_strlen(units
) + (nparams
== 0 ? 0 : 1);
968 png_debug1(3, "pCAL units length = %d\n", units_len
);
969 total_len
= purpose_len
+ units_len
+ 10;
971 params_len
= (png_uint_32p
)png_malloc(png_ptr
, (png_uint_32
)(nparams
972 *sizeof(png_uint_32
)));
974 /* Find the length of each parameter, making sure we don't count the
975 null terminator for the last parameter. */
976 for (i
= 0; i
< nparams
; i
++)
978 params_len
[i
] = png_strlen(params
[i
]) + (i
== nparams
- 1 ? 0 : 1);
979 png_debug2(3, "pCAL parameter %d length = %d\n", i
, params_len
[i
]);
980 total_len
+= (png_size_t
)params_len
[i
];
983 png_debug1(3, "pCAL total length = %d\n", total_len
);
984 png_write_chunk_start(png_ptr
, png_pCAL
, (png_uint_32
)total_len
);
985 png_write_chunk_data(png_ptr
, (png_bytep
)new_purpose
, purpose_len
);
986 png_save_int_32(buf
, X0
);
987 png_save_int_32(buf
+ 4, X1
);
988 buf
[8] = (png_byte
)type
;
989 buf
[9] = (png_byte
)nparams
;
990 png_write_chunk_data(png_ptr
, buf
, (png_size_t
)10);
991 png_write_chunk_data(png_ptr
, (png_bytep
)units
, (png_size_t
)units_len
);
993 png_free(png_ptr
, new_purpose
);
995 for (i
= 0; i
< nparams
; i
++)
997 png_write_chunk_data(png_ptr
, (png_bytep
)params
[i
],
998 (png_size_t
)params_len
[i
]);
1001 png_free(png_ptr
, params_len
);
1002 png_write_chunk_end(png_ptr
);
1006 #if defined(PNG_WRITE_pHYs_SUPPORTED)
1007 /* write the pHYs chunk */
1009 png_write_pHYs(png_structp png_ptr
, png_uint_32 x_pixels_per_unit
,
1010 png_uint_32 y_pixels_per_unit
,
1015 png_debug(1, "in png_write_pHYs\n");
1016 if (unit_type
>= PNG_RESOLUTION_LAST
)
1017 png_warning(png_ptr
, "Unrecognized unit type for pHYs chunk");
1019 png_save_uint_32(buf
, x_pixels_per_unit
);
1020 png_save_uint_32(buf
+ 4, y_pixels_per_unit
);
1021 buf
[8] = (png_byte
)unit_type
;
1023 png_write_chunk(png_ptr
, png_pHYs
, buf
, (png_size_t
)9);
1027 #if defined(PNG_WRITE_tIME_SUPPORTED)
1028 /* Write the tIME chunk. Use either png_convert_from_struct_tm()
1029 * or png_convert_from_time_t(), or fill in the structure yourself.
1032 png_write_tIME(png_structp png_ptr
, png_timep mod_time
)
1036 png_debug(1, "in png_write_tIME\n");
1037 if (mod_time
->month
> 12 || mod_time
->month
< 1 ||
1038 mod_time
->day
> 31 || mod_time
->day
< 1 ||
1039 mod_time
->hour
> 23 || mod_time
->second
> 60)
1041 png_warning(png_ptr
, "Invalid time specified for tIME chunk");
1045 png_save_uint_16(buf
, mod_time
->year
);
1046 buf
[2] = mod_time
->month
;
1047 buf
[3] = mod_time
->day
;
1048 buf
[4] = mod_time
->hour
;
1049 buf
[5] = mod_time
->minute
;
1050 buf
[6] = mod_time
->second
;
1052 png_write_chunk(png_ptr
, png_tIME
, buf
, (png_size_t
)7);
1056 /* initializes the row writing capability of libpng */
1058 png_write_start_row(png_structp png_ptr
)
1060 png_size_t buf_size
;
1062 png_debug(1, "in png_write_start_row\n");
1063 buf_size
= (png_size_t
)(((png_ptr
->width
* png_ptr
->usr_channels
*
1064 png_ptr
->usr_bit_depth
+ 7) >> 3) + 1);
1066 /* set up row buffer */
1067 png_ptr
->row_buf
= (png_bytep
)png_malloc(png_ptr
, (png_uint_32
)buf_size
);
1068 png_ptr
->row_buf
[0] = PNG_FILTER_VALUE_NONE
;
1070 /* set up filtering buffer, if using this filter */
1071 if (png_ptr
->do_filter
& PNG_FILTER_SUB
)
1073 png_ptr
->sub_row
= (png_bytep
)png_malloc(png_ptr
,
1074 (png_ptr
->rowbytes
+ 1));
1075 png_ptr
->sub_row
[0] = PNG_FILTER_VALUE_SUB
;
1078 /* We only need to keep the previous row if we are using one of these. */
1079 if (png_ptr
->do_filter
& (PNG_FILTER_AVG
| PNG_FILTER_UP
| PNG_FILTER_PAETH
))
1081 /* set up previous row buffer */
1082 png_ptr
->prev_row
= (png_bytep
)png_malloc(png_ptr
, (png_uint_32
)buf_size
);
1083 png_memset(png_ptr
->prev_row
, 0, buf_size
);
1085 if (png_ptr
->do_filter
& PNG_FILTER_UP
)
1087 png_ptr
->up_row
= (png_bytep
)png_malloc(png_ptr
,
1088 (png_ptr
->rowbytes
+ 1));
1089 png_ptr
->up_row
[0] = PNG_FILTER_VALUE_UP
;
1092 if (png_ptr
->do_filter
& PNG_FILTER_AVG
)
1094 png_ptr
->avg_row
= (png_bytep
)png_malloc(png_ptr
,
1095 (png_ptr
->rowbytes
+ 1));
1096 png_ptr
->avg_row
[0] = PNG_FILTER_VALUE_AVG
;
1099 if (png_ptr
->do_filter
& PNG_FILTER_PAETH
)
1101 png_ptr
->paeth_row
= (png_bytep
)png_malloc(png_ptr
,
1102 (png_ptr
->rowbytes
+ 1));
1103 png_ptr
->paeth_row
[0] = PNG_FILTER_VALUE_PAETH
;
1107 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1108 /* if interlaced, we need to set up width and height of pass */
1109 if (png_ptr
->interlaced
)
1111 if (!(png_ptr
->transformations
& PNG_INTERLACE
))
1113 png_ptr
->num_rows
= (png_ptr
->height
+ png_pass_yinc
[0] - 1 -
1114 png_pass_ystart
[0]) / png_pass_yinc
[0];
1115 png_ptr
->usr_width
= (png_ptr
->width
+ png_pass_inc
[0] - 1 -
1116 png_pass_start
[0]) / png_pass_inc
[0];
1120 png_ptr
->num_rows
= png_ptr
->height
;
1121 png_ptr
->usr_width
= png_ptr
->width
;
1127 png_ptr
->num_rows
= png_ptr
->height
;
1128 png_ptr
->usr_width
= png_ptr
->width
;
1130 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
1131 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
1134 /* Internal use only. Called when finished processing a row of data. */
1136 png_write_finish_row(png_structp png_ptr
)
1140 png_debug(1, "in png_write_finish_row\n");
1142 png_ptr
->row_number
++;
1144 /* see if we are done */
1145 if (png_ptr
->row_number
< png_ptr
->num_rows
)
1148 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1149 /* if interlaced, go to next pass */
1150 if (png_ptr
->interlaced
)
1152 png_ptr
->row_number
= 0;
1153 if (png_ptr
->transformations
& PNG_INTERLACE
)
1159 /* loop until we find a non-zero width or height pass */
1163 if (png_ptr
->pass
>= 7)
1165 png_ptr
->usr_width
= (png_ptr
->width
+
1166 png_pass_inc
[png_ptr
->pass
] - 1 -
1167 png_pass_start
[png_ptr
->pass
]) /
1168 png_pass_inc
[png_ptr
->pass
];
1169 png_ptr
->num_rows
= (png_ptr
->height
+
1170 png_pass_yinc
[png_ptr
->pass
] - 1 -
1171 png_pass_ystart
[png_ptr
->pass
]) /
1172 png_pass_yinc
[png_ptr
->pass
];
1173 if (png_ptr
->transformations
& PNG_INTERLACE
)
1175 } while (png_ptr
->usr_width
== 0 || png_ptr
->num_rows
== 0);
1179 /* reset the row above the image for the next pass */
1180 if (png_ptr
->pass
< 7)
1182 if (png_ptr
->prev_row
!= NULL
)
1183 png_memset(png_ptr
->prev_row
, 0,
1184 (png_size_t
) (((png_uint_32
)png_ptr
->usr_channels
*
1185 (png_uint_32
)png_ptr
->usr_bit_depth
*
1186 png_ptr
->width
+ 7) >> 3) + 1);
1192 /* if we get here, we've just written the last row, so we need
1193 to flush the compressor */
1196 /* tell the compressor we are done */
1197 ret
= deflate(&png_ptr
->zstream
, Z_FINISH
);
1198 /* check for an error */
1199 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
)
1201 if (png_ptr
->zstream
.msg
!= NULL
)
1202 png_error(png_ptr
, png_ptr
->zstream
.msg
);
1204 png_error(png_ptr
, "zlib error");
1206 /* check to see if we need more room */
1207 if (!(png_ptr
->zstream
.avail_out
) && ret
== Z_OK
)
1209 png_write_IDAT(png_ptr
, png_ptr
->zbuf
, png_ptr
->zbuf_size
);
1210 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
1211 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
1213 } while (ret
!= Z_STREAM_END
);
1215 /* write any extra space */
1216 if (png_ptr
->zstream
.avail_out
< png_ptr
->zbuf_size
)
1218 png_write_IDAT(png_ptr
, png_ptr
->zbuf
, png_ptr
->zbuf_size
-
1219 png_ptr
->zstream
.avail_out
);
1222 deflateReset(&png_ptr
->zstream
);
1225 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
1226 /* Pick out the correct pixels for the interlace pass.
1227 * The basic idea here is to go through the row with a source
1228 * pointer and a destination pointer (sp and dp), and copy the
1229 * correct pixels for the pass. As the row gets compacted,
1230 * sp will always be >= dp, so we should never overwrite anything.
1231 * See the default: case for the easiest code to understand.
1234 png_do_write_interlace(png_row_infop row_info
, png_bytep row
, int pass
)
1236 png_debug(1, "in png_do_write_interlace\n");
1237 /* we don't have to do anything on the last pass (6) */
1238 #if defined(PNG_USELESS_TESTS_SUPPORTED)
1239 if (row
!= NULL
&& row_info
!= NULL
&& pass
< 6)
1244 /* each pixel depth is handled separately */
1245 switch (row_info
->pixel_depth
)
1255 png_uint_32 row_width
= row_info
->width
;
1260 for (i
= png_pass_start
[pass
]; i
< row_width
;
1261 i
+= png_pass_inc
[pass
])
1263 sp
= row
+ (png_size_t
)(i
>> 3);
1264 value
= (int)(*sp
>> (7 - (int)(i
& 7))) & 0x1;
1265 d
|= (value
<< shift
);
1270 *dp
++ = (png_byte
)d
;
1289 png_uint_32 row_width
= row_info
->width
;
1294 for (i
= png_pass_start
[pass
]; i
< row_width
;
1295 i
+= png_pass_inc
[pass
])
1297 sp
= row
+ (png_size_t
)(i
>> 2);
1298 value
= (*sp
>> ((3 - (int)(i
& 3)) << 1)) & 0x3;
1299 d
|= (value
<< shift
);
1304 *dp
++ = (png_byte
)d
;
1322 png_uint_32 row_width
= row_info
->width
;
1327 for (i
= png_pass_start
[pass
]; i
< row_width
;
1328 i
+= png_pass_inc
[pass
])
1330 sp
= row
+ (png_size_t
)(i
>> 1);
1331 value
= (*sp
>> ((1 - (int)(i
& 1)) << 2)) & 0xf;
1332 d
|= (value
<< shift
);
1337 *dp
++ = (png_byte
)d
;
1352 png_uint_32 row_width
= row_info
->width
;
1353 png_size_t pixel_bytes
;
1355 /* start at the beginning */
1357 /* find out how many bytes each pixel takes up */
1358 pixel_bytes
= (row_info
->pixel_depth
>> 3);
1359 /* loop through the row, only looking at the pixels that
1361 for (i
= png_pass_start
[pass
]; i
< row_width
;
1362 i
+= png_pass_inc
[pass
])
1364 /* find out where the original pixel is */
1365 sp
= row
+ (png_size_t
)i
* pixel_bytes
;
1366 /* move the pixel */
1368 png_memcpy(dp
, sp
, pixel_bytes
);
1375 /* set new row width */
1376 row_info
->width
= (row_info
->width
+
1377 png_pass_inc
[pass
] - 1 -
1378 png_pass_start
[pass
]) /
1380 row_info
->rowbytes
= ((row_info
->width
*
1381 row_info
->pixel_depth
+ 7) >> 3);
1386 /* This filters the row, chooses which filter to use, if it has not already
1387 * been specified by the application, and then writes the row out with the
1390 #define PNG_MAXSUM (~((png_uint_32)0) >> 1)
1391 #define PNG_HISHIFT 10
1392 #define PNG_LOMASK ((png_uint_32)0xffffL)
1393 #define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
1395 png_write_find_filter(png_structp png_ptr
, png_row_infop row_info
)
1397 png_bytep prev_row
, best_row
, row_buf
;
1398 png_uint_32 mins
, bpp
;
1399 png_byte filter_to_do
= png_ptr
->do_filter
;
1400 png_uint_32 row_bytes
= row_info
->rowbytes
;
1401 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1402 int num_p_filters
= (int)png_ptr
->num_prev_filters
;
1405 png_debug(1, "in png_write_find_filter\n");
1406 /* find out how many bytes offset each pixel is */
1407 bpp
= (row_info
->pixel_depth
+ 7) / 8;
1409 prev_row
= png_ptr
->prev_row
;
1410 best_row
= row_buf
= png_ptr
->row_buf
;
1413 /* The prediction method we use is to find which method provides the
1414 * smallest value when summing the absolute values of the distances
1415 * from zero, using anything >= 128 as negative numbers. This is known
1416 * as the "minimum sum of absolute differences" heuristic. Other
1417 * heuristics are the "weighted minimum sum of absolute differences"
1418 * (experimental and can in theory improve compression), and the "zlib
1419 * predictive" method (not implemented yet), which does test compressions
1420 * of lines using different filter methods, and then chooses the
1421 * (series of) filter(s) that give minimum compressed data size (VERY
1422 * computationally expensive).
1424 * GRR 980525: consider also
1425 * (1) minimum sum of absolute differences from running average (i.e.,
1426 * keep running sum of non-absolute differences & count of bytes)
1427 * [track dispersion, too? restart average if dispersion too large?]
1428 * (1b) minimum sum of absolute differences from sliding average, probably
1429 * with window size <= deflate window (usually 32K)
1430 * (2) minimum sum of squared differences from zero or running average
1431 * (i.e., ~ root-mean-square approach)
1435 /* We don't need to test the 'no filter' case if this is the only filter
1436 * that has been chosen, as it doesn't actually do anything to the data.
1438 if (filter_to_do
& PNG_FILTER_NONE
&&
1439 filter_to_do
!= PNG_FILTER_NONE
)
1442 png_uint_32 sum
= 0;
1446 for (i
= 0, rp
= row_buf
+ 1; i
< row_bytes
; i
++, rp
++)
1449 sum
+= (v
< 128) ? v
: 256 - v
;
1452 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1453 if (png_ptr
->heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
1455 png_uint_32 sumhi
, sumlo
;
1457 sumlo
= sum
& PNG_LOMASK
;
1458 sumhi
= (sum
>> PNG_HISHIFT
) & PNG_HIMASK
; /* Gives us some footroom */
1460 /* Reduce the sum if we match any of the previous rows */
1461 for (j
= 0; j
< num_p_filters
; j
++)
1463 if (png_ptr
->prev_filters
[j
] == PNG_FILTER_VALUE_NONE
)
1465 sumlo
= (sumlo
* png_ptr
->filter_weights
[j
]) >>
1467 sumhi
= (sumhi
* png_ptr
->filter_weights
[j
]) >>
1472 /* Factor in the cost of this filter (this is here for completeness,
1473 * but it makes no sense to have a "cost" for the NONE filter, as
1474 * it has the minimum possible computational cost - none).
1476 sumlo
= (sumlo
* png_ptr
->filter_costs
[PNG_FILTER_VALUE_NONE
]) >>
1478 sumhi
= (sumhi
* png_ptr
->filter_costs
[PNG_FILTER_VALUE_NONE
]) >>
1481 if (sumhi
> PNG_HIMASK
)
1484 sum
= (sumhi
<< PNG_HISHIFT
) + sumlo
;
1491 if (filter_to_do
== PNG_FILTER_SUB
)
1492 /* it's the only filter so no testing is needed */
1494 png_bytep rp
, lp
, dp
;
1496 for (i
= 0, rp
= row_buf
+ 1, dp
= png_ptr
->sub_row
+ 1; i
< bpp
;
1501 for (lp
= row_buf
+ 1; i
< row_bytes
;
1502 i
++, rp
++, lp
++, dp
++)
1504 *dp
= (png_byte
)(((int)*rp
- (int)*lp
) & 0xff);
1506 best_row
= png_ptr
->sub_row
;
1509 else if (filter_to_do
& PNG_FILTER_SUB
)
1511 png_bytep rp
, dp
, lp
;
1512 png_uint_32 sum
= 0, lmins
= mins
;
1516 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1517 /* We temporarily increase the "minimum sum" by the factor we
1518 * would reduce the sum of this filter, so that we can do the
1519 * early exit comparison without scaling the sum each time.
1521 if (png_ptr
->heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
1524 png_uint_32 lmhi
, lmlo
;
1525 lmlo
= lmins
& PNG_LOMASK
;
1526 lmhi
= (lmins
>> PNG_HISHIFT
) & PNG_HIMASK
;
1528 for (j
= 0; j
< num_p_filters
; j
++)
1530 if (png_ptr
->prev_filters
[j
] == PNG_FILTER_VALUE_SUB
)
1532 lmlo
= (lmlo
* png_ptr
->inv_filter_weights
[j
]) >>
1534 lmhi
= (lmhi
* png_ptr
->inv_filter_weights
[j
]) >>
1539 lmlo
= (lmlo
* png_ptr
->inv_filter_costs
[PNG_FILTER_VALUE_SUB
]) >>
1541 lmhi
= (lmhi
* png_ptr
->inv_filter_costs
[PNG_FILTER_VALUE_SUB
]) >>
1544 if (lmhi
> PNG_HIMASK
)
1547 lmins
= (lmhi
<< PNG_HISHIFT
) + lmlo
;
1551 for (i
= 0, rp
= row_buf
+ 1, dp
= png_ptr
->sub_row
+ 1; i
< bpp
;
1556 sum
+= (v
< 128) ? v
: 256 - v
;
1558 for (lp
= row_buf
+ 1; i
< row_info
->rowbytes
;
1559 i
++, rp
++, lp
++, dp
++)
1561 v
= *dp
= (png_byte
)(((int)*rp
- (int)*lp
) & 0xff);
1563 sum
+= (v
< 128) ? v
: 256 - v
;
1565 if (sum
> lmins
) /* We are already worse, don't continue. */
1569 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1570 if (png_ptr
->heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
1573 png_uint_32 sumhi
, sumlo
;
1574 sumlo
= sum
& PNG_LOMASK
;
1575 sumhi
= (sum
>> PNG_HISHIFT
) & PNG_HIMASK
;
1577 for (j
= 0; j
< num_p_filters
; j
++)
1579 if (png_ptr
->prev_filters
[j
] == PNG_FILTER_VALUE_SUB
)
1581 sumlo
= (sumlo
* png_ptr
->inv_filter_weights
[j
]) >>
1583 sumhi
= (sumhi
* png_ptr
->inv_filter_weights
[j
]) >>
1588 sumlo
= (sumlo
* png_ptr
->inv_filter_costs
[PNG_FILTER_VALUE_SUB
]) >>
1590 sumhi
= (sumhi
* png_ptr
->inv_filter_costs
[PNG_FILTER_VALUE_SUB
]) >>
1593 if (sumhi
> PNG_HIMASK
)
1596 sum
= (sumhi
<< PNG_HISHIFT
) + sumlo
;
1603 best_row
= png_ptr
->sub_row
;
1608 if (filter_to_do
== PNG_FILTER_UP
)
1610 png_bytep rp
, dp
, pp
;
1613 for (i
= 0, rp
= row_buf
+ 1, dp
= png_ptr
->up_row
+ 1,
1614 pp
= prev_row
+ 1; i
< row_bytes
;
1615 i
++, rp
++, pp
++, dp
++)
1617 *dp
= (png_byte
)(((int)*rp
- (int)*pp
) & 0xff);
1619 best_row
= png_ptr
->up_row
;
1622 else if (filter_to_do
& PNG_FILTER_UP
)
1624 png_bytep rp
, dp
, pp
;
1625 png_uint_32 sum
= 0, lmins
= mins
;
1630 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1631 if (png_ptr
->heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
1634 png_uint_32 lmhi
, lmlo
;
1635 lmlo
= lmins
& PNG_LOMASK
;
1636 lmhi
= (lmins
>> PNG_HISHIFT
) & PNG_HIMASK
;
1638 for (j
= 0; j
< num_p_filters
; j
++)
1640 if (png_ptr
->prev_filters
[j
] == PNG_FILTER_VALUE_UP
)
1642 lmlo
= (lmlo
* png_ptr
->inv_filter_weights
[j
]) >>
1644 lmhi
= (lmhi
* png_ptr
->inv_filter_weights
[j
]) >>
1649 lmlo
= (lmlo
* png_ptr
->inv_filter_costs
[PNG_FILTER_VALUE_UP
]) >>
1651 lmhi
= (lmhi
* png_ptr
->inv_filter_costs
[PNG_FILTER_VALUE_UP
]) >>
1654 if (lmhi
> PNG_HIMASK
)
1657 lmins
= (lmhi
<< PNG_HISHIFT
) + lmlo
;
1661 for (i
= 0, rp
= row_buf
+ 1, dp
= png_ptr
->up_row
+ 1,
1662 pp
= prev_row
+ 1; i
< row_bytes
; i
++)
1664 v
= *dp
++ = (png_byte
)(((int)*rp
++ - (int)*pp
++) & 0xff);
1666 sum
+= (v
< 128) ? v
: 256 - v
;
1668 if (sum
> lmins
) /* We are already worse, don't continue. */
1672 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1673 if (png_ptr
->heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
1676 png_uint_32 sumhi
, sumlo
;
1677 sumlo
= sum
& PNG_LOMASK
;
1678 sumhi
= (sum
>> PNG_HISHIFT
) & PNG_HIMASK
;
1680 for (j
= 0; j
< num_p_filters
; j
++)
1682 if (png_ptr
->prev_filters
[j
] == PNG_FILTER_VALUE_UP
)
1684 sumlo
= (sumlo
* png_ptr
->filter_weights
[j
]) >>
1686 sumhi
= (sumhi
* png_ptr
->filter_weights
[j
]) >>
1691 sumlo
= (sumlo
* png_ptr
->filter_costs
[PNG_FILTER_VALUE_UP
]) >>
1693 sumhi
= (sumhi
* png_ptr
->filter_costs
[PNG_FILTER_VALUE_UP
]) >>
1696 if (sumhi
> PNG_HIMASK
)
1699 sum
= (sumhi
<< PNG_HISHIFT
) + sumlo
;
1706 best_row
= png_ptr
->up_row
;
1711 if (filter_to_do
== PNG_FILTER_AVG
)
1713 png_bytep rp
, dp
, pp
, lp
;
1715 for (i
= 0, rp
= row_buf
+ 1, dp
= png_ptr
->avg_row
+ 1,
1716 pp
= prev_row
+ 1; i
< bpp
; i
++)
1718 *dp
++ = (png_byte
)(((int)*rp
++ - ((int)*pp
++ / 2)) & 0xff);
1720 for (lp
= row_buf
+ 1; i
< row_bytes
; i
++)
1722 *dp
++ = (png_byte
)(((int)*rp
++ - (((int)*pp
++ + (int)*lp
++) / 2))
1725 best_row
= png_ptr
->avg_row
;
1728 else if (filter_to_do
& PNG_FILTER_AVG
)
1730 png_bytep rp
, dp
, pp
, lp
;
1731 png_uint_32 sum
= 0, lmins
= mins
;
1735 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1736 if (png_ptr
->heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
1739 png_uint_32 lmhi
, lmlo
;
1740 lmlo
= lmins
& PNG_LOMASK
;
1741 lmhi
= (lmins
>> PNG_HISHIFT
) & PNG_HIMASK
;
1743 for (j
= 0; j
< num_p_filters
; j
++)
1745 if (png_ptr
->prev_filters
[j
] == PNG_FILTER_VALUE_AVG
)
1747 lmlo
= (lmlo
* png_ptr
->inv_filter_weights
[j
]) >>
1749 lmhi
= (lmhi
* png_ptr
->inv_filter_weights
[j
]) >>
1754 lmlo
= (lmlo
* png_ptr
->inv_filter_costs
[PNG_FILTER_VALUE_AVG
]) >>
1756 lmhi
= (lmhi
* png_ptr
->inv_filter_costs
[PNG_FILTER_VALUE_AVG
]) >>
1759 if (lmhi
> PNG_HIMASK
)
1762 lmins
= (lmhi
<< PNG_HISHIFT
) + lmlo
;
1766 for (i
= 0, rp
= row_buf
+ 1, dp
= png_ptr
->avg_row
+ 1,
1767 pp
= prev_row
+ 1; i
< bpp
; i
++)
1769 v
= *dp
++ = (png_byte
)(((int)*rp
++ - ((int)*pp
++ / 2)) & 0xff);
1771 sum
+= (v
< 128) ? v
: 256 - v
;
1773 for (lp
= row_buf
+ 1; i
< row_bytes
; i
++)
1776 (png_byte
)(((int)*rp
++ - (((int)*pp
++ + (int)*lp
++) / 2)) & 0xff);
1778 sum
+= (v
< 128) ? v
: 256 - v
;
1780 if (sum
> lmins
) /* We are already worse, don't continue. */
1784 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1785 if (png_ptr
->heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
1788 png_uint_32 sumhi
, sumlo
;
1789 sumlo
= sum
& PNG_LOMASK
;
1790 sumhi
= (sum
>> PNG_HISHIFT
) & PNG_HIMASK
;
1792 for (j
= 0; j
< num_p_filters
; j
++)
1794 if (png_ptr
->prev_filters
[j
] == PNG_FILTER_VALUE_NONE
)
1796 sumlo
= (sumlo
* png_ptr
->filter_weights
[j
]) >>
1798 sumhi
= (sumhi
* png_ptr
->filter_weights
[j
]) >>
1803 sumlo
= (sumlo
* png_ptr
->filter_costs
[PNG_FILTER_VALUE_AVG
]) >>
1805 sumhi
= (sumhi
* png_ptr
->filter_costs
[PNG_FILTER_VALUE_AVG
]) >>
1808 if (sumhi
> PNG_HIMASK
)
1811 sum
= (sumhi
<< PNG_HISHIFT
) + sumlo
;
1818 best_row
= png_ptr
->avg_row
;
1823 if (filter_to_do
== PNG_FILTER_PAETH
)
1825 png_bytep rp
, dp
, pp
, cp
, lp
;
1827 for (i
= 0, rp
= row_buf
+ 1, dp
= png_ptr
->paeth_row
+ 1,
1828 pp
= prev_row
+ 1; i
< bpp
; i
++)
1830 *dp
++ = (png_byte
)(((int)*rp
++ - (int)*pp
++) & 0xff);
1833 for (lp
= row_buf
+ 1, cp
= prev_row
+ 1; i
< row_bytes
; i
++)
1835 int a
, b
, c
, pa
, pb
, pc
, p
;
1849 pa
= p
< 0 ? -p
: p
;
1850 pb
= pc
< 0 ? -pc
: pc
;
1851 pc
= (p
+ pc
) < 0 ? -(p
+ pc
) : p
+ pc
;
1854 p
= (pa
<= pb
&& pa
<=pc
) ? a
: (pb
<= pc
) ? b
: c
;
1856 *dp
++ = (png_byte
)(((int)*rp
++ - p
) & 0xff);
1858 best_row
= png_ptr
->paeth_row
;
1861 else if (filter_to_do
& PNG_FILTER_PAETH
)
1863 png_bytep rp
, dp
, pp
, cp
, lp
;
1864 png_uint_32 sum
= 0, lmins
= mins
;
1868 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1869 if (png_ptr
->heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
1872 png_uint_32 lmhi
, lmlo
;
1873 lmlo
= lmins
& PNG_LOMASK
;
1874 lmhi
= (lmins
>> PNG_HISHIFT
) & PNG_HIMASK
;
1876 for (j
= 0; j
< num_p_filters
; j
++)
1878 if (png_ptr
->prev_filters
[j
] == PNG_FILTER_VALUE_PAETH
)
1880 lmlo
= (lmlo
* png_ptr
->inv_filter_weights
[j
]) >>
1882 lmhi
= (lmhi
* png_ptr
->inv_filter_weights
[j
]) >>
1887 lmlo
= (lmlo
* png_ptr
->inv_filter_costs
[PNG_FILTER_VALUE_PAETH
]) >>
1889 lmhi
= (lmhi
* png_ptr
->inv_filter_costs
[PNG_FILTER_VALUE_PAETH
]) >>
1892 if (lmhi
> PNG_HIMASK
)
1895 lmins
= (lmhi
<< PNG_HISHIFT
) + lmlo
;
1899 for (i
= 0, rp
= row_buf
+ 1, dp
= png_ptr
->paeth_row
+ 1,
1900 pp
= prev_row
+ 1; i
< bpp
; i
++)
1902 v
= *dp
++ = (png_byte
)(((int)*rp
++ - (int)*pp
++) & 0xff);
1904 sum
+= (v
< 128) ? v
: 256 - v
;
1907 for (lp
= row_buf
+ 1, cp
= prev_row
+ 1; i
< row_bytes
; i
++)
1909 int a
, b
, c
, pa
, pb
, pc
, p
;
1915 #ifndef PNG_SLOW_PAETH
1923 pa
= p
< 0 ? -p
: p
;
1924 pb
= pc
< 0 ? -pc
: pc
;
1925 pc
= (p
+ pc
) < 0 ? -(p
+ pc
) : p
+ pc
;
1927 p
= (pa
<= pb
&& pa
<=pc
) ? a
: (pb
<= pc
) ? b
: c
;
1928 #else /* PNG_SLOW_PAETH */
1933 if (pa
<= pb
&& pa
<= pc
)
1939 #endif /* PNG_SLOW_PAETH */
1941 v
= *dp
++ = (png_byte
)(((int)*rp
++ - p
) & 0xff);
1943 sum
+= (v
< 128) ? v
: 256 - v
;
1945 if (sum
> lmins
) /* We are already worse, don't continue. */
1949 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1950 if (png_ptr
->heuristic_method
== PNG_FILTER_HEURISTIC_WEIGHTED
)
1953 png_uint_32 sumhi
, sumlo
;
1954 sumlo
= sum
& PNG_LOMASK
;
1955 sumhi
= (sum
>> PNG_HISHIFT
) & PNG_HIMASK
;
1957 for (j
= 0; j
< num_p_filters
; j
++)
1959 if (png_ptr
->prev_filters
[j
] == PNG_FILTER_VALUE_PAETH
)
1961 sumlo
= (sumlo
* png_ptr
->filter_weights
[j
]) >>
1963 sumhi
= (sumhi
* png_ptr
->filter_weights
[j
]) >>
1968 sumlo
= (sumlo
* png_ptr
->filter_costs
[PNG_FILTER_VALUE_PAETH
]) >>
1970 sumhi
= (sumhi
* png_ptr
->filter_costs
[PNG_FILTER_VALUE_PAETH
]) >>
1973 if (sumhi
> PNG_HIMASK
)
1976 sum
= (sumhi
<< PNG_HISHIFT
) + sumlo
;
1982 best_row
= png_ptr
->paeth_row
;
1986 /* Do the actual writing of the filtered row data from the chosen filter. */
1988 png_write_filtered_row(png_ptr
, best_row
);
1990 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1991 /* Save the type of filter we picked this time for future calculations */
1992 if (png_ptr
->num_prev_filters
> 0)
1995 for (j
= 1; j
< num_p_filters
; j
++)
1997 png_ptr
->prev_filters
[j
] = png_ptr
->prev_filters
[j
- 1];
1999 png_ptr
->prev_filters
[j
] = best_row
[0];
2005 /* Do the actual writing of a previously filtered row. */
2007 png_write_filtered_row(png_structp png_ptr
, png_bytep filtered_row
)
2009 png_debug(1, "in png_write_filtered_row\n");
2010 png_debug1(2, "filter = %d\n", filtered_row
[0]);
2011 /* set up the zlib input buffer */
2012 png_ptr
->zstream
.next_in
= filtered_row
;
2013 png_ptr
->zstream
.avail_in
= (uInt
)png_ptr
->row_info
.rowbytes
+ 1;
2014 /* repeat until we have compressed all the data */
2017 int ret
; /* return of zlib */
2019 /* compress the data */
2020 ret
= deflate(&png_ptr
->zstream
, Z_NO_FLUSH
);
2021 /* check for compression errors */
2024 if (png_ptr
->zstream
.msg
!= NULL
)
2025 png_error(png_ptr
, png_ptr
->zstream
.msg
);
2027 png_error(png_ptr
, "zlib error");
2030 /* see if it is time to write another IDAT */
2031 if (!(png_ptr
->zstream
.avail_out
))
2033 /* write the IDAT and reset the zlib output buffer */
2034 png_write_IDAT(png_ptr
, png_ptr
->zbuf
, png_ptr
->zbuf_size
);
2035 png_ptr
->zstream
.next_out
= png_ptr
->zbuf
;
2036 png_ptr
->zstream
.avail_out
= (uInt
)png_ptr
->zbuf_size
;
2038 /* repeat until all data has been compressed */
2039 } while (png_ptr
->zstream
.avail_in
);
2041 /* swap the current and previous rows */
2042 if (png_ptr
->prev_row
!= NULL
)
2046 tptr
= png_ptr
->prev_row
;
2047 png_ptr
->prev_row
= png_ptr
->row_buf
;
2048 png_ptr
->row_buf
= tptr
;
2051 /* finish row - updates counters and flushes zlib if last row */
2052 png_write_finish_row(png_ptr
);
2054 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
2055 png_ptr
->flush_rows
++;
2057 if (png_ptr
->flush_dist
> 0 &&
2058 png_ptr
->flush_rows
>= png_ptr
->flush_dist
)
2060 png_write_flush(png_ptr
);
2062 #endif /* PNG_WRITE_FLUSH_SUPPORTED */