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