Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2 | /* pngwutil.c - utilities to write a PNG file |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 3 | * |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 4 | * Last changed in libpng 1.5.3 [(PENDING RELEASE)] |
Glenn Randers-Pehrson | 64b863c | 2011-01-04 09:57:06 -0600 | [diff] [blame] | 5 | * Copyright (c) 1998-2011 Glenn Randers-Pehrson |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 6 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
| 7 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
Glenn Randers-Pehrson | 3e61d79 | 2009-06-24 09:31:28 -0500 | [diff] [blame] | 8 | * |
Glenn Randers-Pehrson | bfbf865 | 2009-06-26 21:46:52 -0500 | [diff] [blame] | 9 | * This code is released under the libpng license. |
Glenn Randers-Pehrson | c332bbc | 2009-06-25 13:43:50 -0500 | [diff] [blame] | 10 | * For conditions of distribution and use, see the disclaimer |
Glenn Randers-Pehrson | 037023b | 2009-06-24 10:27:36 -0500 | [diff] [blame] | 11 | * and license in png.h |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 12 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 13 | |
Glenn Randers-Pehrson | 9c90d7f | 2009-07-19 13:11:25 -0500 | [diff] [blame] | 14 | #include "pngpriv.h" |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 15 | |
Glenn Randers-Pehrson | c3cd22b | 2010-03-08 21:10:25 -0600 | [diff] [blame] | 16 | #ifdef PNG_WRITE_SUPPORTED |
| 17 | |
Glenn Randers-Pehrson | 34713ce | 2010-04-28 07:49:28 -0500 | [diff] [blame] | 18 | #ifdef PNG_WRITE_INT_FUNCTIONS_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 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. |
| 22 | */ |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 23 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 24 | png_save_uint_32(png_bytep buf, png_uint_32 i) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 25 | { |
| 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); |
| 30 | } |
| 31 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 32 | #ifdef PNG_SAVE_INT_32_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 33 | /* The png_save_int_32 function assumes integers are stored in two's |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 34 | * complement format. If this isn't the case, then this routine needs to |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 35 | * be modified to write data in two's complement format. Note that, |
| 36 | * the following works correctly even if png_int_32 has more than 32 bits |
| 37 | * (compare the more complex code required on read for sign extention.) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 38 | */ |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 39 | void PNGAPI |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 40 | png_save_int_32(png_bytep buf, png_int_32 i) |
| 41 | { |
| 42 | buf[0] = (png_byte)((i >> 24) & 0xff); |
| 43 | buf[1] = (png_byte)((i >> 16) & 0xff); |
| 44 | buf[2] = (png_byte)((i >> 8) & 0xff); |
| 45 | buf[3] = (png_byte)(i & 0xff); |
| 46 | } |
Glenn Randers-Pehrson | 9c90d7f | 2009-07-19 13:11:25 -0500 | [diff] [blame] | 47 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 48 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 49 | /* Place a 16-bit number into a buffer in PNG byte order. |
| 50 | * The parameter is declared unsigned int, not png_uint_16, |
| 51 | * just to avoid potential problems on pre-ANSI C compilers. |
| 52 | */ |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 53 | void PNGAPI |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 54 | png_save_uint_16(png_bytep buf, unsigned int i) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 55 | { |
| 56 | buf[0] = (png_byte)((i >> 8) & 0xff); |
| 57 | buf[1] = (png_byte)(i & 0xff); |
| 58 | } |
Glenn Randers-Pehrson | 34713ce | 2010-04-28 07:49:28 -0500 | [diff] [blame] | 59 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 60 | |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 61 | /* Simple function to write the signature. If we have already written |
| 62 | * the magic bytes of the signature, or more likely, the PNG stream is |
| 63 | * being embedded into another stream and doesn't need its own signature, |
| 64 | * we should call png_set_sig_bytes() to tell libpng how many of the |
| 65 | * bytes have already been written. |
| 66 | */ |
Glenn Randers-Pehrson | 9c90d7f | 2009-07-19 13:11:25 -0500 | [diff] [blame] | 67 | void PNGAPI |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 68 | png_write_sig(png_structp png_ptr) |
| 69 | { |
| 70 | png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10}; |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 71 | |
Glenn Randers-Pehrson | 9c90d7f | 2009-07-19 13:11:25 -0500 | [diff] [blame] | 72 | #ifdef PNG_IO_STATE_SUPPORTED |
| 73 | /* Inform the I/O callback that the signature is being written */ |
| 74 | png_ptr->io_state = PNG_IO_WRITING | PNG_IO_SIGNATURE; |
| 75 | #endif |
| 76 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 77 | /* Write the rest of the 8 byte signature */ |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 78 | png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes], |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 79 | (png_size_t)(8 - png_ptr->sig_bytes)); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 80 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 81 | if (png_ptr->sig_bytes < 3) |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 82 | png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE; |
| 83 | } |
| 84 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 85 | /* Write a PNG chunk all at once. The type is an array of ASCII characters |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 86 | * representing the chunk name. The array must be at least 4 bytes in |
| 87 | * length, and does not need to be null terminated. To be safe, pass the |
| 88 | * pre-defined chunk names here, and if you need a new one, define it |
| 89 | * where the others are defined. The length is the length of the data. |
| 90 | * All the data must be present. If that is not possible, use the |
| 91 | * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end() |
| 92 | * functions instead. |
| 93 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 94 | void PNGAPI |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 95 | png_write_chunk(png_structp png_ptr, png_const_bytep chunk_name, |
| 96 | png_const_bytep data, png_size_t length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 97 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 98 | if (png_ptr == NULL) |
| 99 | return; |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 100 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 101 | png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 102 | png_write_chunk_data(png_ptr, data, (png_size_t)length); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 103 | png_write_chunk_end(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 104 | } |
| 105 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 106 | /* Write the start of a PNG chunk. The type is the chunk type. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 107 | * The total_length is the sum of the lengths of all the data you will be |
| 108 | * passing in png_write_chunk_data(). |
| 109 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 110 | void PNGAPI |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 111 | png_write_chunk_start(png_structp png_ptr, png_const_bytep chunk_name, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 112 | png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 113 | { |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 114 | png_byte buf[8]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 115 | |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 116 | png_debug2(0, "Writing %s chunk, length = %lu", chunk_name, |
| 117 | (unsigned long)length); |
| 118 | |
| 119 | if (png_ptr == NULL) |
| 120 | return; |
| 121 | |
Glenn Randers-Pehrson | 9c90d7f | 2009-07-19 13:11:25 -0500 | [diff] [blame] | 122 | #ifdef PNG_IO_STATE_SUPPORTED |
| 123 | /* Inform the I/O callback that the chunk header is being written. |
| 124 | * PNG_IO_CHUNK_HDR requires a single I/O call. |
| 125 | */ |
| 126 | png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR; |
| 127 | #endif |
| 128 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 129 | /* Write the length and the chunk name */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 130 | png_save_uint_32(buf, length); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 131 | png_memcpy(buf + 4, chunk_name, 4); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 132 | png_write_data(png_ptr, buf, (png_size_t)8); |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 133 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 134 | /* Put the chunk name into png_ptr->chunk_name */ |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 135 | png_memcpy(png_ptr->chunk_name, chunk_name, 4); |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 136 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 137 | /* Reset the crc and run it over the chunk name */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 138 | png_reset_crc(png_ptr); |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 139 | |
Glenn Randers-Pehrson | 9c90d7f | 2009-07-19 13:11:25 -0500 | [diff] [blame] | 140 | png_calculate_crc(png_ptr, chunk_name, 4); |
| 141 | |
| 142 | #ifdef PNG_IO_STATE_SUPPORTED |
| 143 | /* Inform the I/O callback that chunk data will (possibly) be written. |
| 144 | * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls. |
| 145 | */ |
| 146 | png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA; |
| 147 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 148 | } |
| 149 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 150 | /* Write the data of a PNG chunk started with png_write_chunk_start(). |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 151 | * Note that multiple calls to this function are allowed, and that the |
| 152 | * sum of the lengths from these calls *must* add up to the total_length |
| 153 | * given to png_write_chunk_start(). |
| 154 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 155 | void PNGAPI |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 156 | png_write_chunk_data(png_structp png_ptr, png_const_bytep data, |
| 157 | png_size_t length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 158 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 159 | /* Write the data, and run the CRC over it */ |
| 160 | if (png_ptr == NULL) |
| 161 | return; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 162 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 163 | if (data != NULL && length > 0) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 164 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 165 | png_write_data(png_ptr, data, length); |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 166 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 167 | /* Update the CRC after writing the data, |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 168 | * in case that the user I/O routine alters it. |
| 169 | */ |
| 170 | png_calculate_crc(png_ptr, data, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 174 | /* Finish a chunk started with png_write_chunk_start(). */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 175 | void PNGAPI |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 176 | png_write_chunk_end(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 177 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 178 | png_byte buf[4]; |
| 179 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 180 | if (png_ptr == NULL) return; |
| 181 | |
Glenn Randers-Pehrson | 9c90d7f | 2009-07-19 13:11:25 -0500 | [diff] [blame] | 182 | #ifdef PNG_IO_STATE_SUPPORTED |
| 183 | /* Inform the I/O callback that the chunk CRC is being written. |
| 184 | * PNG_IO_CHUNK_CRC requires a single I/O function call. |
| 185 | */ |
| 186 | png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC; |
| 187 | #endif |
| 188 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 189 | /* Write the crc in a single operation */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 190 | png_save_uint_32(buf, png_ptr->crc); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 191 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 192 | png_write_data(png_ptr, buf, (png_size_t)4); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 193 | } |
| 194 | |
Glenn Randers-Pehrson | 205483d | 2011-04-01 12:33:42 -0500 | [diff] [blame] | 195 | #ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 196 | /* This pair of functions encapsulates the operation of (a) compressing a |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 197 | * text string, and (b) issuing it later as a series of chunk data writes. |
| 198 | * The compression_state structure is shared context for these functions |
| 199 | * set up by the caller in order to make the whole mess thread-safe. |
| 200 | */ |
| 201 | |
| 202 | typedef struct |
| 203 | { |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 204 | png_const_bytep input; /* The uncompressed input data */ |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 205 | png_size_t input_len; /* Its length */ |
| 206 | int num_output_ptr; /* Number of output pointers used */ |
| 207 | int max_output_ptr; /* Size of output_ptr */ |
| 208 | png_bytep *output_ptr; /* Array of pointers to output */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 209 | } compression_state; |
| 210 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 211 | /* Compress given text into storage in the png_ptr structure */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 212 | static int /* PRIVATE */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 213 | png_text_compress(png_structp png_ptr, |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 214 | png_const_charp text, png_size_t text_len, int compression, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 215 | compression_state *comp) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 216 | { |
| 217 | int ret; |
| 218 | |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 219 | comp->num_output_ptr = 0; |
| 220 | comp->max_output_ptr = 0; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 221 | comp->output_ptr = NULL; |
| 222 | comp->input = NULL; |
Glenn Randers-Pehrson | 6785856 | 2011-04-02 08:26:42 -0500 | [diff] [blame^] | 223 | comp->input_len = text_len; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 224 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 225 | /* We may just want to pass the text right through */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 226 | if (compression == PNG_TEXT_COMPRESSION_NONE) |
| 227 | { |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 228 | comp->input = (png_const_bytep)text; |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 229 | return((int)text_len); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | if (compression >= PNG_TEXT_COMPRESSION_LAST) |
| 233 | { |
Glenn Randers-Pehrson | 6a9e480 | 2010-02-19 09:47:43 -0600 | [diff] [blame] | 234 | #ifdef PNG_CONSOLE_IO_SUPPORTED |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 235 | char msg[50]; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 236 | png_snprintf(msg, 50, "Unknown compression type %d", compression); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 237 | png_warning(png_ptr, msg); |
| 238 | #else |
| 239 | png_warning(png_ptr, "Unknown compression type"); |
| 240 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* We can't write the chunk until we find out how much data we have, |
| 244 | * which means we need to run the compressor first and save the |
| 245 | * output. This shouldn't be a problem, as the vast majority of |
| 246 | * comments should be reasonable, but we will set up an array of |
| 247 | * malloc'd pointers to be sure. |
| 248 | * |
| 249 | * If we knew the application was well behaved, we could simplify this |
| 250 | * greatly by assuming we can always malloc an output buffer large |
| 251 | * enough to hold the compressed text ((1001 * text_len / 1000) + 12) |
| 252 | * and malloc this directly. The only time this would be a bad idea is |
| 253 | * if we can't malloc more than 64K and we have 64K of random input |
| 254 | * data, or if the input string is incredibly large (although this |
| 255 | * wouldn't cause a failure, just a slowdown due to swapping). |
| 256 | */ |
| 257 | |
Glenn Randers-Pehrson | f837831 | 2011-03-31 22:06:04 -0500 | [diff] [blame] | 258 | if (!(png_ptr->mode & PNG_ZLIB_READY_FOR_ZTXT)) |
| 259 | { |
Glenn Randers-Pehrson | ee1369b | 2011-03-31 23:44:39 -0500 | [diff] [blame] | 260 | |
| 261 | /* png_warning(png_ptr, "Initialize compressor for ztxt"); */ |
Glenn Randers-Pehrson | f837831 | 2011-03-31 22:06:04 -0500 | [diff] [blame] | 262 | /* Free memory from previously opened zstream */ |
| 263 | deflateEnd(&png_ptr->zstream); |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 264 | |
Glenn Randers-Pehrson | f837831 | 2011-03-31 22:06:04 -0500 | [diff] [blame] | 265 | /* Initialize the compressor for zTXt compression. */ |
| 266 | ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_text_level, |
| 267 | png_ptr->zlib_text_method, png_ptr->zlib_text_window_bits, |
| 268 | png_ptr->zlib_text_mem_level, png_ptr->zlib_text_strategy); |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 269 | |
Glenn Randers-Pehrson | f837831 | 2011-03-31 22:06:04 -0500 | [diff] [blame] | 270 | if (ret != Z_OK) |
| 271 | { |
| 272 | if (ret == Z_VERSION_ERROR) |
| 273 | png_error(png_ptr, |
| 274 | "zlib failed to initialize compressor for text-- version error"); |
| 275 | |
| 276 | if (ret == Z_STREAM_ERROR) |
| 277 | png_error(png_ptr, |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 278 | "zlib failed to initialize compressor for text-- stream error"); |
| 279 | |
Glenn Randers-Pehrson | f837831 | 2011-03-31 22:06:04 -0500 | [diff] [blame] | 280 | if (ret == Z_MEM_ERROR) |
| 281 | png_error(png_ptr, |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 282 | "zlib failed to initialize compressor for text-- mem error"); |
| 283 | |
Glenn Randers-Pehrson | f837831 | 2011-03-31 22:06:04 -0500 | [diff] [blame] | 284 | png_error(png_ptr, "zlib failed to initialize compressor for text"); |
| 285 | } |
| 286 | png_ptr->mode |= PNG_ZLIB_READY_FOR_ZTXT; |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 287 | } |
| 288 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 289 | /* Set up the compression buffers */ |
Glenn Randers-Pehrson | b3edc73 | 2010-11-21 14:06:41 -0600 | [diff] [blame] | 290 | /* TODO: the following cast hides a potential overflow problem. */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 291 | png_ptr->zstream.avail_in = (uInt)text_len; |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 292 | |
Glenn Randers-Pehrson | b3edc73 | 2010-11-21 14:06:41 -0600 | [diff] [blame] | 293 | /* NOTE: assume zlib doesn't overwrite the input */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 294 | png_ptr->zstream.next_in = (Bytef *)text; |
Glenn Randers-Pehrson | b3edc73 | 2010-11-21 14:06:41 -0600 | [diff] [blame] | 295 | png_ptr->zstream.avail_out = png_ptr->zbuf_size; |
| 296 | png_ptr->zstream.next_out = png_ptr->zbuf; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 297 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 298 | /* This is the same compression loop as in png_write_row() */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 299 | do |
| 300 | { |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 301 | /* Compress the data */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 302 | ret = deflate(&png_ptr->zstream, Z_NO_FLUSH); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 303 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 304 | if (ret != Z_OK) |
| 305 | { |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 306 | /* Error */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 307 | if (png_ptr->zstream.msg != NULL) |
| 308 | png_error(png_ptr, png_ptr->zstream.msg); |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 309 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 310 | else |
| 311 | png_error(png_ptr, "zlib error"); |
| 312 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 313 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 314 | /* Check to see if we need more room */ |
Glenn Randers-Pehrson | 16e1166 | 2004-11-01 14:13:40 -0600 | [diff] [blame] | 315 | if (!(png_ptr->zstream.avail_out)) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 316 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 317 | /* Make sure the output array has room */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 318 | if (comp->num_output_ptr >= comp->max_output_ptr) |
| 319 | { |
| 320 | int old_max; |
| 321 | |
| 322 | old_max = comp->max_output_ptr; |
| 323 | comp->max_output_ptr = comp->num_output_ptr + 4; |
| 324 | if (comp->output_ptr != NULL) |
| 325 | { |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 326 | png_bytepp old_ptr; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 327 | |
| 328 | old_ptr = comp->output_ptr; |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 329 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 330 | comp->output_ptr = (png_bytepp)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 331 | (png_alloc_size_t) |
| 332 | (comp->max_output_ptr * png_sizeof(png_charpp))); |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 333 | |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 334 | png_memcpy(comp->output_ptr, old_ptr, old_max |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 335 | * png_sizeof(png_charp)); |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 336 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 337 | png_free(png_ptr, old_ptr); |
| 338 | } |
| 339 | else |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 340 | comp->output_ptr = (png_bytepp)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 341 | (png_alloc_size_t) |
| 342 | (comp->max_output_ptr * png_sizeof(png_charp))); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 343 | } |
| 344 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 345 | /* Save the data */ |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 346 | comp->output_ptr[comp->num_output_ptr] = |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 347 | (png_bytep)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 348 | (png_alloc_size_t)png_ptr->zbuf_size); |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 349 | |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 350 | png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 351 | png_ptr->zbuf_size); |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 352 | |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 353 | comp->num_output_ptr++; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 354 | |
| 355 | /* and reset the buffer */ |
| 356 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
| 357 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 358 | } |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 359 | /* Continue until we don't have any more to compress */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 360 | } while (png_ptr->zstream.avail_in); |
| 361 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 362 | /* Finish the compression */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 363 | do |
| 364 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 365 | /* Tell zlib we are finished */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 366 | ret = deflate(&png_ptr->zstream, Z_FINISH); |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 367 | |
| 368 | if (ret == Z_OK) |
| 369 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 370 | /* Check to see if we need more room */ |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 371 | if (!(png_ptr->zstream.avail_out)) |
| 372 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 373 | /* Check to make sure our output array has room */ |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 374 | if (comp->num_output_ptr >= comp->max_output_ptr) |
| 375 | { |
| 376 | int old_max; |
| 377 | |
| 378 | old_max = comp->max_output_ptr; |
| 379 | comp->max_output_ptr = comp->num_output_ptr + 4; |
| 380 | if (comp->output_ptr != NULL) |
| 381 | { |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 382 | png_bytepp old_ptr; |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 383 | |
| 384 | old_ptr = comp->output_ptr; |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 385 | |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 386 | /* This could be optimized to realloc() */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 387 | comp->output_ptr = (png_bytepp)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 388 | (png_alloc_size_t)(comp->max_output_ptr * |
| 389 | png_sizeof(png_charp))); |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 390 | |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 391 | png_memcpy(comp->output_ptr, old_ptr, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 392 | old_max * png_sizeof(png_charp)); |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 393 | |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 394 | png_free(png_ptr, old_ptr); |
| 395 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 396 | |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 397 | else |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 398 | comp->output_ptr = (png_bytepp)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 399 | (png_alloc_size_t)(comp->max_output_ptr * |
| 400 | png_sizeof(png_charp))); |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 401 | } |
| 402 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 403 | /* Save the data */ |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 404 | comp->output_ptr[comp->num_output_ptr] = |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 405 | (png_bytep)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 406 | (png_alloc_size_t)png_ptr->zbuf_size); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 407 | |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 408 | png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 409 | png_ptr->zbuf_size); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 410 | |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 411 | comp->num_output_ptr++; |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 412 | |
| 413 | /* and reset the buffer pointers */ |
| 414 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
| 415 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 416 | } |
| 417 | } |
| 418 | else if (ret != Z_STREAM_END) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 419 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 420 | /* We got an error */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 421 | if (png_ptr->zstream.msg != NULL) |
| 422 | png_error(png_ptr, png_ptr->zstream.msg); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 423 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 424 | else |
| 425 | png_error(png_ptr, "zlib error"); |
| 426 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 427 | } while (ret != Z_STREAM_END); |
| 428 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 429 | /* Text length is number of buffers plus last buffer */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 430 | text_len = png_ptr->zbuf_size * comp->num_output_ptr; |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 431 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 432 | if (png_ptr->zstream.avail_out < png_ptr->zbuf_size) |
| 433 | text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out; |
| 434 | |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 435 | return((int)text_len); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 436 | } |
| 437 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 438 | /* Ship the compressed text out via chunk writes */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 439 | static void /* PRIVATE */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 440 | png_write_compressed_data_out(png_structp png_ptr, compression_state *comp) |
| 441 | { |
| 442 | int i; |
| 443 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 444 | /* Handle the no-compression case */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 445 | if (comp->input) |
| 446 | { |
Glenn Randers-Pehrson | bcb3aac | 2010-09-10 22:05:27 -0500 | [diff] [blame] | 447 | png_write_chunk_data(png_ptr, comp->input, comp->input_len); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 448 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 449 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 450 | } |
| 451 | |
Glenn Randers-Pehrson | 6785856 | 2011-04-02 08:26:42 -0500 | [diff] [blame^] | 452 | #if 1 /* For testing */ |
| 453 | |
| 454 | if (comp->input_len >= 2 && comp->input_len < 16384) |
| 455 | { |
| 456 | unsigned int z_cmf; /* zlib compression method and flags */ |
| 457 | |
| 458 | /* Optimize the CMF field in the zlib stream. This hack of the zlib |
| 459 | * stream is compliant to the stream specification. |
| 460 | */ |
| 461 | |
| 462 | if (comp->num_output_ptr) |
| 463 | z_cmf = comp->output_ptr[0][0]; |
| 464 | else |
| 465 | z_cmf = png_ptr->zbuf[0]; |
| 466 | |
| 467 | if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70) |
| 468 | { |
| 469 | unsigned int z_cinfo; |
| 470 | unsigned int half_z_window_size; |
| 471 | png_uint_32 uncompressed_idat_size = comp->input_len; |
| 472 | |
| 473 | z_cinfo = z_cmf >> 4; |
| 474 | half_z_window_size = 1 << (z_cinfo + 7); |
| 475 | |
| 476 | while (uncompressed_idat_size <= half_z_window_size && |
| 477 | half_z_window_size >= 256) |
| 478 | { |
| 479 | z_cinfo--; |
| 480 | half_z_window_size >>= 1; |
| 481 | } |
| 482 | |
| 483 | z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4); |
| 484 | |
| 485 | if (comp->num_output_ptr) |
| 486 | { |
| 487 | |
| 488 | if (comp->output_ptr[0][0] != z_cmf) |
| 489 | { |
| 490 | int tmp; |
| 491 | |
| 492 | comp->output_ptr[0][0] = (png_byte)z_cmf; |
| 493 | tmp = comp->output_ptr[0][1] & 0xe0; |
| 494 | tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f; |
| 495 | comp->output_ptr[0][1] = (png_byte)tmp; |
| 496 | } |
| 497 | } |
| 498 | else |
| 499 | { |
| 500 | int tmp; |
| 501 | |
| 502 | png_ptr->zbuf[0] = (png_byte)z_cmf; |
| 503 | tmp = png_ptr->zbuf[1] & 0xe0; |
| 504 | tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f; |
| 505 | png_ptr->zbuf[1] = (png_byte)tmp; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | else |
| 510 | png_error(png_ptr, |
| 511 | "Invalid zlib compression method or flags in non-IDAT chunk"); |
| 512 | } |
| 513 | #endif |
| 514 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 515 | /* Write saved output buffers, if any */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 516 | for (i = 0; i < comp->num_output_ptr; i++) |
| 517 | { |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 518 | png_write_chunk_data(png_ptr, comp->output_ptr[i], |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 519 | (png_size_t)png_ptr->zbuf_size); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 520 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 521 | png_free(png_ptr, comp->output_ptr[i]); |
| 522 | } |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 523 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 524 | if (comp->max_output_ptr != 0) |
| 525 | png_free(png_ptr, comp->output_ptr); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 526 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 527 | /* Write anything left in zbuf */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 528 | if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size) |
| 529 | png_write_chunk_data(png_ptr, png_ptr->zbuf, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 530 | (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out)); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 531 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 532 | /* Reset zlib for another zTXt/iTXt or image data */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 533 | deflateReset(&png_ptr->zstream); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 534 | } |
Glenn Randers-Pehrson | 205483d | 2011-04-01 12:33:42 -0500 | [diff] [blame] | 535 | #endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 536 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 537 | /* Write the IHDR chunk, and update the png_struct with the necessary |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 538 | * information. Note that the rest of this code depends upon this |
| 539 | * information being correct. |
| 540 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 541 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 542 | png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 543 | int bit_depth, int color_type, int compression_type, int filter_type, |
| 544 | int interlace_type) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 545 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 546 | PNG_IHDR; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 547 | int ret; |
| 548 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 549 | png_byte buf[13]; /* Buffer to store the IHDR info */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 550 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 551 | png_debug(1, "in png_write_IHDR"); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 552 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 553 | /* Check that we have valid input data from the application info */ |
| 554 | switch (color_type) |
| 555 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 556 | case PNG_COLOR_TYPE_GRAY: |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 557 | switch (bit_depth) |
| 558 | { |
| 559 | case 1: |
| 560 | case 2: |
| 561 | case 4: |
| 562 | case 8: |
Glenn Randers-Pehrson | 7d3e673 | 2010-08-26 17:14:07 -0500 | [diff] [blame] | 563 | #ifdef PNG_WRITE_16BIT_SUPPORTED |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 564 | case 16: |
Glenn Randers-Pehrson | 7d3e673 | 2010-08-26 17:14:07 -0500 | [diff] [blame] | 565 | #endif |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 566 | png_ptr->channels = 1; break; |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 567 | |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 568 | default: |
| 569 | png_error(png_ptr, |
| 570 | "Invalid bit depth for grayscale image"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 571 | } |
| 572 | break; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 573 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 574 | case PNG_COLOR_TYPE_RGB: |
Glenn Randers-Pehrson | 7d3e673 | 2010-08-26 17:14:07 -0500 | [diff] [blame] | 575 | #ifdef PNG_WRITE_16BIT_SUPPORTED |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 576 | if (bit_depth != 8 && bit_depth != 16) |
Glenn Randers-Pehrson | 7d3e673 | 2010-08-26 17:14:07 -0500 | [diff] [blame] | 577 | #else |
| 578 | if (bit_depth != 8) |
| 579 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 580 | png_error(png_ptr, "Invalid bit depth for RGB image"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 581 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 582 | png_ptr->channels = 3; |
| 583 | break; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 584 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 585 | case PNG_COLOR_TYPE_PALETTE: |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 586 | switch (bit_depth) |
| 587 | { |
| 588 | case 1: |
| 589 | case 2: |
| 590 | case 4: |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 591 | case 8: |
| 592 | png_ptr->channels = 1; |
| 593 | break; |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 594 | |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 595 | default: |
| 596 | png_error(png_ptr, "Invalid bit depth for paletted image"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 597 | } |
| 598 | break; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 599 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 600 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 601 | if (bit_depth != 8 && bit_depth != 16) |
| 602 | png_error(png_ptr, "Invalid bit depth for grayscale+alpha image"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 603 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 604 | png_ptr->channels = 2; |
| 605 | break; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 606 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 607 | case PNG_COLOR_TYPE_RGB_ALPHA: |
Glenn Randers-Pehrson | 7d3e673 | 2010-08-26 17:14:07 -0500 | [diff] [blame] | 608 | #ifdef PNG_WRITE_16BIT_SUPPORTED |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 609 | if (bit_depth != 8 && bit_depth != 16) |
Glenn Randers-Pehrson | 7d3e673 | 2010-08-26 17:14:07 -0500 | [diff] [blame] | 610 | #else |
| 611 | if (bit_depth != 8) |
| 612 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 613 | png_error(png_ptr, "Invalid bit depth for RGBA image"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 614 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 615 | png_ptr->channels = 4; |
| 616 | break; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 617 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 618 | default: |
| 619 | png_error(png_ptr, "Invalid image color type specified"); |
| 620 | } |
| 621 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 622 | if (compression_type != PNG_COMPRESSION_TYPE_BASE) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 623 | { |
| 624 | png_warning(png_ptr, "Invalid compression type specified"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 625 | compression_type = PNG_COMPRESSION_TYPE_BASE; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 626 | } |
| 627 | |
Glenn Randers-Pehrson | 408b421 | 2000-12-18 09:33:57 -0600 | [diff] [blame] | 628 | /* Write filter_method 64 (intrapixel differencing) only if |
| 629 | * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and |
| 630 | * 2. Libpng did not write a PNG signature (this filter_method is only |
| 631 | * used in PNG datastreams that are embedded in MNG datastreams) and |
| 632 | * 3. The application called png_permit_mng_features with a mask that |
| 633 | * included PNG_FLAG_MNG_FILTER_64 and |
| 634 | * 4. The filter_method is 64 and |
| 635 | * 5. The color_type is RGB or RGBA |
| 636 | */ |
Glenn Randers-Pehrson | 2ad31ae | 2000-12-15 08:54:42 -0600 | [diff] [blame] | 637 | if ( |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 638 | #ifdef PNG_MNG_FEATURES_SUPPORTED |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 639 | !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && |
| 640 | ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) && |
| 641 | (color_type == PNG_COLOR_TYPE_RGB || |
| 642 | color_type == PNG_COLOR_TYPE_RGB_ALPHA) && |
| 643 | (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) && |
Glenn Randers-Pehrson | 2ad31ae | 2000-12-15 08:54:42 -0600 | [diff] [blame] | 644 | #endif |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 645 | filter_type != PNG_FILTER_TYPE_BASE) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 646 | { |
| 647 | png_warning(png_ptr, "Invalid filter type specified"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 648 | filter_type = PNG_FILTER_TYPE_BASE; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 649 | } |
| 650 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 651 | #ifdef PNG_WRITE_INTERLACING_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 652 | if (interlace_type != PNG_INTERLACE_NONE && |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 653 | interlace_type != PNG_INTERLACE_ADAM7) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 654 | { |
| 655 | png_warning(png_ptr, "Invalid interlace type specified"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 656 | interlace_type = PNG_INTERLACE_ADAM7; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 657 | } |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 658 | #else |
| 659 | interlace_type=PNG_INTERLACE_NONE; |
| 660 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 661 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 662 | /* Save the relevent information */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 663 | png_ptr->bit_depth = (png_byte)bit_depth; |
| 664 | png_ptr->color_type = (png_byte)color_type; |
| 665 | png_ptr->interlaced = (png_byte)interlace_type; |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 666 | #ifdef PNG_MNG_FEATURES_SUPPORTED |
Glenn Randers-Pehrson | 2ad31ae | 2000-12-15 08:54:42 -0600 | [diff] [blame] | 667 | png_ptr->filter_type = (png_byte)filter_type; |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 668 | #endif |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 669 | png_ptr->compression_type = (png_byte)compression_type; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 670 | png_ptr->width = width; |
| 671 | png_ptr->height = height; |
| 672 | |
| 673 | png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels); |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 674 | png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width); |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 675 | /* Set the usr info, so any transformations can modify it */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 676 | png_ptr->usr_width = png_ptr->width; |
| 677 | png_ptr->usr_bit_depth = png_ptr->bit_depth; |
| 678 | png_ptr->usr_channels = png_ptr->channels; |
| 679 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 680 | /* Pack the header information into the buffer */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 681 | png_save_uint_32(buf, width); |
| 682 | png_save_uint_32(buf + 4, height); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 683 | buf[8] = (png_byte)bit_depth; |
| 684 | buf[9] = (png_byte)color_type; |
| 685 | buf[10] = (png_byte)compression_type; |
| 686 | buf[11] = (png_byte)filter_type; |
| 687 | buf[12] = (png_byte)interlace_type; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 688 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 689 | /* Write the chunk */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 690 | png_write_chunk(png_ptr, png_IHDR, buf, (png_size_t)13); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 691 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 692 | /* Initialize zlib with PNG info */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 693 | png_ptr->zstream.zalloc = png_zalloc; |
| 694 | png_ptr->zstream.zfree = png_zfree; |
| 695 | png_ptr->zstream.opaque = (voidpf)png_ptr; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 696 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 697 | if (!(png_ptr->do_filter)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 698 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 699 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE || |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 700 | png_ptr->bit_depth < 8) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 701 | png_ptr->do_filter = PNG_FILTER_NONE; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 702 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 703 | else |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 704 | png_ptr->do_filter = PNG_ALL_FILTERS; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 705 | } |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 706 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 707 | if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 708 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 709 | if (png_ptr->do_filter != PNG_FILTER_NONE) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 710 | png_ptr->zlib_strategy = Z_FILTERED; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 711 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 712 | else |
| 713 | png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY; |
| 714 | } |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 715 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 716 | if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 717 | png_ptr->zlib_level = Z_DEFAULT_COMPRESSION; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 718 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 719 | if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 720 | png_ptr->zlib_mem_level = 8; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 721 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 722 | if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS)) |
Glenn Randers-Pehrson | 5b77916 | 2004-09-04 13:25:08 -0500 | [diff] [blame] | 723 | png_ptr->zlib_window_bits = 15; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 724 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 725 | if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 726 | png_ptr->zlib_method = 8; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 727 | |
Glenn Randers-Pehrson | 205483d | 2011-04-01 12:33:42 -0500 | [diff] [blame] | 728 | #ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 729 | #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION |
| 730 | if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_STRATEGY)) |
| 731 | png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY; |
| 732 | |
| 733 | if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_LEVEL)) |
Glenn Randers-Pehrson | c683100 | 2011-04-01 15:24:18 -0500 | [diff] [blame] | 734 | png_ptr->zlib_text_level = png_ptr->zlib_level; |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 735 | |
| 736 | if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_MEM_LEVEL)) |
Glenn Randers-Pehrson | c683100 | 2011-04-01 15:24:18 -0500 | [diff] [blame] | 737 | png_ptr->zlib_text_mem_level = png_ptr->zlib_mem_level; |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 738 | |
| 739 | if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_WINDOW_BITS)) |
Glenn Randers-Pehrson | c683100 | 2011-04-01 15:24:18 -0500 | [diff] [blame] | 740 | png_ptr->zlib_text_window_bits = png_ptr->zlib_window_bits; |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 741 | |
| 742 | if (!(png_ptr->flags & PNG_FLAG_ZTXT_CUSTOM_METHOD)) |
Glenn Randers-Pehrson | c683100 | 2011-04-01 15:24:18 -0500 | [diff] [blame] | 743 | png_ptr->zlib_text_method = png_ptr->zlib_method; |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 744 | #else |
| 745 | png_ptr->zlib_text_strategy = Z_DEFAULT_STRATEGY; |
Glenn Randers-Pehrson | c683100 | 2011-04-01 15:24:18 -0500 | [diff] [blame] | 746 | png_ptr->zlib_text_level = png_ptr->zlib_level; |
| 747 | png_ptr->zlib_text_mem_level = png_ptr->zlib_mem_level; |
| 748 | png_ptr->zlib_text_window_bits = png_ptr->zlib_window_bits; |
| 749 | png_ptr->zlib_text_method = png_ptr->zlib_method; |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 750 | #endif /* PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION */ |
Glenn Randers-Pehrson | 205483d | 2011-04-01 12:33:42 -0500 | [diff] [blame] | 751 | #endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */ |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 752 | |
| 753 | /* Initialize the zlib compressor */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 754 | ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 755 | png_ptr->zlib_method, png_ptr->zlib_window_bits, |
| 756 | png_ptr->zlib_mem_level, png_ptr->zlib_strategy); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 757 | |
Glenn Randers-Pehrson | f837831 | 2011-03-31 22:06:04 -0500 | [diff] [blame] | 758 | png_ptr->mode = PNG_HAVE_IHDR; /* not READY_FOR_ZTXT */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 759 | } |
| 760 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 761 | /* Write the palette. We are careful not to trust png_color to be in the |
Glenn Randers-Pehrson | 345bc27 | 1998-06-14 14:43:31 -0500 | [diff] [blame] | 762 | * correct order for PNG, so people can redefine it to any convenient |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 763 | * structure. |
| 764 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 765 | void /* PRIVATE */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 766 | png_write_PLTE(png_structp png_ptr, png_const_colorp palette, |
| 767 | png_uint_32 num_pal) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 768 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 769 | PNG_PLTE; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 770 | png_uint_32 i; |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 771 | png_const_colorp pal_ptr; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 772 | png_byte buf[3]; |
| 773 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 774 | png_debug(1, "in png_write_PLTE"); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 775 | |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 776 | if (( |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 777 | #ifdef PNG_MNG_FEATURES_SUPPORTED |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 778 | !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) && |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 779 | #endif |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 780 | num_pal == 0) || num_pal > 256) |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 781 | { |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 782 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 783 | { |
| 784 | png_error(png_ptr, "Invalid number of colors in palette"); |
| 785 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 786 | |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 787 | else |
| 788 | { |
| 789 | png_warning(png_ptr, "Invalid number of colors in palette"); |
| 790 | return; |
| 791 | } |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR)) |
| 795 | { |
| 796 | png_warning(png_ptr, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 797 | "Ignoring request to write a PLTE chunk in grayscale PNG"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 798 | |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 799 | return; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 800 | } |
| 801 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 802 | png_ptr->num_palette = (png_uint_16)num_pal; |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 803 | png_debug1(3, "num_palette = %d", png_ptr->num_palette); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 804 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 805 | png_write_chunk_start(png_ptr, png_PLTE, (png_uint_32)(num_pal * 3)); |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 806 | #ifdef PNG_POINTER_INDEXING_SUPPORTED |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 807 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 808 | for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 809 | { |
| 810 | buf[0] = pal_ptr->red; |
| 811 | buf[1] = pal_ptr->green; |
| 812 | buf[2] = pal_ptr->blue; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 813 | png_write_chunk_data(png_ptr, buf, (png_size_t)3); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 814 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 815 | |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 816 | #else |
Glenn Randers-Pehrson | e3f3c4e | 2010-02-07 18:08:50 -0600 | [diff] [blame] | 817 | /* This is a little slower but some buggy compilers need to do this |
| 818 | * instead |
| 819 | */ |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 820 | pal_ptr=palette; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 821 | |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 822 | for (i = 0; i < num_pal; i++) |
| 823 | { |
| 824 | buf[0] = pal_ptr[i].red; |
| 825 | buf[1] = pal_ptr[i].green; |
| 826 | buf[2] = pal_ptr[i].blue; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 827 | png_write_chunk_data(png_ptr, buf, (png_size_t)3); |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 828 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 829 | |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 830 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 831 | png_write_chunk_end(png_ptr); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 832 | png_ptr->mode |= PNG_HAVE_PLTE; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 833 | } |
| 834 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 835 | /* Write an IDAT chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 836 | void /* PRIVATE */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 837 | png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 838 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 839 | PNG_IDAT; |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 840 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 841 | png_debug(1, "in png_write_IDAT"); |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 842 | |
Glenn Randers-Pehrson | 5b77916 | 2004-09-04 13:25:08 -0500 | [diff] [blame] | 843 | if (!(png_ptr->mode & PNG_HAVE_IDAT) && |
| 844 | png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE) |
| 845 | { |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 846 | unsigned int z_cmf; /* zlib compression method and flags */ |
| 847 | |
Glenn Randers-Pehrson | 205483d | 2011-04-01 12:33:42 -0500 | [diff] [blame] | 848 | #ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED |
| 849 | int ret; |
Glenn Randers-Pehrson | ec8296a | 2011-04-01 15:09:05 -0500 | [diff] [blame] | 850 | |
| 851 | /* Reinitialize the compressor if necessary */ |
Glenn Randers-Pehrson | f837831 | 2011-03-31 22:06:04 -0500 | [diff] [blame] | 852 | if (png_ptr->mode & PNG_ZLIB_READY_FOR_ZTXT) |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 853 | { |
Glenn Randers-Pehrson | f837831 | 2011-03-31 22:06:04 -0500 | [diff] [blame] | 854 | /* Free memory from previously opened zstream */ |
| 855 | deflateEnd(&png_ptr->zstream); |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 856 | |
Glenn Randers-Pehrson | f837831 | 2011-03-31 22:06:04 -0500 | [diff] [blame] | 857 | ret = deflateInit2(&png_ptr->zstream, png_ptr->zlib_level, |
| 858 | png_ptr->zlib_method, png_ptr->zlib_window_bits, |
| 859 | png_ptr->zlib_mem_level, png_ptr->zlib_strategy); |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 860 | |
Glenn Randers-Pehrson | f837831 | 2011-03-31 22:06:04 -0500 | [diff] [blame] | 861 | if (ret != Z_OK) |
| 862 | { |
| 863 | if (ret == Z_VERSION_ERROR) |
| 864 | png_error(png_ptr, |
| 865 | "zlib failed to initialize compressor -- version error"); |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 866 | |
Glenn Randers-Pehrson | f837831 | 2011-03-31 22:06:04 -0500 | [diff] [blame] | 867 | if (ret == Z_STREAM_ERROR) |
| 868 | png_error(png_ptr, |
| 869 | "zlib failed to initialize compressor -- stream error"); |
| 870 | |
| 871 | if (ret == Z_MEM_ERROR) |
| 872 | png_error(png_ptr, |
| 873 | "zlib failed to initialize compressor -- mem error"); |
| 874 | |
| 875 | png_error(png_ptr, "zlib failed to initialize compressor"); |
| 876 | } |
| 877 | png_ptr->mode &= ~PNG_ZLIB_READY_FOR_ZTXT; /* Ready for IDAT */ |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 878 | } |
Glenn Randers-Pehrson | 205483d | 2011-04-01 12:33:42 -0500 | [diff] [blame] | 879 | #endif /* PNG_WRITE_COMPRESSED_TEXT_SUPPORTED */ |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 880 | |
| 881 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 882 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
| 883 | /* libpng is not interested in zstream.data_type, so set it |
| 884 | * to a predefined value, to avoid its evaluation inside zlib |
| 885 | */ |
| 886 | png_ptr->zstream.data_type = Z_BINARY; |
| 887 | |
Glenn Randers-Pehrson | ec8296a | 2011-04-01 15:09:05 -0500 | [diff] [blame] | 888 | /* Optimize the CMF field in the zlib stream. This hack of the zlib |
| 889 | * stream is compliant to the stream specification. |
| 890 | */ |
Glenn Randers-Pehrson | 6b3d50b | 2011-03-31 20:14:29 -0500 | [diff] [blame] | 891 | z_cmf = data[0]; |
| 892 | |
Glenn Randers-Pehrson | 5b77916 | 2004-09-04 13:25:08 -0500 | [diff] [blame] | 893 | if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70) |
| 894 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 895 | /* Avoid memory underflows and multiplication overflows. |
| 896 | * |
| 897 | * The conditions below are practically always satisfied; |
| 898 | * however, they still must be checked. |
| 899 | */ |
Glenn Randers-Pehrson | 5b77916 | 2004-09-04 13:25:08 -0500 | [diff] [blame] | 900 | if (length >= 2 && |
| 901 | png_ptr->height < 16384 && png_ptr->width < 16384) |
| 902 | { |
Glenn Randers-Pehrson | c3b3240 | 2011-04-01 22:10:41 -0500 | [diff] [blame] | 903 | /* Compute the maximum possible length of the datastream */ |
| 904 | |
| 905 | /* Number of pixels, plus for each row a filter byte and possible |
Glenn Randers-Pehrson | 6785856 | 2011-04-02 08:26:42 -0500 | [diff] [blame^] | 906 | * and possibly a padding byte, so increase the maximum |
| 907 | * size to account for these. |
Glenn Randers-Pehrson | ec8296a | 2011-04-01 15:09:05 -0500 | [diff] [blame] | 908 | */ |
Glenn Randers-Pehrson | e99107b | 2011-04-02 05:49:03 -0500 | [diff] [blame] | 909 | unsigned int z_cinfo; |
| 910 | unsigned int half_z_window_size; |
Glenn Randers-Pehrson | 5b77916 | 2004-09-04 13:25:08 -0500 | [diff] [blame] | 911 | png_uint_32 uncompressed_idat_size = png_ptr->height * |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 912 | ((png_ptr->width * |
| 913 | png_ptr->channels * png_ptr->bit_depth + 15) >> 3); |
Glenn Randers-Pehrson | c3b3240 | 2011-04-01 22:10:41 -0500 | [diff] [blame] | 914 | |
| 915 | /* If it's interlaced, each block of 8 rows is sent as up to |
| 916 | * 14 rows, i.e., 6 additional rows, each with a filter byte |
| 917 | * and possibly a padding byte |
| 918 | */ |
| 919 | if (png_ptr->interlaced) |
| 920 | uncompressed_idat_size += ((png_ptr->height + 7)/8) * |
| 921 | (png_ptr->bit_depth < 8 ? 12 : 6); |
| 922 | |
Glenn Randers-Pehrson | e99107b | 2011-04-02 05:49:03 -0500 | [diff] [blame] | 923 | z_cinfo = z_cmf >> 4; |
| 924 | half_z_window_size = 1 << (z_cinfo + 7); |
| 925 | |
Glenn Randers-Pehrson | 5b77916 | 2004-09-04 13:25:08 -0500 | [diff] [blame] | 926 | while (uncompressed_idat_size <= half_z_window_size && |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 927 | half_z_window_size >= 256) |
Glenn Randers-Pehrson | 5b77916 | 2004-09-04 13:25:08 -0500 | [diff] [blame] | 928 | { |
| 929 | z_cinfo--; |
| 930 | half_z_window_size >>= 1; |
| 931 | } |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 932 | |
Glenn Randers-Pehrson | 5b77916 | 2004-09-04 13:25:08 -0500 | [diff] [blame] | 933 | z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4); |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 934 | |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 935 | if (data[0] != z_cmf) |
Glenn Randers-Pehrson | 5b77916 | 2004-09-04 13:25:08 -0500 | [diff] [blame] | 936 | { |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 937 | int tmp; |
Glenn Randers-Pehrson | 5b77916 | 2004-09-04 13:25:08 -0500 | [diff] [blame] | 938 | data[0] = (png_byte)z_cmf; |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 939 | tmp = data[1] & 0xe0; |
| 940 | tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f; |
| 941 | data[1] = (png_byte)tmp; |
Glenn Randers-Pehrson | 5b77916 | 2004-09-04 13:25:08 -0500 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 945 | |
Glenn Randers-Pehrson | 5b77916 | 2004-09-04 13:25:08 -0500 | [diff] [blame] | 946 | else |
| 947 | png_error(png_ptr, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 948 | "Invalid zlib compression method or flags in IDAT"); |
Glenn Randers-Pehrson | 5b77916 | 2004-09-04 13:25:08 -0500 | [diff] [blame] | 949 | } |
| 950 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 951 | png_write_chunk(png_ptr, png_IDAT, data, length); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 952 | png_ptr->mode |= PNG_HAVE_IDAT; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 953 | } |
| 954 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 955 | /* Write an IEND chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 956 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 957 | png_write_IEND(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 958 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 959 | PNG_IEND; |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 960 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 961 | png_debug(1, "in png_write_IEND"); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 962 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 963 | png_write_chunk(png_ptr, png_IEND, NULL, (png_size_t)0); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 964 | png_ptr->mode |= PNG_HAVE_IEND; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 965 | } |
| 966 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 967 | #ifdef PNG_WRITE_gAMA_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 968 | /* Write a gAMA chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 969 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 970 | png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 971 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 972 | PNG_gAMA; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 973 | png_byte buf[4]; |
| 974 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 975 | png_debug(1, "in png_write_gAMA"); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 976 | |
Glenn Randers-Pehrson | 626afd6 | 2009-08-20 22:52:51 -0500 | [diff] [blame] | 977 | /* file_gamma is saved in 1/100,000ths */ |
Glenn Randers-Pehrson | b182893 | 2001-06-23 08:03:17 -0500 | [diff] [blame] | 978 | png_save_uint_32(buf, (png_uint_32)file_gamma); |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 979 | png_write_chunk(png_ptr, png_gAMA, buf, (png_size_t)4); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 980 | } |
| 981 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 982 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 983 | #ifdef PNG_WRITE_sRGB_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 984 | /* Write a sRGB chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 985 | void /* PRIVATE */ |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 986 | png_write_sRGB(png_structp png_ptr, int srgb_intent) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 987 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 988 | PNG_sRGB; |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 989 | png_byte buf[1]; |
| 990 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 991 | png_debug(1, "in png_write_sRGB"); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 992 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 993 | if (srgb_intent >= PNG_sRGB_INTENT_LAST) |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 994 | png_warning(png_ptr, |
| 995 | "Invalid sRGB rendering intent specified"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 996 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 997 | buf[0]=(png_byte)srgb_intent; |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 998 | png_write_chunk(png_ptr, png_sRGB, buf, (png_size_t)1); |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 999 | } |
| 1000 | #endif |
| 1001 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1002 | #ifdef PNG_WRITE_iCCP_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1003 | /* Write an iCCP chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1004 | void /* PRIVATE */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1005 | png_write_iCCP(png_structp png_ptr, png_const_charp name, int compression_type, |
| 1006 | png_const_charp profile, int profile_len) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1007 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1008 | PNG_iCCP; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1009 | png_size_t name_len; |
| 1010 | png_charp new_name; |
| 1011 | compression_state comp; |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1012 | int embedded_profile_len = 0; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1013 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1014 | png_debug(1, "in png_write_iCCP"); |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 1015 | |
| 1016 | comp.num_output_ptr = 0; |
| 1017 | comp.max_output_ptr = 0; |
| 1018 | comp.output_ptr = NULL; |
| 1019 | comp.input = NULL; |
| 1020 | comp.input_len = 0; |
| 1021 | |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1022 | if ((name_len = png_check_keyword(png_ptr, name, &new_name)) == 0) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1023 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1024 | |
Glenn Randers-Pehrson | 76e5fd6 | 2000-12-28 07:50:05 -0600 | [diff] [blame] | 1025 | if (compression_type != PNG_COMPRESSION_TYPE_BASE) |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1026 | png_warning(png_ptr, "Unknown compression type in iCCP chunk"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1027 | |
Glenn Randers-Pehrson | 1394480 | 2000-06-24 07:42:42 -0500 | [diff] [blame] | 1028 | if (profile == NULL) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1029 | profile_len = 0; |
| 1030 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1031 | if (profile_len > 3) |
Glenn Randers-Pehrson | 7edd458 | 2006-12-07 19:16:44 -0600 | [diff] [blame] | 1032 | embedded_profile_len = |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1033 | ((*( (png_const_bytep)profile ))<<24) | |
| 1034 | ((*( (png_const_bytep)profile + 1))<<16) | |
| 1035 | ((*( (png_const_bytep)profile + 2))<< 8) | |
| 1036 | ((*( (png_const_bytep)profile + 3)) ); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1037 | |
Glenn Randers-Pehrson | 3a054e1 | 2009-08-01 08:53:23 -0500 | [diff] [blame] | 1038 | if (embedded_profile_len < 0) |
| 1039 | { |
| 1040 | png_warning(png_ptr, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1041 | "Embedded profile length in iCCP chunk is negative"); |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1042 | |
Glenn Randers-Pehrson | 3a054e1 | 2009-08-01 08:53:23 -0500 | [diff] [blame] | 1043 | png_free(png_ptr, new_name); |
| 1044 | return; |
| 1045 | } |
| 1046 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1047 | if (profile_len < embedded_profile_len) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1048 | { |
| 1049 | png_warning(png_ptr, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1050 | "Embedded profile length too large in iCCP chunk"); |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1051 | |
Glenn Randers-Pehrson | a6cc627 | 2009-04-01 14:18:43 -0500 | [diff] [blame] | 1052 | png_free(png_ptr, new_name); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1053 | return; |
| 1054 | } |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1055 | |
| 1056 | if (profile_len > embedded_profile_len) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1057 | { |
| 1058 | png_warning(png_ptr, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1059 | "Truncating profile to actual length in iCCP chunk"); |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1060 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1061 | profile_len = embedded_profile_len; |
| 1062 | } |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1063 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1064 | if (profile_len) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1065 | profile_len = png_text_compress(png_ptr, profile, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1066 | (png_size_t)profile_len, PNG_COMPRESSION_TYPE_BASE, &comp); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1067 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1068 | /* Make sure we include the NULL after the name and the compression type */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1069 | png_write_chunk_start(png_ptr, png_iCCP, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1070 | (png_uint_32)(name_len + profile_len + 2)); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1071 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1072 | new_name[name_len + 1] = 0x00; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1073 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1074 | png_write_chunk_data(png_ptr, (png_bytep)new_name, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1075 | (png_size_t)(name_len + 2)); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1076 | |
| 1077 | if (profile_len) |
Glenn Randers-Pehrson | 6785856 | 2011-04-02 08:26:42 -0500 | [diff] [blame^] | 1078 | { |
| 1079 | comp.input_len = profile_len; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1080 | png_write_compressed_data_out(png_ptr, &comp); |
Glenn Randers-Pehrson | 6785856 | 2011-04-02 08:26:42 -0500 | [diff] [blame^] | 1081 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1082 | |
| 1083 | png_write_chunk_end(png_ptr); |
| 1084 | png_free(png_ptr, new_name); |
| 1085 | } |
| 1086 | #endif |
| 1087 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1088 | #ifdef PNG_WRITE_sPLT_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1089 | /* Write a sPLT chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1090 | void /* PRIVATE */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1091 | png_write_sPLT(png_structp png_ptr, png_const_sPLT_tp spalette) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1092 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1093 | PNG_sPLT; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1094 | png_size_t name_len; |
| 1095 | png_charp new_name; |
| 1096 | png_byte entrybuf[10]; |
Glenn Randers-Pehrson | 9c90d7f | 2009-07-19 13:11:25 -0500 | [diff] [blame] | 1097 | png_size_t entry_size = (spalette->depth == 8 ? 6 : 10); |
| 1098 | png_size_t palette_size = entry_size * spalette->nentries; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 1099 | png_sPLT_entryp ep; |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 1100 | #ifndef PNG_POINTER_INDEXING_SUPPORTED |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1101 | int i; |
| 1102 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1103 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1104 | png_debug(1, "in png_write_sPLT"); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 1105 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1106 | if ((name_len = png_check_keyword(png_ptr,spalette->name, &new_name))==0) |
| 1107 | return; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1108 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1109 | /* Make sure we include the NULL after the name */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1110 | png_write_chunk_start(png_ptr, png_sPLT, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1111 | (png_uint_32)(name_len + 2 + palette_size)); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1112 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1113 | png_write_chunk_data(png_ptr, (png_bytep)new_name, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1114 | (png_size_t)(name_len + 1)); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1115 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1116 | png_write_chunk_data(png_ptr, &spalette->depth, (png_size_t)1); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1117 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1118 | /* Loop through each palette entry, writing appropriately */ |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 1119 | #ifdef PNG_POINTER_INDEXING_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1120 | for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1121 | { |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1122 | if (spalette->depth == 8) |
| 1123 | { |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1124 | entrybuf[0] = (png_byte)ep->red; |
| 1125 | entrybuf[1] = (png_byte)ep->green; |
| 1126 | entrybuf[2] = (png_byte)ep->blue; |
| 1127 | entrybuf[3] = (png_byte)ep->alpha; |
| 1128 | png_save_uint_16(entrybuf + 4, ep->frequency); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1129 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1130 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1131 | else |
| 1132 | { |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1133 | png_save_uint_16(entrybuf + 0, ep->red); |
| 1134 | png_save_uint_16(entrybuf + 2, ep->green); |
| 1135 | png_save_uint_16(entrybuf + 4, ep->blue); |
| 1136 | png_save_uint_16(entrybuf + 6, ep->alpha); |
| 1137 | png_save_uint_16(entrybuf + 8, ep->frequency); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1138 | } |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1139 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1140 | png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1141 | } |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1142 | #else |
| 1143 | ep=spalette->entries; |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1144 | for (i = 0; i>spalette->nentries; i++) |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1145 | { |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1146 | if (spalette->depth == 8) |
| 1147 | { |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1148 | entrybuf[0] = (png_byte)ep[i].red; |
| 1149 | entrybuf[1] = (png_byte)ep[i].green; |
| 1150 | entrybuf[2] = (png_byte)ep[i].blue; |
| 1151 | entrybuf[3] = (png_byte)ep[i].alpha; |
| 1152 | png_save_uint_16(entrybuf + 4, ep[i].frequency); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1153 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1154 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1155 | else |
| 1156 | { |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1157 | png_save_uint_16(entrybuf + 0, ep[i].red); |
| 1158 | png_save_uint_16(entrybuf + 2, ep[i].green); |
| 1159 | png_save_uint_16(entrybuf + 4, ep[i].blue); |
| 1160 | png_save_uint_16(entrybuf + 6, ep[i].alpha); |
| 1161 | png_save_uint_16(entrybuf + 8, ep[i].frequency); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1162 | } |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1163 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1164 | png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size); |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 1165 | } |
| 1166 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1167 | |
| 1168 | png_write_chunk_end(png_ptr); |
| 1169 | png_free(png_ptr, new_name); |
| 1170 | } |
| 1171 | #endif |
| 1172 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1173 | #ifdef PNG_WRITE_sBIT_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1174 | /* Write the sBIT chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1175 | void /* PRIVATE */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1176 | png_write_sBIT(png_structp png_ptr, png_const_color_8p sbit, int color_type) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1177 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 1178 | PNG_sBIT; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1179 | png_byte buf[4]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1180 | png_size_t size; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1181 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1182 | png_debug(1, "in png_write_sBIT"); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 1183 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1184 | /* Make sure we don't depend upon the order of PNG_COLOR_8 */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1185 | if (color_type & PNG_COLOR_MASK_COLOR) |
| 1186 | { |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1187 | png_byte maxbits; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1188 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 1189 | maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 : |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1190 | png_ptr->usr_bit_depth); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1191 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1192 | if (sbit->red == 0 || sbit->red > maxbits || |
| 1193 | sbit->green == 0 || sbit->green > maxbits || |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1194 | sbit->blue == 0 || sbit->blue > maxbits) |
| 1195 | { |
| 1196 | png_warning(png_ptr, "Invalid sBIT depth specified"); |
| 1197 | return; |
| 1198 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1199 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1200 | buf[0] = sbit->red; |
| 1201 | buf[1] = sbit->green; |
| 1202 | buf[2] = sbit->blue; |
| 1203 | size = 3; |
| 1204 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1205 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1206 | else |
| 1207 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1208 | if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth) |
| 1209 | { |
| 1210 | png_warning(png_ptr, "Invalid sBIT depth specified"); |
| 1211 | return; |
| 1212 | } |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1213 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1214 | buf[0] = sbit->gray; |
| 1215 | size = 1; |
| 1216 | } |
| 1217 | |
| 1218 | if (color_type & PNG_COLOR_MASK_ALPHA) |
| 1219 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1220 | if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth) |
| 1221 | { |
| 1222 | png_warning(png_ptr, "Invalid sBIT depth specified"); |
| 1223 | return; |
| 1224 | } |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1225 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1226 | buf[size++] = sbit->alpha; |
| 1227 | } |
| 1228 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1229 | png_write_chunk(png_ptr, png_sBIT, buf, size); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1230 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1231 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1232 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1233 | #ifdef PNG_WRITE_cHRM_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1234 | /* Write the cHRM chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1235 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1236 | png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1237 | png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y, |
| 1238 | png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x, |
| 1239 | png_fixed_point blue_y) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1240 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1241 | PNG_cHRM; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1242 | png_byte buf[32]; |
| 1243 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1244 | png_debug(1, "in png_write_cHRM"); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 1245 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1246 | /* Each value is saved in 1/100,000ths */ |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1247 | #ifdef PNG_CHECK_cHRM_SUPPORTED |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 1248 | if (png_check_cHRM_fixed(png_ptr, white_x, white_y, red_x, red_y, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1249 | green_x, green_y, blue_x, blue_y)) |
Glenn Randers-Pehrson | 71a3c1f | 2008-11-26 12:00:38 -0600 | [diff] [blame] | 1250 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1251 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1252 | png_save_uint_32(buf, (png_uint_32)white_x); |
| 1253 | png_save_uint_32(buf + 4, (png_uint_32)white_y); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1254 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1255 | png_save_uint_32(buf + 8, (png_uint_32)red_x); |
| 1256 | png_save_uint_32(buf + 12, (png_uint_32)red_y); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1257 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1258 | png_save_uint_32(buf + 16, (png_uint_32)green_x); |
| 1259 | png_save_uint_32(buf + 20, (png_uint_32)green_y); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1260 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1261 | png_save_uint_32(buf + 24, (png_uint_32)blue_x); |
| 1262 | png_save_uint_32(buf + 28, (png_uint_32)blue_y); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1263 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1264 | png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32); |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 1265 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1266 | } |
| 1267 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1268 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1269 | #ifdef PNG_WRITE_tRNS_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1270 | /* Write the tRNS chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1271 | void /* PRIVATE */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1272 | png_write_tRNS(png_structp png_ptr, png_const_bytep trans_alpha, |
| 1273 | png_const_color_16p tran, int num_trans, int color_type) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1274 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 1275 | PNG_tRNS; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1276 | png_byte buf[6]; |
| 1277 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1278 | png_debug(1, "in png_write_tRNS"); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 1279 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1280 | if (color_type == PNG_COLOR_TYPE_PALETTE) |
| 1281 | { |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1282 | if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1283 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1284 | png_warning(png_ptr, "Invalid number of transparent colors specified"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1285 | return; |
| 1286 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1287 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1288 | /* Write the chunk out as it is */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1289 | png_write_chunk(png_ptr, png_tRNS, trans_alpha, (png_size_t)num_trans); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1290 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1291 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1292 | else if (color_type == PNG_COLOR_TYPE_GRAY) |
| 1293 | { |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1294 | /* One 16 bit value */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1295 | if (tran->gray >= (1 << png_ptr->bit_depth)) |
Glenn Randers-Pehrson | 1ea0ff3 | 2001-08-07 22:25:59 -0500 | [diff] [blame] | 1296 | { |
| 1297 | png_warning(png_ptr, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1298 | "Ignoring attempt to write tRNS chunk out-of-range for bit_depth"); |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1299 | |
Glenn Randers-Pehrson | 1ea0ff3 | 2001-08-07 22:25:59 -0500 | [diff] [blame] | 1300 | return; |
| 1301 | } |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1302 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1303 | png_save_uint_16(buf, tran->gray); |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1304 | png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)2); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1305 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1306 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1307 | else if (color_type == PNG_COLOR_TYPE_RGB) |
| 1308 | { |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1309 | /* Three 16 bit values */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1310 | png_save_uint_16(buf, tran->red); |
| 1311 | png_save_uint_16(buf + 2, tran->green); |
| 1312 | png_save_uint_16(buf + 4, tran->blue); |
Glenn Randers-Pehrson | 7d3e673 | 2010-08-26 17:14:07 -0500 | [diff] [blame] | 1313 | #ifdef PNG_WRITE_16BIT_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1314 | if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4])) |
Glenn Randers-Pehrson | 7d3e673 | 2010-08-26 17:14:07 -0500 | [diff] [blame] | 1315 | #else |
| 1316 | if (buf[0] | buf[2] | buf[4]) |
| 1317 | #endif |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1318 | { |
| 1319 | png_warning(png_ptr, |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1320 | "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8"); |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1321 | return; |
| 1322 | } |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1323 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1324 | png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)6); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1325 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1326 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1327 | else |
| 1328 | { |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 1329 | png_warning(png_ptr, "Can't write tRNS with an alpha channel"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1330 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1331 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1332 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1333 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1334 | #ifdef PNG_WRITE_bKGD_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1335 | /* Write the background chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1336 | void /* PRIVATE */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1337 | png_write_bKGD(png_structp png_ptr, png_const_color_16p back, int color_type) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1338 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 1339 | PNG_bKGD; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1340 | png_byte buf[6]; |
| 1341 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1342 | png_debug(1, "in png_write_bKGD"); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 1343 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1344 | if (color_type == PNG_COLOR_TYPE_PALETTE) |
| 1345 | { |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1346 | if ( |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1347 | #ifdef PNG_MNG_FEATURES_SUPPORTED |
Glenn Randers-Pehrson | 5e5c1e1 | 2000-11-10 12:26:19 -0600 | [diff] [blame] | 1348 | (png_ptr->num_palette || |
| 1349 | (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) && |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1350 | #endif |
Glenn Randers-Pehrson | d6d8075 | 2008-12-02 09:49:43 -0600 | [diff] [blame] | 1351 | back->index >= png_ptr->num_palette) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1352 | { |
| 1353 | png_warning(png_ptr, "Invalid background palette index"); |
| 1354 | return; |
| 1355 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1356 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1357 | buf[0] = back->index; |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1358 | png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)1); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1359 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1360 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1361 | else if (color_type & PNG_COLOR_MASK_COLOR) |
| 1362 | { |
| 1363 | png_save_uint_16(buf, back->red); |
| 1364 | png_save_uint_16(buf + 2, back->green); |
| 1365 | png_save_uint_16(buf + 4, back->blue); |
Glenn Randers-Pehrson | 7d3e673 | 2010-08-26 17:14:07 -0500 | [diff] [blame] | 1366 | #ifdef PNG_WRITE_16BIT_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1367 | if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4])) |
Glenn Randers-Pehrson | 7d3e673 | 2010-08-26 17:14:07 -0500 | [diff] [blame] | 1368 | #else |
| 1369 | if (buf[0] | buf[2] | buf[4]) |
| 1370 | #endif |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1371 | { |
| 1372 | png_warning(png_ptr, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1373 | "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1374 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1375 | return; |
| 1376 | } |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1377 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1378 | png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)6); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1379 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1380 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1381 | else |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1382 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1383 | if (back->gray >= (1 << png_ptr->bit_depth)) |
Glenn Randers-Pehrson | 1ea0ff3 | 2001-08-07 22:25:59 -0500 | [diff] [blame] | 1384 | { |
| 1385 | png_warning(png_ptr, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1386 | "Ignoring attempt to write bKGD chunk out-of-range for bit_depth"); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1387 | |
Glenn Randers-Pehrson | 1ea0ff3 | 2001-08-07 22:25:59 -0500 | [diff] [blame] | 1388 | return; |
| 1389 | } |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1390 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1391 | png_save_uint_16(buf, back->gray); |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1392 | png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)2); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1393 | } |
| 1394 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1395 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1396 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1397 | #ifdef PNG_WRITE_hIST_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1398 | /* Write the histogram */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1399 | void /* PRIVATE */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1400 | png_write_hIST(png_structp png_ptr, png_const_uint_16p hist, int num_hist) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1401 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 1402 | PNG_hIST; |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1403 | int i; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1404 | png_byte buf[3]; |
| 1405 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1406 | png_debug(1, "in png_write_hIST"); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 1407 | |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1408 | if (num_hist > (int)png_ptr->num_palette) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1409 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1410 | png_debug2(3, "num_hist = %d, num_palette = %d", num_hist, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1411 | png_ptr->num_palette); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1412 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1413 | png_warning(png_ptr, "Invalid number of histogram entries specified"); |
| 1414 | return; |
| 1415 | } |
| 1416 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1417 | png_write_chunk_start(png_ptr, png_hIST, (png_uint_32)(num_hist * 2)); |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1418 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1419 | for (i = 0; i < num_hist; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1420 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1421 | png_save_uint_16(buf, hist[i]); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1422 | png_write_chunk_data(png_ptr, buf, (png_size_t)2); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1423 | } |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1424 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1425 | png_write_chunk_end(png_ptr); |
| 1426 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1427 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1428 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1429 | #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \ |
| 1430 | defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1431 | /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification, |
| 1432 | * and if invalid, correct the keyword rather than discarding the entire |
| 1433 | * chunk. The PNG 1.0 specification requires keywords 1-79 characters in |
| 1434 | * length, forbids leading or trailing whitespace, multiple internal spaces, |
| 1435 | * and the non-break space (0x80) from ISO 8859-1. Returns keyword length. |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1436 | * |
| 1437 | * The new_key is allocated to hold the corrected keyword and must be freed |
| 1438 | * by the calling routine. This avoids problems with trying to write to |
| 1439 | * static keywords without having to have duplicate copies of the strings. |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1440 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1441 | png_size_t /* PRIVATE */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1442 | png_check_keyword(png_structp png_ptr, png_const_charp key, png_charpp new_key) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1443 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1444 | png_size_t key_len; |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1445 | png_const_charp ikp; |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1446 | png_charp kp, dp; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1447 | int kflag; |
Glenn Randers-Pehrson | f6b4f45 | 2000-12-01 09:23:06 -0600 | [diff] [blame] | 1448 | int kwarn=0; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1449 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1450 | png_debug(1, "in png_check_keyword"); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 1451 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1452 | *new_key = NULL; |
| 1453 | |
| 1454 | if (key == NULL || (key_len = png_strlen(key)) == 0) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1455 | { |
Glenn Randers-Pehrson | f6b4f45 | 2000-12-01 09:23:06 -0600 | [diff] [blame] | 1456 | png_warning(png_ptr, "zero length keyword"); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1457 | return ((png_size_t)0); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1458 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1459 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1460 | png_debug1(2, "Keyword to be checked is '%s'", key); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1461 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1462 | *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2)); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1463 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 1464 | if (*new_key == NULL) |
| 1465 | { |
| 1466 | png_warning(png_ptr, "Out of memory while procesing keyword"); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1467 | return ((png_size_t)0); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 1468 | } |
Glenn Randers-Pehrson | e68f5a3 | 2001-05-14 09:20:53 -0500 | [diff] [blame] | 1469 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1470 | /* Replace non-printing characters with a blank and print a warning */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1471 | for (ikp = key, dp = *new_key; *ikp != '\0'; ikp++, dp++) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1472 | { |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1473 | if ((png_byte)*ikp < 0x20 || |
| 1474 | ((png_byte)*ikp > 0x7E && (png_byte)*ikp < 0xA1)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1475 | { |
Glenn Randers-Pehrson | 6a9e480 | 2010-02-19 09:47:43 -0600 | [diff] [blame] | 1476 | #ifdef PNG_CONSOLE_IO_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1477 | char msg[40]; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1478 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1479 | png_snprintf(msg, 40, |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1480 | "invalid keyword character 0x%02X", (png_byte)*ikp); |
Glenn Randers-Pehrson | f6b4f45 | 2000-12-01 09:23:06 -0600 | [diff] [blame] | 1481 | png_warning(png_ptr, msg); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1482 | #else |
Glenn Randers-Pehrson | f6b4f45 | 2000-12-01 09:23:06 -0600 | [diff] [blame] | 1483 | png_warning(png_ptr, "invalid character in keyword"); |
Glenn Randers-Pehrson | 70e3f54 | 1998-01-03 22:40:55 -0600 | [diff] [blame] | 1484 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1485 | *dp = ' '; |
| 1486 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1487 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1488 | else |
| 1489 | { |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1490 | *dp = *ikp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1491 | } |
| 1492 | } |
| 1493 | *dp = '\0'; |
| 1494 | |
| 1495 | /* Remove any trailing white space. */ |
| 1496 | kp = *new_key + key_len - 1; |
| 1497 | if (*kp == ' ') |
| 1498 | { |
Glenn Randers-Pehrson | f6b4f45 | 2000-12-01 09:23:06 -0600 | [diff] [blame] | 1499 | png_warning(png_ptr, "trailing spaces removed from keyword"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1500 | |
| 1501 | while (*kp == ' ') |
| 1502 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1503 | *(kp--) = '\0'; |
| 1504 | key_len--; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1505 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | /* Remove any leading white space. */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1509 | kp = *new_key; |
| 1510 | if (*kp == ' ') |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1511 | { |
Glenn Randers-Pehrson | f6b4f45 | 2000-12-01 09:23:06 -0600 | [diff] [blame] | 1512 | png_warning(png_ptr, "leading spaces removed from keyword"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1513 | |
| 1514 | while (*kp == ' ') |
| 1515 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1516 | kp++; |
| 1517 | key_len--; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1518 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1519 | } |
| 1520 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1521 | png_debug1(2, "Checking for multiple internal spaces in '%s'", kp); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1522 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1523 | /* Remove multiple internal spaces. */ |
| 1524 | for (kflag = 0, dp = *new_key; *kp != '\0'; kp++) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1525 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1526 | if (*kp == ' ' && kflag == 0) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1527 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1528 | *(dp++) = *kp; |
| 1529 | kflag = 1; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1530 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1531 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1532 | else if (*kp == ' ') |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1533 | { |
| 1534 | key_len--; |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1535 | kwarn = 1; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1536 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1537 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1538 | else |
| 1539 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1540 | *(dp++) = *kp; |
| 1541 | kflag = 0; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1542 | } |
| 1543 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1544 | *dp = '\0'; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1545 | if (kwarn) |
Glenn Randers-Pehrson | f6b4f45 | 2000-12-01 09:23:06 -0600 | [diff] [blame] | 1546 | png_warning(png_ptr, "extra interior spaces removed from keyword"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1547 | |
| 1548 | if (key_len == 0) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1549 | { |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 1550 | png_free(png_ptr, *new_key); |
Glenn Randers-Pehrson | f6b4f45 | 2000-12-01 09:23:06 -0600 | [diff] [blame] | 1551 | png_warning(png_ptr, "Zero length keyword"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | if (key_len > 79) |
| 1555 | { |
Glenn Randers-Pehrson | f6b4f45 | 2000-12-01 09:23:06 -0600 | [diff] [blame] | 1556 | png_warning(png_ptr, "keyword length must be 1 - 79 characters"); |
Glenn Randers-Pehrson | 71a3c1f | 2008-11-26 12:00:38 -0600 | [diff] [blame] | 1557 | (*new_key)[79] = '\0'; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1558 | key_len = 79; |
| 1559 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1560 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 1561 | return (key_len); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1562 | } |
| 1563 | #endif |
| 1564 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1565 | #ifdef PNG_WRITE_tEXt_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1566 | /* Write a tEXt chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1567 | void /* PRIVATE */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1568 | png_write_tEXt(png_structp png_ptr, png_const_charp key, png_const_charp text, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1569 | png_size_t text_len) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1570 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 1571 | PNG_tEXt; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1572 | png_size_t key_len; |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1573 | png_charp new_key; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1574 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1575 | png_debug(1, "in png_write_tEXt"); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 1576 | |
Glenn Randers-Pehrson | 5ca69e4 | 2008-12-06 05:38:27 -0600 | [diff] [blame] | 1577 | if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1578 | return; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1579 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1580 | if (text == NULL || *text == '\0') |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1581 | text_len = 0; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1582 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1583 | else |
| 1584 | text_len = png_strlen(text); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1585 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1586 | /* Make sure we include the 0 after the key */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1587 | png_write_chunk_start(png_ptr, png_tEXt, |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1588 | (png_uint_32)(key_len + text_len + 1)); |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1589 | /* |
| 1590 | * We leave it to the application to meet PNG-1.0 requirements on the |
| 1591 | * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of |
| 1592 | * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them. |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1593 | * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG. |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 1594 | */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1595 | png_write_chunk_data(png_ptr, (png_bytep)new_key, |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1596 | (png_size_t)(key_len + 1)); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1597 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1598 | if (text_len) |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1599 | png_write_chunk_data(png_ptr, (png_const_bytep)text, |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1600 | (png_size_t)text_len); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1601 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1602 | png_write_chunk_end(png_ptr); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1603 | png_free(png_ptr, new_key); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1604 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1605 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1606 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1607 | #ifdef PNG_WRITE_zTXt_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1608 | /* Write a compressed text chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1609 | void /* PRIVATE */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1610 | png_write_zTXt(png_structp png_ptr, png_const_charp key, png_const_charp text, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1611 | png_size_t text_len, int compression) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1612 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 1613 | PNG_zTXt; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1614 | png_size_t key_len; |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1615 | png_byte buf; |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1616 | png_charp new_key; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1617 | compression_state comp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1618 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1619 | png_debug(1, "in png_write_zTXt"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1620 | |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 1621 | comp.num_output_ptr = 0; |
| 1622 | comp.max_output_ptr = 0; |
| 1623 | comp.output_ptr = NULL; |
| 1624 | comp.input = NULL; |
| 1625 | comp.input_len = 0; |
| 1626 | |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1627 | if ((key_len = png_check_keyword(png_ptr, key, &new_key)) == 0) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1628 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1629 | png_free(png_ptr, new_key); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1630 | return; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1631 | } |
| 1632 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1633 | if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE) |
| 1634 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1635 | png_write_tEXt(png_ptr, new_key, text, (png_size_t)0); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1636 | png_free(png_ptr, new_key); |
| 1637 | return; |
| 1638 | } |
| 1639 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1640 | text_len = png_strlen(text); |
| 1641 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1642 | /* Compute the compressed data; do it now for the length */ |
Glenn Randers-Pehrson | 6942d53 | 2000-05-01 09:31:54 -0500 | [diff] [blame] | 1643 | text_len = png_text_compress(png_ptr, text, text_len, compression, |
| 1644 | &comp); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1645 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1646 | /* Write start of chunk */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1647 | png_write_chunk_start(png_ptr, png_zTXt, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1648 | (png_uint_32)(key_len+text_len + 2)); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1649 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1650 | /* Write key */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1651 | png_write_chunk_data(png_ptr, (png_bytep)new_key, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1652 | (png_size_t)(key_len + 1)); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1653 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1654 | png_free(png_ptr, new_key); |
| 1655 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1656 | buf = (png_byte)compression; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1657 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1658 | /* Write compression */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1659 | png_write_chunk_data(png_ptr, &buf, (png_size_t)1); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1660 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1661 | /* Write the compressed data */ |
Glenn Randers-Pehrson | 6785856 | 2011-04-02 08:26:42 -0500 | [diff] [blame^] | 1662 | comp.input_len = text_len; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1663 | png_write_compressed_data_out(png_ptr, &comp); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1664 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1665 | /* Close the chunk */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1666 | png_write_chunk_end(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1667 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1668 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1669 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1670 | #ifdef PNG_WRITE_iTXt_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1671 | /* Write an iTXt chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1672 | void /* PRIVATE */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1673 | png_write_iTXt(png_structp png_ptr, int compression, png_const_charp key, |
| 1674 | png_const_charp lang, png_const_charp lang_key, png_const_charp text) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1675 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1676 | PNG_iTXt; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1677 | png_size_t lang_len, key_len, lang_key_len, text_len; |
Glenn Randers-Pehrson | 5ca69e4 | 2008-12-06 05:38:27 -0600 | [diff] [blame] | 1678 | png_charp new_lang; |
| 1679 | png_charp new_key = NULL; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1680 | png_byte cbuf[2]; |
| 1681 | compression_state comp; |
| 1682 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1683 | png_debug(1, "in png_write_iTXt"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1684 | |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 1685 | comp.num_output_ptr = 0; |
| 1686 | comp.max_output_ptr = 0; |
| 1687 | comp.output_ptr = NULL; |
| 1688 | comp.input = NULL; |
| 1689 | |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1690 | if ((key_len = png_check_keyword(png_ptr, key, &new_key)) == 0) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1691 | return; |
Glenn Randers-Pehrson | 5ca69e4 | 2008-12-06 05:38:27 -0600 | [diff] [blame] | 1692 | |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1693 | if ((lang_len = png_check_keyword(png_ptr, lang, &new_lang)) == 0) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1694 | { |
| 1695 | png_warning(png_ptr, "Empty language field in iTXt chunk"); |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame] | 1696 | new_lang = NULL; |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 1697 | lang_len = 0; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1698 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1699 | |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame] | 1700 | if (lang_key == NULL) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1701 | lang_key_len = 0; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1702 | |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame] | 1703 | else |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1704 | lang_key_len = png_strlen(lang_key); |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame] | 1705 | |
| 1706 | if (text == NULL) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1707 | text_len = 0; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1708 | |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame] | 1709 | else |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1710 | text_len = png_strlen(text); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1711 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1712 | /* Compute the compressed data; do it now for the length */ |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1713 | text_len = png_text_compress(png_ptr, text, text_len, compression - 2, |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1714 | &comp); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1715 | |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame] | 1716 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1717 | /* Make sure we include the compression flag, the compression byte, |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1718 | * and the NULs after the key, lang, and lang_key parts |
| 1719 | */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1720 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1721 | png_write_chunk_start(png_ptr, png_iTXt, (png_uint_32)( |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1722 | 5 /* comp byte, comp flag, terminators for key, lang and lang_key */ |
| 1723 | + key_len |
| 1724 | + lang_len |
| 1725 | + lang_key_len |
| 1726 | + text_len)); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1727 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1728 | /* We leave it to the application to meet PNG-1.0 requirements on the |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1729 | * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of |
| 1730 | * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them. |
| 1731 | * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG. |
| 1732 | */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1733 | png_write_chunk_data(png_ptr, (png_bytep)new_key, (png_size_t)(key_len + 1)); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1734 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1735 | /* Set the compression flag */ |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1736 | if (compression == PNG_ITXT_COMPRESSION_NONE || |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1737 | compression == PNG_TEXT_COMPRESSION_NONE) |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1738 | cbuf[0] = 0; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1739 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1740 | else /* compression == PNG_ITXT_COMPRESSION_zTXt */ |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1741 | cbuf[0] = 1; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1742 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1743 | /* Set the compression method */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1744 | cbuf[1] = 0; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1745 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1746 | png_write_chunk_data(png_ptr, cbuf, (png_size_t)2); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 1747 | |
Glenn Randers-Pehrson | 73d57cb | 2002-03-25 18:49:08 -0600 | [diff] [blame] | 1748 | cbuf[0] = 0; |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1749 | png_write_chunk_data(png_ptr, (new_lang ? (png_const_bytep)new_lang : cbuf), |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1750 | (png_size_t)(lang_len + 1)); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1751 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1752 | png_write_chunk_data(png_ptr, (lang_key ? (png_const_bytep)lang_key : cbuf), |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1753 | (png_size_t)(lang_key_len + 1)); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1754 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1755 | png_write_compressed_data_out(png_ptr, &comp); |
| 1756 | |
| 1757 | png_write_chunk_end(png_ptr); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1758 | |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1759 | png_free(png_ptr, new_key); |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1760 | png_free(png_ptr, new_lang); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1761 | } |
| 1762 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1763 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1764 | #ifdef PNG_WRITE_oFFs_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1765 | /* Write the oFFs chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1766 | void /* PRIVATE */ |
Glenn Randers-Pehrson | b182893 | 2001-06-23 08:03:17 -0500 | [diff] [blame] | 1767 | png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1768 | int unit_type) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1769 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 1770 | PNG_oFFs; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1771 | png_byte buf[9]; |
| 1772 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1773 | png_debug(1, "in png_write_oFFs"); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 1774 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1775 | if (unit_type >= PNG_OFFSET_LAST) |
| 1776 | png_warning(png_ptr, "Unrecognized unit type for oFFs chunk"); |
| 1777 | |
Glenn Randers-Pehrson | b182893 | 2001-06-23 08:03:17 -0500 | [diff] [blame] | 1778 | png_save_int_32(buf, x_offset); |
| 1779 | png_save_int_32(buf + 4, y_offset); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1780 | buf[8] = (png_byte)unit_type; |
| 1781 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1782 | png_write_chunk(png_ptr, png_oFFs, buf, (png_size_t)9); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1783 | } |
| 1784 | #endif |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1785 | #ifdef PNG_WRITE_pCAL_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1786 | /* Write the pCAL chunk (described in the PNG extensions document) */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1787 | void /* PRIVATE */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1788 | png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0, |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 1789 | png_int_32 X1, int type, int nparams, png_const_charp units, |
| 1790 | png_charpp params) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1791 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 1792 | PNG_pCAL; |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 1793 | png_size_t purpose_len, units_len, total_len; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1794 | png_uint_32p params_len; |
| 1795 | png_byte buf[10]; |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1796 | png_charp new_purpose; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1797 | int i; |
| 1798 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1799 | png_debug1(1, "in png_write_pCAL (%d parameters)", nparams); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 1800 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1801 | if (type >= PNG_EQUATION_LAST) |
| 1802 | png_warning(png_ptr, "Unrecognized equation type for pCAL chunk"); |
| 1803 | |
| 1804 | purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1; |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1805 | png_debug1(3, "pCAL purpose length = %d", (int)purpose_len); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1806 | units_len = png_strlen(units) + (nparams == 0 ? 0 : 1); |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1807 | png_debug1(3, "pCAL units length = %d", (int)units_len); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1808 | total_len = purpose_len + units_len + 10; |
| 1809 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1810 | params_len = (png_uint_32p)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1811 | (png_alloc_size_t)(nparams * png_sizeof(png_uint_32))); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1812 | |
| 1813 | /* Find the length of each parameter, making sure we don't count the |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1814 | * null terminator for the last parameter. |
| 1815 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1816 | for (i = 0; i < nparams; i++) |
| 1817 | { |
| 1818 | params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1); |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1819 | png_debug2(3, "pCAL parameter %d length = %lu", i, |
Glenn Randers-Pehrson | d233287 | 2010-10-12 19:19:28 -0500 | [diff] [blame] | 1820 | (unsigned long)params_len[i]); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1821 | total_len += (png_size_t)params_len[i]; |
| 1822 | } |
| 1823 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1824 | png_debug1(3, "pCAL total length = %d", (int)total_len); |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1825 | png_write_chunk_start(png_ptr, png_pCAL, (png_uint_32)total_len); |
| 1826 | png_write_chunk_data(png_ptr, (png_const_bytep)new_purpose, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1827 | (png_size_t)purpose_len); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1828 | png_save_int_32(buf, X0); |
| 1829 | png_save_int_32(buf + 4, X1); |
| 1830 | buf[8] = (png_byte)type; |
| 1831 | buf[9] = (png_byte)nparams; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1832 | png_write_chunk_data(png_ptr, buf, (png_size_t)10); |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1833 | png_write_chunk_data(png_ptr, (png_const_bytep)units, (png_size_t)units_len); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1834 | |
| 1835 | png_free(png_ptr, new_purpose); |
| 1836 | |
| 1837 | for (i = 0; i < nparams; i++) |
| 1838 | { |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1839 | png_write_chunk_data(png_ptr, (png_const_bytep)params[i], |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1840 | (png_size_t)params_len[i]); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1841 | } |
| 1842 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 1843 | png_free(png_ptr, params_len); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1844 | png_write_chunk_end(png_ptr); |
| 1845 | } |
| 1846 | #endif |
| 1847 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1848 | #ifdef PNG_WRITE_sCAL_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1849 | /* Write the sCAL chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1850 | void /* PRIVATE */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1851 | png_write_sCAL_s(png_structp png_ptr, int unit, png_const_charp width, |
| 1852 | png_const_charp height) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1853 | { |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1854 | PNG_sCAL; |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 1855 | png_byte buf[64]; |
| 1856 | png_size_t wlen, hlen, total_len; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1857 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1858 | png_debug(1, "in png_write_sCAL_s"); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1859 | |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 1860 | wlen = png_strlen(width); |
| 1861 | hlen = png_strlen(height); |
| 1862 | total_len = wlen + hlen + 2; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1863 | |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 1864 | if (total_len > 64) |
| 1865 | { |
| 1866 | png_warning(png_ptr, "Can't write sCAL (buffer too small)"); |
| 1867 | return; |
| 1868 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1869 | |
Glenn Randers-Pehrson | 6bc53be | 2006-06-16 07:52:03 -0500 | [diff] [blame] | 1870 | buf[0] = (png_byte)unit; |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 1871 | png_memcpy(buf + 1, width, wlen + 1); /* Append the '\0' here */ |
| 1872 | png_memcpy(buf + wlen + 2, height, hlen); /* Do NOT append the '\0' here */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1873 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1874 | png_debug1(3, "sCAL total length = %u", (unsigned int)total_len); |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1875 | png_write_chunk(png_ptr, png_sCAL, buf, total_len); |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1876 | } |
| 1877 | #endif |
| 1878 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1879 | #ifdef PNG_WRITE_pHYs_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1880 | /* Write the pHYs chunk */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1881 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1882 | png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1883 | png_uint_32 y_pixels_per_unit, |
| 1884 | int unit_type) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1885 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 1886 | PNG_pHYs; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1887 | png_byte buf[9]; |
| 1888 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1889 | png_debug(1, "in png_write_pHYs"); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 1890 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1891 | if (unit_type >= PNG_RESOLUTION_LAST) |
| 1892 | png_warning(png_ptr, "Unrecognized unit type for pHYs chunk"); |
| 1893 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1894 | png_save_uint_32(buf, x_pixels_per_unit); |
| 1895 | png_save_uint_32(buf + 4, y_pixels_per_unit); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1896 | buf[8] = (png_byte)unit_type; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1897 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1898 | png_write_chunk(png_ptr, png_pHYs, buf, (png_size_t)9); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1899 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1900 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1901 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 1902 | #ifdef PNG_WRITE_tIME_SUPPORTED |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 1903 | /* Write the tIME chunk. Use either png_convert_from_struct_tm() |
| 1904 | * or png_convert_from_time_t(), or fill in the structure yourself. |
| 1905 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1906 | void /* PRIVATE */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1907 | png_write_tIME(png_structp png_ptr, png_const_timep mod_time) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1908 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 1909 | PNG_tIME; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1910 | png_byte buf[7]; |
| 1911 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1912 | png_debug(1, "in png_write_tIME"); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 1913 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1914 | if (mod_time->month > 12 || mod_time->month < 1 || |
| 1915 | mod_time->day > 31 || mod_time->day < 1 || |
| 1916 | mod_time->hour > 23 || mod_time->second > 60) |
| 1917 | { |
| 1918 | png_warning(png_ptr, "Invalid time specified for tIME chunk"); |
| 1919 | return; |
| 1920 | } |
| 1921 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1922 | png_save_uint_16(buf, mod_time->year); |
| 1923 | buf[2] = mod_time->month; |
| 1924 | buf[3] = mod_time->day; |
| 1925 | buf[4] = mod_time->hour; |
| 1926 | buf[5] = mod_time->minute; |
| 1927 | buf[6] = mod_time->second; |
| 1928 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1929 | png_write_chunk(png_ptr, png_tIME, buf, (png_size_t)7); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1930 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1931 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1932 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1933 | /* Initializes the row writing capability of libpng */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 1934 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1935 | png_write_start_row(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1936 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1937 | #ifdef PNG_WRITE_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1938 | /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1939 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1940 | /* Start of interlace block */ |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 1941 | int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1942 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1943 | /* Offset to next interlace block */ |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 1944 | int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1945 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1946 | /* Start of interlace block in the y direction */ |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 1947 | int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1948 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1949 | /* Offset to next interlace block in the y direction */ |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 1950 | int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 1951 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 1952 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1953 | png_size_t buf_size; |
| 1954 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 1955 | png_debug(1, "in png_write_start_row"); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 1956 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1957 | buf_size = (png_size_t)(PNG_ROWBYTES( |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1958 | png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1959 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1960 | /* Set up row buffer */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 1961 | png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1962 | (png_alloc_size_t)buf_size); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1963 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1964 | png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1965 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 1966 | #ifdef PNG_WRITE_FILTER_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 1967 | /* Set up filtering buffer, if using this filter */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1968 | if (png_ptr->do_filter & PNG_FILTER_SUB) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1969 | { |
Glenn Randers-Pehrson | bcb3aac | 2010-09-10 22:05:27 -0500 | [diff] [blame] | 1970 | png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, png_ptr->rowbytes + 1); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1971 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1972 | png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1973 | } |
| 1974 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1975 | /* We only need to keep the previous row if we are using one of these. */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1976 | if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH)) |
| 1977 | { |
Glenn Randers-Pehrson | dfa99af | 2009-10-29 23:33:46 -0500 | [diff] [blame] | 1978 | /* Set up previous row buffer */ |
| 1979 | png_ptr->prev_row = (png_bytep)png_calloc(png_ptr, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 1980 | (png_alloc_size_t)buf_size); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1981 | |
| 1982 | if (png_ptr->do_filter & PNG_FILTER_UP) |
| 1983 | { |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 1984 | png_ptr->up_row = (png_bytep)png_malloc(png_ptr, |
Glenn Randers-Pehrson | bcb3aac | 2010-09-10 22:05:27 -0500 | [diff] [blame] | 1985 | png_ptr->rowbytes + 1); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1986 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1987 | png_ptr->up_row[0] = PNG_FILTER_VALUE_UP; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | if (png_ptr->do_filter & PNG_FILTER_AVG) |
| 1991 | { |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 1992 | png_ptr->avg_row = (png_bytep)png_malloc(png_ptr, |
Glenn Randers-Pehrson | bcb3aac | 2010-09-10 22:05:27 -0500 | [diff] [blame] | 1993 | png_ptr->rowbytes + 1); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1994 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 1995 | png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | if (png_ptr->do_filter & PNG_FILTER_PAETH) |
| 1999 | { |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 2000 | png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr, |
Glenn Randers-Pehrson | bcb3aac | 2010-09-10 22:05:27 -0500 | [diff] [blame] | 2001 | png_ptr->rowbytes + 1); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2002 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2003 | png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2004 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2005 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 2006 | #endif /* PNG_WRITE_FILTER_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2007 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 2008 | #ifdef PNG_WRITE_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2009 | /* If interlaced, we need to set up width and height of pass */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2010 | if (png_ptr->interlaced) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2011 | { |
| 2012 | if (!(png_ptr->transformations & PNG_INTERLACE)) |
| 2013 | { |
| 2014 | png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2015 | png_pass_ystart[0]) / png_pass_yinc[0]; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2016 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2017 | png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 - |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2018 | png_pass_start[0]) / png_pass_inc[0]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2019 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2020 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2021 | else |
| 2022 | { |
| 2023 | png_ptr->num_rows = png_ptr->height; |
| 2024 | png_ptr->usr_width = png_ptr->width; |
| 2025 | } |
| 2026 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2027 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2028 | else |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 2029 | #endif |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2030 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2031 | png_ptr->num_rows = png_ptr->height; |
| 2032 | png_ptr->usr_width = png_ptr->width; |
| 2033 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2034 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2035 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
| 2036 | png_ptr->zstream.next_out = png_ptr->zbuf; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2037 | } |
| 2038 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2039 | /* Internal use only. Called when finished processing a row of data. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2040 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2041 | png_write_finish_row(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2042 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2043 | #ifdef PNG_WRITE_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2044 | /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2045 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2046 | /* Start of interlace block */ |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2047 | int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2048 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2049 | /* Offset to next interlace block */ |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2050 | int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2051 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2052 | /* Start of interlace block in the y direction */ |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2053 | int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2054 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2055 | /* Offset to next interlace block in the y direction */ |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2056 | int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame] | 2057 | #endif |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2058 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2059 | int ret; |
| 2060 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2061 | png_debug(1, "in png_write_finish_row"); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 2062 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 2063 | /* Next row */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2064 | png_ptr->row_number++; |
Guy Schalnat | c21f90c | 1996-06-17 16:24:45 -0500 | [diff] [blame] | 2065 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2066 | /* See if we are done */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2067 | if (png_ptr->row_number < png_ptr->num_rows) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2068 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2069 | |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 2070 | #ifdef PNG_WRITE_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2071 | /* If interlaced, go to next pass */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2072 | if (png_ptr->interlaced) |
| 2073 | { |
| 2074 | png_ptr->row_number = 0; |
| 2075 | if (png_ptr->transformations & PNG_INTERLACE) |
| 2076 | { |
| 2077 | png_ptr->pass++; |
| 2078 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2079 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2080 | else |
| 2081 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2082 | /* Loop until we find a non-zero width or height pass */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2083 | do |
| 2084 | { |
| 2085 | png_ptr->pass++; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2086 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2087 | if (png_ptr->pass >= 7) |
| 2088 | break; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2089 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2090 | png_ptr->usr_width = (png_ptr->width + |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2091 | png_pass_inc[png_ptr->pass] - 1 - |
| 2092 | png_pass_start[png_ptr->pass]) / |
| 2093 | png_pass_inc[png_ptr->pass]; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2094 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2095 | png_ptr->num_rows = (png_ptr->height + |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2096 | png_pass_yinc[png_ptr->pass] - 1 - |
| 2097 | png_pass_ystart[png_ptr->pass]) / |
| 2098 | png_pass_yinc[png_ptr->pass]; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2099 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2100 | if (png_ptr->transformations & PNG_INTERLACE) |
| 2101 | break; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2102 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2103 | } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0); |
| 2104 | |
| 2105 | } |
| 2106 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2107 | /* Reset the row above the image for the next pass */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2108 | if (png_ptr->pass < 7) |
Guy Schalnat | c21f90c | 1996-06-17 16:24:45 -0500 | [diff] [blame] | 2109 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2110 | if (png_ptr->prev_row != NULL) |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2111 | png_memset(png_ptr->prev_row, 0, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2112 | (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels* |
| 2113 | png_ptr->usr_bit_depth, png_ptr->width)) + 1); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2114 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2115 | return; |
Guy Schalnat | c21f90c | 1996-06-17 16:24:45 -0500 | [diff] [blame] | 2116 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2117 | } |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 2118 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2119 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2120 | /* If we get here, we've just written the last row, so we need |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2121 | to flush the compressor */ |
| 2122 | do |
| 2123 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2124 | /* Tell the compressor we are done */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2125 | ret = deflate(&png_ptr->zstream, Z_FINISH); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2126 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2127 | /* Check for an error */ |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 2128 | if (ret == Z_OK) |
| 2129 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2130 | /* Check to see if we need more room */ |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 2131 | if (!(png_ptr->zstream.avail_out)) |
| 2132 | { |
| 2133 | png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); |
| 2134 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 2135 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
| 2136 | } |
| 2137 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2138 | |
Glenn Randers-Pehrson | 104622b | 2000-05-29 08:58:03 -0500 | [diff] [blame] | 2139 | else if (ret != Z_STREAM_END) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2140 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2141 | if (png_ptr->zstream.msg != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2142 | png_error(png_ptr, png_ptr->zstream.msg); |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2143 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2144 | else |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2145 | png_error(png_ptr, "zlib error"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2146 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2147 | } while (ret != Z_STREAM_END); |
| 2148 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2149 | /* Write any extra space */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2150 | if (png_ptr->zstream.avail_out < png_ptr->zbuf_size) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2151 | { |
| 2152 | png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size - |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2153 | png_ptr->zstream.avail_out); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2154 | } |
| 2155 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 2156 | deflateReset(&png_ptr->zstream); |
Glenn Randers-Pehrson | 878b31e | 2004-11-12 22:04:56 -0600 | [diff] [blame] | 2157 | png_ptr->zstream.data_type = Z_BINARY; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2158 | } |
| 2159 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2160 | #ifdef PNG_WRITE_INTERLACING_SUPPORTED |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 2161 | /* Pick out the correct pixels for the interlace pass. |
| 2162 | * The basic idea here is to go through the row with a source |
| 2163 | * pointer and a destination pointer (sp and dp), and copy the |
| 2164 | * correct pixels for the pass. As the row gets compacted, |
| 2165 | * sp will always be >= dp, so we should never overwrite anything. |
| 2166 | * See the default: case for the easiest code to understand. |
| 2167 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2168 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2169 | png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2170 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2171 | /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2172 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2173 | /* Start of interlace block */ |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2174 | int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2175 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2176 | /* Offset to next interlace block */ |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 2177 | int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 2178 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 2179 | png_debug(1, "in png_do_write_interlace"); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 2180 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2181 | /* We don't have to do anything on the last pass (6) */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2182 | if (pass < 6) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2183 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2184 | /* Each pixel depth is handled separately */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2185 | switch (row_info->pixel_depth) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2186 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2187 | case 1: |
| 2188 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2189 | png_bytep sp; |
| 2190 | png_bytep dp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2191 | int shift; |
| 2192 | int d; |
| 2193 | int value; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2194 | png_uint_32 i; |
| 2195 | png_uint_32 row_width = row_info->width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2196 | |
| 2197 | dp = row; |
| 2198 | d = 0; |
| 2199 | shift = 7; |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 2200 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2201 | for (i = png_pass_start[pass]; i < row_width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2202 | i += png_pass_inc[pass]) |
| 2203 | { |
| 2204 | sp = row + (png_size_t)(i >> 3); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2205 | value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2206 | d |= (value << shift); |
| 2207 | |
| 2208 | if (shift == 0) |
| 2209 | { |
| 2210 | shift = 7; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2211 | *dp++ = (png_byte)d; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2212 | d = 0; |
| 2213 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2214 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2215 | else |
| 2216 | shift--; |
| 2217 | |
| 2218 | } |
| 2219 | if (shift != 7) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2220 | *dp = (png_byte)d; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2221 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2222 | break; |
| 2223 | } |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2224 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2225 | case 2: |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2226 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2227 | png_bytep sp; |
| 2228 | png_bytep dp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2229 | int shift; |
| 2230 | int d; |
| 2231 | int value; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2232 | png_uint_32 i; |
| 2233 | png_uint_32 row_width = row_info->width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2234 | |
| 2235 | dp = row; |
| 2236 | shift = 6; |
| 2237 | d = 0; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2238 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2239 | for (i = png_pass_start[pass]; i < row_width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2240 | i += png_pass_inc[pass]) |
| 2241 | { |
| 2242 | sp = row + (png_size_t)(i >> 2); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2243 | value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2244 | d |= (value << shift); |
| 2245 | |
| 2246 | if (shift == 0) |
| 2247 | { |
| 2248 | shift = 6; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2249 | *dp++ = (png_byte)d; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2250 | d = 0; |
| 2251 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2252 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2253 | else |
| 2254 | shift -= 2; |
| 2255 | } |
| 2256 | if (shift != 6) |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2257 | *dp = (png_byte)d; |
| 2258 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2259 | break; |
| 2260 | } |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2261 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2262 | case 4: |
| 2263 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2264 | png_bytep sp; |
| 2265 | png_bytep dp; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2266 | int shift; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2267 | int d; |
| 2268 | int value; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2269 | png_uint_32 i; |
| 2270 | png_uint_32 row_width = row_info->width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2271 | |
| 2272 | dp = row; |
| 2273 | shift = 4; |
| 2274 | d = 0; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2275 | for (i = png_pass_start[pass]; i < row_width; |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2276 | i += png_pass_inc[pass]) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2277 | { |
| 2278 | sp = row + (png_size_t)(i >> 1); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 2279 | value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2280 | d |= (value << shift); |
| 2281 | |
| 2282 | if (shift == 0) |
| 2283 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2284 | shift = 4; |
| 2285 | *dp++ = (png_byte)d; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2286 | d = 0; |
| 2287 | } |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 2288 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2289 | else |
| 2290 | shift -= 4; |
| 2291 | } |
| 2292 | if (shift != 4) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2293 | *dp = (png_byte)d; |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 2294 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2295 | break; |
| 2296 | } |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2297 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2298 | default: |
| 2299 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 2300 | png_bytep sp; |
| 2301 | png_bytep dp; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2302 | png_uint_32 i; |
| 2303 | png_uint_32 row_width = row_info->width; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2304 | png_size_t pixel_bytes; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2305 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2306 | /* Start at the beginning */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2307 | dp = row; |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 2308 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2309 | /* Find out how many bytes each pixel takes up */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2310 | pixel_bytes = (row_info->pixel_depth >> 3); |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2311 | |
| 2312 | /* Loop through the row, only looking at the pixels that matter */ |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2313 | for (i = png_pass_start[pass]; i < row_width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2314 | i += png_pass_inc[pass]) |
| 2315 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2316 | /* Find out where the original pixel is */ |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 2317 | sp = row + (png_size_t)i * pixel_bytes; |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 2318 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2319 | /* Move the pixel */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2320 | if (dp != sp) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2321 | png_memcpy(dp, sp, pixel_bytes); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2322 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2323 | /* Next pixel */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2324 | dp += pixel_bytes; |
| 2325 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 2326 | break; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2327 | } |
| 2328 | } |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2329 | /* Set new row width */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2330 | row_info->width = (row_info->width + |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2331 | png_pass_inc[pass] - 1 - |
| 2332 | png_pass_start[pass]) / |
| 2333 | png_pass_inc[pass]; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2334 | |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2335 | row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, |
| 2336 | row_info->width); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2337 | } |
| 2338 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2339 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2340 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2341 | /* This filters the row, chooses which filter to use, if it has not already |
| 2342 | * been specified by the application, and then writes the row out with the |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 2343 | * chosen filter. |
| 2344 | */ |
Glenn Randers-Pehrson | 7806777 | 2004-11-02 21:49:39 -0600 | [diff] [blame] | 2345 | #define PNG_MAXSUM (((png_uint_32)(-1)) >> 1) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2346 | #define PNG_HISHIFT 10 |
Glenn Randers-Pehrson | ea3bcd7 | 1998-03-07 14:33:00 -0600 | [diff] [blame] | 2347 | #define PNG_LOMASK ((png_uint_32)0xffffL) |
| 2348 | #define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT)) |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 2349 | void /* PRIVATE */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2350 | png_write_find_filter(png_structp png_ptr, png_row_infop row_info) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2351 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2352 | png_bytep best_row; |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 2353 | #ifdef PNG_WRITE_FILTER_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2354 | png_bytep prev_row, row_buf; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2355 | png_uint_32 mins, bpp; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2356 | png_byte filter_to_do = png_ptr->do_filter; |
Glenn Randers-Pehrson | bcb3aac | 2010-09-10 22:05:27 -0500 | [diff] [blame] | 2357 | png_size_t row_bytes = row_info->rowbytes; |
Glenn Randers-Pehrson | 9d8b41e | 2009-07-19 14:45:43 -0500 | [diff] [blame] | 2358 | #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2359 | int num_p_filters = (int)png_ptr->num_prev_filters; |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 2360 | #endif |
Glenn Randers-Pehrson | 9d8b41e | 2009-07-19 14:45:43 -0500 | [diff] [blame] | 2361 | |
| 2362 | png_debug(1, "in png_write_find_filter"); |
| 2363 | |
| 2364 | #ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED |
Glenn Randers-Pehrson | 4ace0e1 | 2009-07-19 15:04:35 -0500 | [diff] [blame] | 2365 | if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS) |
Glenn Randers-Pehrson | 9c90d7f | 2009-07-19 13:11:25 -0500 | [diff] [blame] | 2366 | { |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2367 | /* These will never be selected so we need not test them. */ |
| 2368 | filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_PAETH); |
Glenn Randers-Pehrson | 9c90d7f | 2009-07-19 13:11:25 -0500 | [diff] [blame] | 2369 | } |
Glenn Randers-Pehrson | 821b710 | 2010-06-24 16:16:32 -0500 | [diff] [blame] | 2370 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2371 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 2372 | /* Find out how many bytes offset each pixel is */ |
Glenn Randers-Pehrson | 272489d | 2004-08-04 06:34:52 -0500 | [diff] [blame] | 2373 | bpp = (row_info->pixel_depth + 7) >> 3; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2374 | |
| 2375 | prev_row = png_ptr->prev_row; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2376 | #endif |
| 2377 | best_row = png_ptr->row_buf; |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 2378 | #ifdef PNG_WRITE_FILTER_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 2379 | row_buf = best_row; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2380 | mins = PNG_MAXSUM; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2381 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2382 | /* The prediction method we use is to find which method provides the |
| 2383 | * smallest value when summing the absolute values of the distances |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 2384 | * from zero, using anything >= 128 as negative numbers. This is known |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2385 | * as the "minimum sum of absolute differences" heuristic. Other |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2386 | * heuristics are the "weighted minimum sum of absolute differences" |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2387 | * (experimental and can in theory improve compression), and the "zlib |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2388 | * predictive" method (not implemented yet), which does test compressions |
| 2389 | * of lines using different filter methods, and then chooses the |
| 2390 | * (series of) filter(s) that give minimum compressed data size (VERY |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2391 | * computationally expensive). |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2392 | * |
| 2393 | * GRR 980525: consider also |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 2394 | * |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2395 | * (1) minimum sum of absolute differences from running average (i.e., |
| 2396 | * keep running sum of non-absolute differences & count of bytes) |
| 2397 | * [track dispersion, too? restart average if dispersion too large?] |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 2398 | * |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2399 | * (1b) minimum sum of absolute differences from sliding average, probably |
| 2400 | * with window size <= deflate window (usually 32K) |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 2401 | * |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 2402 | * (2) minimum sum of squared differences from zero or running average |
| 2403 | * (i.e., ~ root-mean-square approach) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2404 | */ |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2405 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2406 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2407 | /* We don't need to test the 'no filter' case if this is the only filter |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2408 | * that has been chosen, as it doesn't actually do anything to the data. |
| 2409 | */ |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2410 | if ((filter_to_do & PNG_FILTER_NONE) && filter_to_do != PNG_FILTER_NONE) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2411 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2412 | png_bytep rp; |
| 2413 | png_uint_32 sum = 0; |
Glenn Randers-Pehrson | bcb3aac | 2010-09-10 22:05:27 -0500 | [diff] [blame] | 2414 | png_size_t i; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2415 | int v; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2416 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2417 | for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2418 | { |
| 2419 | v = *rp; |
| 2420 | sum += (v < 128) ? v : 256 - v; |
| 2421 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2422 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2423 | #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2424 | if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
| 2425 | { |
| 2426 | png_uint_32 sumhi, sumlo; |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 2427 | int j; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2428 | sumlo = sum & PNG_LOMASK; |
| 2429 | sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */ |
| 2430 | |
| 2431 | /* Reduce the sum if we match any of the previous rows */ |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2432 | for (j = 0; j < num_p_filters; j++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2433 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2434 | if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2435 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2436 | sumlo = (sumlo * png_ptr->filter_weights[j]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2437 | PNG_WEIGHT_SHIFT; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2438 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2439 | sumhi = (sumhi * png_ptr->filter_weights[j]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2440 | PNG_WEIGHT_SHIFT; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2441 | } |
| 2442 | } |
| 2443 | |
| 2444 | /* Factor in the cost of this filter (this is here for completeness, |
| 2445 | * but it makes no sense to have a "cost" for the NONE filter, as |
| 2446 | * it has the minimum possible computational cost - none). |
| 2447 | */ |
| 2448 | sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2449 | PNG_COST_SHIFT; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2450 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2451 | sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2452 | PNG_COST_SHIFT; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2453 | |
| 2454 | if (sumhi > PNG_HIMASK) |
| 2455 | sum = PNG_MAXSUM; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2456 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2457 | else |
| 2458 | sum = (sumhi << PNG_HISHIFT) + sumlo; |
| 2459 | } |
| 2460 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2461 | mins = sum; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2462 | } |
| 2463 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2464 | /* Sub filter */ |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2465 | if (filter_to_do == PNG_FILTER_SUB) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2466 | /* It's the only filter so no testing is needed */ |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2467 | { |
| 2468 | png_bytep rp, lp, dp; |
Glenn Randers-Pehrson | bcb3aac | 2010-09-10 22:05:27 -0500 | [diff] [blame] | 2469 | png_size_t i; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2470 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2471 | for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp; |
| 2472 | i++, rp++, dp++) |
| 2473 | { |
| 2474 | *dp = *rp; |
| 2475 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2476 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2477 | for (lp = row_buf + 1; i < row_bytes; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2478 | i++, rp++, lp++, dp++) |
| 2479 | { |
| 2480 | *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); |
| 2481 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2482 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2483 | best_row = png_ptr->sub_row; |
| 2484 | } |
| 2485 | |
| 2486 | else if (filter_to_do & PNG_FILTER_SUB) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2487 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2488 | png_bytep rp, dp, lp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2489 | png_uint_32 sum = 0, lmins = mins; |
Glenn Randers-Pehrson | bcb3aac | 2010-09-10 22:05:27 -0500 | [diff] [blame] | 2490 | png_size_t i; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2491 | int v; |
| 2492 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2493 | #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2494 | /* We temporarily increase the "minimum sum" by the factor we |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2495 | * would reduce the sum of this filter, so that we can do the |
| 2496 | * early exit comparison without scaling the sum each time. |
| 2497 | */ |
| 2498 | if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
| 2499 | { |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 2500 | int j; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2501 | png_uint_32 lmhi, lmlo; |
| 2502 | lmlo = lmins & PNG_LOMASK; |
| 2503 | lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; |
| 2504 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2505 | for (j = 0; j < num_p_filters; j++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2506 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2507 | if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2508 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2509 | lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2510 | PNG_WEIGHT_SHIFT; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2511 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2512 | lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2513 | PNG_WEIGHT_SHIFT; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2514 | } |
| 2515 | } |
| 2516 | |
| 2517 | lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2518 | PNG_COST_SHIFT; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2519 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2520 | lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2521 | PNG_COST_SHIFT; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2522 | |
| 2523 | if (lmhi > PNG_HIMASK) |
| 2524 | lmins = PNG_MAXSUM; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2525 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2526 | else |
| 2527 | lmins = (lmhi << PNG_HISHIFT) + lmlo; |
| 2528 | } |
| 2529 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2530 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2531 | for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp; |
| 2532 | i++, rp++, dp++) |
| 2533 | { |
| 2534 | v = *dp = *rp; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2535 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2536 | sum += (v < 128) ? v : 256 - v; |
| 2537 | } |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2538 | |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 2539 | for (lp = row_buf + 1; i < row_bytes; |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 2540 | i++, rp++, lp++, dp++) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2541 | { |
| 2542 | v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2543 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2544 | sum += (v < 128) ? v : 256 - v; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2545 | |
| 2546 | if (sum > lmins) /* We are already worse, don't continue. */ |
| 2547 | break; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2548 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2549 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2550 | #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2551 | if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
| 2552 | { |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 2553 | int j; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2554 | png_uint_32 sumhi, sumlo; |
| 2555 | sumlo = sum & PNG_LOMASK; |
| 2556 | sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; |
| 2557 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2558 | for (j = 0; j < num_p_filters; j++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2559 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2560 | if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2561 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2562 | sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2563 | PNG_WEIGHT_SHIFT; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2564 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2565 | sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2566 | PNG_WEIGHT_SHIFT; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2571 | PNG_COST_SHIFT; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2572 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2573 | sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2574 | PNG_COST_SHIFT; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2575 | |
| 2576 | if (sumhi > PNG_HIMASK) |
| 2577 | sum = PNG_MAXSUM; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2578 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2579 | else |
| 2580 | sum = (sumhi << PNG_HISHIFT) + sumlo; |
| 2581 | } |
| 2582 | #endif |
| 2583 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2584 | if (sum < mins) |
| 2585 | { |
| 2586 | mins = sum; |
| 2587 | best_row = png_ptr->sub_row; |
| 2588 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2589 | } |
| 2590 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2591 | /* Up filter */ |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2592 | if (filter_to_do == PNG_FILTER_UP) |
| 2593 | { |
| 2594 | png_bytep rp, dp, pp; |
Glenn Randers-Pehrson | bcb3aac | 2010-09-10 22:05:27 -0500 | [diff] [blame] | 2595 | png_size_t i; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2596 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2597 | for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2598 | pp = prev_row + 1; i < row_bytes; |
| 2599 | i++, rp++, pp++, dp++) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2600 | { |
| 2601 | *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff); |
| 2602 | } |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 2603 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2604 | best_row = png_ptr->up_row; |
| 2605 | } |
| 2606 | |
| 2607 | else if (filter_to_do & PNG_FILTER_UP) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 2608 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2609 | png_bytep rp, dp, pp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2610 | png_uint_32 sum = 0, lmins = mins; |
Glenn Randers-Pehrson | bcb3aac | 2010-09-10 22:05:27 -0500 | [diff] [blame] | 2611 | png_size_t i; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2612 | int v; |
| 2613 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2614 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2615 | #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2616 | if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
| 2617 | { |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 2618 | int j; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2619 | png_uint_32 lmhi, lmlo; |
| 2620 | lmlo = lmins & PNG_LOMASK; |
| 2621 | lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; |
| 2622 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2623 | for (j = 0; j < num_p_filters; j++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2624 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2625 | if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2626 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2627 | lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2628 | PNG_WEIGHT_SHIFT; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2629 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2630 | lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2631 | PNG_WEIGHT_SHIFT; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2632 | } |
| 2633 | } |
| 2634 | |
| 2635 | lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2636 | PNG_COST_SHIFT; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2637 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2638 | lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2639 | PNG_COST_SHIFT; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2640 | |
| 2641 | if (lmhi > PNG_HIMASK) |
| 2642 | lmins = PNG_MAXSUM; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2643 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2644 | else |
| 2645 | lmins = (lmhi << PNG_HISHIFT) + lmlo; |
| 2646 | } |
| 2647 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2648 | |
| 2649 | for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2650 | pp = prev_row + 1; i < row_bytes; i++) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2651 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2652 | v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2653 | |
| 2654 | sum += (v < 128) ? v : 256 - v; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2655 | |
| 2656 | if (sum > lmins) /* We are already worse, don't continue. */ |
| 2657 | break; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2658 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2659 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2660 | #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2661 | if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
| 2662 | { |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 2663 | int j; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2664 | png_uint_32 sumhi, sumlo; |
| 2665 | sumlo = sum & PNG_LOMASK; |
| 2666 | sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; |
| 2667 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2668 | for (j = 0; j < num_p_filters; j++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2669 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2670 | if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2671 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2672 | sumlo = (sumlo * png_ptr->filter_weights[j]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2673 | PNG_WEIGHT_SHIFT; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2674 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2675 | sumhi = (sumhi * png_ptr->filter_weights[j]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2676 | PNG_WEIGHT_SHIFT; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2677 | } |
| 2678 | } |
| 2679 | |
| 2680 | sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2681 | PNG_COST_SHIFT; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2682 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2683 | sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2684 | PNG_COST_SHIFT; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2685 | |
| 2686 | if (sumhi > PNG_HIMASK) |
| 2687 | sum = PNG_MAXSUM; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2688 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2689 | else |
| 2690 | sum = (sumhi << PNG_HISHIFT) + sumlo; |
| 2691 | } |
| 2692 | #endif |
| 2693 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2694 | if (sum < mins) |
| 2695 | { |
| 2696 | mins = sum; |
| 2697 | best_row = png_ptr->up_row; |
| 2698 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2699 | } |
| 2700 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 2701 | /* Avg filter */ |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2702 | if (filter_to_do == PNG_FILTER_AVG) |
| 2703 | { |
| 2704 | png_bytep rp, dp, pp, lp; |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2705 | png_uint_32 i; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2706 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2707 | for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1, |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2708 | pp = prev_row + 1; i < bpp; i++) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2709 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2710 | *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2711 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2712 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2713 | for (lp = row_buf + 1; i < row_bytes; i++) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2714 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2715 | *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) |
| 2716 | & 0xff); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2717 | } |
| 2718 | best_row = png_ptr->avg_row; |
| 2719 | } |
| 2720 | |
| 2721 | else if (filter_to_do & PNG_FILTER_AVG) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2722 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2723 | png_bytep rp, dp, pp, lp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2724 | png_uint_32 sum = 0, lmins = mins; |
Glenn Randers-Pehrson | bcb3aac | 2010-09-10 22:05:27 -0500 | [diff] [blame] | 2725 | png_size_t i; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2726 | int v; |
| 2727 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2728 | #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2729 | if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
| 2730 | { |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 2731 | int j; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2732 | png_uint_32 lmhi, lmlo; |
| 2733 | lmlo = lmins & PNG_LOMASK; |
| 2734 | lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; |
| 2735 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2736 | for (j = 0; j < num_p_filters; j++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2737 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2738 | if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2739 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2740 | lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2741 | PNG_WEIGHT_SHIFT; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2742 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2743 | lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2744 | PNG_WEIGHT_SHIFT; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2745 | } |
| 2746 | } |
| 2747 | |
| 2748 | lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2749 | PNG_COST_SHIFT; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2750 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2751 | lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2752 | PNG_COST_SHIFT; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2753 | |
| 2754 | if (lmhi > PNG_HIMASK) |
| 2755 | lmins = PNG_MAXSUM; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2756 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2757 | else |
| 2758 | lmins = (lmhi << PNG_HISHIFT) + lmlo; |
| 2759 | } |
| 2760 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2761 | |
| 2762 | for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1, |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2763 | pp = prev_row + 1; i < bpp; i++) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2764 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2765 | v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2766 | |
| 2767 | sum += (v < 128) ? v : 256 - v; |
| 2768 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2769 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2770 | for (lp = row_buf + 1; i < row_bytes; i++) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2771 | { |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 2772 | v = *dp++ = |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2773 | (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2774 | |
| 2775 | sum += (v < 128) ? v : 256 - v; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2776 | |
| 2777 | if (sum > lmins) /* We are already worse, don't continue. */ |
| 2778 | break; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2779 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2780 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2781 | #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2782 | if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
| 2783 | { |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 2784 | int j; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2785 | png_uint_32 sumhi, sumlo; |
| 2786 | sumlo = sum & PNG_LOMASK; |
| 2787 | sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; |
| 2788 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2789 | for (j = 0; j < num_p_filters; j++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2790 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2791 | if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2792 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2793 | sumlo = (sumlo * png_ptr->filter_weights[j]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2794 | PNG_WEIGHT_SHIFT; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2795 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2796 | sumhi = (sumhi * png_ptr->filter_weights[j]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2797 | PNG_WEIGHT_SHIFT; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2798 | } |
| 2799 | } |
| 2800 | |
| 2801 | sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2802 | PNG_COST_SHIFT; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2803 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2804 | sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2805 | PNG_COST_SHIFT; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2806 | |
| 2807 | if (sumhi > PNG_HIMASK) |
| 2808 | sum = PNG_MAXSUM; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2809 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2810 | else |
| 2811 | sum = (sumhi << PNG_HISHIFT) + sumlo; |
| 2812 | } |
| 2813 | #endif |
| 2814 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2815 | if (sum < mins) |
| 2816 | { |
| 2817 | mins = sum; |
| 2818 | best_row = png_ptr->avg_row; |
| 2819 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2820 | } |
| 2821 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2822 | /* Paeth filter */ |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2823 | if (filter_to_do == PNG_FILTER_PAETH) |
| 2824 | { |
| 2825 | png_bytep rp, dp, pp, cp, lp; |
Glenn Randers-Pehrson | bcb3aac | 2010-09-10 22:05:27 -0500 | [diff] [blame] | 2826 | png_size_t i; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2827 | |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2828 | for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2829 | pp = prev_row + 1; i < bpp; i++) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2830 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2831 | *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2832 | } |
| 2833 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2834 | for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++) |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2835 | { |
| 2836 | int a, b, c, pa, pb, pc, p; |
| 2837 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2838 | b = *pp++; |
| 2839 | c = *cp++; |
| 2840 | a = *lp++; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2841 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2842 | p = b - c; |
| 2843 | pc = a - c; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2844 | |
| 2845 | #ifdef PNG_USE_ABS |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2846 | pa = abs(p); |
| 2847 | pb = abs(pc); |
| 2848 | pc = abs(p + pc); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2849 | #else |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2850 | pa = p < 0 ? -p : p; |
| 2851 | pb = pc < 0 ? -pc : pc; |
| 2852 | pc = (p + pc) < 0 ? -(p + pc) : p + pc; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2853 | #endif |
| 2854 | |
| 2855 | p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; |
| 2856 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2857 | *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2858 | } |
| 2859 | best_row = png_ptr->paeth_row; |
| 2860 | } |
| 2861 | |
| 2862 | else if (filter_to_do & PNG_FILTER_PAETH) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2863 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2864 | png_bytep rp, dp, pp, cp, lp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2865 | png_uint_32 sum = 0, lmins = mins; |
Glenn Randers-Pehrson | bcb3aac | 2010-09-10 22:05:27 -0500 | [diff] [blame] | 2866 | png_size_t i; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2867 | int v; |
| 2868 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2869 | #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2870 | if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
| 2871 | { |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 2872 | int j; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2873 | png_uint_32 lmhi, lmlo; |
| 2874 | lmlo = lmins & PNG_LOMASK; |
| 2875 | lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; |
| 2876 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2877 | for (j = 0; j < num_p_filters; j++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2878 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2879 | if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2880 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2881 | lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2882 | PNG_WEIGHT_SHIFT; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2883 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2884 | lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2885 | PNG_WEIGHT_SHIFT; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2886 | } |
| 2887 | } |
| 2888 | |
| 2889 | lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2890 | PNG_COST_SHIFT; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2891 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2892 | lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2893 | PNG_COST_SHIFT; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2894 | |
| 2895 | if (lmhi > PNG_HIMASK) |
| 2896 | lmins = PNG_MAXSUM; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2897 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2898 | else |
| 2899 | lmins = (lmhi << PNG_HISHIFT) + lmlo; |
| 2900 | } |
| 2901 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2902 | |
| 2903 | for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1, |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2904 | pp = prev_row + 1; i < bpp; i++) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2905 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2906 | v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2907 | |
| 2908 | sum += (v < 128) ? v : 256 - v; |
| 2909 | } |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2910 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2911 | for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2912 | { |
| 2913 | int a, b, c, pa, pb, pc, p; |
| 2914 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2915 | b = *pp++; |
| 2916 | c = *cp++; |
| 2917 | a = *lp++; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2918 | |
| 2919 | #ifndef PNG_SLOW_PAETH |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2920 | p = b - c; |
| 2921 | pc = a - c; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2922 | #ifdef PNG_USE_ABS |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2923 | pa = abs(p); |
| 2924 | pb = abs(pc); |
| 2925 | pc = abs(p + pc); |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2926 | #else |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2927 | pa = p < 0 ? -p : p; |
| 2928 | pb = pc < 0 ? -pc : pc; |
| 2929 | pc = (p + pc) < 0 ? -(p + pc) : p + pc; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2930 | #endif |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2931 | p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; |
| 2932 | #else /* PNG_SLOW_PAETH */ |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2933 | p = a + b - c; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2934 | pa = abs(p - a); |
| 2935 | pb = abs(p - b); |
| 2936 | pc = abs(p - c); |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 2937 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2938 | if (pa <= pb && pa <= pc) |
| 2939 | p = a; |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 2940 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2941 | else if (pb <= pc) |
| 2942 | p = b; |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 2943 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2944 | else |
| 2945 | p = c; |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2946 | #endif /* PNG_SLOW_PAETH */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2947 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2948 | v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2949 | |
| 2950 | sum += (v < 128) ? v : 256 - v; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2951 | |
| 2952 | if (sum > lmins) /* We are already worse, don't continue. */ |
| 2953 | break; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2954 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2955 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 2956 | #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2957 | if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
| 2958 | { |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 2959 | int j; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2960 | png_uint_32 sumhi, sumlo; |
| 2961 | sumlo = sum & PNG_LOMASK; |
| 2962 | sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; |
| 2963 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2964 | for (j = 0; j < num_p_filters; j++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2965 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2966 | if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2967 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2968 | sumlo = (sumlo * png_ptr->filter_weights[j]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2969 | PNG_WEIGHT_SHIFT; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2970 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 2971 | sumhi = (sumhi * png_ptr->filter_weights[j]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2972 | PNG_WEIGHT_SHIFT; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2973 | } |
| 2974 | } |
| 2975 | |
| 2976 | sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2977 | PNG_COST_SHIFT; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2978 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2979 | sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >> |
Glenn Randers-Pehrson | 16908a1 | 2010-03-06 07:34:28 -0600 | [diff] [blame] | 2980 | PNG_COST_SHIFT; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2981 | |
| 2982 | if (sumhi > PNG_HIMASK) |
| 2983 | sum = PNG_MAXSUM; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 2984 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2985 | else |
| 2986 | sum = (sumhi << PNG_HISHIFT) + sumlo; |
| 2987 | } |
| 2988 | #endif |
| 2989 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2990 | if (sum < mins) |
| 2991 | { |
| 2992 | best_row = png_ptr->paeth_row; |
| 2993 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 2994 | } |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 2995 | #endif /* PNG_WRITE_FILTER_SUPPORTED */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2996 | /* Do the actual writing of the filtered row data from the chosen filter. */ |
Glenn Randers-Pehrson | 896239b | 1998-04-21 15:03:57 -0500 | [diff] [blame] | 2997 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 2998 | png_write_filtered_row(png_ptr, best_row); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 2999 | |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 3000 | #ifdef PNG_WRITE_FILTER_SUPPORTED |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 3001 | #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3002 | /* Save the type of filter we picked this time for future calculations */ |
| 3003 | if (png_ptr->num_prev_filters > 0) |
| 3004 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3005 | int j; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 3006 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3007 | for (j = 1; j < num_p_filters; j++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3008 | { |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3009 | png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3010 | } |
Glenn Randers-Pehrson | ccadcae | 2010-10-23 17:29:13 -0500 | [diff] [blame] | 3011 | |
Glenn Randers-Pehrson | 1d96361 | 1998-05-02 12:52:25 -0500 | [diff] [blame] | 3012 | png_ptr->prev_filters[j] = best_row[0]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3013 | } |
| 3014 | #endif |
Glenn Randers-Pehrson | dbd4014 | 2009-08-31 08:42:02 -0500 | [diff] [blame] | 3015 | #endif /* PNG_WRITE_FILTER_SUPPORTED */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3016 | } |
| 3017 | |
| 3018 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3019 | /* Do the actual writing of a previously filtered row. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 3020 | void /* PRIVATE */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3021 | png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row) |
| 3022 | { |
Glenn Randers-Pehrson | bcb3aac | 2010-09-10 22:05:27 -0500 | [diff] [blame] | 3023 | png_size_t avail; |
| 3024 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 3025 | png_debug(1, "in png_write_filtered_row"); |
Glenn Randers-Pehrson | 3358a98 | 2009-08-13 18:04:26 -0500 | [diff] [blame] | 3026 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 3027 | png_debug1(2, "filter = %d", filtered_row[0]); |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 3028 | /* Set up the zlib input buffer */ |
Glenn Randers-Pehrson | 5e5c1e1 | 2000-11-10 12:26:19 -0600 | [diff] [blame] | 3029 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3030 | png_ptr->zstream.next_in = filtered_row; |
Glenn Randers-Pehrson | bcb3aac | 2010-09-10 22:05:27 -0500 | [diff] [blame] | 3031 | png_ptr->zstream.avail_in = 0; |
| 3032 | avail = png_ptr->row_info.rowbytes + 1; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 3033 | /* Repeat until we have compressed all the data */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3034 | do |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 3035 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 3036 | int ret; /* Return of zlib */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3037 | |
Glenn Randers-Pehrson | bcb3aac | 2010-09-10 22:05:27 -0500 | [diff] [blame] | 3038 | /* Record the number of bytes available - zlib supports at least 65535 |
| 3039 | * bytes at one step, depending on the size of the zlib type 'uInt', the |
| 3040 | * maximum size zlib can write at once is ZLIB_IO_MAX (from pngpriv.h). |
| 3041 | * Use this because on 16 bit systems 'rowbytes' can be up to 65536 (i.e. |
| 3042 | * one more than 16 bits) and, in this case 'rowbytes+1' can overflow a |
| 3043 | * uInt. ZLIB_IO_MAX can be safely reduced to cause zlib to be called |
| 3044 | * with smaller chunks of data. |
| 3045 | */ |
| 3046 | if (png_ptr->zstream.avail_in == 0) |
| 3047 | { |
| 3048 | if (avail > ZLIB_IO_MAX) |
| 3049 | { |
| 3050 | png_ptr->zstream.avail_in = ZLIB_IO_MAX; |
| 3051 | avail -= ZLIB_IO_MAX; |
| 3052 | } |
| 3053 | |
| 3054 | else |
| 3055 | { |
| 3056 | /* So this will fit in the available uInt space: */ |
| 3057 | png_ptr->zstream.avail_in = (uInt)avail; |
| 3058 | avail = 0; |
| 3059 | } |
| 3060 | } |
| 3061 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 3062 | /* Compress the data */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3063 | ret = deflate(&png_ptr->zstream, Z_NO_FLUSH); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 3064 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 3065 | /* Check for compression errors */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3066 | if (ret != Z_OK) |
| 3067 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3068 | if (png_ptr->zstream.msg != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3069 | png_error(png_ptr, png_ptr->zstream.msg); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 3070 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3071 | else |
| 3072 | png_error(png_ptr, "zlib error"); |
| 3073 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3074 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 3075 | /* See if it is time to write another IDAT */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3076 | if (!(png_ptr->zstream.avail_out)) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3077 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 3078 | /* Write the IDAT and reset the zlib output buffer */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3079 | png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 3080 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 3081 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3082 | } |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 3083 | /* Repeat until all data has been compressed */ |
Glenn Randers-Pehrson | bcb3aac | 2010-09-10 22:05:27 -0500 | [diff] [blame] | 3084 | } while (avail > 0 || png_ptr->zstream.avail_in > 0); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3085 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 3086 | /* Swap the current and previous rows */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 3087 | if (png_ptr->prev_row != NULL) |
Guy Schalnat | c21f90c | 1996-06-17 16:24:45 -0500 | [diff] [blame] | 3088 | { |
| 3089 | png_bytep tptr; |
| 3090 | |
| 3091 | tptr = png_ptr->prev_row; |
| 3092 | png_ptr->prev_row = png_ptr->row_buf; |
| 3093 | png_ptr->row_buf = tptr; |
| 3094 | } |
| 3095 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 3096 | /* Finish row - updates counters and flushes zlib if last row */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3097 | png_write_finish_row(png_ptr); |
| 3098 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 3099 | #ifdef PNG_WRITE_FLUSH_SUPPORTED |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3100 | png_ptr->flush_rows++; |
| 3101 | |
| 3102 | if (png_ptr->flush_dist > 0 && |
| 3103 | png_ptr->flush_rows >= png_ptr->flush_dist) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3104 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 3105 | png_write_flush(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3106 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 3107 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3108 | } |
Glenn Randers-Pehrson | 3097f61 | 2001-05-07 14:52:45 -0500 | [diff] [blame] | 3109 | #endif /* PNG_WRITE_SUPPORTED */ |