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