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 |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 4 | libpng 1.0 beta 6 - version 0.96 |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 5 | For conditions of distribution and use, see copyright notice in png.h |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 6 | Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 7 | Copyright (c) 1996, 1997 Andreas Dilger |
| 8 | May 12, 1997 |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 9 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 10 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 11 | #define PNG_INTERNAL |
| 12 | #include "png.h" |
| 13 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 14 | /* Place a 32-bit number into a buffer in PNG byte order. We work |
| 15 | * with unsigned numbers for convenience, although one supported |
| 16 | * ancillary chunk uses signed (two's complement) numbers. |
| 17 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 18 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 19 | png_save_uint_32(png_bytep buf, png_uint_32 i) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 20 | { |
| 21 | buf[0] = (png_byte)((i >> 24) & 0xff); |
| 22 | buf[1] = (png_byte)((i >> 16) & 0xff); |
| 23 | buf[2] = (png_byte)((i >> 8) & 0xff); |
| 24 | buf[3] = (png_byte)(i & 0xff); |
| 25 | } |
| 26 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 27 | #if defined(PNG_WRITE_pCAL_SUPPORTED) |
| 28 | /* The png_save_int_32 function assumes integers are stored in two's |
| 29 | complement format. If this isn't the case, then this routine needs to |
| 30 | be modified to write data in two's complement format. */ |
| 31 | void |
| 32 | png_save_int_32(png_bytep buf, png_int_32 i) |
| 33 | { |
| 34 | buf[0] = (png_byte)((i >> 24) & 0xff); |
| 35 | buf[1] = (png_byte)((i >> 16) & 0xff); |
| 36 | buf[2] = (png_byte)((i >> 8) & 0xff); |
| 37 | buf[3] = (png_byte)(i & 0xff); |
| 38 | } |
| 39 | #endif |
| 40 | |
| 41 | /* Place a 16-bit number into a buffer in PNG byte order. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 42 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 43 | png_save_uint_16(png_bytep buf, png_uint_16 i) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 44 | { |
| 45 | buf[0] = (png_byte)((i >> 8) & 0xff); |
| 46 | buf[1] = (png_byte)(i & 0xff); |
| 47 | } |
| 48 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 49 | /* Write a PNG chunk all at once. The type is an array of ASCII characters |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 50 | representing the chunk name. The array must be at least 4 bytes in |
| 51 | length, and does not need to be null terminated. To be safe, pass the |
| 52 | pre-defined chunk names here, and if you need a new one, define it |
| 53 | where the others are defined. The length is the length of the data. |
| 54 | All the data must be present. If that is not possible, use the |
| 55 | png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end() |
| 56 | functions instead. */ |
| 57 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 58 | png_write_chunk(png_structp png_ptr, png_bytep chunk_name, |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 59 | png_bytep data, png_size_t length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 60 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 61 | png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 62 | png_write_chunk_data(png_ptr, data, length); |
| 63 | png_write_chunk_end(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 64 | } |
| 65 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 66 | /* Write the start of a PNG chunk. The type is the chunk type. |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 67 | The total_length is the sum of the lengths of all the data you will be |
| 68 | passing in png_write_chunk_data() */ |
| 69 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 70 | png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name, |
| 71 | png_uint_32 length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 72 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 73 | png_byte buf[4]; |
| 74 | png_debug2(0, "Writing %s chunk (%d bytes)\n", chunk_name, length); |
| 75 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 76 | /* write the length */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 77 | png_save_uint_32(buf, length); |
| 78 | png_write_data(png_ptr, buf, (png_size_t)4); |
| 79 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 80 | /* write the chunk name */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 81 | png_write_data(png_ptr, chunk_name, (png_size_t)4); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 82 | /* reset the crc and run it over the chunk name */ |
| 83 | png_reset_crc(png_ptr); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 84 | png_calculate_crc(png_ptr, chunk_name, (png_size_t)4); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 85 | } |
| 86 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 87 | /* Write the data of a PNG chunk started with png_write_chunk_start(). |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 88 | Note that multiple calls to this function are allowed, and that the |
| 89 | sum of the lengths from these calls *must* add up to the total_length |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 90 | given to png_write_chunk_start(). */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 91 | void |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 92 | 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] | 93 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 94 | /* write the data, and run the CRC over it */ |
| 95 | if (data != NULL && length > 0) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 96 | { |
| 97 | png_calculate_crc(png_ptr, data, length); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 98 | png_write_data(png_ptr, data, length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 102 | /* Finish a chunk started with png_write_chunk_start(). */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 103 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 104 | png_write_chunk_end(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 105 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 106 | png_byte buf[4]; |
| 107 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 108 | /* write the crc */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 109 | #ifdef PNG_USE_OWN_CRC |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 110 | png_save_uint_32(buf, ~png_ptr->crc); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 111 | #else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 112 | png_save_uint_32(buf, png_ptr->crc); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 113 | #endif |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 114 | |
| 115 | png_write_data(png_ptr, buf, (png_size_t)4); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 118 | /* Simple function to write the signature. If we have already written |
| 119 | * the magic bytes of the signature, or more likely, the PNG stream is |
| 120 | * being embedded into another stream and doesn't need its own signature, |
| 121 | * we should call png_set_sig_bytes() to tell libpng how many of the |
| 122 | * bytes have already been written. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 123 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 124 | png_write_sig(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 125 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 126 | /* write the rest of the 8 byte signature */ |
| 127 | png_write_data(png_ptr, &png_sig[png_ptr->sig_bytes], |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 128 | (png_size_t)8 - png_ptr->sig_bytes); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* Write the IHDR chunk, and update the png_struct with the necessary |
| 132 | information. Note that the rest of this code depends upon this |
| 133 | information being correct. */ |
| 134 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 135 | 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] | 136 | int bit_depth, int color_type, int compression_type, int filter_type, |
| 137 | int interlace_type) |
| 138 | { |
| 139 | png_byte buf[13]; /* buffer to store the IHDR info */ |
| 140 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 141 | png_debug(1, "in png_write_IHDR\n"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 142 | /* Check that we have valid input data from the application info */ |
| 143 | switch (color_type) |
| 144 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 145 | case PNG_COLOR_TYPE_GRAY: |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 146 | switch (bit_depth) |
| 147 | { |
| 148 | case 1: |
| 149 | case 2: |
| 150 | case 4: |
| 151 | case 8: |
| 152 | case 16: png_ptr->channels = 1; break; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 153 | default: png_error(png_ptr,"Invalid bit depth for grayscale image"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 154 | } |
| 155 | break; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 156 | case PNG_COLOR_TYPE_RGB: |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 157 | if (bit_depth != 8 && bit_depth != 16) |
| 158 | png_error(png_ptr, "Invalid bit depth for RGB image"); |
| 159 | png_ptr->channels = 3; |
| 160 | break; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 161 | case PNG_COLOR_TYPE_PALETTE: |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 162 | switch (bit_depth) |
| 163 | { |
| 164 | case 1: |
| 165 | case 2: |
| 166 | case 4: |
| 167 | case 8: png_ptr->channels = 1; break; |
| 168 | default: png_error(png_ptr, "Invalid bit depth for paletted image"); |
| 169 | } |
| 170 | break; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 171 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 172 | if (bit_depth != 8 && bit_depth != 16) |
| 173 | png_error(png_ptr, "Invalid bit depth for grayscale+alpha image"); |
| 174 | png_ptr->channels = 2; |
| 175 | break; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 176 | case PNG_COLOR_TYPE_RGB_ALPHA: |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 177 | if (bit_depth != 8 && bit_depth != 16) |
| 178 | png_error(png_ptr, "Invalid bit depth for RGBA image"); |
| 179 | png_ptr->channels = 4; |
| 180 | break; |
| 181 | default: |
| 182 | png_error(png_ptr, "Invalid image color type specified"); |
| 183 | } |
| 184 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 185 | if (compression_type != PNG_COMPRESSION_TYPE_BASE) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 186 | { |
| 187 | png_warning(png_ptr, "Invalid compression type specified"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 188 | compression_type = PNG_COMPRESSION_TYPE_BASE; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 191 | if (filter_type != PNG_FILTER_TYPE_BASE) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 192 | { |
| 193 | png_warning(png_ptr, "Invalid filter type specified"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 194 | filter_type = PNG_FILTER_TYPE_BASE; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 195 | } |
| 196 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 197 | if (interlace_type != PNG_INTERLACE_NONE && |
| 198 | interlace_type != PNG_INTERLACE_ADAM7) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 199 | { |
| 200 | png_warning(png_ptr, "Invalid interlace type specified"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 201 | interlace_type = PNG_INTERLACE_ADAM7; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /* save off the relevent information */ |
| 205 | png_ptr->bit_depth = (png_byte)bit_depth; |
| 206 | png_ptr->color_type = (png_byte)color_type; |
| 207 | png_ptr->interlaced = (png_byte)interlace_type; |
| 208 | png_ptr->width = width; |
| 209 | png_ptr->height = height; |
| 210 | |
| 211 | png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 212 | png_ptr->rowbytes = ((width * (png_size_t)png_ptr->pixel_depth + 7) >> 3); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 213 | /* set the usr info, so any transformations can modify it */ |
| 214 | png_ptr->usr_width = png_ptr->width; |
| 215 | png_ptr->usr_bit_depth = png_ptr->bit_depth; |
| 216 | png_ptr->usr_channels = png_ptr->channels; |
| 217 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 218 | /* pack the header information into the buffer */ |
| 219 | png_save_uint_32(buf, width); |
| 220 | png_save_uint_32(buf + 4, height); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 221 | buf[8] = (png_byte)bit_depth; |
| 222 | buf[9] = (png_byte)color_type; |
| 223 | buf[10] = (png_byte)compression_type; |
| 224 | buf[11] = (png_byte)filter_type; |
| 225 | buf[12] = (png_byte)interlace_type; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 226 | |
| 227 | /* write the chunk */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 228 | png_write_chunk(png_ptr, png_IHDR, buf, (png_size_t)13); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 229 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 230 | /* initialize zlib with PNG info */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 231 | png_ptr->zstream.zalloc = png_zalloc; |
| 232 | png_ptr->zstream.zfree = png_zfree; |
| 233 | png_ptr->zstream.opaque = (voidpf)png_ptr; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 234 | if (!(png_ptr->do_filter)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 235 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 236 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE || |
| 237 | png_ptr->bit_depth < 8) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 238 | png_ptr->do_filter = PNG_FILTER_NONE; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 239 | else |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 240 | png_ptr->do_filter = PNG_ALL_FILTERS; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 241 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 242 | if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 243 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 244 | if (png_ptr->do_filter != PNG_FILTER_NONE) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 245 | png_ptr->zlib_strategy = Z_FILTERED; |
| 246 | else |
| 247 | png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY; |
| 248 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 249 | if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 250 | png_ptr->zlib_level = Z_DEFAULT_COMPRESSION; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 251 | if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 252 | png_ptr->zlib_mem_level = 8; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 253 | if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 254 | png_ptr->zlib_window_bits = 15; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 255 | if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD)) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 256 | png_ptr->zlib_method = 8; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 257 | deflateInit2(&png_ptr->zstream, png_ptr->zlib_level, |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 258 | png_ptr->zlib_method, png_ptr->zlib_window_bits, |
| 259 | png_ptr->zlib_mem_level, png_ptr->zlib_strategy); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 260 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 261 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 262 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 263 | png_ptr->mode = PNG_HAVE_IHDR; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /* write the palette. We are careful not to trust png_color to be in the |
| 267 | correct order for PNG, so people can redefine it to any convient |
| 268 | structure. */ |
| 269 | void |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 270 | 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] | 271 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 272 | png_uint_32 i; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 273 | png_colorp pal_ptr; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 274 | png_byte buf[3]; |
| 275 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 276 | png_debug(1, "in png_write_PLTE\n"); |
| 277 | if (num_pal == 0 || num_pal > 256) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 278 | { |
| 279 | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 280 | { |
| 281 | png_error(png_ptr, "Invalid number of colors in palette"); |
| 282 | } |
| 283 | else |
| 284 | { |
| 285 | png_warning(png_ptr, "Invalid number of colors in palette"); |
| 286 | return; |
| 287 | } |
| 288 | } |
| 289 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 290 | png_ptr->num_palette = (png_uint_16)num_pal; |
| 291 | png_debug1(3, "num_palette = %d\n", png_ptr->num_palette); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 292 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 293 | png_write_chunk_start(png_ptr, png_PLTE, num_pal * 3); |
| 294 | for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 295 | { |
| 296 | buf[0] = pal_ptr->red; |
| 297 | buf[1] = pal_ptr->green; |
| 298 | buf[2] = pal_ptr->blue; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 299 | png_write_chunk_data(png_ptr, buf, (png_size_t)3); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 300 | } |
| 301 | png_write_chunk_end(png_ptr); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 302 | png_ptr->mode |= PNG_HAVE_PLTE; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /* write an IDAT chunk */ |
| 306 | void |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 307 | 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] | 308 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 309 | png_debug(1, "in png_write_IDAT\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 310 | png_write_chunk(png_ptr, png_IDAT, data, length); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 311 | png_ptr->mode |= PNG_HAVE_IDAT; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /* write an IEND chunk */ |
| 315 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 316 | png_write_IEND(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 317 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 318 | png_debug(1, "in png_write_IEND\n"); |
| 319 | png_write_chunk(png_ptr, png_IEND, NULL, (png_size_t)0); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 320 | png_ptr->mode |= PNG_HAVE_IEND; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 321 | } |
| 322 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 323 | #if defined(PNG_WRITE_gAMA_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 324 | /* write a gAMA chunk */ |
| 325 | void |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 326 | png_write_gAMA(png_structp png_ptr, double file_gamma) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 327 | { |
| 328 | png_uint_32 igamma; |
| 329 | png_byte buf[4]; |
| 330 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 331 | png_debug(1, "in png_write_gAMA\n"); |
| 332 | /* file_gamma is saved in 1/100,000ths */ |
| 333 | igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 334 | png_save_uint_32(buf, igamma); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 335 | png_write_chunk(png_ptr, png_gAMA, buf, (png_size_t)4); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 336 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 337 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 338 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 339 | #if defined(PNG_WRITE_sBIT_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 340 | /* write the sBIT chunk */ |
| 341 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 342 | 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] | 343 | { |
| 344 | png_byte buf[4]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 345 | png_size_t size; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 346 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 347 | png_debug(1, "in png_write_sBIT\n"); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 348 | /* 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] | 349 | if (color_type & PNG_COLOR_MASK_COLOR) |
| 350 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 351 | int maxbits; |
| 352 | |
| 353 | maxbits = color_type==PNG_COLOR_TYPE_PALETTE ? 8:png_ptr->usr_bit_depth; |
| 354 | if (sbit->red == 0 || sbit->red > maxbits || |
| 355 | sbit->green == 0 || sbit->green > maxbits || |
| 356 | sbit->blue == 0 || sbit->blue > maxbits) |
| 357 | { |
| 358 | png_warning(png_ptr, "Invalid sBIT depth specified"); |
| 359 | return; |
| 360 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 361 | buf[0] = sbit->red; |
| 362 | buf[1] = sbit->green; |
| 363 | buf[2] = sbit->blue; |
| 364 | size = 3; |
| 365 | } |
| 366 | else |
| 367 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 368 | if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth) |
| 369 | { |
| 370 | png_warning(png_ptr, "Invalid sBIT depth specified"); |
| 371 | return; |
| 372 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 373 | buf[0] = sbit->gray; |
| 374 | size = 1; |
| 375 | } |
| 376 | |
| 377 | if (color_type & PNG_COLOR_MASK_ALPHA) |
| 378 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 379 | if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth) |
| 380 | { |
| 381 | png_warning(png_ptr, "Invalid sBIT depth specified"); |
| 382 | return; |
| 383 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 384 | buf[size++] = sbit->alpha; |
| 385 | } |
| 386 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 387 | png_write_chunk(png_ptr, png_sBIT, buf, size); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 388 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 389 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 390 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 391 | #if defined(PNG_WRITE_cHRM_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 392 | /* write the cHRM chunk */ |
| 393 | void |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 394 | 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] | 395 | double red_x, double red_y, double green_x, double green_y, |
| 396 | double blue_x, double blue_y) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 397 | { |
| 398 | png_uint_32 itemp; |
| 399 | png_byte buf[32]; |
| 400 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 401 | png_debug(1, "in png_write_cHRM\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 402 | /* each value is saved int 1/100,000ths */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 403 | if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 || |
| 404 | white_x + white_y > 1.0) |
| 405 | { |
| 406 | png_warning(png_ptr, "Invalid cHRM white point specified"); |
| 407 | return; |
| 408 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 409 | itemp = (png_uint_32)(white_x * 100000.0 + 0.5); |
| 410 | png_save_uint_32(buf, itemp); |
| 411 | itemp = (png_uint_32)(white_y * 100000.0 + 0.5); |
| 412 | png_save_uint_32(buf + 4, itemp); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 413 | |
| 414 | if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 || |
| 415 | red_x + red_y > 1.0) |
| 416 | { |
| 417 | png_warning(png_ptr, "Invalid cHRM red point specified"); |
| 418 | return; |
| 419 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 420 | itemp = (png_uint_32)(red_x * 100000.0 + 0.5); |
| 421 | png_save_uint_32(buf + 8, itemp); |
| 422 | itemp = (png_uint_32)(red_y * 100000.0 + 0.5); |
| 423 | png_save_uint_32(buf + 12, itemp); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 424 | |
| 425 | if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 || |
| 426 | green_x + green_y > 1.0) |
| 427 | { |
| 428 | png_warning(png_ptr, "Invalid cHRM green point specified"); |
| 429 | return; |
| 430 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 431 | itemp = (png_uint_32)(green_x * 100000.0 + 0.5); |
| 432 | png_save_uint_32(buf + 16, itemp); |
| 433 | itemp = (png_uint_32)(green_y * 100000.0 + 0.5); |
| 434 | png_save_uint_32(buf + 20, itemp); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 435 | |
| 436 | if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 || |
| 437 | blue_x + blue_y > 1.0) |
| 438 | { |
| 439 | png_warning(png_ptr, "Invalid cHRM blue point specified"); |
| 440 | return; |
| 441 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 442 | itemp = (png_uint_32)(blue_x * 100000.0 + 0.5); |
| 443 | png_save_uint_32(buf + 24, itemp); |
| 444 | itemp = (png_uint_32)(blue_y * 100000.0 + 0.5); |
| 445 | png_save_uint_32(buf + 28, itemp); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 446 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 447 | png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 448 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 449 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 450 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 451 | #if defined(PNG_WRITE_tRNS_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 452 | /* write the tRNS chunk */ |
| 453 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 454 | 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] | 455 | int num_trans, int color_type) |
| 456 | { |
| 457 | png_byte buf[6]; |
| 458 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 459 | png_debug(1, "in png_write_tRNS\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 460 | if (color_type == PNG_COLOR_TYPE_PALETTE) |
| 461 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 462 | if (num_trans <= 0 || num_trans > png_ptr->num_palette) |
| 463 | { |
| 464 | png_warning(png_ptr,"Invalid number of transparent colors specified"); |
| 465 | return; |
| 466 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 467 | /* write the chunk out as it is */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 468 | png_write_chunk(png_ptr, png_tRNS, trans, (png_size_t)num_trans); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 469 | } |
| 470 | else if (color_type == PNG_COLOR_TYPE_GRAY) |
| 471 | { |
| 472 | /* one 16 bit value */ |
| 473 | png_save_uint_16(buf, tran->gray); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 474 | png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)2); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 475 | } |
| 476 | else if (color_type == PNG_COLOR_TYPE_RGB) |
| 477 | { |
| 478 | /* three 16 bit values */ |
| 479 | png_save_uint_16(buf, tran->red); |
| 480 | png_save_uint_16(buf + 2, tran->green); |
| 481 | png_save_uint_16(buf + 4, tran->blue); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 482 | png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)6); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 483 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 484 | else |
| 485 | { |
| 486 | png_warning(png_ptr, "Can't write tRNS with and alpha channel"); |
| 487 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 488 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 489 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 490 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 491 | #if defined(PNG_WRITE_bKGD_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 492 | /* write the background chunk */ |
| 493 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 494 | 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] | 495 | { |
| 496 | png_byte buf[6]; |
| 497 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 498 | png_debug(1, "in png_write_bKGD\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 499 | if (color_type == PNG_COLOR_TYPE_PALETTE) |
| 500 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 501 | if (back->index > png_ptr->num_palette) |
| 502 | { |
| 503 | png_warning(png_ptr, "Invalid background palette index"); |
| 504 | return; |
| 505 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 506 | buf[0] = back->index; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 507 | png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)1); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 508 | } |
| 509 | else if (color_type & PNG_COLOR_MASK_COLOR) |
| 510 | { |
| 511 | png_save_uint_16(buf, back->red); |
| 512 | png_save_uint_16(buf + 2, back->green); |
| 513 | png_save_uint_16(buf + 4, back->blue); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 514 | png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)6); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 515 | } |
| 516 | else |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 517 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 518 | png_save_uint_16(buf, back->gray); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 519 | png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)2); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 520 | } |
| 521 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 522 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 523 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 524 | #if defined(PNG_WRITE_hIST_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 525 | /* write the histogram */ |
| 526 | void |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 527 | png_write_hIST(png_structp png_ptr, png_uint_16p hist, png_uint_32 num_hist) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 528 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 529 | png_uint_32 i; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 530 | png_byte buf[3]; |
| 531 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 532 | png_debug(1, "in png_write_hIST\n"); |
| 533 | if (num_hist > png_ptr->num_palette) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 534 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 535 | png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist, |
| 536 | png_ptr->num_palette); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 537 | png_warning(png_ptr, "Invalid number of histogram entries specified"); |
| 538 | return; |
| 539 | } |
| 540 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 541 | png_write_chunk_start(png_ptr, png_hIST, num_hist * 2); |
| 542 | for (i = 0; i < num_hist; i++) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 543 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 544 | png_save_uint_16(buf, hist[i]); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 545 | png_write_chunk_data(png_ptr, buf, (png_size_t)2); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 546 | } |
| 547 | png_write_chunk_end(png_ptr); |
| 548 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 549 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 550 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 551 | #if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED) |
| 552 | /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification, |
| 553 | * and if invalid, correct the keyword rather than discarding the entire |
| 554 | * chunk. The PNG 1.0 specification requires keywords 1-79 characters in |
| 555 | * length, forbids leading or trailing whitespace, multiple internal spaces, |
| 556 | * 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^] | 557 | * |
| 558 | * The new_key is allocated to hold the corrected keyword and must be freed |
| 559 | * by the calling routine. This avoids problems with trying to write to |
| 560 | * static keywords without having to have duplicate copies of the strings. |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 561 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 562 | png_size_t |
| 563 | png_check_keyword(png_structp png_ptr, png_charp key, png_bytepp new_key) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 564 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 565 | png_size_t key_len; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 566 | png_charp kp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 567 | png_bytep dp; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 568 | int kflag; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 569 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 570 | png_debug(1, "in png_check_keyword\n"); |
| 571 | *new_key = NULL; |
| 572 | |
| 573 | if (key == NULL || (key_len = png_strlen(key)) == 0) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 574 | { |
| 575 | char msg[40]; |
| 576 | |
| 577 | sprintf(msg, "Zero length %s keyword", png_ptr->chunk_name); |
| 578 | png_warning(png_ptr, msg); |
| 579 | return 0; |
| 580 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 581 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 582 | png_debug1(2, "Keyword to be checked is '%s'\n", key); |
| 583 | |
| 584 | *new_key = (png_bytep)png_malloc(png_ptr, key_len + 1); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 585 | |
| 586 | /* Replace non-printing characters with a blank and print a warning */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 587 | for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 588 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 589 | if (*kp < 0x20 || ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1)) |
| 590 | { |
| 591 | char msg[40]; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 592 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 593 | sprintf(msg, "Invalid %s keyword character 0x%02X", |
| 594 | png_ptr->chunk_name, *kp); |
| 595 | png_warning(png_ptr, msg); |
| 596 | *dp = ' '; |
| 597 | } |
| 598 | else |
| 599 | { |
| 600 | *dp = *kp; |
| 601 | } |
| 602 | } |
| 603 | *dp = '\0'; |
| 604 | |
| 605 | /* Remove any trailing white space. */ |
| 606 | kp = *new_key + key_len - 1; |
| 607 | if (*kp == ' ') |
| 608 | { |
| 609 | char msg[50]; |
| 610 | sprintf(msg, "Trailing spaces removed from %s keyword", |
| 611 | png_ptr->chunk_name); |
| 612 | |
| 613 | png_warning(png_ptr, msg); |
| 614 | |
| 615 | while (*kp == ' ') |
| 616 | { |
| 617 | *(kp--) = '\0'; |
| 618 | key_len--; |
| 619 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /* Remove any leading white space. */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 623 | kp = *new_key; |
| 624 | if (*kp == ' ') |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 625 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 626 | char msg[50]; |
| 627 | sprintf(msg, "Leading spaces removed from %s keyword", |
| 628 | png_ptr->chunk_name); |
| 629 | |
| 630 | png_warning(png_ptr, msg); |
| 631 | |
| 632 | while (*kp == ' ') |
| 633 | { |
| 634 | kp++; |
| 635 | key_len--; |
| 636 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 637 | } |
| 638 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 639 | png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 640 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 641 | /* Remove multiple internal spaces. */ |
| 642 | for (kflag = 0, dp = *new_key; *kp != '\0'; kp++) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 643 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 644 | if (*kp == ' ' && kflag == 0) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 645 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 646 | *(dp++) = *kp; |
| 647 | kflag = 1; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 648 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 649 | else if (*kp == ' ') |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 650 | { |
| 651 | key_len--; |
| 652 | } |
| 653 | else |
| 654 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 655 | *(dp++) = *kp; |
| 656 | kflag = 0; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 657 | } |
| 658 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 659 | *dp = '\0'; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 660 | |
| 661 | if (key_len == 0) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 662 | { |
| 663 | char msg[40]; |
| 664 | |
| 665 | sprintf(msg, "Zero length %s keyword", png_ptr->chunk_name); |
| 666 | png_warning(png_ptr, msg); |
| 667 | } |
| 668 | |
| 669 | if (key_len > 79) |
| 670 | { |
| 671 | char msg[50]; |
| 672 | |
| 673 | sprintf(msg, "%s keyword length must be 1 - 79 characters", |
| 674 | png_ptr->chunk_name); |
| 675 | png_warning(png_ptr, msg); |
| 676 | new_key[79] = '\0'; |
| 677 | key_len = 79; |
| 678 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 679 | |
| 680 | return key_len; |
| 681 | } |
| 682 | #endif |
| 683 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 684 | #if defined(PNG_WRITE_tEXt_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 685 | /* write a tEXt chunk */ |
| 686 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 687 | 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^] | 688 | png_size_t text_len) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 689 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 690 | png_size_t key_len; |
| 691 | png_bytep new_key; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 692 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 693 | png_debug(1, "in png_write_tEXt\n"); |
| 694 | if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0) |
| 695 | { |
| 696 | char msg[40]; |
| 697 | |
| 698 | sprintf(msg, "Empty keyword in %s chunk", "tEXt"); |
| 699 | png_warning(png_ptr, msg); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 700 | return; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 701 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 702 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 703 | if (text == NULL || *text == '\0') |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 704 | text_len = 0; |
| 705 | |
| 706 | /* make sure we include the 0 after the key */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 707 | png_write_chunk_start(png_ptr, png_tEXt, (png_uint_32)key_len+text_len+1); |
| 708 | png_write_chunk_data(png_ptr, new_key, key_len + 1); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 709 | if (text_len) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 710 | png_write_chunk_data(png_ptr, (png_bytep)text, text_len); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 711 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 712 | png_write_chunk_end(png_ptr); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 713 | png_free(png_ptr, new_key); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 714 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 715 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 716 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 717 | #if defined(PNG_WRITE_zTXt_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 718 | /* write a compressed text chunk */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 719 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 720 | 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^] | 721 | png_size_t text_len, int compression) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 722 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 723 | png_size_t key_len; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 724 | char buf[1]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 725 | png_bytep new_key; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 726 | int i, ret; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 727 | png_charpp output_ptr = NULL; /* array of pointers to output */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 728 | int num_output_ptr = 0; /* number of output pointers used */ |
| 729 | int max_output_ptr = 0; /* size of output_ptr */ |
| 730 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 731 | png_debug(1, "in png_write_zTXt\n"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 732 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 733 | 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] | 734 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 735 | char msg[40]; |
| 736 | |
| 737 | sprintf(msg, "Empty keyword in %s chunk", "zTXt"); |
| 738 | png_warning(png_ptr, msg); |
| 739 | return; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 740 | } |
| 741 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 742 | if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE) |
| 743 | { |
| 744 | png_write_tEXt(png_ptr, new_key, text, (png_size_t)0); |
| 745 | png_free(png_ptr, new_key); |
| 746 | return; |
| 747 | } |
| 748 | |
| 749 | png_free(png_ptr, new_key); |
| 750 | |
| 751 | if (compression >= PNG_TEXT_COMPRESSION_LAST) |
| 752 | { |
| 753 | char msg[50]; |
| 754 | sprintf(msg, "Unknown zTXt compression type %d", compression); |
| 755 | png_warning(png_ptr, msg); |
| 756 | compression = PNG_TEXT_COMPRESSION_zTXt; |
| 757 | } |
| 758 | |
| 759 | /* We can't write the chunk until we find out how much data we have, |
| 760 | * which means we need to run the compressor first, and save the |
| 761 | * output. This shouldn't be a problem, as the vast majority of |
| 762 | * comments should be reasonable, but we will set up an array of |
| 763 | * malloc'd pointers to be sure. |
| 764 | * |
| 765 | * If we knew the application was well behaved, we could simplify this |
| 766 | * greatly by assuming we can always malloc an output buffer large |
| 767 | * enough to hold the compressed text ((1001 * text_len / 1000) + 12) |
| 768 | * and malloc this directly. The only time this would be a bad idea is |
| 769 | * if we can't malloc more than 64K and we have 64K of random input |
| 770 | * data, or if the input string is incredibly large (although this |
| 771 | * wouldn't cause a failure, just a slowdown due to swapping). |
| 772 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 773 | |
| 774 | /* set up the compression buffers */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 775 | png_ptr->zstream.avail_in = (uInt)text_len; |
| 776 | png_ptr->zstream.next_in = (Bytef *)text; |
| 777 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
| 778 | png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 779 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 780 | /* this is the same compression loop as in png_write_row() */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 781 | do |
| 782 | { |
| 783 | /* compress the data */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 784 | ret = deflate(&png_ptr->zstream, Z_NO_FLUSH); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 785 | if (ret != Z_OK) |
| 786 | { |
| 787 | /* error */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 788 | if (png_ptr->zstream.msg != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 789 | png_error(png_ptr, png_ptr->zstream.msg); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 790 | else |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 791 | png_error(png_ptr, "zlib error"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 792 | } |
| 793 | /* check to see if we need more room */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 794 | if (!png_ptr->zstream.avail_out && png_ptr->zstream.avail_in) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 795 | { |
| 796 | /* make sure the output array has room */ |
| 797 | if (num_output_ptr >= max_output_ptr) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 798 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 799 | int old_max; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 800 | |
| 801 | old_max = max_output_ptr; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 802 | max_output_ptr = num_output_ptr + 4; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 803 | if (output_ptr != NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 804 | { |
| 805 | png_charpp old_ptr; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 806 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 807 | old_ptr = output_ptr; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 808 | output_ptr = (png_charpp)png_malloc(png_ptr, |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 809 | max_output_ptr * sizeof (png_charpp)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 810 | png_memcpy(output_ptr, old_ptr, old_max * sizeof (png_charp)); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 811 | png_free(png_ptr, old_ptr); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 812 | } |
| 813 | else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 814 | output_ptr = (png_charpp)png_malloc(png_ptr, |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 815 | max_output_ptr * sizeof (png_charp)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | /* save the data */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 819 | output_ptr[num_output_ptr] = png_malloc(png_ptr, png_ptr->zbuf_size); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 820 | png_memcpy(output_ptr[num_output_ptr], png_ptr->zbuf, |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 821 | png_ptr->zbuf_size); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 822 | num_output_ptr++; |
| 823 | |
| 824 | /* and reset the buffer */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 825 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
| 826 | png_ptr->zstream.next_out = png_ptr->zbuf; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 827 | } |
| 828 | /* continue until we don't have anymore to compress */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 829 | } while (png_ptr->zstream.avail_in); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 830 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 831 | /* finish the compression */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 832 | do |
| 833 | { |
| 834 | /* tell zlib we are finished */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 835 | ret = deflate(&png_ptr->zstream, Z_FINISH); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 836 | if (ret != Z_OK && ret != Z_STREAM_END) |
| 837 | { |
| 838 | /* we got an error */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 839 | if (png_ptr->zstream.msg != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 840 | png_error(png_ptr, png_ptr->zstream.msg); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 841 | else |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 842 | png_error(png_ptr, "zlib error"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | /* check to see if we need more room */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 846 | if (!(png_ptr->zstream.avail_out) && ret == Z_OK) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 847 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 848 | /* check to make sure our output array has room */ |
| 849 | if (num_output_ptr >= max_output_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 850 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 851 | int old_max; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 852 | |
| 853 | old_max = max_output_ptr; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 854 | max_output_ptr = num_output_ptr + 4; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 855 | if (output_ptr != NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 856 | { |
| 857 | png_charpp old_ptr; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 858 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 859 | old_ptr = output_ptr; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 860 | /* This could be optimized to realloc() */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 861 | output_ptr = (png_charpp)png_malloc(png_ptr, |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 862 | max_output_ptr * sizeof (png_charpp)); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 863 | png_memcpy(output_ptr, old_ptr, old_max * sizeof (png_charp)); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 864 | png_free(png_ptr, old_ptr); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 865 | } |
| 866 | else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 867 | output_ptr = (png_charpp)png_malloc(png_ptr, |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 868 | max_output_ptr * sizeof (png_charp)); |
| 869 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 870 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 871 | /* save off the data */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 872 | output_ptr[num_output_ptr] = png_malloc(png_ptr, |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 873 | png_ptr->zbuf_size); |
| 874 | png_memcpy(output_ptr[num_output_ptr], png_ptr->zbuf, |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 875 | png_ptr->zbuf_size); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 876 | num_output_ptr++; |
| 877 | |
| 878 | /* and reset the buffer pointers */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 879 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
| 880 | png_ptr->zstream.next_out = png_ptr->zbuf; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 881 | } |
| 882 | } while (ret != Z_STREAM_END); |
| 883 | |
| 884 | /* text length is number of buffers plus last buffer */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 885 | text_len = png_ptr->zbuf_size * num_output_ptr; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 886 | if (png_ptr->zstream.avail_out < png_ptr->zbuf_size) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 887 | text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 888 | |
| 889 | /* write start of chunk */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 890 | png_write_chunk_start(png_ptr, png_zTXt, (png_uint_32)(key_len+text_len+2)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 891 | /* write key */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 892 | png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 893 | buf[0] = (png_byte)compression; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 894 | /* write compression */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 895 | png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 896 | |
| 897 | /* write saved output buffers, if any */ |
| 898 | for (i = 0; i < num_output_ptr; i++) |
| 899 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 900 | png_write_chunk_data(png_ptr,(png_bytep)output_ptr[i],png_ptr->zbuf_size); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 901 | png_free(png_ptr, output_ptr[i]); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 902 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 903 | if (max_output_ptr != 0) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 904 | png_free(png_ptr, output_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 905 | /* write anything left in zbuf */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 906 | if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 907 | png_write_chunk_data(png_ptr, png_ptr->zbuf, |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 908 | png_ptr->zbuf_size - png_ptr->zstream.avail_out); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 909 | /* close the chunk */ |
| 910 | png_write_chunk_end(png_ptr); |
| 911 | |
| 912 | /* reset zlib for another zTXt or the image data */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 913 | deflateReset(&png_ptr->zstream); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 914 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 915 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 916 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 917 | |
| 918 | #if defined(PNG_WRITE_oFFs_SUPPORTED) |
| 919 | /* write the oFFs chunk */ |
| 920 | void |
| 921 | png_write_oFFs(png_structp png_ptr, png_uint_32 x_offset, |
| 922 | png_uint_32 y_offset, |
| 923 | int unit_type) |
| 924 | { |
| 925 | png_byte buf[9]; |
| 926 | |
| 927 | png_debug(1, "in png_write_oFFs\n"); |
| 928 | if (unit_type >= PNG_OFFSET_LAST) |
| 929 | png_warning(png_ptr, "Unrecognized unit type for oFFs chunk"); |
| 930 | |
| 931 | png_save_uint_32(buf, x_offset); |
| 932 | png_save_uint_32(buf + 4, y_offset); |
| 933 | buf[8] = (png_byte)unit_type; |
| 934 | |
| 935 | png_write_chunk(png_ptr, png_oFFs, buf, (png_size_t)9); |
| 936 | } |
| 937 | #endif |
| 938 | |
| 939 | #if defined(PNG_WRITE_pCAL_SUPPORTED) |
| 940 | /* write the pCAL chunk (png-scivis-19970203) */ |
| 941 | void |
| 942 | png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0, |
| 943 | png_int_32 X1, int type, int nparams, png_charp units, png_charpp params) |
| 944 | { |
| 945 | png_size_t purpose_len, units_len, total_len; |
| 946 | png_uint_32p params_len; |
| 947 | png_byte buf[10]; |
| 948 | png_bytep new_purpose; |
| 949 | int i; |
| 950 | |
| 951 | png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams); |
| 952 | if (type >= PNG_EQUATION_LAST) |
| 953 | png_warning(png_ptr, "Unrecognized equation type for pCAL chunk"); |
| 954 | |
| 955 | purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1; |
| 956 | png_debug1(3, "pCAL purpose length = %d\n", purpose_len); |
| 957 | units_len = png_strlen(units) + (nparams == 0 ? 0 : 1); |
| 958 | png_debug1(3, "pCAL units length = %d\n", units_len); |
| 959 | total_len = purpose_len + units_len + 10; |
| 960 | |
| 961 | params_len = (png_uint_32p)png_malloc(png_ptr, nparams*sizeof(png_uint_32)); |
| 962 | |
| 963 | /* Find the length of each parameter, making sure we don't count the |
| 964 | null terminator for the last parameter. */ |
| 965 | for (i = 0; i < nparams; i++) |
| 966 | { |
| 967 | params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1); |
| 968 | png_debug2(3, "pCAL parameter %d length = %d\n", i, params_len[i]); |
| 969 | total_len += (png_size_t)params_len[i]; |
| 970 | } |
| 971 | |
| 972 | png_debug1(3, "pCAL total length = %d\n", total_len); |
| 973 | png_write_chunk_start(png_ptr, png_pCAL, total_len); |
| 974 | png_write_chunk_data(png_ptr, new_purpose, purpose_len); |
| 975 | png_save_int_32(buf, X0); |
| 976 | png_save_int_32(buf + 4, X1); |
| 977 | buf[8] = (png_byte)type; |
| 978 | buf[9] = (png_byte)nparams; |
| 979 | png_write_chunk_data(png_ptr, buf, (png_size_t)10); |
| 980 | png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len); |
| 981 | |
| 982 | png_free(png_ptr, new_purpose); |
| 983 | |
| 984 | for (i = 0; i < nparams; i++) |
| 985 | { |
| 986 | png_write_chunk_data(png_ptr, (png_bytep)params[i], |
| 987 | (png_size_t)params_len[i]); |
| 988 | } |
| 989 | |
| 990 | png_write_chunk_end(png_ptr); |
| 991 | } |
| 992 | #endif |
| 993 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 994 | #if defined(PNG_WRITE_pHYs_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 995 | /* write the pHYs chunk */ |
| 996 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 997 | 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] | 998 | png_uint_32 y_pixels_per_unit, |
| 999 | int unit_type) |
| 1000 | { |
| 1001 | png_byte buf[9]; |
| 1002 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1003 | png_debug(1, "in png_write_pHYs\n"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1004 | if (unit_type >= PNG_RESOLUTION_LAST) |
| 1005 | png_warning(png_ptr, "Unrecognized unit type for pHYs chunk"); |
| 1006 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1007 | png_save_uint_32(buf, x_pixels_per_unit); |
| 1008 | png_save_uint_32(buf + 4, y_pixels_per_unit); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1009 | buf[8] = (png_byte)unit_type; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1010 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1011 | png_write_chunk(png_ptr, png_pHYs, buf, (png_size_t)9); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1012 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1013 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1014 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1015 | #if defined(PNG_WRITE_tIME_SUPPORTED) |
| 1016 | /* write the tIME chunk. Use either png_convert_from_struct_tm() |
| 1017 | or png_convert_from_time_t(), or fill in the structure yourself */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1018 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1019 | png_write_tIME(png_structp png_ptr, png_timep mod_time) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1020 | { |
| 1021 | png_byte buf[7]; |
| 1022 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1023 | png_debug(1, "in png_write_tIME\n"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1024 | if (mod_time->month > 12 || mod_time->month < 1 || |
| 1025 | mod_time->day > 31 || mod_time->day < 1 || |
| 1026 | mod_time->hour > 23 || mod_time->second > 60) |
| 1027 | { |
| 1028 | png_warning(png_ptr, "Invalid time specified for tIME chunk"); |
| 1029 | return; |
| 1030 | } |
| 1031 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1032 | png_save_uint_16(buf, mod_time->year); |
| 1033 | buf[2] = mod_time->month; |
| 1034 | buf[3] = mod_time->day; |
| 1035 | buf[4] = mod_time->hour; |
| 1036 | buf[5] = mod_time->minute; |
| 1037 | buf[6] = mod_time->second; |
| 1038 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1039 | png_write_chunk(png_ptr, png_tIME, buf, (png_size_t)7); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1040 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1041 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1042 | |
| 1043 | /* initializes the row writing capability of libpng */ |
| 1044 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1045 | png_write_start_row(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1046 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1047 | png_size_t buf_size; |
| 1048 | |
| 1049 | png_debug(1, "in png_write_start_row\n"); |
| 1050 | buf_size = (png_size_t)(((png_ptr->width * png_ptr->usr_channels * |
| 1051 | png_ptr->usr_bit_depth + 7) >> 3) + 1); |
| 1052 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1053 | /* set up row buffer */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1054 | png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, buf_size); |
| 1055 | png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1056 | |
| 1057 | /* set up filtering buffer, if using this filter */ |
| 1058 | if (png_ptr->do_filter & PNG_FILTER_SUB) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1059 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1060 | png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1061 | png_ptr->rowbytes + 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1062 | png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1063 | } |
| 1064 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1065 | /* 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] | 1066 | if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH)) |
| 1067 | { |
| 1068 | /* set up previous row buffer */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1069 | png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, buf_size); |
| 1070 | png_memset(png_ptr->prev_row, 0, buf_size); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1071 | |
| 1072 | if (png_ptr->do_filter & PNG_FILTER_UP) |
| 1073 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1074 | png_ptr->up_row = (png_bytep )png_malloc(png_ptr, |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1075 | png_ptr->rowbytes + 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1076 | png_ptr->up_row[0] = PNG_FILTER_VALUE_UP; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | if (png_ptr->do_filter & PNG_FILTER_AVG) |
| 1080 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1081 | png_ptr->avg_row = (png_bytep )png_malloc(png_ptr, |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1082 | png_ptr->rowbytes + 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1083 | png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | if (png_ptr->do_filter & PNG_FILTER_PAETH) |
| 1087 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1088 | png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr, |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1089 | png_ptr->rowbytes + 1); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1090 | png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1091 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | /* if interlaced, we need to set up width and height of pass */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1095 | if (png_ptr->interlaced) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1096 | { |
| 1097 | if (!(png_ptr->transformations & PNG_INTERLACE)) |
| 1098 | { |
| 1099 | png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - |
| 1100 | png_pass_ystart[0]) / png_pass_yinc[0]; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1101 | png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 - |
| 1102 | png_pass_start[0]) / png_pass_inc[0]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1103 | } |
| 1104 | else |
| 1105 | { |
| 1106 | png_ptr->num_rows = png_ptr->height; |
| 1107 | png_ptr->usr_width = png_ptr->width; |
| 1108 | } |
| 1109 | } |
| 1110 | else |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1111 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1112 | png_ptr->num_rows = png_ptr->height; |
| 1113 | png_ptr->usr_width = png_ptr->width; |
| 1114 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1115 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
| 1116 | png_ptr->zstream.next_out = png_ptr->zbuf; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1117 | } |
| 1118 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1119 | /* Internal use only. Called when finished processing a row of data. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1120 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1121 | png_write_finish_row(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1122 | { |
| 1123 | int ret; |
| 1124 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1125 | png_debug(1, "in png_write_finish_row\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1126 | /* next row */ |
| 1127 | png_ptr->row_number++; |
Guy Schalnat | c21f90c | 1996-06-17 16:24:45 -0500 | [diff] [blame] | 1128 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1129 | /* see if we are done */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1130 | if (png_ptr->row_number < png_ptr->num_rows) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1131 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1132 | |
| 1133 | /* if interlaced, go to next pass */ |
| 1134 | if (png_ptr->interlaced) |
| 1135 | { |
| 1136 | png_ptr->row_number = 0; |
| 1137 | if (png_ptr->transformations & PNG_INTERLACE) |
| 1138 | { |
| 1139 | png_ptr->pass++; |
| 1140 | } |
| 1141 | else |
| 1142 | { |
| 1143 | /* loop until we find a non-zero width or height pass */ |
| 1144 | do |
| 1145 | { |
| 1146 | png_ptr->pass++; |
| 1147 | if (png_ptr->pass >= 7) |
| 1148 | break; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1149 | png_ptr->usr_width = (png_ptr->width + |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1150 | png_pass_inc[png_ptr->pass] - 1 - |
| 1151 | png_pass_start[png_ptr->pass]) / |
| 1152 | png_pass_inc[png_ptr->pass]; |
| 1153 | png_ptr->num_rows = (png_ptr->height + |
| 1154 | png_pass_yinc[png_ptr->pass] - 1 - |
| 1155 | png_pass_ystart[png_ptr->pass]) / |
| 1156 | png_pass_yinc[png_ptr->pass]; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1157 | if (png_ptr->transformations & PNG_INTERLACE) |
| 1158 | break; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1159 | } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0); |
| 1160 | |
| 1161 | } |
| 1162 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1163 | /* reset the row above the image for the next pass */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1164 | if (png_ptr->pass < 7) |
Guy Schalnat | c21f90c | 1996-06-17 16:24:45 -0500 | [diff] [blame] | 1165 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1166 | if (png_ptr->prev_row != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1167 | png_memset(png_ptr->prev_row, 0, |
| 1168 | (png_size_t) (((png_uint_32)png_ptr->usr_channels * |
Guy Schalnat | c21f90c | 1996-06-17 16:24:45 -0500 | [diff] [blame] | 1169 | (png_uint_32)png_ptr->usr_bit_depth * |
| 1170 | png_ptr->width + 7) >> 3) + 1); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1171 | return; |
Guy Schalnat | c21f90c | 1996-06-17 16:24:45 -0500 | [diff] [blame] | 1172 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | /* if we get here, we've just written the last row, so we need |
| 1176 | to flush the compressor */ |
| 1177 | do |
| 1178 | { |
| 1179 | /* tell the compressor we are done */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1180 | ret = deflate(&png_ptr->zstream, Z_FINISH); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1181 | /* check for an error */ |
| 1182 | if (ret != Z_OK && ret != Z_STREAM_END) |
| 1183 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1184 | if (png_ptr->zstream.msg != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1185 | png_error(png_ptr, png_ptr->zstream.msg); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1186 | else |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1187 | png_error(png_ptr, "zlib error"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1188 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1189 | /* check to see if we need more room */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1190 | if (!(png_ptr->zstream.avail_out) && ret == Z_OK) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1191 | { |
| 1192 | png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1193 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 1194 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1195 | } |
| 1196 | } while (ret != Z_STREAM_END); |
| 1197 | |
| 1198 | /* write any extra space */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1199 | if (png_ptr->zstream.avail_out < png_ptr->zbuf_size) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1200 | { |
| 1201 | png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size - |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1202 | png_ptr->zstream.avail_out); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1203 | } |
| 1204 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1205 | deflateReset(&png_ptr->zstream); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1206 | } |
| 1207 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1208 | #if defined(PNG_WRITE_INTERLACING_SUPPORTED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1209 | /* pick out the correct pixels for the interlace pass. |
| 1210 | |
| 1211 | The basic idea here is to go through the row with a source |
| 1212 | pointer and a destination pointer (sp and dp), and copy the |
| 1213 | correct pixels for the pass. As the row gets compacted, |
| 1214 | sp will always be >= dp, so we should never overwrite anything. |
| 1215 | See the default: case for the easiest code to understand. |
| 1216 | */ |
| 1217 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1218 | 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] | 1219 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1220 | png_debug(1, "in png_do_write_interlace\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1221 | /* we don't have to do anything on the last pass (6) */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1222 | #if defined(PNG_USELESS_TESTS_SUPPORTED) |
| 1223 | if (row != NULL && row_info != NULL && pass < 6) |
| 1224 | #else |
| 1225 | if (pass < 6) |
| 1226 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1227 | { |
| 1228 | /* each pixel depth is handled seperately */ |
| 1229 | switch (row_info->pixel_depth) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1230 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1231 | case 1: |
| 1232 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1233 | png_bytep sp; |
| 1234 | png_bytep dp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1235 | int shift; |
| 1236 | int d; |
| 1237 | int value; |
| 1238 | png_uint_32 i; |
| 1239 | |
| 1240 | dp = row; |
| 1241 | d = 0; |
| 1242 | shift = 7; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1243 | for (i = png_pass_start[pass]; i < row_info->width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1244 | i += png_pass_inc[pass]) |
| 1245 | { |
| 1246 | sp = row + (png_size_t)(i >> 3); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1247 | value = (int)(*sp >> (7 - (int)(i & 7))) & 0x1; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1248 | d |= (value << shift); |
| 1249 | |
| 1250 | if (shift == 0) |
| 1251 | { |
| 1252 | shift = 7; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1253 | *dp++ = (png_byte)d; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1254 | d = 0; |
| 1255 | } |
| 1256 | else |
| 1257 | shift--; |
| 1258 | |
| 1259 | } |
| 1260 | if (shift != 7) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1261 | *dp = (png_byte)d; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1262 | break; |
| 1263 | } |
| 1264 | case 2: |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1265 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1266 | png_bytep sp; |
| 1267 | png_bytep dp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1268 | int shift; |
| 1269 | int d; |
| 1270 | int value; |
| 1271 | png_uint_32 i; |
| 1272 | |
| 1273 | dp = row; |
| 1274 | shift = 6; |
| 1275 | d = 0; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1276 | for (i = png_pass_start[pass]; i < row_info->width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1277 | i += png_pass_inc[pass]) |
| 1278 | { |
| 1279 | sp = row + (png_size_t)(i >> 2); |
| 1280 | value = (*sp >> ((3 - (int)(i & 3)) << 1)) & 0x3; |
| 1281 | d |= (value << shift); |
| 1282 | |
| 1283 | if (shift == 0) |
| 1284 | { |
| 1285 | shift = 6; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1286 | *dp++ = (png_byte)d; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1287 | d = 0; |
| 1288 | } |
| 1289 | else |
| 1290 | shift -= 2; |
| 1291 | } |
| 1292 | if (shift != 6) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1293 | *dp = (png_byte)d; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1294 | break; |
| 1295 | } |
| 1296 | case 4: |
| 1297 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1298 | png_bytep sp; |
| 1299 | png_bytep dp; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1300 | int shift; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1301 | int d; |
| 1302 | int value; |
| 1303 | png_uint_32 i; |
| 1304 | |
| 1305 | dp = row; |
| 1306 | shift = 4; |
| 1307 | d = 0; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1308 | for (i = png_pass_start[pass]; i < row_info->width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1309 | i += png_pass_inc[pass]) |
| 1310 | { |
| 1311 | sp = row + (png_size_t)(i >> 1); |
| 1312 | value = (*sp >> ((1 - (int)(i & 1)) << 2)) & 0xf; |
| 1313 | d |= (value << shift); |
| 1314 | |
| 1315 | if (shift == 0) |
| 1316 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1317 | shift = 4; |
| 1318 | *dp++ = (png_byte)d; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1319 | d = 0; |
| 1320 | } |
| 1321 | else |
| 1322 | shift -= 4; |
| 1323 | } |
| 1324 | if (shift != 4) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1325 | *dp = (png_byte)d; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1326 | break; |
| 1327 | } |
| 1328 | default: |
| 1329 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 1330 | png_bytep sp; |
| 1331 | png_bytep dp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1332 | png_uint_32 i; |
| 1333 | png_size_t pixel_bytes; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1334 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1335 | /* start at the beginning */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1336 | dp = row; |
| 1337 | /* find out how many bytes each pixel takes up */ |
| 1338 | pixel_bytes = (row_info->pixel_depth >> 3); |
| 1339 | /* loop through the row, only looking at the pixels that |
| 1340 | matter */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1341 | for (i = png_pass_start[pass]; i < row_info->width; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1342 | i += png_pass_inc[pass]) |
| 1343 | { |
| 1344 | /* find out where the original pixel is */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1345 | sp = row + i * pixel_bytes; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1346 | /* move the pixel */ |
| 1347 | if (dp != sp) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1348 | png_memcpy(dp, sp, pixel_bytes); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1349 | /* next pixel */ |
| 1350 | dp += pixel_bytes; |
| 1351 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 1352 | break; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1353 | } |
| 1354 | } |
| 1355 | /* set new row width */ |
| 1356 | row_info->width = (row_info->width + |
| 1357 | png_pass_inc[pass] - 1 - |
| 1358 | png_pass_start[pass]) / |
| 1359 | png_pass_inc[pass]; |
| 1360 | row_info->rowbytes = ((row_info->width * |
| 1361 | row_info->pixel_depth + 7) >> 3); |
| 1362 | |
| 1363 | } |
| 1364 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1365 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1366 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1367 | /* This filters the row, chooses which filter to use, if it has not already |
| 1368 | * been specified by the application, and then writes the row out with the |
| 1369 | * chosen filter. */ |
| 1370 | #define PNG_MAXSUM (~0x0UL >> 1) |
| 1371 | #define PNG_HISHIFT 10 |
| 1372 | #define PNG_LOMASK 0xffffL |
| 1373 | #define PNG_HIMASK (~PNG_LOMASK >> PNG_HISHIFT) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1374 | void |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1375 | 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] | 1376 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1377 | png_bytep prev_row, best_row, row_buf; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1378 | png_uint_32 mins, bpp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1379 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1380 | png_debug(1, "in png_write_find_filter\n"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1381 | /* find out how many bytes offset each pixel is */ |
| 1382 | bpp = (row_info->pixel_depth + 7) / 8; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1383 | |
| 1384 | prev_row = png_ptr->prev_row; |
| 1385 | best_row = row_buf = png_ptr->row_buf; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1386 | mins = PNG_MAXSUM; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1387 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1388 | /* The prediction method we use is to find which method provides the |
| 1389 | * smallest value when summing the absolute values of the distances |
| 1390 | * from zero using anything >= 128 as negative numbers. This is known |
| 1391 | * as the "minimum sum of absolute differences" heuristic. Other |
| 1392 | * heruistics are the "weighted minumum sum of absolute differences" |
| 1393 | * (experimental and can in theory improve compression), and the "zlib |
| 1394 | * predictive" method (not implemented in libpng 0.95), which does test |
| 1395 | * compressions of lines using different filter methods, and then chooses |
| 1396 | * the (series of) filter(s) which give minimum compressed data size (VERY |
| 1397 | * computationally expensive). |
| 1398 | */ |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1399 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1400 | /* 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^] | 1401 | * that has been chosen, as it doesn't actually do anything to the data. |
| 1402 | */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1403 | if (png_ptr->do_filter & PNG_FILTER_NONE && |
| 1404 | png_ptr->do_filter != PNG_FILTER_NONE) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1405 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1406 | png_bytep rp; |
| 1407 | png_uint_32 sum = 0; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1408 | png_size_t i; |
| 1409 | int v; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1410 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1411 | for (i = 0, rp = row_buf + 1; i < row_info->rowbytes; i++, rp++) |
| 1412 | { |
| 1413 | v = *rp; |
| 1414 | sum += (v < 128) ? v : 256 - v; |
| 1415 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1416 | |
| 1417 | #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) |
| 1418 | if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
| 1419 | { |
| 1420 | png_uint_32 sumhi, sumlo; |
| 1421 | sumlo = sum & PNG_LOMASK; |
| 1422 | sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */ |
| 1423 | |
| 1424 | /* Reduce the sum if we match any of the previous rows */ |
| 1425 | for (i = 0; i < png_ptr->num_prev_filters; i++) |
| 1426 | { |
| 1427 | if (png_ptr->prev_filters[i] == PNG_FILTER_NONE) |
| 1428 | { |
| 1429 | sumlo = (sumlo * png_ptr->filter_weights[i]) >> |
| 1430 | PNG_WEIGHT_SHIFT; |
| 1431 | sumhi = (sumhi * png_ptr->filter_weights[i]) >> |
| 1432 | PNG_WEIGHT_SHIFT; |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | /* Factor in the cost of this filter (this is here for completeness, |
| 1437 | * but it makes no sense to have a "cost" for the NONE filter, as |
| 1438 | * it has the minimum possible computational cost - none). |
| 1439 | */ |
| 1440 | sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >> |
| 1441 | PNG_COST_SHIFT; |
| 1442 | sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >> |
| 1443 | PNG_COST_SHIFT; |
| 1444 | |
| 1445 | if (sumhi > PNG_HIMASK) |
| 1446 | sum = PNG_MAXSUM; |
| 1447 | else |
| 1448 | sum = (sumhi << PNG_HISHIFT) + sumlo; |
| 1449 | } |
| 1450 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1451 | mins = sum; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1452 | } |
| 1453 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1454 | /* sub filter */ |
| 1455 | if (png_ptr->do_filter & PNG_FILTER_SUB) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1456 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1457 | png_bytep rp, dp, lp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1458 | png_uint_32 sum = 0, lmins = mins; |
| 1459 | png_size_t i; |
| 1460 | int v; |
| 1461 | |
| 1462 | #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) |
| 1463 | /* We temporarily increase the "minumum sum" by the factor we |
| 1464 | * would reduce the sum of this filter, so that we can do the |
| 1465 | * early exit comparison without scaling the sum each time. |
| 1466 | */ |
| 1467 | if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
| 1468 | { |
| 1469 | png_uint_32 lmhi, lmlo; |
| 1470 | lmlo = lmins & PNG_LOMASK; |
| 1471 | lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; |
| 1472 | |
| 1473 | for (i = 0; i < png_ptr->num_prev_filters; i++) |
| 1474 | { |
| 1475 | if (png_ptr->prev_filters[i] == PNG_FILTER_VALUE_SUB) |
| 1476 | { |
| 1477 | lmlo = (lmlo * png_ptr->inv_filter_weights[i]) >> |
| 1478 | PNG_WEIGHT_SHIFT; |
| 1479 | lmhi = (lmhi * png_ptr->inv_filter_weights[i]) >> |
| 1480 | PNG_WEIGHT_SHIFT; |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> |
| 1485 | PNG_COST_SHIFT; |
| 1486 | lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> |
| 1487 | PNG_COST_SHIFT; |
| 1488 | |
| 1489 | if (lmhi > PNG_HIMASK) |
| 1490 | lmins = PNG_MAXSUM; |
| 1491 | else |
| 1492 | lmins = (lmhi << PNG_HISHIFT) + lmlo; |
| 1493 | } |
| 1494 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1495 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1496 | for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp; |
| 1497 | i++, rp++, dp++) |
| 1498 | { |
| 1499 | v = *dp = *rp; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1500 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1501 | sum += (v < 128) ? v : 256 - v; |
| 1502 | } |
| 1503 | for (lp = row_buf + 1; i < row_info->rowbytes; i++, rp++, lp++, dp++) |
| 1504 | { |
| 1505 | v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1506 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1507 | sum += (v < 128) ? v : 256 - v; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1508 | |
| 1509 | if (sum > lmins) /* We are already worse, don't continue. */ |
| 1510 | break; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1511 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1512 | |
| 1513 | #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) |
| 1514 | if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
| 1515 | { |
| 1516 | png_uint_32 sumhi, sumlo; |
| 1517 | sumlo = sum & PNG_LOMASK; |
| 1518 | sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; |
| 1519 | |
| 1520 | for (i = 0; i < png_ptr->num_prev_filters; i++) |
| 1521 | { |
| 1522 | if (png_ptr->prev_filters[i] == PNG_FILTER_VALUE_SUB) |
| 1523 | { |
| 1524 | sumlo = (sumlo * png_ptr->inv_filter_weights[i]) >> |
| 1525 | PNG_WEIGHT_SHIFT; |
| 1526 | sumhi = (sumhi * png_ptr->inv_filter_weights[i]) >> |
| 1527 | PNG_WEIGHT_SHIFT; |
| 1528 | } |
| 1529 | } |
| 1530 | |
| 1531 | sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> |
| 1532 | PNG_COST_SHIFT; |
| 1533 | sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >> |
| 1534 | PNG_COST_SHIFT; |
| 1535 | |
| 1536 | if (sumhi > PNG_HIMASK) |
| 1537 | sum = PNG_MAXSUM; |
| 1538 | else |
| 1539 | sum = (sumhi << PNG_HISHIFT) + sumlo; |
| 1540 | } |
| 1541 | #endif |
| 1542 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1543 | if (sum < mins) |
| 1544 | { |
| 1545 | mins = sum; |
| 1546 | best_row = png_ptr->sub_row; |
| 1547 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1548 | } |
| 1549 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1550 | /* up filter */ |
| 1551 | if (png_ptr->do_filter & PNG_FILTER_UP) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1552 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1553 | png_bytep rp, dp, pp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1554 | png_uint_32 sum = 0, lmins = mins; |
| 1555 | png_size_t i; |
| 1556 | int v; |
| 1557 | |
| 1558 | #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) |
| 1559 | if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
| 1560 | { |
| 1561 | png_uint_32 lmhi, lmlo; |
| 1562 | lmlo = lmins & PNG_LOMASK; |
| 1563 | lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; |
| 1564 | |
| 1565 | for (i = 0; i < png_ptr->num_prev_filters; i++) |
| 1566 | { |
| 1567 | if (png_ptr->prev_filters[i] == PNG_FILTER_VALUE_UP) |
| 1568 | { |
| 1569 | lmlo = (lmlo * png_ptr->inv_filter_weights[i]) >> |
| 1570 | PNG_WEIGHT_SHIFT; |
| 1571 | lmhi = (lmhi * png_ptr->inv_filter_weights[i]) >> |
| 1572 | PNG_WEIGHT_SHIFT; |
| 1573 | } |
| 1574 | } |
| 1575 | |
| 1576 | lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >> |
| 1577 | PNG_COST_SHIFT; |
| 1578 | lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >> |
| 1579 | PNG_COST_SHIFT; |
| 1580 | |
| 1581 | if (lmhi > PNG_HIMASK) |
| 1582 | lmins = PNG_MAXSUM; |
| 1583 | else |
| 1584 | lmins = (lmhi << PNG_HISHIFT) + lmlo; |
| 1585 | } |
| 1586 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1587 | |
| 1588 | for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1, |
| 1589 | pp = prev_row + 1; i < row_info->rowbytes; i++, rp++, pp++, dp++) |
| 1590 | { |
| 1591 | v = *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff); |
| 1592 | |
| 1593 | sum += (v < 128) ? v : 256 - v; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1594 | |
| 1595 | if (sum > lmins) /* We are already worse, don't continue. */ |
| 1596 | break; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1597 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1598 | |
| 1599 | #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) |
| 1600 | if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
| 1601 | { |
| 1602 | png_uint_32 sumhi, sumlo; |
| 1603 | sumlo = sum & PNG_LOMASK; |
| 1604 | sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; |
| 1605 | |
| 1606 | for (i = 0; i < png_ptr->num_prev_filters; i++) |
| 1607 | { |
| 1608 | if (png_ptr->prev_filters[i] == PNG_FILTER_UP) |
| 1609 | { |
| 1610 | sumlo = (sumlo * png_ptr->filter_weights[i]) >> |
| 1611 | PNG_WEIGHT_SHIFT; |
| 1612 | sumhi = (sumhi * png_ptr->filter_weights[i]) >> |
| 1613 | PNG_WEIGHT_SHIFT; |
| 1614 | } |
| 1615 | } |
| 1616 | |
| 1617 | sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >> |
| 1618 | PNG_COST_SHIFT; |
| 1619 | sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >> |
| 1620 | PNG_COST_SHIFT; |
| 1621 | |
| 1622 | if (sumhi > PNG_HIMASK) |
| 1623 | sum = PNG_MAXSUM; |
| 1624 | else |
| 1625 | sum = (sumhi << PNG_HISHIFT) + sumlo; |
| 1626 | } |
| 1627 | #endif |
| 1628 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1629 | if (sum < mins) |
| 1630 | { |
| 1631 | mins = sum; |
| 1632 | best_row = png_ptr->up_row; |
| 1633 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1634 | } |
| 1635 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1636 | /* avg filter */ |
| 1637 | if (png_ptr->do_filter & PNG_FILTER_AVG) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1638 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1639 | png_bytep rp, dp, pp, lp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1640 | png_uint_32 sum = 0, lmins = mins; |
| 1641 | png_size_t i; |
| 1642 | int v; |
| 1643 | |
| 1644 | #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) |
| 1645 | if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
| 1646 | { |
| 1647 | png_uint_32 lmhi, lmlo; |
| 1648 | lmlo = lmins & PNG_LOMASK; |
| 1649 | lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; |
| 1650 | |
| 1651 | for (i = 0; i < png_ptr->num_prev_filters; i++) |
| 1652 | { |
| 1653 | if (png_ptr->prev_filters[i] == PNG_FILTER_VALUE_AVG) |
| 1654 | { |
| 1655 | lmlo = (lmlo * png_ptr->inv_filter_weights[i]) >> |
| 1656 | PNG_WEIGHT_SHIFT; |
| 1657 | lmhi = (lmhi * png_ptr->inv_filter_weights[i]) >> |
| 1658 | PNG_WEIGHT_SHIFT; |
| 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >> |
| 1663 | PNG_COST_SHIFT; |
| 1664 | lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >> |
| 1665 | PNG_COST_SHIFT; |
| 1666 | |
| 1667 | if (lmhi > PNG_HIMASK) |
| 1668 | lmins = PNG_MAXSUM; |
| 1669 | else |
| 1670 | lmins = (lmhi << PNG_HISHIFT) + lmlo; |
| 1671 | } |
| 1672 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1673 | |
| 1674 | for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1, |
| 1675 | pp = prev_row + 1; i < bpp; i++, rp++, pp++, dp++) |
| 1676 | { |
| 1677 | v = *dp = (png_byte)(((int)*rp - ((int)*pp / 2)) & 0xff); |
| 1678 | |
| 1679 | sum += (v < 128) ? v : 256 - v; |
| 1680 | } |
| 1681 | for (lp = row_buf + 1; i < row_info->rowbytes; |
| 1682 | i++, rp++, pp++, lp++, dp++) |
| 1683 | { |
| 1684 | v = *dp = (png_byte)(((int)*rp - (((int)*pp + (int)*lp) / 2)) & 0xff); |
| 1685 | |
| 1686 | sum += (v < 128) ? v : 256 - v; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1687 | |
| 1688 | if (sum > lmins) /* We are already worse, don't continue. */ |
| 1689 | break; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1690 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1691 | |
| 1692 | #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) |
| 1693 | if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
| 1694 | { |
| 1695 | png_uint_32 sumhi, sumlo; |
| 1696 | sumlo = sum & PNG_LOMASK; |
| 1697 | sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; |
| 1698 | |
| 1699 | for (i = 0; i < png_ptr->num_prev_filters; i++) |
| 1700 | { |
| 1701 | if (png_ptr->prev_filters[i] == PNG_FILTER_NONE) |
| 1702 | { |
| 1703 | sumlo = (sumlo * png_ptr->filter_weights[i]) >> |
| 1704 | PNG_WEIGHT_SHIFT; |
| 1705 | sumhi = (sumhi * png_ptr->filter_weights[i]) >> |
| 1706 | PNG_WEIGHT_SHIFT; |
| 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >> |
| 1711 | PNG_COST_SHIFT; |
| 1712 | sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >> |
| 1713 | PNG_COST_SHIFT; |
| 1714 | |
| 1715 | if (sumhi > PNG_HIMASK) |
| 1716 | sum = PNG_MAXSUM; |
| 1717 | else |
| 1718 | sum = (sumhi << PNG_HISHIFT) + sumlo; |
| 1719 | } |
| 1720 | #endif |
| 1721 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1722 | if (sum < mins) |
| 1723 | { |
| 1724 | mins = sum; |
| 1725 | best_row = png_ptr->avg_row; |
| 1726 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1727 | } |
| 1728 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1729 | /* Paeth filter */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1730 | if (png_ptr->do_filter & PNG_FILTER_PAETH) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1731 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1732 | png_bytep rp, dp, pp, cp, lp; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1733 | png_uint_32 sum = 0, lmins = mins; |
| 1734 | png_size_t i; |
| 1735 | int v; |
| 1736 | |
| 1737 | #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) |
| 1738 | if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
| 1739 | { |
| 1740 | png_uint_32 lmhi, lmlo; |
| 1741 | lmlo = lmins & PNG_LOMASK; |
| 1742 | lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; |
| 1743 | |
| 1744 | for (i = 0; i < png_ptr->num_prev_filters; i++) |
| 1745 | { |
| 1746 | if (png_ptr->prev_filters[i] == PNG_FILTER_VALUE_PAETH) |
| 1747 | { |
| 1748 | lmlo = (lmlo * png_ptr->inv_filter_weights[i]) >> |
| 1749 | PNG_WEIGHT_SHIFT; |
| 1750 | lmhi = (lmhi * png_ptr->inv_filter_weights[i]) >> |
| 1751 | PNG_WEIGHT_SHIFT; |
| 1752 | } |
| 1753 | } |
| 1754 | |
| 1755 | lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >> |
| 1756 | PNG_COST_SHIFT; |
| 1757 | lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >> |
| 1758 | PNG_COST_SHIFT; |
| 1759 | |
| 1760 | if (lmhi > PNG_HIMASK) |
| 1761 | lmins = PNG_MAXSUM; |
| 1762 | else |
| 1763 | lmins = (lmhi << PNG_HISHIFT) + lmlo; |
| 1764 | } |
| 1765 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1766 | |
| 1767 | for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1, |
| 1768 | pp = prev_row + 1; i < bpp; i++, rp++, pp++, dp++) |
| 1769 | { |
| 1770 | v = *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff); |
| 1771 | |
| 1772 | sum += (v < 128) ? v : 256 - v; |
| 1773 | } |
| 1774 | for (lp = row_buf + 1, cp = prev_row + 1; i < row_info->rowbytes; |
| 1775 | i++, rp++, pp++, lp++, dp++, cp++) |
| 1776 | { |
| 1777 | int a, b, c, pa, pb, pc, p; |
| 1778 | |
| 1779 | b = *pp; |
| 1780 | c = *cp; |
| 1781 | a = *lp; |
| 1782 | |
| 1783 | p = a + b - c; |
| 1784 | pa = abs(p - a); |
| 1785 | pb = abs(p - b); |
| 1786 | pc = abs(p - c); |
| 1787 | |
| 1788 | if (pa <= pb && pa <= pc) |
| 1789 | p = a; |
| 1790 | else if (pb <= pc) |
| 1791 | p = b; |
| 1792 | else |
| 1793 | p = c; |
| 1794 | |
| 1795 | v = *dp = (png_byte)(((int)*rp - p) & 0xff); |
| 1796 | |
| 1797 | sum += (v < 128) ? v : 256 - v; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1798 | |
| 1799 | if (sum > lmins) /* We are already worse, don't continue. */ |
| 1800 | break; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1801 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1802 | |
| 1803 | #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) |
| 1804 | if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) |
| 1805 | { |
| 1806 | png_uint_32 sumhi, sumlo; |
| 1807 | sumlo = sum & PNG_LOMASK; |
| 1808 | sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; |
| 1809 | |
| 1810 | for (i = 0; i < png_ptr->num_prev_filters; i++) |
| 1811 | { |
| 1812 | if (png_ptr->prev_filters[i] == PNG_FILTER_PAETH) |
| 1813 | { |
| 1814 | sumlo = (sumlo * png_ptr->filter_weights[i]) >> |
| 1815 | PNG_WEIGHT_SHIFT; |
| 1816 | sumhi = (sumhi * png_ptr->filter_weights[i]) >> |
| 1817 | PNG_WEIGHT_SHIFT; |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >> |
| 1822 | PNG_COST_SHIFT; |
| 1823 | sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >> |
| 1824 | PNG_COST_SHIFT; |
| 1825 | |
| 1826 | if (sumhi > PNG_HIMASK) |
| 1827 | sum = PNG_MAXSUM; |
| 1828 | else |
| 1829 | sum = (sumhi << PNG_HISHIFT) + sumlo; |
| 1830 | } |
| 1831 | #endif |
| 1832 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1833 | if (sum < mins) |
| 1834 | { |
| 1835 | best_row = png_ptr->paeth_row; |
| 1836 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1837 | } |
| 1838 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1839 | /* Do the actual writing of the filtered row data from the chosen filter. */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1840 | png_write_filtered_row(png_ptr, best_row); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1841 | |
| 1842 | #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) |
| 1843 | /* Save the type of filter we picked this time for future calculations */ |
| 1844 | if (png_ptr->num_prev_filters > 0) |
| 1845 | { |
| 1846 | png_byte i; |
| 1847 | |
| 1848 | for (i = 1; i < png_ptr->num_prev_filters; i++) |
| 1849 | { |
| 1850 | png_ptr->prev_filters[i] = png_ptr->prev_filters[i - 1]; |
| 1851 | } |
| 1852 | png_ptr->prev_filters[i] = best_row[0]; |
| 1853 | } |
| 1854 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1858 | /* Do the actual writing of a previously filtered row. */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1859 | void |
| 1860 | png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row) |
| 1861 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1862 | png_debug(1, "in png_write_filtered_row\n"); |
| 1863 | png_debug1(2, "filter = %d\n", filtered_row[0]); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1864 | /* set up the zlib input buffer */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1865 | png_ptr->zstream.next_in = filtered_row; |
| 1866 | png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1867 | /* repeat until we have compressed all the data */ |
| 1868 | do |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1869 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1870 | int ret; /* return of zlib */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1871 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1872 | /* compress the data */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1873 | ret = deflate(&png_ptr->zstream, Z_NO_FLUSH); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1874 | /* check for compression errors */ |
| 1875 | if (ret != Z_OK) |
| 1876 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1877 | if (png_ptr->zstream.msg != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1878 | png_error(png_ptr, png_ptr->zstream.msg); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1879 | else |
| 1880 | png_error(png_ptr, "zlib error"); |
| 1881 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1882 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1883 | /* see if it is time to write another IDAT */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1884 | if (!(png_ptr->zstream.avail_out)) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1885 | { |
| 1886 | /* write the IDAT and reset the zlib output buffer */ |
| 1887 | png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1888 | png_ptr->zstream.next_out = png_ptr->zbuf; |
| 1889 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1890 | } |
| 1891 | /* repeat until all data has been compressed */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 1892 | } while (png_ptr->zstream.avail_in); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1893 | |
Guy Schalnat | c21f90c | 1996-06-17 16:24:45 -0500 | [diff] [blame] | 1894 | /* swap the current and previous rows */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame^] | 1895 | if (png_ptr->prev_row != NULL) |
Guy Schalnat | c21f90c | 1996-06-17 16:24:45 -0500 | [diff] [blame] | 1896 | { |
| 1897 | png_bytep tptr; |
| 1898 | |
| 1899 | tptr = png_ptr->prev_row; |
| 1900 | png_ptr->prev_row = png_ptr->row_buf; |
| 1901 | png_ptr->row_buf = tptr; |
| 1902 | } |
| 1903 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1904 | /* finish row - updates counters and flushes zlib if last row */ |
| 1905 | png_write_finish_row(png_ptr); |
| 1906 | |
| 1907 | #if defined(PNG_WRITE_FLUSH_SUPPORTED) |
| 1908 | png_ptr->flush_rows++; |
| 1909 | |
| 1910 | if (png_ptr->flush_dist > 0 && |
| 1911 | png_ptr->flush_rows >= png_ptr->flush_dist) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1912 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1913 | png_write_flush(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1914 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 1915 | #endif /* PNG_WRITE_FLUSH_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1916 | } |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 1917 | |