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