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