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