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