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