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