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