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