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